teamMemberService.ts
7.97 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { createApiClient } from "../lib/apiClient";
import type {
PagedResultDto,
TeamMemberCreateInput,
TeamMemberDto,
TeamMemberGetListInput,
TeamMemberUpdateInput,
} from "../types/teamMember";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/team-member";
function toStringArray(v: unknown): string[] {
if (Array.isArray(v)) return v.map((x) => String(x));
return [];
}
function toIdArray(v: unknown): string[] {
if (!Array.isArray(v)) return [];
const out: string[] = [];
for (const x of v) {
if (x === null || x === undefined) continue;
if (typeof x === "string" || typeof x === "number") {
out.push(String(x));
continue;
}
if (typeof x === "object") {
const o = x as Record<string, unknown>;
const id = o.id ?? o.Id ?? o.locationId ?? o.LocationId ?? o.location_id ?? o.locationID;
if (id !== null && id !== undefined) out.push(String(id));
}
}
return out;
}
function toLocationLabels(v: unknown): string[] {
if (!Array.isArray(v)) return [];
const out: string[] = [];
for (const x of v) {
if (x === null || x === undefined) continue;
if (typeof x === "string" || typeof x === "number") {
out.push(String(x));
continue;
}
if (typeof x === "object") {
const o = x as Record<string, unknown>;
const code =
(o.locationCode ?? o.LocationCode ?? o.code ?? o.Code ?? o.location_code ?? o.locationCodeId) as unknown;
const name =
(o.locationName ?? o.LocationName ?? o.name ?? o.Name ?? o.location_name) as unknown;
const id = (o.id ?? o.Id ?? o.locationId ?? o.LocationId) as unknown;
const codeS = code === null || code === undefined ? "" : String(code).trim();
const nameS = name === null || name === undefined ? "" : String(name).trim();
const idS = id === null || id === undefined ? "" : String(id).trim();
if (codeS && nameS) out.push(`${codeS} - ${nameS}`);
else if (nameS) out.push(nameS);
else if (codeS) out.push(codeS);
else if (idS) out.push(idS);
}
}
return out;
}
function normalizeTeamMemberDto(row: unknown): TeamMemberDto {
if (!row || typeof row !== "object") return { id: "" };
const r = row as Record<string, unknown>;
// ABP 常见 camelCase + 少量 PascalCase 兼容
const id = String(
r.id ?? r.Id ?? r.userId ?? r.UserId ?? r.user_id ?? r.UserID ?? r.memberId ?? r.MemberId ?? "",
);
// name fields: fullName or name
const fullName = (r.fullName ?? r.FullName ?? r.name ?? r.Name) as string | null | undefined;
const userName = (r.userName ?? r.UserName ?? r.username ?? r.UserName) as string | null | undefined;
const email = (r.email ?? r.Email) as string | null | undefined;
const phone = (r.phone ?? r.Phone) as string | null | undefined;
// role 可能是扁平字段(roleId/roleName),也可能是嵌套对象(role: { id, roleName })
let roleId = (r.roleId ?? r.RoleId) as string | null | undefined;
let roleName = (r.roleName ?? r.RoleName ?? r.roleName ?? r.Role) as string | null | undefined;
const roleObj = (r.role ?? r.Role) as unknown;
if ((!roleId || !roleName) && roleObj && typeof roleObj === "object") {
const ro = roleObj as Record<string, unknown>;
roleId = (ro.id ?? ro.Id ?? ro.roleId ?? ro.RoleId ?? roleId) as string | null | undefined;
roleName =
(ro.roleName ?? ro.RoleName ?? ro.name ?? ro.Name ?? ro.role ?? ro.Role ?? roleName) as
| string
| null
| undefined;
}
const stateRaw = r.state ?? r.State;
const state =
typeof stateRaw === "boolean" ? (stateRaw as boolean) : stateRaw === "true" ? true : stateRaw === "false" ? false : undefined;
const rawLocationIds =
r.locationIds ?? r.LocationIds ?? r.assignedLocationIds ?? r.AssignedLocationIds ?? r.location_id_list ?? r.LocationIdList;
let locationIds = toIdArray(rawLocationIds);
const rawLocations =
r.locations ?? r.Locations ?? r.assignedLocations ?? r.AssignedLocations ?? r.locationNames ?? r.LocationNames;
// locations 可能包含对象数组:{ id, locationCode, locationName } 或类似
let locations = toLocationLabels(rawLocations);
// 如果 locations 返回的是对象数组但没有显式 locationIds,则从 locations 对象里抽 id
if (locationIds.length === 0 && Array.isArray(rawLocations)) {
const inferredIds: string[] = [];
for (const x of rawLocations) {
if (typeof x !== "object" || !x) continue;
const o = x as Record<string, unknown>;
const id = o.id ?? o.Id ?? o.locationId ?? o.LocationId;
if (id !== null && id !== undefined) inferredIds.push(String(id));
}
if (inferredIds.length) locationIds = inferredIds;
}
// 有些接口可能只返回一个 locations,但我们仍尽量填充
return {
id,
fullName,
userName,
email,
phone,
roleId,
roleName,
locationIds,
locations,
state: state ?? (r.status ? (String(r.status).toLowerCase() === "active") : undefined),
};
}
export async function getTeamMembers(
input: TeamMemberGetListInput,
signal?: AbortSignal,
): Promise<PagedResultDto<TeamMemberDto>> {
const raw = await api.requestJson<PagedResultDto<unknown>>({
path: PATH,
method: "GET",
query: {
SkipCount: input.skipCount,
MaxResultCount: input.maxResultCount,
Keyword: input.keyword,
},
signal,
});
// requestJson 已做分页 shape 规范化,但这里做 DTO 规范
const items = (raw.items ?? []) as unknown[];
return {
totalCount: raw.totalCount ?? 0,
items: items.map(normalizeTeamMemberDto),
};
}
export async function getTeamMemberById(id: string, signal?: AbortSignal): Promise<TeamMemberDto> {
const raw = await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "GET",
signal,
});
return normalizeTeamMemberDto(raw);
}
function toPhoneNumber(v: string | number | null | undefined): number | null {
if (v === null || v === undefined || v === "") return null;
const s = String(v).trim();
if (!s) return null;
const num = Number(s.replace(/\D/g, "")) || 0;
return num;
}
function buildCreatePayload(input: TeamMemberCreateInput): Record<string, unknown> {
const phoneVal = input.phone != null && input.phone !== "" ? toPhoneNumber(String(input.phone)) : null;
return {
fullName: input.fullName,
userName: input.userName,
password: input.password,
email: input.email ?? null,
phone: phoneVal,
roleId: input.roleId,
// 兼容:后端可能叫 locationIds 或 locations
locationIds: input.locationIds,
locations: input.locationIds,
state: input.state,
};
}
function buildUpdatePayload(input: TeamMemberUpdateInput): Record<string, unknown> {
const phoneVal = input.phone != null && input.phone !== "" ? toPhoneNumber(String(input.phone)) : null;
const payload: Record<string, unknown> = {
fullName: input.fullName,
userName: input.userName,
email: input.email ?? null,
phone: phoneVal,
roleId: input.roleId,
locationIds: input.locationIds,
locations: input.locationIds,
state: input.state,
};
if (input.password) payload.password = input.password;
return payload;
}
export async function createTeamMember(input: TeamMemberCreateInput): Promise<TeamMemberDto> {
const raw = await api.requestJson<unknown>({
path: PATH,
method: "POST",
body: buildCreatePayload(input),
});
return normalizeTeamMemberDto(raw);
}
export async function updateTeamMember(id: string, input: TeamMemberUpdateInput): Promise<TeamMemberDto> {
const raw = await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "PUT",
body: buildUpdatePayload(input),
});
return normalizeTeamMemberDto(raw);
}
export async function deleteTeamMember(id: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "DELETE",
});
}