labelService.ts
5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { createApiClient } from "../lib/apiClient";
import type {
LabelCreateInput,
LabelDto,
LabelGetListInput,
LabelUpdateInput,
PagedResultDto,
} from "../types/label";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/label";
function parseIdList(v: unknown): string[] | undefined {
if (!Array.isArray(v)) return undefined;
return [...new Set(v.map((x) => String(x).trim()).filter(Boolean))];
}
export function normalizeLabelDto(raw: unknown): LabelDto {
const r = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
const regionIds = parseIdList(r.regionIds ?? r.RegionIds);
const groupIds = parseIdList(r.groupIds ?? r.GroupIds);
const mergedRegionIds = [...new Set([...(regionIds ?? []), ...(groupIds ?? [])])];
const appliedRaw = r.appliedRegionType ?? r.AppliedRegionType;
const appliedRegionType =
typeof appliedRaw === "string" && appliedRaw.trim()
? appliedRaw.trim().toUpperCase()
: mergedRegionIds.length > 0
? "SPECIFIED"
: null;
const regionRaw = r.region ?? r.Region;
const locationIds = parseIdList(r.locationIds ?? r.LocationIds) ?? [];
const locationIdRaw = r.locationId ?? r.LocationId;
const locationId =
locationIdRaw != null && String(locationIdRaw).trim()
? String(locationIdRaw).trim()
: locationIds[0] ?? null;
const mergedLocationIds = [
...new Set([
...(locationId ? [locationId] : []),
...locationIds,
]),
];
return {
...(r as object),
id: String(r.id ?? r.Id ?? r.labelCode ?? r.LabelCode ?? ""),
locationId,
locationIds: mergedLocationIds.length ? mergedLocationIds : locationIds,
regionIds: mergedRegionIds.length ? mergedRegionIds : regionIds,
groupIds: mergedRegionIds.length ? mergedRegionIds : groupIds,
appliedRegionType: appliedRegionType as LabelDto["appliedRegionType"],
region: typeof regionRaw === "string" ? regionRaw : null,
} as LabelDto;
}
function locationBodyFromInput(input: LabelCreateInput | LabelUpdateInput): Record<string, unknown> {
const locationIds = [
...new Set((input.locationIds ?? []).map((x) => String(x).trim()).filter(Boolean)),
];
const body: Record<string, unknown> = {};
if (locationIds.length) {
body.locationIds = locationIds;
body.locationId = locationIds[0];
}
return body;
}
function scopeBodyFromInput(input: LabelCreateInput | LabelUpdateInput): Record<string, unknown> {
const regionIds = [
...new Set(
[...(input.regionIds ?? []), ...(input.groupIds ?? [])]
.map((x) => String(x).trim())
.filter(Boolean),
),
];
const applied =
String(input.appliedRegionType ?? "").trim().toUpperCase() ||
(regionIds.length > 0 ? "SPECIFIED" : "");
const body: Record<string, unknown> = {};
if (applied === "ALL" || applied === "SPECIFIED") {
body.appliedRegionType = applied;
}
if (regionIds.length) {
body.regionIds = regionIds;
body.groupIds = regionIds;
} else if (applied === "ALL") {
body.regionIds = [];
body.groupIds = [];
}
return body;
}
export async function getLabels(input: LabelGetListInput, signal?: AbortSignal): Promise<PagedResultDto<LabelDto>> {
const res = await api.requestJson<PagedResultDto<LabelDto> & { items?: unknown[]; Items?: unknown[] }>({
path: PATH,
method: "GET",
query: {
SkipCount: input.skipCount,
MaxResultCount: input.maxResultCount,
Sorting: input.sorting,
Keyword: input.keyword,
PartnerId: input.partnerId,
GroupId: input.groupId,
LocationId: input.locationId,
ProductId: input.productId,
LabelCategoryId: input.labelCategoryId,
LabelTypeId: input.labelTypeId,
TemplateCode: input.templateCode,
State: input.state,
},
signal,
});
const itemsRaw = res.items ?? res.Items ?? [];
const items = (Array.isArray(itemsRaw) ? itemsRaw : []).map((x) => normalizeLabelDto(x));
return {
...res,
items,
totalCount: res.totalCount ?? (res as { TotalCount?: number }).TotalCount ?? items.length,
};
}
export async function getLabel(labelCode: string, signal?: AbortSignal): Promise<LabelDto> {
const raw = await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(labelCode)}`,
method: "GET",
signal,
});
return normalizeLabelDto(raw);
}
export async function createLabel(input: LabelCreateInput): Promise<LabelDto> {
const raw = await api.requestJson<unknown>({
path: PATH,
method: "POST",
body: {
labelCode: String(input.labelCode ?? "").trim() || null,
labelName: input.labelName,
templateCode: input.templateCode,
...locationBodyFromInput(input),
labelCategoryId: input.labelCategoryId,
labelTypeId: String(input.labelTypeId ?? "").trim() || null,
productIds: input.productIds,
labelInfoJson: input.labelInfoJson,
state: input.state ?? true,
...scopeBodyFromInput(input),
},
});
return normalizeLabelDto(raw);
}
export async function updateLabel(labelCode: string, input: LabelUpdateInput): Promise<LabelDto> {
const raw = await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(labelCode)}`,
method: "PUT",
body: {
labelName: input.labelName,
templateCode: input.templateCode,
...locationBodyFromInput(input),
labelCategoryId: input.labelCategoryId,
labelTypeId: input.labelTypeId,
productIds: input.productIds,
labelInfoJson: input.labelInfoJson,
state: input.state ?? true,
...scopeBodyFromInput(input),
},
});
return normalizeLabelDto(raw);
}
export async function deleteLabel(labelCode: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(labelCode)}`,
method: "DELETE",
});
}