productLocationService.ts
2.69 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
import { createApiClient } from "../lib/apiClient";
import type {
ProductLocationByStoreDto,
ProductLocationCreateInput,
ProductLocationGetListInput,
ProductLocationLinkDto,
ProductLocationUpdateInput,
PagedResultDto,
} from "../types/productLocation";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/product-location";
function normalizeLink(raw: unknown): ProductLocationLinkDto {
const r = raw as Record<string, unknown>;
return {
id: (r?.id ?? r?.Id) as string | undefined,
locationId: (r?.locationId ?? r?.LocationId) as string | null | undefined,
productId: (r?.productId ?? r?.ProductId) as string | null | undefined,
};
}
export async function getProductLocations(
input: ProductLocationGetListInput,
signal?: AbortSignal,
): Promise<PagedResultDto<ProductLocationLinkDto>> {
const res = await api.requestJson<PagedResultDto<ProductLocationLinkDto>>({
path: PATH,
method: "GET",
query: {
SkipCount: input.skipCount,
MaxResultCount: input.maxResultCount,
Sorting: input.sorting,
LocationId: input.locationId,
ProductId: input.productId,
},
signal,
});
return {
...res,
items: (res.items ?? []).map((x) => normalizeLink(x)),
};
}
/** 门店下已关联的产品(文档 7.2) */
export async function getProductIdsByLocation(locationId: string, signal?: AbortSignal): Promise<string[]> {
const raw = await api.requestJson<ProductLocationByStoreDto>({
path: `${PATH}/${encodeURIComponent(locationId)}`,
method: "GET",
signal,
});
if (Array.isArray(raw?.productIds)) return raw.productIds.map(String);
if (Array.isArray(raw?.items)) {
return (raw.items ?? [])
.map((x) => x?.productId)
.filter((x): x is string => typeof x === "string" && x.length > 0);
}
return [];
}
export async function createProductLocation(input: ProductLocationCreateInput): Promise<unknown> {
return api.requestJson<unknown>({
path: PATH,
method: "POST",
body: {
locationId: input.locationId,
productIds: input.productIds,
},
});
}
export async function updateProductLocation(locationId: string, input: ProductLocationUpdateInput): Promise<unknown> {
return api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(locationId)}`,
method: "PUT",
body: {
productIds: input.productIds,
},
});
}
export async function deleteProductLocation(locationId: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(locationId)}`,
method: "DELETE",
});
}