diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-25 14:39:21 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-25 14:39:21 +0300 |
commit | 0afe8f1530affad1e58a65385b2a4bf888ab86cb (patch) | |
tree | 3bd5599bc5c3ce23777c5b369eaadd744f3c5c1f /populateDb.ts | |
parent | e488591b9548264d0305a5f34786138bd6c6622b (diff) | |
parent | 64f5f8c3f9660f649dfdaad07d84aa8c26b9661e (diff) | |
download | which-api-0afe8f1530affad1e58a65385b2a4bf888ab86cb.tar.gz |
Merge pull request #8 from which-ecosystem/votes
Votes
Diffstat (limited to 'populateDb.ts')
-rw-r--r-- | populateDb.ts | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/populateDb.ts b/populateDb.ts index a21b669..757280a 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -1,9 +1,9 @@ import mongoose from 'mongoose'; import bluebird from 'bluebird'; import _ from 'lodash'; +import { User, Poll, Vote } from 'which-types'; + import app from './app'; -import { UserSchema } from './models/users/user.schema'; -import { PollSchema, ImageData } from './models/polls/poll.schema'; mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); @@ -28,12 +28,17 @@ const names: string[] = [ 'William' ]; -const generateImageData = (): ImageData => ({ - url: _.sample(imageUrls) || '', - votes: Math.floor(Math.random() * 101) -}); +const choices = [ + 'left', + 'right' +]; + + +const createPoll = (authorId: string): Promise<Poll> => { + const generateImageData = () => ({ + url: _.sample(imageUrls) || '', + }); -const createPoll = (authorId: string): Promise<PollSchema> => { return app.service('polls').create({ contents: { left: generateImageData(), @@ -43,21 +48,33 @@ const createPoll = (authorId: string): Promise<PollSchema> => { }); }; -const createUser = (name: string): Promise<UserSchema> => { +const createUser = (username: string): Promise<User> => { return app.service('users').create({ avatarUrl: _.sample(imageUrls) || '', password: 'supersecret', - name + username }); }; +const createVote = (userId: string, pollId: string): Promise<Vote> => { + return app.service('votes').create({ + pollId, + which: _.sample(choices) + }, { user: { _id: userId } }); +} + const populate = async () => { const users = await bluebird.map(names, name => createUser(name)); - await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { - const sampleUser = _.sample(users); - return createPoll(sampleUser?._id); + const polls = await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { + const user = _.sample(users); + return createPoll(user?._id || ''); + }); + + const votes = await bluebird.map(users, user => { + const pollsToVote = _.sampleSize(polls, _.random(0, POLLS_AMOUNT)); + return bluebird.map(pollsToVote, poll => createVote(user?._id || '', poll?._id || '')); }); }; |