-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathsetup.js
101 lines (79 loc) · 2.59 KB
/
setup.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require('dotenv').config({ path: '.env' });
require('dotenv').config({ path: '.env.local' });
const { globSync } = require('glob');
const fs = require('fs');
const { generate: uniqueId } = require('shortid');
const mongoose = require('mongoose');
const setup = async (req, res) => {
const Admin = mongoose.model('Admin');
const AdminPassword = mongoose.model('AdminPassword');
const Setting = mongoose.model('Setting');
const PaymentMode = mongoose.model('PaymentMode');
const Taxes = mongoose.model('Taxes');
const newAdminPassword = new AdminPassword();
const { name, email, password, language, timezone, country, config = {} } = req.body;
const objectSchema = Joi.object({
name: Joi.string().required(),
email: Joi.string()
.email({ tlds: { allow: true } })
.required(),
password: Joi.string().required(),
});
const { error, value } = objectSchema.validate({ name, email, password });
if (error) {
return res.status(409).json({
success: false,
result: null,
error: error,
message: 'Invalid/Missing credentials.',
errorMessage: error.message,
});
}
const salt = uniqueId();
const passwordHash = newAdminPassword.generateHash(salt, password);
const accountOwnner = {
email,
name,
role: 'owner',
};
const result = await new Admin(accountOwnner).save();
const AdminPasswordData = {
password: passwordHash,
emailVerified: true,
salt: salt,
user: result._id,
};
await new AdminPassword(AdminPasswordData).save();
const settingData = [];
const settingsFiles = globSync('./src/setup/defaultSettings/**/*.json');
for (const filePath of settingsFiles) {
const file = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
const settingsToUpdate = {
idurar_app_email: email,
idurar_app_company_email: email,
idurar_app_timezone: timezone,
idurar_app_country: country,
idurar_app_language: language || 'en_us',
};
const newSettings = file.map((x) => {
const settingValue = settingsToUpdate[x.settingKey];
return settingValue ? { ...x, settingValue } : { ...x };
});
settingData.push(...newSettings);
}
await Setting.insertMany(settingData);
await Taxes.insertMany([{ taxName: 'Tax 0%', taxValue: '0', isDefault: true }]);
await PaymentMode.insertMany([
{
name: 'Default Payment',
description: 'Default Payment Mode (Cash , Wire Transfer)',
isDefault: true,
},
]);
return res.status(200).json({
success: true,
result: {},
message: 'Successfully IDURAR App Setup',
});
};
module.exports = setup;