labelTemplateService.ts
5.06 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
import { createApiClient } from "../lib/apiClient";
import type {
LabelTemplateCreateInput,
LabelTemplateDto,
LabelTemplateGetListInput,
LabelTemplateUpdateInput,
PagedResultDto,
} from "../types/labelTemplate";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/label-template";
function normalizeTemplateCode(raw: unknown): string {
const r = raw as Record<string, unknown> | null | undefined;
if (!r || typeof r !== "object") return "";
const id = r.id ?? r.templateCode ?? r.TemplateCode;
return typeof id === "string" ? id.trim() : String(id ?? "").trim();
}
function normalizeLabelTemplateDto(raw: unknown): LabelTemplateDto {
const r = raw as Record<string, unknown>;
const ids =
(Array.isArray(r.appliedLocationIds) ? r.appliedLocationIds : null) ??
(Array.isArray(r.AppliedLocationIds) ? r.AppliedLocationIds : null) ??
[];
const appliedLocationIds = ids.map((x) => String(x));
const id = normalizeTemplateCode(raw);
const templateNameVo = r.templateName ?? r.TemplateName;
const templateCodeVo = r.templateCode ?? r.TemplateCode;
const locationTextVo = r.locationText ?? r.LocationText;
const sizeTextVo = r.sizeText ?? r.SizeText;
const ccRaw = r.contentsCount ?? r.ContentsCount;
const contentsCountVo = typeof ccRaw === "number" ? ccRaw : undefined;
const lastEditedVo = r.lastEdited ?? r.LastEdited;
const nameFromList =
(typeof r.name === "string" && r.name.trim() ? r.name : null) ??
(typeof templateNameVo === "string" && String(templateNameVo).trim() ? String(templateNameVo) : null);
return {
...(r as object),
id,
name: nameFromList ?? (r.name as LabelTemplateDto["name"]),
templateName: (typeof templateNameVo === "string" ? templateNameVo : null) ?? (r.templateName as string | null),
templateCode: (typeof templateCodeVo === "string" ? templateCodeVo : null) ?? (r.templateCode as string | null),
locationText: (typeof locationTextVo === "string" ? locationTextVo : null) ?? (r.locationText as string | null),
sizeText: (typeof sizeTextVo === "string" ? sizeTextVo : null) ?? (r.sizeText as string | null),
contentsCount: contentsCountVo ?? (r.contentsCount as number | null),
lastEdited: (typeof lastEditedVo === "string" ? lastEditedVo : null) ?? (r.lastEdited as string | null),
appliedLocationIds,
elements: Array.isArray(r.elements) ? (r.elements as LabelTemplateDto["elements"]) : [],
} as LabelTemplateDto;
}
export async function getLabelTemplates(input: LabelTemplateGetListInput, signal?: AbortSignal): Promise<PagedResultDto<LabelTemplateDto>> {
const res = await api.requestJson<PagedResultDto<LabelTemplateDto>>({
path: PATH,
method: "GET",
query: {
SkipCount: input.skipCount,
MaxResultCount: input.maxResultCount,
Sorting: input.sorting,
Keyword: input.keyword,
LocationId: input.locationId,
LabelType: input.labelType,
State: input.state,
},
signal,
});
const items = (res.items ?? []).map((x) => normalizeLabelTemplateDto(x));
return { ...res, items };
}
export async function getLabelTemplate(templateCode: string, signal?: AbortSignal): Promise<LabelTemplateDto> {
const raw = await api.requestJson<LabelTemplateDto>({
path: `${PATH}/${encodeURIComponent(templateCode)}`,
method: "GET",
signal,
});
return normalizeLabelTemplateDto(raw);
}
export async function createLabelTemplate(input: LabelTemplateCreateInput): Promise<LabelTemplateDto> {
const created = await api.requestJson<LabelTemplateDto>({
path: PATH,
method: "POST",
body: {
id: input.id,
name: input.name,
labelType: input.labelType,
unit: input.unit,
width: input.width,
height: input.height,
appliedLocation: input.appliedLocation,
showRuler: input.showRuler ?? true,
showGrid: input.showGrid ?? true,
state: input.state ?? true,
elements: input.elements,
appliedLocationIds: input.appliedLocationIds ?? [],
},
});
return normalizeLabelTemplateDto(created);
}
export async function updateLabelTemplate(templateCode: string, input: LabelTemplateUpdateInput): Promise<LabelTemplateDto> {
const updated = await api.requestJson<LabelTemplateDto>({
path: `${PATH}/${encodeURIComponent(templateCode)}`,
method: "PUT",
body: {
id: input.id,
name: input.name,
labelType: input.labelType,
unit: input.unit,
width: input.width,
height: input.height,
appliedLocation: input.appliedLocation,
showRuler: input.showRuler ?? true,
showGrid: input.showGrid ?? true,
state: input.state ?? true,
elements: input.elements,
appliedLocationIds: input.appliedLocationIds ?? [],
},
});
return normalizeLabelTemplateDto(updated);
}
export async function deleteLabelTemplate(templateCode: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(templateCode)}`,
method: "DELETE",
});
}