labelCategoryService.ts
4.67 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
import { createApiClient } from "../lib/apiClient";
import { serializeButtonAppearanceForApi } from "../lib/categoryButtonAppearance";
import type {
LabelCategoryCreateInput,
LabelCategoryDto,
LabelCategoryGetListInput,
LabelCategoryUpdateInput,
PagedResultDto,
} from "../types/labelCategory";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/label-category";
function normalizeLabelCategoryDto(raw: unknown): LabelCategoryDto {
const r = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
const noRaw = r.noOfLabels ?? r.NoOfLabels;
const noOfLabels =
typeof noRaw === "number" && Number.isFinite(noRaw)
? noRaw
: typeof noRaw === "string" && noRaw.trim() !== "" && Number.isFinite(Number(noRaw))
? Number(noRaw)
: null;
const orderRaw = r.orderNum ?? r.OrderNum;
const orderNum =
typeof orderRaw === "number" && Number.isFinite(orderRaw)
? orderRaw
: typeof orderRaw === "string" && orderRaw.trim() !== "" && Number.isFinite(Number(orderRaw))
? Number(orderRaw)
: null;
return { ...(r as object), id: String(r.id ?? r.Id ?? ""), noOfLabels, orderNum } as LabelCategoryDto;
}
export async function getLabelCategories(input: LabelCategoryGetListInput, signal?: AbortSignal): Promise<PagedResultDto<LabelCategoryDto>> {
const res = await api.requestJson<PagedResultDto<LabelCategoryDto> & { items?: unknown[]; Items?: unknown[] }>({
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,
});
const itemsRaw = res.items ?? res.Items ?? [];
const items = (Array.isArray(itemsRaw) ? itemsRaw : []).map((x) => normalizeLabelCategoryDto(x));
return { ...res, items, totalCount: res.totalCount ?? (res as { TotalCount?: number }).TotalCount ?? items.length };
}
export async function getLabelCategory(id: string, signal?: AbortSignal): Promise<LabelCategoryDto> {
return api.requestJson<LabelCategoryDto>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "GET",
signal,
});
}
export async function createLabelCategory(input: LabelCategoryCreateInput): Promise<LabelCategoryDto> {
return api.requestJson<LabelCategoryDto>({
path: PATH,
method: "POST",
body: {
categoryCode: String(input.categoryCode ?? "").trim() || null,
categoryName: input.categoryName,
categoryPhotoUrl: input.categoryPhotoUrl,
displayText: (input.displayText ?? "").trim() || null,
buttonBgColor: (input.buttonBgColor ?? "").trim() || null,
buttonImageUrl: (input.buttonImageUrl ?? "").trim() || null,
buttonAppearance: serializeButtonAppearanceForApi(input.buttonAppearance),
buttonStyleJson: (input.buttonStyleJson ?? "").trim() || null,
state: input.state ?? true,
orderNum: input.orderNum,
availabilityType: String(input.availabilityType ?? "ALL").trim().toUpperCase() || "ALL",
locationIds:
Array.isArray(input.locationIds) && input.locationIds.length
? [...new Set(input.locationIds.map((x) => String(x).trim()).filter(Boolean))]
: null,
},
});
}
export async function updateLabelCategory(id: string, input: LabelCategoryUpdateInput): Promise<LabelCategoryDto> {
return api.requestJson<LabelCategoryDto>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "PUT",
body: {
categoryCode: String(input.categoryCode ?? "").trim() || null,
categoryName: input.categoryName,
categoryPhotoUrl: input.categoryPhotoUrl,
displayText: (input.displayText ?? "").trim() || null,
buttonBgColor: (input.buttonBgColor ?? "").trim() || null,
buttonImageUrl: (input.buttonImageUrl ?? "").trim() || null,
buttonAppearance: serializeButtonAppearanceForApi(input.buttonAppearance),
buttonStyleJson: (input.buttonStyleJson ?? "").trim() || null,
state: input.state ?? true,
orderNum: input.orderNum,
availabilityType: String(input.availabilityType ?? "ALL").trim().toUpperCase() || "ALL",
locationIds:
Array.isArray(input.locationIds) && input.locationIds.length
? [...new Set(input.locationIds.map((x) => String(x).trim()).filter(Boolean))]
: null,
},
});
}
export async function deleteLabelCategory(id: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "DELETE",
});
}