labelMultipleOptionService.ts
6.85 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { createApiClient } from "../lib/apiClient";
import type {
LabelMultipleOptionCreateInput,
LabelMultipleOptionDto,
LabelMultipleOptionGetListInput,
LabelMultipleOptionUpdateInput,
PagedResultDto,
} from "../types/labelMultipleOption";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/label-multiple-option";
/** 接口存的是 JSON 字符串;列表/详情可能返回 string 或已反序列化的 array */
function parseOptionValuesJsonField(raw: unknown): string[] {
if (raw == null) return [];
if (Array.isArray(raw)) return raw.map((x) => String(x));
if (typeof raw === "string") {
const t = raw.trim();
if (!t) return [];
try {
const p = JSON.parse(t) as unknown;
if (Array.isArray(p)) return p.map((x) => String(x));
return [];
} catch {
return [];
}
}
return [];
}
function normalizeLabelMultipleOptionDto(item: LabelMultipleOptionDto): LabelMultipleOptionDto {
const raw = item as LabelMultipleOptionDto & {
optionValuesJson?: unknown;
lastEdited?: unknown;
LastEdited?: unknown;
};
const lastRaw = raw.lastEdited ?? raw.LastEdited;
const lastEdited =
typeof lastRaw === "string"
? lastRaw
: lastRaw != null
? String(lastRaw)
: null;
const parseIds = (v: unknown): string[] | undefined => {
if (!Array.isArray(v)) return undefined;
return [...new Set(v.map((x) => String(x).trim()).filter(Boolean))];
};
const groupIds = parseIds(
(raw as Record<string, unknown>).groupIds ??
(raw as Record<string, unknown>).GroupIds ??
(raw as Record<string, unknown>).regionIds ??
(raw as Record<string, unknown>).RegionIds,
);
const locationIds = parseIds(
(raw as Record<string, unknown>).locationIds ?? (raw as Record<string, unknown>).LocationIds,
);
const partnerIds = parseIds(
(raw as Record<string, unknown>).partnerIds ??
(raw as Record<string, unknown>).PartnerIds ??
(raw as Record<string, unknown>).companyIds ??
(raw as Record<string, unknown>).CompanyIds,
);
const companyRaw = (raw as Record<string, unknown>).company ?? (raw as Record<string, unknown>).Company;
return {
...item,
optionValuesJson: parseOptionValuesJsonField(raw.optionValuesJson),
lastEdited,
appliedPartnerType:
String(
(raw as Record<string, unknown>).appliedPartnerType ??
(raw as Record<string, unknown>).AppliedPartnerType ??
"",
).trim() || undefined,
partnerIds,
companyIds: partnerIds,
availabilityType:
String(
(raw as Record<string, unknown>).availabilityType ??
(raw as Record<string, unknown>).AvailabilityType ??
"",
).trim() || undefined,
groupIds,
regionIds: groupIds,
locationIds,
company: typeof companyRaw === "string" ? companyRaw : null,
};
}
function normalizePaged(res: PagedResultDto<LabelMultipleOptionDto>): PagedResultDto<LabelMultipleOptionDto> {
return {
totalCount: res.totalCount ?? 0,
items: (res.items ?? []).map(normalizeLabelMultipleOptionDto),
};
}
/** 创建/更新 Body:`optionValuesJson` 须为 string[] 的 JSON 字符串 */
function optionValuesJsonToApiBody(values: string[]): string {
return JSON.stringify(values);
}
function scopeBodyFromInput(input: LabelMultipleOptionCreateInput): Record<string, unknown> {
const groupIds = [
...new Set(
[...(input.groupIds ?? []), ...(input.regionIds ?? [])]
.map((x) => String(x).trim())
.filter(Boolean),
),
];
const locationIds = [
...new Set((input.locationIds ?? []).map((x) => String(x).trim()).filter(Boolean)),
];
const partnerIds = [
...new Set(
[...(input.partnerIds ?? []), ...(input.companyIds ?? [])]
.map((x) => String(x).trim())
.filter(Boolean),
),
];
const body: Record<string, unknown> = {};
const appliedPartnerType = String(input.appliedPartnerType ?? "").trim().toUpperCase();
if (appliedPartnerType === "ALL" || appliedPartnerType === "SPECIFIED") {
body.appliedPartnerType = appliedPartnerType;
}
if (partnerIds.length) {
body.partnerIds = partnerIds;
body.companyIds = partnerIds;
}
const availabilityType = String(input.availabilityType ?? "").trim().toUpperCase();
if (availabilityType === "ALL" || availabilityType === "SPECIFIED") {
body.availabilityType = availabilityType;
}
if (groupIds.length) {
body.groupIds = groupIds;
body.regionIds = groupIds;
}
if (locationIds.length) {
body.locationIds = locationIds;
}
return body;
}
export async function getLabelMultipleOptions(input: LabelMultipleOptionGetListInput, signal?: AbortSignal): Promise<PagedResultDto<LabelMultipleOptionDto>> {
const res = await api.requestJson<PagedResultDto<LabelMultipleOptionDto>>({
path: PATH,
method: "GET",
query: {
SkipCount: input.skipCount,
MaxResultCount: input.maxResultCount,
Sorting: input.sorting,
Keyword: input.keyword,
State: input.state,
GroupId: input.groupId,
LocationId: input.locationId,
},
signal,
});
return normalizePaged(res);
}
export async function getLabelMultipleOption(id: string, signal?: AbortSignal): Promise<LabelMultipleOptionDto> {
const d = await api.requestJson<LabelMultipleOptionDto>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "GET",
signal,
});
return normalizeLabelMultipleOptionDto(d);
}
export async function createLabelMultipleOption(input: LabelMultipleOptionCreateInput): Promise<LabelMultipleOptionDto> {
const d = await api.requestJson<LabelMultipleOptionDto>({
path: PATH,
method: "POST",
body: {
optionCode: String(input.optionCode ?? "").trim() || null,
optionName: input.optionName,
optionValuesJson: optionValuesJsonToApiBody(input.optionValuesJson),
state: input.state ?? true,
orderNum: input.orderNum,
...scopeBodyFromInput(input),
},
});
return normalizeLabelMultipleOptionDto(d);
}
export async function updateLabelMultipleOption(id: string, input: LabelMultipleOptionUpdateInput): Promise<LabelMultipleOptionDto> {
const d = await api.requestJson<LabelMultipleOptionDto>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "PUT",
body: {
optionCode: String(input.optionCode ?? "").trim() || null,
optionName: input.optionName,
optionValuesJson: optionValuesJsonToApiBody(input.optionValuesJson),
state: input.state ?? true,
orderNum: input.orderNum,
...scopeBodyFromInput(input),
},
});
return normalizeLabelMultipleOptionDto(d);
}
export async function deleteLabelMultipleOption(id: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "DELETE",
});
}