aboutsummaryrefslogtreecommitdiff
path: root/src/api/database/seeds/initial.js
blob: 7c04daddcd0734e272fbdf07b3d63cdacf349a14 (plain) (blame)
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
/* eslint-disable no-console */
const bcrypt = require('bcrypt');
const moment = require('moment');
const Util = require('../../utils/Util');

exports.seed = async db => {
	const now = moment.utc().toDate();

	// Save environment variables to the database
	try {
		const defaults = Util.getEnvironmentDefaults();
		const keys = Object.keys(defaults);
		for (const item of keys) {
			await Util.writeConfigToDb({
				key: item,
				value: defaults[item]
			});
		}
	} catch (error) {
		console.error(error);
	}

	// Create admin user if it doesnt exist
	const user = await db.table('users').where({ username: 'admin' }).first();
	if (user) {
		console.log();
		console.log('=========================================================');
		console.log('==       admin account already exists, skipping.       ==');
		console.log('=========================================================');
		console.log('==    Run `pm2 start pm2.json` to start the service    ==');
		console.log('=========================================================');
		console.log();
		return;
	}
	try {
		const hash = await bcrypt.hash('admin', 10);
		await db.table('users').insert({
			username: 'admin',
			password: hash,
			passwordEditedAt: now,
			createdAt: now,
			editedAt: now,
			enabled: true,
			isAdmin: true
		});
		console.log();
		console.log('=========================================================');
		console.log('==       Successfully created the admin account.       ==');
		console.log('=========================================================');
		console.log('==    Run `pm2 start pm2.json` to start the service    ==');
		console.log('=========================================================');
		console.log();
	} catch (error) {
		console.error(error);
	}
};