/
index.ts
506 lines (433 loc) · 11.2 KB
/
index.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/** @module Lib.Workspace */
import { IDataBaseEntry } from '@/database/index.d'
import {
IGroupWithData,
IOrganizationWithData,
IUserWithData,
IWorkspaceGet,
IWorkspaceWithData
} from '../index.d'
import { LIMIT50 } from '@/config/string'
import WorkspaceDB, { INewWorkspace, TWorkspaceGet } from '@/database/workspace'
import User from '../user'
import Group from '../group'
import Organization from '../organization'
import Project from '../project'
/**
* Custom Types
*/
export type TWorkspace = IWorkspaceWithData<
('name' | 'owners' | 'users' | 'groups' | 'projects')[]
>
/**
* Add
* @param user User
* @param workspace Workspace
* @returns Workspace
*/
const add = async (
user: { id: string },
workspace: { name: string }
): Promise<INewWorkspace> => {
// Check name
workspace.name = workspace.name.substring(0, LIMIT50).trim()
// Add workspace
const newWorkspace = await WorkspaceDB.add(user, workspace)
// Add workspace to user
await User.update(user, [
{
type: 'array',
method: 'append',
key: 'workspaces',
value: newWorkspace.id
}
])
// Return
return newWorkspace
}
/**
* Get
* @param id Id
* @param data Data
* @returns Workspace
*/
const get = async <T extends TWorkspaceGet>(
id: string,
data: T
): Promise<IWorkspaceGet<T> | undefined> => {
const workspaceData = await WorkspaceDB.get(id, data)
if (!workspaceData) return
if (data.includes('owners') && !workspaceData.owners)
workspaceData.owners = []
if (data.includes('users') && !workspaceData.users) workspaceData.users = []
if (data.includes('groups') && !workspaceData.groups)
workspaceData.groups = []
if (data.includes('projects') && !workspaceData.projects)
workspaceData.projects = []
if (data.includes('archivedprojects') && !workspaceData.archivedprojects)
workspaceData.archivedprojects = []
return workspaceData as IWorkspaceGet<T>
}
/**
* Get users data
* @param users Users
* @returns Users data
*/
const getUsersData = async (
users: string[]
): Promise<
IUserWithData<('email' | 'lastname' | 'firstname' | 'avatar')[]>[]
> => {
if (!users) return []
const usersData = []
for (const user of users) {
const userData = await User.getWithData(user, [
'lastname',
'firstname',
'email',
'avatar'
])
if (!userData) continue
usersData.push(userData)
}
return usersData
}
/**
* Get groups data
* @param groups Groups
* @returns Groups data
*/
const getGroupsData = async (
groups: string[]
): Promise<IGroupWithData<'name'[]>[]> => {
if (!groups) return []
const groupsData = []
for (const group of groups) {
const groupData = await Group.getWithData(group, ['name'])
if (!groupData) continue
groupsData.push(groupData)
}
return groupsData
}
/**
* Get with data
* @param id Id
* @param data Data
* @returns Workspace
*/
const getWithData = async <T extends TWorkspaceGet>(
id: string,
data: T
): Promise<IWorkspaceWithData<T> | undefined> => {
const workspace = await get(id, data)
if (!workspace) return
const { owners, users, groups, ...workspaceData } = workspace
// Get owners
const ownersData = await getUsersData(owners)
// Get users
const usersData = await getUsersData(users)
// Get groups
const groupsData = await getGroupsData(groups)
// Return
return {
...workspaceData,
users: usersData,
owners: ownersData,
groups: groupsData
} as IWorkspaceWithData<T>
}
/**
* Get workspaces
* @param workspaces Workspaces
* @returns Workspace
*/
const getWorkspaces = async (workspaces: string[]): Promise<TWorkspace[]> => {
const workspacesData = []
for (const workspace of workspaces) {
const workspaceData = await getWithData(workspace, [
'name',
'owners',
'users',
'groups',
'projects'
])
if (!workspaceData) continue
workspacesData.push(workspaceData)
}
return workspacesData
}
/**
* Get organizations data
* @param user User
* @param organizations Organizations
* @returns Data
*/
const getOrganizationsData = async (
user: { id: string },
organizations: IOrganizationWithData<'groups'[]>[]
): Promise<{
groupsWorkspaces: TWorkspace[]
groupsProjects: TWorkspace[]
}> => {
const groupsWorkspaces: TWorkspace[] = []
const groupsProjects: TWorkspace[] = []
for (const organization of organizations) {
for (const group of organization.groups) {
const groupData = await Group.get(group.id, [
'name',
'users',
'workspaces',
'projects'
])
if (!groupData) continue
if (!groupData.users.find((u) => u === user.id)) continue
// Workspaces
if (groupData.workspaces.length) {
const groupWorkspaces = await getWorkspaces(groupData.workspaces)
groupsWorkspaces.push(
...groupWorkspaces.filter(
(w) => !w.owners.find((o) => o.id === user.id)
)
)
}
// Projects
if (groupData.projects.length) {
const customGroup = {
id: group.id,
name: groupData.name
} as IGroupWithData<'name'[]>
const customWorkspace = {
id: groupData.id,
name: 'Projects from ' + groupData.name,
owners: [],
users: [],
groups: [customGroup],
projects: groupData.projects,
archivedprojects: []
}
groupsProjects.push(customWorkspace)
}
}
}
return {
groupsWorkspaces,
groupsProjects
}
}
/**
* Get by user
* @param user User
* @returns Workspaces
*/
const getByUser = async ({ id }: { id: string }): Promise<TWorkspace[]> => {
// Get workspaces'ids
const user = await User.get(id, ['projects', 'workspaces'])
if (!user) return []
const organizations = await Organization.getByUser({ id }, ['groups'])
const workspaces = []
// Get local workspaces
const localWorkspaces = await getWorkspaces(user.workspaces)
workspaces.push(...localWorkspaces)
// Get local projects
if (user.projects?.length)
workspaces.push({
id: 'shared',
name: 'Shared projects',
owners: [],
users: [],
groups: [],
projects: user.projects,
archivedprojects: []
})
// Get organizations workspaces & projects
const { groupsWorkspaces, groupsProjects } = await getOrganizationsData(
{ id },
organizations
)
// Make unique
const uniqueGroupWorkspaces = groupsWorkspaces.filter(
(value, index, self) => self.findIndex((s) => s.id === value.id) === index
)
const uniqueGroupProjects = groupsProjects.filter(
(value, index, self) => self.findIndex((s) => s.id === value.id) === index
)
workspaces.push(...uniqueGroupWorkspaces)
workspaces.push(...uniqueGroupProjects)
return workspaces
}
/**
* Add to group
* @param group Group
* @param workspace Workspace
*/
const addToGroup = async (group: { id: string }, workspace: { id: string }) => {
const groupData = await Group.get(group.id, ['workspaces'])
if (!groupData) return
if (!groupData.workspaces.includes(workspace.id))
await Group.update({ id: group.id }, [
{
key: 'workspaces',
type: 'array',
method: 'append',
value: workspace.id
}
])
}
/**
* Delete from group
* @param group Group
* @param workspace Workspace
*/
const deleteFromGroup = async (
group: { id: string },
workspace: { id: string }
) => {
await Group.update({ id: group.id }, [
{
key: 'workspaces',
type: 'array',
method: 'remove',
value: workspace.id
}
])
}
/**
* Add to user
* @param user User
* @param workspace Workspace
*/
const addToUser = async (
user: { id: string },
workspace: { id: string }
): Promise<void> => {
const userData = await User.get(user.id, ['workspaces'])
if (!userData) return
if (!userData.workspaces.includes(workspace.id))
await User.update({ id: user.id }, [
{
key: 'workspaces',
type: 'array',
method: 'append',
value: workspace.id
}
])
}
/**
* Delete from user
* @param user User
* @param workspace Workspace
*/
const deleteFromUser = async (
user: { id: string },
workspace: { id: string }
) => {
await User.update({ id: user.id }, [
{
key: 'workspaces',
type: 'array',
method: 'remove',
value: workspace.id
}
])
}
/**
* Group update
* @param workspace Workspace
* @param toUpdate Groups to update
*/
const groupUpdate = async (
workspace: { id: string },
toUpdate: { value: string[] }
): Promise<void> => {
// Get data
const workspaceData = await getWithData(workspace.id, ['groups'])
if (!workspaceData) return
// Deleted groups
const deleted = workspaceData.groups.filter(
(group) => !toUpdate.value.includes(group.id)
)
for (const group of deleted) await deleteFromGroup(group, workspace)
// Added groups
const added: string[] = toUpdate.value.filter(
(group: string) => !workspaceData.groups.find((g) => g.id === group)
)
for (const group of added) await addToGroup({ id: group }, workspace)
}
/**
* User update
* @param workspace Workspace
* @param toUpdate Users to udpate
*/
const userUpdate = async (
workspace: { id: string },
toUpdate: { value: string[] }
): Promise<void> => {
// Get data
const workspaceData = await getWithData(workspace.id, ['users'])
if (!workspaceData) return
// Deleted users
const deleted = workspaceData.users.filter(
(user) => !toUpdate.value.includes(user.id)
)
for (const user of deleted) await deleteFromUser(user, workspace)
// Added users
const added: string[] = toUpdate.value.filter(
(user: string) => !workspaceData.users.find((u) => u.id === user)
)
for (const user of added) await addToUser({ id: user }, workspace)
}
/**
* Update
* @param workspace Workspace
* @param data Data
*/
const update = async (
workspace: { id: string },
data: IDataBaseEntry[]
): Promise<void> => {
// Check groups
const groupsUpdate = data.find((d) => d.key === 'groups' && !d.type)
if (groupsUpdate) await groupUpdate(workspace, groupsUpdate)
// Check users
const usersUpdate = data.find((d) => d.key === 'users' && !d.type)
if (usersUpdate) await userUpdate(workspace, usersUpdate)
// Check name
const nameUpdate = data.find((d) => d.key === 'name')
if (nameUpdate) {
nameUpdate.value = nameUpdate.value.substring(0, LIMIT50).trim()
}
// Update workspace
await WorkspaceDB.update(workspace, data)
}
/**
* Delete
* @param user User
* @param workspace Workspace
*/
const del = async (
user: { id: string },
workspace: { id: string }
): Promise<void> => {
// Get data
const data = await getWithData(workspace.id, ['groups', 'projects'])
if (data) {
// Delete from groups
for (const group of data.groups) await deleteFromGroup(group, workspace)
// Delete projects
for (const project of data.projects)
await Project.del(workspace, { id: project })
}
// Delete workspace
await WorkspaceDB.del(workspace)
// Delete workspace reference in user
await User.update(user, [
{
type: 'array',
method: 'remove',
key: 'workspaces',
value: workspace.id
}
])
}
const Workspace = { add, get, getWithData, getByUser, update, del }
export default Workspace