forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-super-admin-account.js
More file actions
55 lines (49 loc) · 1.21 KB
/
create-super-admin-account.js
File metadata and controls
55 lines (49 loc) · 1.21 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
const {
SA_USER_USERNAME,
SA_USER_PASSWORD,
SA_USER_DISPLAY_NAME,
SA_USER_LANG,
} = process.env
const createSuperAdminAccount = async ({
collections,
transaction,
bcrypt,
}) => {
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Transaction
const trx = await transaction(collectionStrings)
let user
try {
await trx.step(async () => {
user = await collections.users.save({
displayName: SA_USER_DISPLAY_NAME,
userName: String(SA_USER_USERNAME).toLowerCase(),
password: bcrypt.hashSync(SA_USER_PASSWORD, 10),
preferredLang: SA_USER_LANG,
phoneValidated: false,
emailValidated: false,
failedLoginAttempts: 0,
tfaSendMethod: 'none',
})
})
} catch (err) {
throw new Error(
`Transaction step error occurred while creating new super admin account: ${err}`,
)
}
try {
await trx.commit()
} catch (err) {
throw new Error(
`Transaction commit error occurred while creating new super admin account: ${err}`,
)
}
return user
}
module.exports = {
createSuperAdminAccount,
}