/
project.ts
333 lines (297 loc) · 8.75 KB
/
project.ts
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { defineStore } from "pinia";
import axios from "axios";
import {
empty,
EMPTY_ID,
MemberId,
PrincipalId,
Project,
ProjectCreate,
ProjectId,
ProjectMember,
ProjectMemberCreate,
ProjectMemberPatch,
ProjectPatch,
ProjectState,
ResourceIdentifier,
ResourceObject,
RowStatus,
unknown,
} from "@/types";
import { getPrincipalFromIncludedList } from "./principal";
function convert(
project: ResourceObject,
includedList: ResourceObject[]
): Project {
const attrs = project.attributes as Omit<
Project,
"id" | "memberList" | "creator" | "updater"
>;
// Only able to assign an empty member list, otherwise would cause circular dependency.
// This should be fine as we shouldn't access member via member.project.memberList
const projectWithoutMemberList: Project = {
id: parseInt(project.id),
rowStatus: attrs.rowStatus,
name: attrs.name,
key: attrs.key,
creator: getPrincipalFromIncludedList(
project.relationships!.creator.data,
includedList
),
updater: getPrincipalFromIncludedList(
project.relationships!.updater.data,
includedList
),
createdTs: attrs.createdTs,
updatedTs: attrs.updatedTs,
memberList: [],
workflowType: attrs.workflowType,
visibility: attrs.visibility,
tenantMode: attrs.tenantMode,
dbNameTemplate: attrs.dbNameTemplate,
roleProvider: attrs.roleProvider,
};
const memberList: ProjectMember[] = [];
for (const item of includedList || []) {
if (item.type == "projectMember") {
const projectMemberIdList = project.relationships!.projectMember
.data as ResourceIdentifier[];
for (const idItem of projectMemberIdList) {
if (idItem.id == item.id) {
const member = convertMember(item, includedList);
member.project = projectWithoutMemberList;
memberList.push(member);
}
}
}
}
// sort the member list
memberList.sort((a: ProjectMember, b: ProjectMember) => {
if (a.createdTs === b.createdTs) {
return a.id - b.id;
}
return a.createdTs - b.createdTs;
});
return {
...(projectWithoutMemberList as Omit<Project, "memberList">),
memberList,
};
}
// For now, this is exclusively used as part of converting the Project.
// Upon calling, the project itself is not constructed yet, so we return
// an unknown project first.
function convertMember(
projectMember: ResourceObject,
includedList: ResourceObject[]
): ProjectMember {
const attrs = projectMember.attributes as Omit<
ProjectMember,
"id" | "project"
>;
return {
id: parseInt(projectMember.id),
project: unknown("PROJECT") as Project,
creator: getPrincipalFromIncludedList(
projectMember.relationships!.creator.data,
includedList
),
updater: getPrincipalFromIncludedList(
projectMember.relationships!.updater.data,
includedList
),
createdTs: attrs.createdTs,
updatedTs: attrs.updatedTs,
role: attrs.role,
principal: getPrincipalFromIncludedList(
projectMember.relationships!.principal.data,
includedList
),
roleProvider: attrs.roleProvider,
payload: JSON.parse((attrs.payload as unknown as string) || "{}"),
};
}
export const useProjectStore = defineStore("project", {
state: (): ProjectState => ({
projectById: new Map(),
}),
actions: {
convert(instance: ResourceObject, includedList: ResourceObject[]): Project {
return convert(instance, includedList || []);
},
getProjectListByUser(
userId: PrincipalId,
rowStatusList?: RowStatus[]
): Project[] {
const result: Project[] = [];
for (const [_, project] of this.projectById) {
if (
(!rowStatusList && project.rowStatus == "NORMAL") ||
(rowStatusList && rowStatusList.includes(project.rowStatus))
) {
for (const member of project.memberList) {
if (member.principal.id == userId) {
result.push(project);
break;
}
}
}
}
return result;
},
getProjectById(projectId: ProjectId): Project {
if (projectId == EMPTY_ID) {
return empty("PROJECT") as Project;
}
return this.projectById.get(projectId) || (unknown("PROJECT") as Project);
},
async fetchAllProjectList() {
const data = (await axios.get(`/api/project`)).data;
const projectList = data.data.map((project: ResourceObject) => {
return convert(project, data.included);
});
this.upsertProjectList(projectList);
return projectList;
},
async fetchProjectListByUser({
userId,
rowStatusList = [],
}: {
userId: PrincipalId;
rowStatusList?: RowStatus[];
}) {
const projectList: Project[] = [];
const fetchProjectList = async (rowStatus?: RowStatus) => {
let path = `/api/project?user=${userId}`;
if (rowStatus) path += `&rowstatus=${rowStatus}`;
const data = (await axios.get(path)).data;
const list: Project[] = data.data.map((project: ResourceObject) => {
return convert(project, data.included);
});
// projects are mutual excluded by different rowstatus
// so we don't need to unique them by id here
projectList.push(...list);
};
if (rowStatusList.length === 0) {
// if no rowStatus specified, fetch all
await fetchProjectList();
} else {
// otherwise, fetch different rowStatus one-by-one
for (const rowStatus of rowStatusList) {
await fetchProjectList(rowStatus);
}
}
this.upsertProjectList(projectList);
return projectList;
},
async fetchProjectById(projectId: ProjectId) {
const data = (await axios.get(`/api/project/${projectId}`)).data;
const project = convert(data.data, data.included);
this.setProjectById({
projectId,
project,
});
return project;
},
async createProject(newProject: ProjectCreate) {
const data = (
await axios.post(`/api/project`, {
data: {
type: "ProjectCreate",
attributes: newProject,
},
})
).data;
const createdProject = convert(data.data, data.included);
this.setProjectById({
projectId: createdProject.id,
project: createdProject,
});
return createdProject;
},
async patchProject({
projectId,
projectPatch,
}: {
projectId: ProjectId;
projectPatch: ProjectPatch;
}) {
const data = (
await axios.patch(`/api/project/${projectId}`, {
data: {
type: "projectPatch",
attributes: projectPatch,
},
})
).data;
const updatedProject = convert(data.data, data.included);
this.setProjectById({
projectId,
project: updatedProject,
});
return updatedProject;
},
// sync member role from vcs
async syncMemberRoleFromVCS({ projectId }: { projectId: ProjectId }) {
await axios.post(`/api/project/${projectId}/sync-member`);
const updatedProject = await this.fetchProjectById(projectId);
return updatedProject;
},
// Project Role Mapping
// Returns existing member if the principalId has already been created.
async createdMember({
projectId,
projectMember,
}: {
projectId: ProjectId;
projectMember: ProjectMemberCreate;
}) {
await axios.post(`/api/project/${projectId}/member`, {
data: {
type: "projectMemberCreate",
attributes: projectMember,
},
});
const updatedProject = await this.fetchProjectById(projectId);
return updatedProject;
},
async patchMember({
projectId,
memberId,
projectMemberPatch,
}: {
projectId: ProjectId;
memberId: MemberId;
projectMemberPatch: ProjectMemberPatch;
}) {
await axios.patch(`/api/project/${projectId}/member/${memberId}`, {
data: {
type: "projectMemberPatch",
attributes: projectMemberPatch,
},
});
const updatedProject = await this.fetchProjectById(projectId);
return updatedProject;
},
async deleteMember(member: ProjectMember) {
await axios.delete(
`/api/project/${member.project.id}/member/${member.id}`
);
const updatedProject = await this.fetchProjectById(member.project.id);
return updatedProject;
},
setProjectById({
projectId,
project,
}: {
projectId: ProjectId;
project: Project;
}) {
this.projectById.set(projectId, project);
},
upsertProjectList(projectList: Project[]) {
for (const project of projectList) {
this.projectById.set(project.id, project);
}
},
},
});