Skip to content

Commit edcd782

Browse files
committed
feat: generate Votes in populateDB script
1 parent 2e28a25 commit edcd782

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

populateDb.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import mongoose from 'mongoose';
22
import bluebird from 'bluebird';
33
import _ from 'lodash';
4+
import { User, Poll, Vote } from 'which-types';
5+
46
import app from './app';
5-
import { UserSchema } from './models/users/user.schema';
6-
import { PollSchema, ImageDataSchema } from './models/polls/poll.schema';
77

88
mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true });
99

1010
const POLLS_AMOUNT = 20;
11+
const VOTES_AMOUNT = 160;
1112

1213
const imageUrls: string[] = [
1314
// eslint-disable max-len
@@ -28,8 +29,17 @@ const names: string[] = [
2829
'William'
2930
];
3031

32+
const choices = [
33+
'left',
34+
'right'
35+
];
36+
37+
38+
const createPoll = (authorId: string): Promise<Poll> => {
39+
const generateImageData = () => ({
40+
url: _.sample(imageUrls) || '',
41+
});
3142

32-
const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): Promise<PollSchema> => {
3343
return app.service('polls').create({
3444
contents: {
3545
left: generateImageData(),
@@ -39,26 +49,36 @@ const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): P
3949
});
4050
};
4151

42-
const createUser = (username: string): Promise<UserSchema> => {
52+
const createUser = (username: string): Promise<User> => {
4353
return app.service('users').create({
4454
avatarUrl: _.sample(imageUrls) || '',
4555
password: 'supersecret',
4656
username
4757
});
4858
};
4959

60+
const createVote = (userId: string, pollId: string): Promise<Vote> => {
61+
return app.service('votes').create({
62+
userId,
63+
pollId,
64+
which: _.sample(choices)
65+
});
66+
}
67+
5068

5169
const populate = async () => {
5270
const users = await bluebird.map(names, name => createUser(name));
5371

54-
const generateImageData = (): ImageDataSchema => ({
55-
url: _.sample(imageUrls) || '',
56-
votes: _.sampleSize(users.map(user => user._id), Math.floor(Math.random() * users.length))
72+
const polls = await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => {
73+
const user = _.sample(users);
74+
return createPoll(user?._id || '');
75+
5776
});
5877

59-
await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => {
60-
const sampleUser = _.sample(users);
61-
return createPoll(sampleUser?._id, generateImageData);
78+
const votes = await bluebird.mapSeries(new Array(VOTES_AMOUNT), async () => {
79+
const user = _.sample(users);
80+
const poll = _.sample(polls);
81+
return createVote(user?._id || '', poll?._id || '');
6282
});
6383
};
6484

0 commit comments

Comments
 (0)