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 --- hooks/convertPoll.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 hooks/convertPoll.ts (limited to 'hooks/convertPoll.ts') diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts new file mode 100644 index 0000000..62ddea1 --- /dev/null +++ b/hooks/convertPoll.ts @@ -0,0 +1,45 @@ +import { HookContext } from '@feathersjs/feathers'; +import bluebird from 'bluebird'; +import _ from 'lodash'; +import { Poll, User } from 'which-types'; + +import { PollSchema } from '../models/polls/poll.schema'; +import UserModel from '../models/users/user.model'; + +const convertPoll = async (poll: PollSchema): Promise => { + return UserModel.findById(poll.authorId) + .lean() + .exec() + .then((author: User | null): Poll | null => { + return author && _.merge( + _.omit(poll, ['authorId']), + { + author, + contents: { + left: { + votes: poll.contents.left.votes.length + }, + right: { + votes: poll.contents.right.votes.length + } + } + } + ); + }) + .catch(err => { + console.error(err); + return err; + }); +}; + +export const convertPollHook = async (context: HookContext): Promise => { + context.result = await convertPoll(context.result); + return context; +}; + +export const convertPollManyHook = async (context: HookContext): Promise => { + const polls = await bluebird.map(context.result, (poll: PollSchema) => convertPoll(poll)); + context.result = _.compact(polls); + return context; +}; + -- cgit v1.2.3 From 676c722a994a214c182bcf26b80eab09ebf9f61e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 21:29:17 +0300 Subject: refactor: unite convertPoll hooks --- hooks/convertPoll.ts | 59 ++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'hooks/convertPoll.ts') diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts index 62ddea1..aacf3de 100644 --- a/hooks/convertPoll.ts +++ b/hooks/convertPoll.ts @@ -4,42 +4,37 @@ import _ from 'lodash'; import { Poll, User } from 'which-types'; import { PollSchema } from '../models/polls/poll.schema'; -import UserModel from '../models/users/user.model'; -const convertPoll = async (poll: PollSchema): Promise => { - return UserModel.findById(poll.authorId) - .lean() - .exec() - .then((author: User | null): Poll | null => { - return author && _.merge( - _.omit(poll, ['authorId']), - { - author, - contents: { - left: { - votes: poll.contents.left.votes.length - }, - right: { - votes: poll.contents.right.votes.length + +export default async (context: HookContext): Promise => { + const { app, result } = context; + + const convert = async (poll: PollSchema): Promise => { + return app.service('users').get(poll.authorId) + .then((author: User | null): Poll | null => { + return author && _.merge( + _.omit(poll, ['authorId']), + { + author, + contents: { + left: { + votes: poll.contents.left.votes.length + }, + right: { + votes: poll.contents.right.votes.length + } } } - } - ); - }) - .catch(err => { - console.error(err); - return err; - }); -}; - -export const convertPollHook = async (context: HookContext): Promise => { - context.result = await convertPoll(context.result); - return context; -}; + ); + }); + }; -export const convertPollManyHook = async (context: HookContext): Promise => { - const polls = await bluebird.map(context.result, (poll: PollSchema) => convertPoll(poll)); - context.result = _.compact(polls); + if (Array.isArray(result)) { + const polls = await bluebird.map(result, (poll: PollSchema) => convert(poll)); + context.result = _.compact(polls); + } else { + context.result = await convert(result); + } return context; }; -- cgit v1.2.3 From 34ec4c9f1bbd13a7a633bdd02425d207986baea3 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 10:39:32 +0300 Subject: feat: count votes by aggregate in convertPoll hook --- hooks/convertPoll.ts | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'hooks/convertPoll.ts') diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts index aacf3de..b4f8376 100644 --- a/hooks/convertPoll.ts +++ b/hooks/convertPoll.ts @@ -1,32 +1,33 @@ import { HookContext } from '@feathersjs/feathers'; +import { Types } from 'mongoose'; import bluebird from 'bluebird'; import _ from 'lodash'; -import { Poll, User } from 'which-types'; +import { Poll, User, Vote } from 'which-types'; import { PollSchema } from '../models/polls/poll.schema'; +import VoteModel from '../models/votes/vote.model'; export default async (context: HookContext): Promise => { - const { app, result } = context; + const { app, result, params: { user } } = context; const convert = async (poll: PollSchema): Promise => { - return app.service('users').get(poll.authorId) - .then((author: User | null): Poll | null => { - return author && _.merge( - _.omit(poll, ['authorId']), - { - author, - contents: { - left: { - votes: poll.contents.left.votes.length - }, - right: { - votes: poll.contents.right.votes.length - } - } - } - ); - }); + const author = await app.service('users').get(poll.authorId); + + const contents = await VoteModel.aggregate([ + { $match: { pollId: Types.ObjectId(poll._id) } }, + { $group: { _id: '$which', total: { $sum: 1 } } } + ]).then(groups => groups.reduce( + (acc, group) => _.set(acc, group._id + '.votes', group.total), + {} + )); + + const userChoice = await VoteModel.findOne({ pollId: poll._id, userId: user?._id }); + + return _.merge( + _.omit(poll, ['authorId']), + { author, contents, userChoice } + ); }; if (Array.isArray(result)) { -- cgit v1.2.3 From cf34d891b349b8bb799393067a77fa23a4874ad5 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 11:25:16 +0300 Subject: fix: provide default value of 0 to votes amount --- hooks/convertPoll.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'hooks/convertPoll.ts') diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts index b4f8376..4da25c1 100644 --- a/hooks/convertPoll.ts +++ b/hooks/convertPoll.ts @@ -19,7 +19,7 @@ export default async (context: HookContext): Promise => { { $group: { _id: '$which', total: { $sum: 1 } } } ]).then(groups => groups.reduce( (acc, group) => _.set(acc, group._id + '.votes', group.total), - {} + { left: { votes: 0 }, right: { votes: 0 } } )); const userChoice = await VoteModel.findOne({ pollId: poll._id, userId: user?._id }); -- cgit v1.2.3 From a9678b794758164a86a8c136c1d8acc8b1d0135d Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 11:53:21 +0300 Subject: fix: add userChoice correctly --- hooks/convertPoll.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'hooks/convertPoll.ts') diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts index 4da25c1..1c32851 100644 --- a/hooks/convertPoll.ts +++ b/hooks/convertPoll.ts @@ -22,7 +22,9 @@ export default async (context: HookContext): Promise => { { left: { votes: 0 }, right: { votes: 0 } } )); - const userChoice = await VoteModel.findOne({ pollId: poll._id, userId: user?._id }); + const userChoice = await VoteModel.findOne( + { pollId: poll._id, userId: user?._id } + ).then(vote => vote?.which); return _.merge( _.omit(poll, ['authorId']), -- cgit v1.2.3