From c3438eb4a7187773e2736dea765bb8281f7edf0e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 18:33:58 +0300 Subject: refactor: move convertPoll to polls hooks --- hooks/convertPoll.ts | 43 ------------------------------------------- services/feed/feed.hooks.ts | 4 ++-- services/polls/polls.hooks.ts | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 46 deletions(-) delete mode 100644 hooks/convertPoll.ts diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts deleted file mode 100644 index 5e6f9f4..0000000 --- a/hooks/convertPoll.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { HookContext } from '@feathersjs/feathers'; -import { Types } from 'mongoose'; -import bluebird from 'bluebird'; -import _ from 'lodash'; -import { Poll } 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, params: { user } } = context; - - const convert = async (poll: PollSchema): Promise => { - 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), - { left: { votes: 0 }, right: { votes: 0 } } - )); - - const userChoice = await VoteModel.findOne( - { pollId: poll._id, userId: user?._id } - ).then(vote => vote?.which); - - return _.merge( - _.omit(poll, ['authorId']), - { author, contents, userChoice } - ); - }; - - 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; -}; - diff --git a/services/feed/feed.hooks.ts b/services/feed/feed.hooks.ts index 54f6d61..6bff8dc 100644 --- a/services/feed/feed.hooks.ts +++ b/services/feed/feed.hooks.ts @@ -7,7 +7,7 @@ const raiseNewVerifedPolls = async (context: HookContext): Promise // Raise unseen verified polls to the very top context.result = _.sortBy( context.result, - poll => !(poll.author.verified && !poll.userChoice) + poll => !(poll.author.verified && !poll.vote) ); return context; }; @@ -16,7 +16,7 @@ const lowerOldPolls = async (context: HookContext): Promise => { // Move all seen polls down context.result = _.sortBy( context.result, - poll => !!poll.userChoice + poll => !!poll.vote ); return context; }; diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index 77fcc7a..9f2183f 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,6 +1,47 @@ -import convertPoll from '../../hooks/convertPoll'; +import { HookContext } from '@feathersjs/feathers'; +import { Types } from 'mongoose'; +import bluebird from 'bluebird'; import _ from 'lodash'; +import { Poll } from 'which-types'; + +import { PollSchema } from '../../models/polls/poll.schema'; +import VoteModel from '../../models/votes/vote.model'; import sortByDate from '../../hooks/sortByDate'; + +const convertPoll = async (context: HookContext): Promise => { + const { app, result, params: { user } } = context; + + const convert = async (poll: PollSchema): Promise => { + 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), + { left: { votes: 0 }, right: { votes: 0 } } + )); + + const vote = await VoteModel.findOne( + { pollId: poll._id, authorId: user?._id } + ); + + return _.merge( + _.omit(poll, ['authorId']), + { author, contents, vote } + ); + }; + + 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; +}; + + export default { before: { find: sortByDate -- cgit v1.2.3