-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-tool.js
More file actions
323 lines (285 loc) · 9.17 KB
/
Copy pathadmin-tool.js
File metadata and controls
323 lines (285 loc) · 9.17 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
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */
const dbConfigs = require('./app/config/database-config');
const {Sequelize} = require('sequelize');
const sequelize = require('sequelize');
const nodemailer = require('nodemailer');
const hbs = require('nodemailer-express-handlebars-plaintext-inline-ccs');
const exhbs = require('express-handlebars');
const registerUser = async (argv) => {
if (argv.verbose) console.info(`register user: ${argv.requestId}`);
const requestId = argv.requestId;
const dbConfig = dbConfigs[argv.environment];
if (argv.verbose) console.dir(dbConfig);
const database = new Sequelize(dbConfig.database,
dbConfig.username,
dbConfig.password || '',
dbConfig);
try {
await database.authenticate();
} catch (err) {
console.error(err);
process.exit(1);
}
if (argv.verbose) console.log('Successfully connected to database');
let userRequest = await database.query(
`SELECT id, username, email, password, "profilePic" FROM `+
`"userRequests" WHERE id=${requestId}`,
{type: sequelize.QueryTypes.SELECT},
);
if (userRequest.length <= 0) {
console.error(`No request with id ${requestId} exists`);
process.exit(1);
} else if (userRequest.length > 1) {
console.error(`There are multiple entries for id` +
` ${requestId}. This should not happen`);
process.exit(1);
} else {
userRequest = userRequest[0];
}
// Start transaction for creating the new user
const transaction = await database.transaction();
if (argv.verbose) console.info('Started transaction');
let user;
try {
if (argv.verbose) console.info('Insert new user');
user = await database
.getQueryInterface().bulkInsert('users', [{
username: userRequest.username,
password: userRequest.password,
email: userRequest.email,
isBetaUser: false,
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: null,
}], {
transaction,
returning: true,
});
user = user[0];
if (!user) {
throw new Error('Could not insert user');
}
if (argv.verbose) console.info('User was inserted successfully');
if (argv.verbose) console.info('Insert new profile picture');
await database.getQueryInterface().bulkInsert('profilePics', [{
userId: user.id,
data: userRequest.profilePic,
}], {
transaction,
});
if (argv.verbose) {
console.info('Profile picture was inserted successfully');
}
if (!argv.noEmail) {
if (argv.verbose) console.info('Prepare sending email');
const options = {
service: argv.emailService,
host: argv.emailHost,
port: argv.emailPort,
auth: {
user: argv.emailUser,
pass: argv.emailPass,
},
};
if (argv.verbose) console.dir(options);
const transporter = nodemailer.createTransport(options);
if (argv.verbose) console.info('Prepared transporter');
transporter.use('compile', hbs({
viewEngine: exhbs.create({
layoutsDir: 'app/views/layouts',
partialsDir: 'app/views/partials',
}),
templatesDir: 'app/views',
plaintextOptions: {
uppercaseHeadings: false,
},
viewPath: 'app/views',
}));
if (argv.verbose) console.info('Registered view engine');
if (argv.verbose) console.info(`Sending email to ${userRequest.email}`);
await transporter.sendMail({
from: '"my-group-car.de" <mygroupcar@gmail.com',
to: user.email,
subject: `Your user account was approved`,
template: 'approved-request-email',
context: {
layout: 'approve-main',
id: user.id,
username: user.username,
email: user.email,
website: argv.website,
profilePicData: Buffer.from(userRequest.profilePic)
.toString('base64'),
},
});
if (argv.verbose) console.info('Successfully sent email');
}
if (argv.verbose) console.info('Delete user request');
await database.getQueryInterface().bulkDelete('userRequests',
{id: userRequest.id}, {transaction});
if (argv.verbose) console.info('User was deleted successfully');
} catch (err) {
console.error(err);
await transaction.rollback();
if (argv.verbose) console.info('Transaction rolled back');
await database.close();
process.exit(1);
}
// Commit transaction
await transaction.commit();
if (argv.verbose) console.info('Transaction successfully committed');
await database.close();
console.info(`Successfully registered user with id ${requestId},` +
` new assigned id is ${user.id}`);
process.exit(0);
};
const listUsers = async (argv) => {
if (argv.verbose) console.info(`list all users`);
const dbConfig = dbConfigs[argv.environment];
if (argv.verbose) console.dir(dbConfig);
const database = new Sequelize(dbConfig.database,
dbConfig.username,
dbConfig.password || '',
dbConfig);
try {
await database.authenticate();
} catch (err) {
console.error(err);
process.exit(1);
}
if (argv.verbose) console.info('Successfully connected to database');
const users = await database.query(
'SELECT id, username, email FROM "userRequests"',
{type: sequelize.QueryTypes.SELECT},
);
console.info(`Found ${users.length} user requests\n`);
const longestNumber = users.map((user) => String(user.id).length)
.reduce((prev, curr) => prev > curr ? prev : curr, 0);
// Print
users.forEach((user) => {
const currentIdLength = String(user.id).length;
console.info(`[${' '.repeat(longestNumber - currentIdLength) +
user.id}] Username: ` +
`${user.username + ' '.repeat(25 - user.username.length)} ` +
`| Email: ${user.email}`);
});
await database.close();
process.exit(0);
};
const removeRequest = async ({verbose, requestId, environment}) => {
if (verbose) console.info(`remove ${requestId}`);
const dbConfig = dbConfigs[environment];
if (verbose) console.dir(dbConfig);
const database = new Sequelize(dbConfig.database,
dbConfig.username,
dbConfig.password || '',
dbConfig);
try {
await database.authenticate();
} catch (err) {
console.error(err);
process.exit(1);
}
if (verbose) console.info('Successfully connected to database');
try {
await database.getQueryInterface().bulkDelete('userRequests', [{
id: requestId,
}]);
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit(0);
};
require('yargs')
.command(
'user-request:register [requestId]',
'Register a user', (yargs) => {
yargs.positional(
'requestId', {
describe: 'The id of the user creation request',
},
);
yargs.option(
'noEmail', {
type: 'boolean',
describe: 'Whether or not the user should receive ' +
'an email if he/she was successfully registered',
},
);
yargs.option(
'emailUser', {
type: 'string',
describe: 'The username of the email account ' +
'to use for sending the email',
default: process.env.MAIL_ACCOUNT_REQUEST_USER,
},
);
yargs.option(
'emailPass', {
type: 'string',
describe: 'The pass to use for authenticating the emailUser',
default: process.env.MAIL_ACCOUNT_REQUEST_PASS,
},
);
yargs.option(
'emailService', {
type: 'string',
describe: 'The name of the email service',
default: 'gmail',
},
);
yargs.option(
'emailPort', {
type: 'number',
describe: 'The smtp port to use',
default: process.env.MAIL_ACCOUNT_REQUEST_PORT,
},
);
yargs.option(
'emailHost', {
type: 'string',
describe: 'The hostname of the email provider',
default: process.env.MAIL_ACCOUNT_REQUEST_HOST,
},
);
},
registerUser,
)
.command(
'user-request:list',
'List all user-requests',
{},
listUsers,
)
.command(
'user-request:remove [requestId]',
'Remove a user request',
(yargs) => {
yargs.positional(
'requestId',
{
describe: 'The id of the user creation request',
},
);
},
removeRequest,
)
.option('verbose', {
alias: 'v',
type: 'boolean',
describe: 'Run with verbose logging',
})
.option('environment', {
alias: 'e',
type: 'string',
describe: 'The environment in which the tool should operate',
default: 'development',
}).option(
'website', {
type: 'string',
describe: 'The website for which the tool should operate',
default: 'my-group-car.de',
},
).argv;