-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfigure.js
219 lines (188 loc) · 5.68 KB
/
configure.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
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
/**
* Created by Eabasir on 31/01/2017.
*/
const models = require('./mongo/models.mongo');
const db = require('./mongo/index');
const _const = require('./lib/const.list');
const env = require('./env');
const fs = require('fs');
const appPages = {feed: true, my_shop: true};
const copydir = require('copy-dir');
const warehouses = require('./warehouses');
const offlineWarehouses = require('./offlineWarehouses');
const deliveryDurationInfo = require('./deliveryDurationInfo');
const helpers = require('./lib/helpers')
let PLACEMENTS = null;
let pKeys = [];
let _hash;
db.dbIsReady()
.then(async () => {
try {
await modelIsReady();
copydir.sync('assets', 'public/assets');
let res = await models()['Warehouse'].find().lean();
if (!res || res.length === 0) {
await models()['Warehouse'].insertMany(warehouses);
console.log('-> ', 'warehouses are added');
}
res = await models()['OfflineWarehouse'].find().lean();
if (!res || res.length === 0) {
await models()['OfflineWarehouse'].insertMany(offlineWarehouses);
console.log('-> ', 'offline warehouses are added');
}
res = await models()['DeliveryDurationInfo'].find().lean();
if (!res || res.length === 0) {
await models()['DeliveryDurationInfo'].insertMany(deliveryDurationInfo);
console.log('-> ', 'delivery duaraion info are added');
}
const defulatPassHash = await helpers.makeHash('admin@123');
return Promise.resolve(defulatPassHash);
} catch (err) {
console.log('-> ', err);
process.exit();
}
})
.then(hash => {
_hash = hash;
return models()['Agent'].find().lean();
})
.then(res => {
if (!res || res.length === 0) {
let agents = [{
username: 'admin@persianmode.com',
secret: _hash,
access_level: _const.ACCESS_LEVEL.ContentManager,
first_name: 'Content',
surname: 'Manager',
}, {
username: 'sm@persianmode.com',
secret: _hash,
access_level: _const.ACCESS_LEVEL.SalesManager,
first_name: 'Sales',
surname: 'Manager',
}, {
username: 'hc@persianmode.com',
secret: _hash,
access_level: _const.ACCESS_LEVEL.HubClerk,
first_name: 'hub',
surname: 'clerck',
}, {
username: 'shop@persianmode.com',
secret: _hash,
access_level: _const.ACCESS_LEVEL.ShopClerk,
first_name: 'shop',
surname: 'clerck',
},
{
username: 'idelivery@persianmode.com',
secret: _hash,
access_level: _const.ACCESS_LEVEL.InternalDeliveryAgent,
first_name: 'internal delivery',
surname: 'agent',
},
{
username: 'delivery@persianmode.com',
secret: _hash,
access_level: _const.ACCESS_LEVEL.DeliveryAgent,
first_name: 'delivery',
surname: 'agent',
},
{
username: 'offline@persianmode.com',
secret: _hash,
access_level: _const.ACCESS_LEVEL.OfflineSystem,
first_name: 'offline',
surname: 'system',
}];
return models()['Agent'].insertMany(agents);
} else
return Promise.resolve()
})
.then(() => {
console.log('-> ', 'default agents has been added!');
return models()['Page'].find().lean();
})
.then(res => {
if (res && res.length)
return Promise.resolve();
PLACEMENTS = JSON.parse(fs.readFileSync('placements.json', 'utf8'));
pKeys = Object.keys(PLACEMENTS);
return Promise.all(pKeys.map((r, i) => {
let isApp = !!appPages[r];
console.log(`-> ${isApp ? 'app' : 'website'} page: '${r}' is added.`);
let query = {address: r},
update = {
address: r,
is_app: isApp,
placement: PLACEMENTS[r]
},
options = {upsert: true, new: true, setDefaultsOnInsert: true};
return models()['Page'].findOneAndUpdate(query, update, options);
}))
})
.then(() => {
console.log('-> ', 'default placements are added!');
return models()['LoyaltyGroup'].find().lean();
})
.then(res => {
if (!res || !res.length)
return models()['LoyaltyGroup'].insertMany([
{
name: 'White',
min_score: 0,
},
{
name: 'Orange',
min_score: 5000,
},
{
name: 'Black',
min_score: 11000,
}
], {ordered: false});
return Promise.resolve();
})
.then(res => {
console.log('-> ', 'loyalty groups are added');
let dictionary = JSON.parse(fs.readFileSync('dictionary.json', 'utf8'));
let data = [];
Object.keys(dictionary).forEach(type => {
let typeData = dictionary[type];
data = data.concat(Object.keys(typeData).map(name => {
return {
type,
name,
value: typeData[name]
}
}));
});
return models()['Dictionary'].insertMany(data, {ordered: false});
})
.then(res => {
console.log('-> ', 'dictionary is added');
process.exit();
})
.catch(err => {
console.log(err);
if (err.name !== 'BulkWriteError') {
console.log('-> ', err);
}
else {
console.log('-> ', 'dictionary is added');
}
process.exit();
}
);
modelIsReady = () => {
return new Promise((resolve, reject) => {
getModels = () => {
setTimeout(() => {
if (!models() || models().length)
getModels();
else
resolve();
}, 500);
}
getModels();
})
}