forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuper-admin-service.js
More file actions
110 lines (106 loc) · 3.23 KB
/
super-admin-service.js
File metadata and controls
110 lines (106 loc) · 3.23 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
const {
checkForSuperAdminAccount,
checkForSuperAdminAffiliation,
checkForSuperAdminOrg,
createSuperAdminAccount,
createSuperAdminAffiliation,
createSuperAdminOrg,
removeSuperAdminAffiliation,
} = require('./database')
const superAdminService = async ({
query,
collections,
transaction,
bcrypt,
log,
}) => {
log('Checking for super admin account.')
// Check to see if default super admin account or org already exists
const admin = await checkForSuperAdminAccount({ query })
const org = await checkForSuperAdminOrg({ query })
let newOrg, newAdmin
// No super admin account or org is found
if (typeof admin === 'undefined' && typeof org === 'undefined') {
log('Super admin account, and org not found, creating new account.')
newAdmin = await createSuperAdminAccount({
collections,
transaction,
bcrypt,
})
newOrg = await createSuperAdminOrg({ collections, transaction })
await createSuperAdminAffiliation({
collections,
transaction,
org: newOrg,
admin: newAdmin,
})
log('Super admin account, org, and affiliation creation successful.')
log('Exiting now.')
}
// Admin account is not found, but org is found
else if (typeof admin === 'undefined' && typeof org !== 'undefined') {
log(
'Super admin account not found, Super admin org found. Creating account.',
)
newAdmin = await createSuperAdminAccount({
collections,
transaction,
bcrypt,
})
log('Removing old super admin affiliation.')
await removeSuperAdminAffiliation({ query, collections, transaction })
log('Creating new super admin affiliation')
await createSuperAdminAffiliation({
collections,
transaction,
org,
admin: newAdmin,
})
log('Super admin account, and affiliation creation successful.')
log('Exiting now.')
}
// Account is found but org is not
else if (typeof admin !== 'undefined' && typeof org === 'undefined') {
log(
'Super admin org not found, Super admin account found. Creating super admin org.',
)
newOrg = await createSuperAdminOrg({ collections, transaction })
log('Removing old super admin affiliation.')
await removeSuperAdminAffiliation({ query, collections, transaction })
log('Creating new super admin affiliation')
await createSuperAdminAffiliation({
collections,
transaction,
org: newOrg,
admin,
})
log('Super admin org, and affiliation creation successful.')
log('Exiting now.')
}
// Account and org are found
else {
log('Found super admin account, and org. Checking for affiliation.')
// Check to see if affiliation exists
const affiliation = await checkForSuperAdminAffiliation({ query })
// Affiliation is not found
if (typeof affiliation === 'undefined') {
log('Super admin affiliation not found, creating new affiliation.')
await createSuperAdminAffiliation({
collections,
transaction,
org,
admin,
})
log('Super admin affiliation creation successful.')
log('Exiting now.')
}
// Affiliation is found
else {
log('Super admin affiliation found.')
log('Exiting now.')
}
}
}
module.exports = {
superAdminService,
}