-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateSuperUser.js
77 lines (63 loc) · 1.99 KB
/
createSuperUser.js
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
#!/usr/bin/env node
import { Command } from 'commander/esm.mjs';
import { createUser } from './src/models/roles.js';
import { StrKey } from 'stellar-sdk'
import { checkUsername } from './src/utils.js';
import shortid from 'shortid';
import { getRole } from './src/models/roles.js';
import { validateUserCreation, profileData } from './src/models/users.js';
import ora from 'ora';
import spinners from 'cli-spinners';
const { dots } = spinners;
const program = new Command();
program
.requiredOption('-u, --username <username>', 'Username')
.requiredOption('-p, --publicKey <publicKey>', 'Stellar Public Key')
.option('-n --name <name>', 'First Name' )
.option('-l --lastName <lastNAme>', 'Last Name' )
program.parse(process.argv);
const spinner = ora({
text: 'Doing some checks...',
color: 'yellow',
spinner: dots
}).start();
if (!StrKey.isValidEd25519PublicKey(program.opts().publicKey)) {
console.error('Invalid Public Key');
process.exit(1);
}
if (!checkUsername(program.opts().username)) {
console.error('Invalid Username');
process.exit(1);
}
const opts = program.opts();
spinner.text = 'Creating user...';
spinner.color = 'green';
const newUser = {
username: opts.username,
publicKey: opts.publicKey,
firstName: opts.name || '',
lastName: opts.lastName || '',
shortID: shortid.generate(),
createdAt: new Date().toString(),
updatedAt: new Date().toString(),
role: getRole("superUser"),
...profileData(opts?.name, opts?.lastName),
};
if (!validateUserCreation(newUser)) {
console.error('Invalid User: ', JSON.stringify(validateUserCreation.errors, null, 2));
process.exit(1);
}
spinner.text = 'Inserting user on database';
spinner.color = 'green';
try {
const user = await createUser(newUser);
console.log('Super user created:');
console.log('Document: ', JSON.stringify(newUser, null, 2));
console.log('Mongo ref: ', JSON.stringify(user, null, 2));
spinner.stop();
process.exit(0);
} catch (err) {
spinner.fail();
console.error(err);
process.exit(1);
}