diff options
Diffstat (limited to 'hooks')
-rw-r--r-- | hooks/convertPoll.ts | 43 | ||||
-rw-r--r-- | hooks/expandAuthor.ts | 32 | ||||
-rw-r--r-- | hooks/requireAuth.ts | 7 | ||||
-rw-r--r-- | hooks/tryAuthenticate.ts | 8 |
4 files changed, 58 insertions, 32 deletions
diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts new file mode 100644 index 0000000..1c32851 --- /dev/null +++ b/hooks/convertPoll.ts @@ -0,0 +1,43 @@ +import { HookContext } from '@feathersjs/feathers'; +import { Types } from 'mongoose'; +import bluebird from 'bluebird'; +import _ from 'lodash'; +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<HookContext> => { + const { app, result, params: { user } } = context; + + const convert = async (poll: PollSchema): Promise<Poll | null> => { + 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/hooks/expandAuthor.ts b/hooks/expandAuthor.ts deleted file mode 100644 index 3b3e3df..0000000 --- a/hooks/expandAuthor.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { HookContext } from '@feathersjs/feathers'; -import bluebird from 'bluebird'; -import _ from 'lodash'; - -import { Poll, PollSchema } from '../models/polls/poll.schema'; -import { User } from '../models/users/user.schema'; -import UserModel from '../models/users/user.model'; - -const expandAuthor = 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 }); - }) - .catch(err => { - console.error(err); - return err; - }); -}; - -export const expandAuthorHook = async (context: HookContext): Promise<HookContext> => { - context.result = await expandAuthor(context.result); - return context; -}; - -export const expandAuthorManyHook = async (context: HookContext): Promise<HookContext> => { - const polls = await bluebird.map(context.result, (poll: PollSchema) => expandAuthor(poll)); - context.result = _.compact(polls); - return context; -}; - diff --git a/hooks/requireAuth.ts b/hooks/requireAuth.ts new file mode 100644 index 0000000..a7b0e96 --- /dev/null +++ b/hooks/requireAuth.ts @@ -0,0 +1,7 @@ +import { HookContext } from '@feathersjs/feathers'; + +export default async (context: HookContext): Promise<HookContext> => { + if (!context.params.user) throw new Error('This endpoint requires auth!'); + return context; +}; + diff --git a/hooks/tryAuthenticate.ts b/hooks/tryAuthenticate.ts new file mode 100644 index 0000000..e179417 --- /dev/null +++ b/hooks/tryAuthenticate.ts @@ -0,0 +1,8 @@ +import { HookContext } from '@feathersjs/feathers'; +import { authenticate } from '@feathersjs/authentication'; + + +export default async (context: HookContext): Promise<HookContext> => { + return authenticate('jwt')(context).catch(() => context); +}; + |