-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
713 lines (628 loc) · 21.1 KB
/
Copy pathindex.js
File metadata and controls
713 lines (628 loc) · 21.1 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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import {
CREATE_WORKSPACE,
SET_WORKSPACES_LIST,
LEAVE_WORKSPACE,
DELETE_WORKSPACE,
SET_CURRENT_WORKSPACE,
INVITE_TO_WORKSPACE,
CONFIRM_INVITE,
JOIN_BY_INVITE_LINK,
UPDATE_WORKSPACE,
FETCH_WORKSPACE,
GRANT_ADMIN_PERMISSIONS,
REMOVE_USER_FROM_WORKSPACE,
GET_BUSINESS_OPERATIONS,
GET_BALANCE,
CHANGE_WORKSPACE_PLAN_FOR_FREE_PLAN,
CANCEL_SUBSCRIPTION,
PAY_WITH_CARD
} from './actionTypes';
import { REMOVE_PROJECTS_BY_WORKSPACE_ID } from '../projects/actionTypes';
import { RESET_STORE } from '../../methodsTypes';
import * as workspaceApi from '../../../api/workspaces/index.ts';
import * as billingApi from '../../../api/billing';
import Vue from 'vue';
import { isPendingMember } from '@/store/modules/workspaces/helpers';
/**
* Mutations enum for this module
*/
const mutationTypes = {
ADD_WORKSPACE: 'ADD_WORKSPACE', // Add new workspace to the list
REMOVE_WORKSPACE: 'REMOVE_WORKSPACE', // Remove workspace from the list
SET_WORKSPACES_LIST: 'SET_WORKSPACES_LIST', // Set new workspaces list
SET_CURRENT_WORKSPACE: 'SET_CURRENT_WORKSPACE', // Set current user workspace,
ADD_PENDING_MEMBER: 'ADD_PENDING_MEMBER', // Add user to workspace
REMOVE_MEMBER: 'REMOVE_MEMBER', // Remove user from workspace
REMOVE_PENDING_MEMBER: 'REMOVE_PENDING_MEMBER', // Remove pending user from workspace
SET_WORKSPACE: 'SET_WORKSPACE', // Set workspace to user workspaces list
UPDATE_MEMBER: 'UPDATE_MEMBER', // Update member in the workspace,
UPDATE_BUSINESS_OPERATIONS: 'UPDATE_BUSINESS_OPERATIONS', // Add a new business operation to the operations history
UPDATE_BALANCE: 'UPDATE_BALANCE', // Update workspace balance
SET_BUSINESS_OPERATIONS: 'SET_BUSINESS_OPERATIONS', // Set billing history
SET_PLAN: 'SET_PLAN', // Set workspace tariff plan
PUSH_BUSINESS_OPERATION: 'PUSH_BUSINESS_OPERATION', // push new business operation to workspace payments history
};
/**
* @typedef {object} Workspace - represents workspace
* @property {string} id - workspace id
* @property {string} name - workspace name
* @property {string} [image] - link to the workspace picture
* @property {string} [description] - workspace description
*/
/**
* Module state
*
* @typedef {object} WorkspacesModuleState
* @property {Array<Workspace>} list - registered workspaces
* @property {Workspace} current - current user workspace
*/
/**
* Creates module state
*
* @returns {WorkspacesModuleState}
*/
function initialState() {
return {
list: [],
current: null,
};
}
/**
* All Vuex getters will be stored under this namespace
*
* @namespace Getters
*/
const getters = {
/**
* Returns workspace by id
*
* @param {WorkspacesModuleState} state - Vuex state
* @returns {function(string): Workspace}
*/
getWorkspaceById: state =>
/**
* @param {string} id project id to find
* @returns {Project}
*/
id => state.list.find(workspace => workspace.id === id),
/**
* Returns current user in the provided workspace
*
* @param {WorkspacesModuleState} state - Vuex state
* @param {object} getters - getters of the this module
* @param {object} rootState - vuex root state
* @returns {function(*): ConfirmedMember}
*/
getCurrentUserInWorkspace: (state, getters, rootState) =>
/**
* @param workspace - workspace to get user
* @returns {ConfirmedMember}
*/
(workspace) => {
const user = rootState.user.data;
return workspace.team.find(_member => !isPendingMember(_member) && _member.user.id === user.id);
},
/**
* Is current user admin in workspace
*
* @param {WorkspacesModuleState} state - Vuex state
* @param {object} getters - getters of the this module
* @param {object} rootState - vuex root state
* @returns {function(*): boolean}
*/
isCurrentUserAdmin: (state, getters, rootState) =>
/**
* @param workspaceId - workspace id to check
* @returns {boolean}
*/
(workspaceId) => {
const workspace = getters.getWorkspaceById(workspaceId);
const userId = rootState.user.data.id;
return workspace.team.some(member => member.user && member.user.id === userId && member.isAdmin);
},
};
const actions = {
/**
* Send request to create new workspace
*
* @param {Function} commit - standard Vuex commit function
* @param {Workspace} workspace - workspace params for creation
* @returns {Workspace} - created workspace
*/
async [CREATE_WORKSPACE]({ commit }, workspace) {
const response = await workspaceApi.createWorkspace(workspace);
const createdWorkspace = response.data.workspace;
commit(mutationTypes.ADD_WORKSPACE, createdWorkspace);
return createdWorkspace;
},
/**
* Send request to leave workspace
*
* @param {object} context - vuex action context
* @param {Function} context.commit - standard Vuex commit function
* @param {Function} context.dispatch - standard Vuex dispatch function
* @param {string} workspaceId - id of workspace for deleting
* @returns {boolean} - check if is there other admins are there.
*/
async [LEAVE_WORKSPACE]({ commit, dispatch }, workspaceId) {
const success = await workspaceApi.leaveWorkspace(workspaceId);
if (!success) {
return false;
}
dispatch(REMOVE_PROJECTS_BY_WORKSPACE_ID, workspaceId);
commit(mutationTypes.SET_CURRENT_WORKSPACE, null);
commit(mutationTypes.REMOVE_WORKSPACE, workspaceId);
},
/**
* Send request to delete workspace
*
* @param {object} context - vuex action context
* @param {Function} context.commit - standard Vuex commit function
* @param {Function} context.dispatch - standard Vuex dispatch function
* @param {string} workspaceId - id of workspace for deleting
*/
async [DELETE_WORKSPACE]({ commit, dispatch }, workspaceId) {
await workspaceApi.deleteWorkspace(workspaceId);
dispatch(REMOVE_PROJECTS_BY_WORKSPACE_ID, workspaceId);
commit(mutationTypes.SET_CURRENT_WORKSPACE, null);
commit(mutationTypes.REMOVE_WORKSPACE, workspaceId);
},
/**
* Sent request to invite user to workspace
*
* @param {object} context - vuex action context
* @param {Function} context.commit - standard Vuex commit function
*
* @param {object} payload - vuex action payload
* @param {string} payload.workspaceId - id of workspace to which user is invited
* @param {string} payload.userEmail - email of the invited user
*/
async [INVITE_TO_WORKSPACE]({ commit }, { workspaceId, userEmail }) {
const response = await workspaceApi.inviteToWorkspace(workspaceId, userEmail);
/**
* Response can contain errors, so we should handle only existed fields
*/
if (!response.data || !response.data.inviteToWorkspace) {
return false;
}
commit(mutationTypes.ADD_PENDING_MEMBER, {
workspaceId,
data: {
email: userEmail,
},
});
return true;
},
/**
* Send request to confirm user invitation
*
* @param {object} context - vuex action context
*
* @param {object} payload - vuex action payload
* @param {string} payload.workspaceId - id of workspace to which user is invited
* @param {string} payload.inviteHash - hash passed to the invite link
*/
async [CONFIRM_INVITE](context, { workspaceId, inviteHash }) {
await workspaceApi.confirmInvite(workspaceId, inviteHash);
},
/**
* Send request to confirm user invitation
*
* @param {object} context - vuex action context
* @param {object} payload - vuex action payload
* @param {string} payload.inviteHash - hash passed to the invite link
*/
async [JOIN_BY_INVITE_LINK](context, { inviteHash }) {
await workspaceApi.joinByInviteLink(inviteHash);
},
/**
* Sets current user workspace
*
* @param {Function} commit - standard Vuex commit function
* @param {Workspace} workspace - new current user workspace
*/
[SET_CURRENT_WORKSPACE]({ commit }, workspace) {
commit(SET_CURRENT_WORKSPACE, workspace);
},
/**
* Sets new workspaces list
*
* @param {Function} commit - standard Vuex commit function
* @param {[Workspace]} workspaces - new workspaces list
*/
[SET_WORKSPACES_LIST]({ commit }, workspaces) {
commit(SET_WORKSPACES_LIST, workspaces);
},
/**
* Get workspaces by ids
*
* @param {Function} commit - standard Vuex commit function
* @param {number} id – workspace id to fetch
* @returns {Promise<Workspace>}
*/
async [FETCH_WORKSPACE]({ commit }, id) {
const workspace = (await workspaceApi.getWorkspaces([id]))[0];
if (!workspace) {
throw new Error('The workspace was not found');
}
commit(mutationTypes.SET_WORKSPACE, workspace);
return workspace;
},
/**
* Update workspace data
*
* @param {Function} commit - standard Vuex commit function
* @param {Workspace} workspace - workspace to update
* @returns {Promise<boolean>}
*/
async [UPDATE_WORKSPACE]({ commit }, workspace) {
const isSaved = await workspaceApi.updateWorkspace(workspace.id, workspace.name, workspace.description, workspace.image);
if (isSaved) {
commit(mutationTypes.SET_WORKSPACE, workspace);
}
return isSaved;
},
/**
* Grant admin permissions
*
* @param {object} context - vuex action context
* @param {Function} context.commit - standard Vuex commit method
*
* @param {object} payload - vuex action payload
* @param {string} payload.workspaceId - id of workspace where user is participate
* @param {string} payload.userId - id of user to grant permissions
* @param {boolean} payload.state - if true, grant permissions, if false, withdraw them
*
* @returns {Promise<boolean>}
*/
async [GRANT_ADMIN_PERMISSIONS]({ commit }, { workspaceId, userId, state = true }) {
const success = await workspaceApi.grantAdminPermissions(workspaceId, userId, state);
if (!success) {
return false;
}
const changes = {
isAdmin: state,
};
commit(mutationTypes.UPDATE_MEMBER, {
workspaceId,
userId,
changes,
});
},
/**
* Remove user from workspace
*
* @param {object} context - vuex action context
* @param {Function} context.commit - standard Vuex dispatch methods
*
* @param {object} payload - vuex action payload
* @param {string} payload.workspaceId - id of workspace where user is participate
* @param {string} payload.userId - id of user to remove
* @param {string} payload.userEmail - user email to remove (instead of id)
*
* @returns {Promise<*>}
*/
async [REMOVE_USER_FROM_WORKSPACE]({ commit }, { workspaceId, userId, userEmail }) {
const success = await workspaceApi.removeUserFromWorkspace(workspaceId, userId, userEmail);
if (!success) {
return;
}
if (userId) {
commit(mutationTypes.REMOVE_MEMBER, {
workspaceId,
userId,
});
} else {
commit(mutationTypes.REMOVE_PENDING_MEMBER, {
workspaceId,
userEmail,
});
}
},
/**
* Fetch payment operations and save them to the store
*
* @param {Function} commit - standard Vuex commit method
* @param {string[]} ids - workspaces ids
* @returns {Promise<void>}
*/
async [GET_BUSINESS_OPERATIONS]({ commit }, { ids }) {
const operations = await billingApi.getBusinessOperations(ids);
commit(mutationTypes.SET_BUSINESS_OPERATIONS, operations || []);
},
/**
* Process payment via saved card
*
* @param {Function} commit - standard Vuex commit method
* @param {PayWithCardInput} args - data for payments
* @returns {Promise<void>}
*/
async [PAY_WITH_CARD]({ commit }, args) {
const operation = await billingApi.payWithCard(args);
commit(mutationTypes.PUSH_BUSINESS_OPERATION, operation);
return operation;
},
/**
* Fetch balance of workspace or workspaces
*
* @param {Function} commit - standard Vuex commit method
* @param {string[]} ids - workspaces ids
*/
async [GET_BALANCE]({ commit }, { ids }) {
const balances = (await workspaceApi.getBalance(ids)) || [];
balances.forEach(balanceWithId => {
commit(mutationTypes.UPDATE_BALANCE, {
workspaceId: balanceWithId.id,
balance: balanceWithId.balance,
});
});
},
/**
* Call change plan for free plan mutation
*
* @param {Function} commit - VueX commit method
* @param {string} workspaceId - id of workspace to change plan
* @returns {Promise<void>}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars-experimental
async [CHANGE_WORKSPACE_PLAN_FOR_FREE_PLAN]({ commit }, { workspaceId }) {
const result = await workspaceApi.changePlanForFreePLan(workspaceId);
commit(mutationTypes.SET_PLAN, {
workspaceId,
plan: result.record.plan,
});
return result;
},
/**
* Call API to cancel subscription on workspace
*
* @param {Function} commit - VueX commit method
* @param {string} workspaceId - id of workspace to change plan
* @returns {Promise<void>}
*/
async [CANCEL_SUBSCRIPTION]({ commit }, { workspaceId }) {
const data = await workspaceApi.cancelSubscription(workspaceId);
commit(mutationTypes.SET_WORKSPACE, data);
},
/**
* Resets module state
*
* @param {Function} commit - standard Vuex commit function
*/
[RESET_STORE]({ commit }) {
commit(RESET_STORE);
},
};
const mutations = {
/**
* Set workspace data
*
* @param {WorkspacesModuleState} state - Vuex state
* @param {Workspace} workspace - workspace params for setting
*/
[mutationTypes.SET_WORKSPACE](state, workspace) {
const index = state.list.findIndex(element => element.id === workspace.id);
if (state.current && workspace.id === state.current.id) {
state.current = workspace;
}
state.list = [
...state.list.slice(0, index),
{
...state.list[index],
...workspace,
},
...state.list.slice(index + 1),
];
},
/**
* Mutation for adding new workspace
*
* @param {WorkspacesModuleState} state - Vuex state
* @param {Workspace} workspace - workspace params for creation
*/
[mutationTypes.ADD_WORKSPACE](state, workspace) {
state.list.push(workspace);
},
/**
* Mutation for deleting workspaces
*
* @param {WorkspacesModuleState} state - Vuex state
* @param {string} workspaceId - id of workspace for deleting
*/
[mutationTypes.REMOVE_WORKSPACE](state, workspaceId) {
let index = null;
state.list.find((element, i) => {
if (element.id === workspaceId) {
index = i;
}
});
if (index !== null) {
state.list.splice(index, 1);
}
},
/**
* Mutation for replacing workspaces list
*
* @param {WorkspacesModuleState} state - Vuex state
* @param {Array<Workspace>} newList - new list of workspaces
*/
[mutationTypes.SET_WORKSPACES_LIST](state, newList) {
Vue.set(state, 'list', newList);
},
/**
* Sets current user workspace
*
* @param {WorkspacesModuleState} state - Vuex state
* @param {Workspace | null} workspace - new current user workspace
*/
[mutationTypes.SET_CURRENT_WORKSPACE](state, workspace) {
state.current = workspace;
},
/**
* Update member in the user list
*
* @param {WorkspacesModuleState} state - current state
*
* @param {object} payload - vuex mutation payload
* @param {string} payload.workspaceId - id of workspace where user should be updated
* @param {string} payload.userId - id of user to update
* @param {object} payload.changes - changes to update
*/
[mutationTypes.UPDATE_MEMBER](state, { workspaceId, userId, changes }) {
const workspaceIndex = state.list.findIndex(w => w.id === workspaceId);
const memberIndex = state.list[workspaceIndex].team.findIndex(member => !isPendingMember(member) && member.user.id === userId);
Object.assign(state.list[workspaceIndex].team[memberIndex], changes);
},
/**
* Add a new business operation to the workspace operations history
*
* @param {WorkspacesModuleState} state - current state
* @param {string} payload.workspaceId - id of workspace where user should be updated
* @param {BusinessOperation} payload.businessOperation - business operation to add to operations history
*/
[mutationTypes.UPDATE_BUSINESS_OPERATIONS](state, { workspaceId, businessOperation }) {
const index = state.list.findIndex(w => w.id === workspaceId);
const workspace = state.list[index];
let updatedPaymentsHistory = [businessOperation];
if (workspace.paymentsHistory) {
updatedPaymentsHistory = [businessOperation].concat(workspace.paymentsHistory);
}
Vue.set(workspace, 'paymentsHistory', updatedPaymentsHistory);
},
/**
* Update workspace balance
*
* @param {WorkspacesModuleState} state - current state
* @param {string} payload.workspaceId - id of workspace where user should be updated
* @param {number} payload.amount - business operation to add to operations history
*/
[mutationTypes.UPDATE_BALANCE](state, { workspaceId, balance }) {
const index = state.list.findIndex(w => w.id === workspaceId);
const workspace = state.list[index];
Vue.set(workspace, 'balance', balance);
},
/**
* Add pending member to workspace
*
* @param {WorkspacesModuleState} state - current state
*
* @param {object} payload - vuex mutation payload
* @param {string} payload.workspaceId - id of workspace to which user should be added
* @param {object} payload.data - user data
*/
[mutationTypes.ADD_PENDING_MEMBER](state, { workspaceId, data }) {
const workspaceIndex = state.list.findIndex(w => w.id === workspaceId);
state.list[workspaceIndex].team.push(data);
},
/**
* Remove member from workspace
*
* @param {WorkspacesModuleState} state - current state
*
* @param {object} payload - vuex mutation payload
* @param {string} payload.workspaceId - id of workspace from which user should be removed
* @param {string} payload.userId - id of user to remove
*/
[mutationTypes.REMOVE_MEMBER](state, { workspaceId, userId }) {
const workspaceIndex = state.list.findIndex(w => w.id === workspaceId);
const memberIndex = state.list[workspaceIndex].team.findIndex(member => member.user.id === userId);
if (memberIndex > -1) {
state.list[workspaceIndex].team.splice(memberIndex, 1);
}
},
/**
* Remove pending member from workspace
*
* @param {WorkspacesModuleState} state - current state
*
* @param {object} payload - vuex mutation payload
* @param {string} payload.workspaceId - id of workspace from which user should be removed
* @param {string} payload.userEmail - email of user to remove
*/
[mutationTypes.REMOVE_PENDING_MEMBER](state, { workspaceId, userEmail }) {
const workspaceIndex = state.list.findIndex(w => w.id === workspaceId);
const memberIndex = state.list[workspaceIndex].team.findIndex(m => m.email === userEmail);
if (memberIndex > -1) {
state.list[workspaceIndex].team.splice(memberIndex, 1);
}
},
/**
* Set transactions info to workspaces by ids
*
* @param {WorkspacesModuleState} state - current state
* @param {BusinessOperation[]} operations - operations to set
*/
[mutationTypes.SET_BUSINESS_OPERATIONS](state, operations = []) {
/**
* Group all operations by payload.workspace.id
* {
* [workspace.id] => BusinessOperation,
* ...
* }
*/
const groupedByWorkspaceId = operations.reduce(
(acc, operation) => {
const { workspace } = operation.payload;
if (!acc[workspace.id]) {
acc[workspace.id] = [];
}
acc[workspace.id].push(operation);
return acc;
},
{}
);
Object.entries(groupedByWorkspaceId)
.forEach(([workspaceId, operationsOfWorkspace]) => {
const index = state.list.findIndex(w => w.id === workspaceId);
const workspace = state.list[index];
Vue.set(workspace, 'paymentsHistory', operationsOfWorkspace);
state.list = [
...state.list.slice(0, index),
workspace,
...state.list.slice(index + 1),
];
}
);
},
/**
* Push new business operation to workspace payments history
*
* @param {object} state - module state
* @param {BusinessOperation} operation - operation to push
*/
[mutationTypes.PUSH_BUSINESS_OPERATION](state, operation) {
const workspace = state.list.find(w => w.id === operation.payload.workspace.id);
Vue.set(workspace, 'paymentsHistory', [operation, ...workspace.paymentsHistory]);
},
/**
* Set workspace plan
*
* @param {object} state - module state
* @param {string} workspaceId - id of workspace to set plan
* @param {Plan} plan - plan to set
*/
[mutationTypes.SET_PLAN](state, { workspaceId, plan }) {
const index = state.list.findIndex(w => w.id === workspaceId);
const workspace = state.list[index];
Vue.set(workspace, 'plan', plan);
state.list = [
...state.list.slice(0, index),
workspace,
...state.list.slice(index + 1),
];
},
/**
* Resets module state
*
* @param {WorkspacesModuleState} state - Vuex state
*/
[RESET_STORE](state) {
Object.assign(state, initialState());
},
};
export default {
state: initialState(),
getters,
actions,
mutations,
};