diff options
author | ilyayudovin <ilyayudovin123@gmail.com> | 2020-06-21 13:10:05 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-06-21 13:13:27 +0300 |
commit | f4c4f2d4789880c0cfc956d08aab10b5a93ebcb1 (patch) | |
tree | de24c4c3ede7d0df755aebbba8cadbf6169d1b0d /populateDb.ts | |
parent | 29dce80de9267a4886c6a598ec90756995821828 (diff) | |
download | which-api-f4c4f2d4789880c0cfc956d08aab10b5a93ebcb1.tar.gz |
feat: change votes type
Diffstat (limited to 'populateDb.ts')
-rw-r--r-- | populateDb.ts | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/populateDb.ts b/populateDb.ts index a21b669..ae42d86 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -3,7 +3,7 @@ import bluebird from 'bluebird'; import _ from 'lodash'; import app from './app'; import { UserSchema } from './models/users/user.schema'; -import { PollSchema, ImageData } from './models/polls/poll.schema'; +import { PollSchema, ImageDataSchema } from './models/polls/poll.schema'; mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); @@ -28,12 +28,10 @@ const names: string[] = [ 'William' ]; -const generateImageData = (): ImageData => ({ - url: _.sample(imageUrls) || '', - votes: Math.floor(Math.random() * 101) -}); -const createPoll = (authorId: string): Promise<PollSchema> => { + +const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): Promise<PollSchema> => { + return app.service('polls').create({ contents: { left: generateImageData(), @@ -55,9 +53,14 @@ const createUser = (name: string): Promise<UserSchema> => { const populate = async () => { const users = await bluebird.map(names, name => createUser(name)); + const generateImageData = (): ImageDataSchema => ({ + url: _.sample(imageUrls) || '', + votes: _.sampleSize(users.map(user => user._id), Math.floor(Math.random() * users.length)) + }); + await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { const sampleUser = _.sample(users); - return createPoll(sampleUser?._id); + return createPoll(sampleUser?._id,generateImageData); }); }; |