Skip to content

Commit 213fca3

Browse files
added invite code generation for admins
1 parent ac5572e commit 213fca3

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

server/helper/seed-helpers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,14 @@ module.exports = {
127127
for (let index = 0; index < array.length; index++) {
128128
await callback(array[index], index, array);
129129
}
130+
},
131+
genInviteCode: () => {
132+
const chars = '23456789abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ';
133+
let code = '';
134+
for (let i = 0; i < 8; i++) {
135+
const n = _.random(0, chars.length-1);
136+
code += chars.substr(n, 1);
137+
}
138+
return code;
130139
}
131140
};

server/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ server.on('listening', () => {
4343
app.service('admin').create({ seedBaseCategories: true });
4444
}
4545
});
46+
app.service('badges').find({ query: { $limit: 0 }})
47+
.then(async (res) => {
48+
if (res.total < 1) {
49+
app.service('admin').create({ seedBaseBadges: true });
50+
}
51+
});
4652
app.info(`Feathers application started on ${app.get('host')}:${port}`);
4753
}
4854
});

server/seeder/development/invites.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
const seedHelpers = require('../../helper/seed-helpers');
2-
const random = require('lodash/random');
3-
4-
function genCode () {
5-
const chars = '23456789abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ';
6-
let code = '';
7-
for (let i = 0; i < 8; i++) {
8-
const n = random(0, chars.length-1);
9-
code += chars.substr(n, 1);
10-
}
11-
return code;
12-
}
132

143
// eslint-disable-next-line no-unused-vars
154
module.exports = (seederstore) => {
@@ -19,7 +8,7 @@ module.exports = (seederstore) => {
198
count: 10,
209
template: {
2110
email: '{{internet.email}}',
22-
code: () => genCode(),
11+
code: () => seedHelpers.genInviteCode(),
2312
badgeIds: () => seedHelpers.randomItems(seederstore.badges, '_id', 0, seederstore.badges.length),
2413
wasSeeded: true
2514
}

server/services/admin/admin.class.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-unused-vars */
22

3-
const { asyncForEach } = require('../../helper/seed-helpers');
4-
const { keyBy } = require('lodash');
3+
const { asyncForEach, genInviteCode } = require('../../helper/seed-helpers');
4+
const { keyBy, isEmpty } = require('lodash');
55

66
class Service {
77
constructor (options) {
@@ -38,10 +38,12 @@ class Service {
3838

3939
return new Promise(async (resolve, reject) => {
4040

41-
try {
42-
await this._fillSeederStore(['users', 'categories']);
43-
} catch (err) {
44-
reject(err);
41+
if (data.seedBaseCategories || data.seedBaseBadges || data.seedFakeData || data.seedDemoData) {
42+
try {
43+
await this._fillSeederStore(['users', 'categories']);
44+
} catch (err) {
45+
reject(err);
46+
}
4547
}
4648

4749
if (data.seedBaseCategories) {
@@ -57,6 +59,19 @@ class Service {
5759
reject(err);
5860
}
5961
}
62+
if (data.seedBaseBadges) {
63+
// run the seeder
64+
this.app.debug('seedBaseBadges...');
65+
require('../../seeder')(this.app, this.seederstore);
66+
try {
67+
await this.app.seed([
68+
require('../../seeder/base/categories')
69+
]);
70+
resolve();
71+
} catch (err) {
72+
reject(err);
73+
}
74+
}
6075
if (data.seedFakeData) {
6176
// run the seeder
6277
this.app.debug('seedFakeData...');
@@ -79,6 +94,26 @@ class Service {
7994
reject(err);
8095
}
8196
}
97+
98+
if (!isEmpty(data.createInvites)) {
99+
// run the seeder
100+
this.app.debug('creatingInviceCodes...');
101+
102+
let output = [];
103+
await asyncForEach(data.createInvites, async (email) => {
104+
try {
105+
const res = await this.app.service('invites').create({
106+
email: email,
107+
code: genInviteCode()
108+
});
109+
output.push(res);
110+
} catch (err) {
111+
this.app.error(err);
112+
}
113+
});
114+
115+
resolve(output);
116+
}
82117
});
83118
}
84119

0 commit comments

Comments
 (0)