diff options
author | eug-vs <eug-vs@keemail.me> | 2020-06-22 21:29:17 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-06-22 21:29:17 +0300 |
commit | 676c722a994a214c182bcf26b80eab09ebf9f61e (patch) | |
tree | 9275da333eac6ca4cf06126be55747744106de99 | |
parent | 4101b4ff7a5c328164fe74ebfa3c42b82f332c43 (diff) | |
download | which-api-676c722a994a214c182bcf26b80eab09ebf9f61e.tar.gz |
refactor: unite convertPoll hooks
-rw-r--r-- | hooks/convertPoll.ts | 59 | ||||
-rw-r--r-- | services/polls/polls.hooks.ts | 8 | ||||
-rw-r--r-- | services/profiles/profiles.hooks.ts | 6 | ||||
-rw-r--r-- | services/votes/votes.hooks.ts | 7 |
4 files changed, 33 insertions, 47 deletions
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<Poll | null> => { - return UserModel.findById(poll.authorId) - .lean<User>() - .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<HookContext> => { + const { app, result } = context; + + const convert = async (poll: PollSchema): Promise<Poll | null> => { + 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<HookContext> => { - context.result = await convertPoll(context.result); - return context; -}; + ); + }); + }; -export const convertPollManyHook = async (context: HookContext): Promise<HookContext> => { - 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; }; diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index a914cd0..eba3e63 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,12 +1,8 @@ -import { - convertPollHook, - convertPollManyHook -} from '../../hooks/convertPoll'; +import convertPoll from '../../hooks/convertPoll'; export default { after: { - get: [convertPollHook], - find: [convertPollManyHook] + all: [convertPoll], } }; diff --git a/services/profiles/profiles.hooks.ts b/services/profiles/profiles.hooks.ts index 4447bee..13d6f63 100644 --- a/services/profiles/profiles.hooks.ts +++ b/services/profiles/profiles.hooks.ts @@ -1,10 +1,8 @@ -import { - convertPollManyHook -} from '../../hooks/convertPoll'; +import convertPoll from '../../hooks/convertPoll'; export default { after: { - get: [convertPollManyHook] + all: [convertPoll] } }; diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts index 2e29008..a41b8a9 100644 --- a/services/votes/votes.hooks.ts +++ b/services/votes/votes.hooks.ts @@ -1,15 +1,12 @@ -import { - convertPollHook -} from '../../hooks/convertPoll'; - import { authenticate } from '@feathersjs/authentication'; +import convertPoll from '../../hooks/convertPoll'; export default { before: { create: [authenticate('jwt')] }, after: { - all: [convertPollHook] + all: [convertPoll] } }; |