From f4c4f2d4789880c0cfc956d08aab10b5a93ebcb1 Mon Sep 17 00:00:00 2001 From: ilyayudovin Date: Sun, 21 Jun 2020 13:10:05 +0300 Subject: feat: change votes type --- populateDb.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'populateDb.ts') 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 => { + +const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): Promise => { + return app.service('polls').create({ contents: { left: generateImageData(), @@ -55,9 +53,14 @@ const createUser = (name: string): Promise => { 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); }); }; -- cgit v1.2.3 From e99b51895afd532a529744396ecae87d47c68503 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 21 Jun 2020 14:29:59 +0300 Subject: feat: change hooks and install which-types --- populateDb.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'populateDb.ts') diff --git a/populateDb.ts b/populateDb.ts index ae42d86..a666427 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -29,9 +29,7 @@ const names: string[] = [ ]; - const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): Promise => { - return app.service('polls').create({ contents: { left: generateImageData(), @@ -60,7 +58,7 @@ const populate = async () => { await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { const sampleUser = _.sample(users); - return createPoll(sampleUser?._id,generateImageData); + return createPoll(sampleUser?._id, generateImageData); }); }; -- cgit v1.2.3 From 75e527334e4e9a94b63704f87aa650c75f13891c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 24 Jun 2020 00:58:45 +0300 Subject: feat: migrate to latest which-types --- populateDb.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'populateDb.ts') diff --git a/populateDb.ts b/populateDb.ts index a666427..1a3b016 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -39,11 +39,11 @@ const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): P }); }; -const createUser = (name: string): Promise => { +const createUser = (username: string): Promise => { return app.service('users').create({ avatarUrl: _.sample(imageUrls) || '', password: 'supersecret', - name + username }); }; -- cgit v1.2.3 From edcd782cdbfd59d051752515ee6309faa3e1f103 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 09:50:28 +0300 Subject: feat: generate Votes in populateDB script --- populateDb.ts | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'populateDb.ts') diff --git a/populateDb.ts b/populateDb.ts index 1a3b016..719b7e7 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -1,13 +1,14 @@ 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, ImageDataSchema } from './models/polls/poll.schema'; mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); const POLLS_AMOUNT = 20; +const VOTES_AMOUNT = 160; const imageUrls: string[] = [ // eslint-disable max-len @@ -28,8 +29,17 @@ const names: string[] = [ 'William' ]; +const choices = [ + 'left', + 'right' +]; + + +const createPoll = (authorId: string): Promise => { + const generateImageData = () => ({ + url: _.sample(imageUrls) || '', + }); -const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): Promise => { return app.service('polls').create({ contents: { left: generateImageData(), @@ -39,7 +49,7 @@ const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): P }); }; -const createUser = (username: string): Promise => { +const createUser = (username: string): Promise => { return app.service('users').create({ avatarUrl: _.sample(imageUrls) || '', password: 'supersecret', @@ -47,18 +57,28 @@ const createUser = (username: string): Promise => { }); }; +const createVote = (userId: string, pollId: string): Promise => { + return app.service('votes').create({ + userId, + pollId, + which: _.sample(choices) + }); +} + 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)) + const polls = await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { + const user = _.sample(users); + return createPoll(user?._id || ''); + }); - await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { - const sampleUser = _.sample(users); - return createPoll(sampleUser?._id, generateImageData); + const votes = await bluebird.mapSeries(new Array(VOTES_AMOUNT), async () => { + const user = _.sample(users); + const poll = _.sample(polls); + return createVote(user?._id || '', poll?._id || ''); }); }; -- cgit v1.2.3 From a58354a1d1bf967fc932723b9138d8c6322591d5 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 11:23:20 +0300 Subject: feat: validate fields in votes --- populateDb.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'populateDb.ts') diff --git a/populateDb.ts b/populateDb.ts index 719b7e7..757280a 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -8,7 +8,6 @@ import app from './app'; mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); const POLLS_AMOUNT = 20; -const VOTES_AMOUNT = 160; const imageUrls: string[] = [ // eslint-disable max-len @@ -59,10 +58,9 @@ const createUser = (username: string): Promise => { const createVote = (userId: string, pollId: string): Promise => { return app.service('votes').create({ - userId, pollId, which: _.sample(choices) - }); + }, { user: { _id: userId } }); } @@ -72,13 +70,11 @@ const populate = async () => { const polls = await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { const user = _.sample(users); return createPoll(user?._id || ''); - }); - const votes = await bluebird.mapSeries(new Array(VOTES_AMOUNT), async () => { - const user = _.sample(users); - const poll = _.sample(polls); - return createVote(user?._id || '', poll?._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 || '')); }); }; -- cgit v1.2.3