forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-super-admin-affiliation.js
More file actions
47 lines (42 loc) · 1016 Bytes
/
create-super-admin-affiliation.js
File metadata and controls
47 lines (42 loc) · 1016 Bytes
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
const createSuperAdminAffiliation = async ({
collections,
transaction,
org,
admin,
}) => {
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Transaction
const trx = await transaction(collectionStrings)
let affiliation
try {
await trx.step(async () => {
affiliation = await collections.affiliations.save({
_from: org._id,
_to: admin._id,
permission: 'super_admin',
defaultSA: true,
})
})
} catch (err) {
await trx.abort()
throw new Error(
`Transaction step error occurred while creating new super admin affiliation: ${err}`,
)
}
try {
await trx.commit()
} catch (err) {
await trx.abort()
throw new Error(
`Transaction commit error occurred while creating new super admin affiliation: ${err}`,
)
}
return affiliation
}
module.exports = {
createSuperAdminAffiliation,
}