diff options
Diffstat (limited to 'services')
-rw-r--r-- | services/feed/feed.hooks.ts | 4 | ||||
-rw-r--r-- | services/feedback/feedback.hooks.ts | 9 | ||||
-rw-r--r-- | services/feedback/feedback.service.ts | 13 | ||||
-rw-r--r-- | services/index.ts | 2 | ||||
-rw-r--r-- | services/polls/polls.hooks.ts | 43 | ||||
-rw-r--r-- | services/votes/votes.hooks.ts | 10 |
6 files changed, 70 insertions, 11 deletions
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<HookContext> // 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<HookContext> => { // Move all seen polls down context.result = _.sortBy( context.result, - poll => !!poll.userChoice + poll => !!poll.vote ); return context; }; diff --git a/services/feedback/feedback.hooks.ts b/services/feedback/feedback.hooks.ts new file mode 100644 index 0000000..56e9000 --- /dev/null +++ b/services/feedback/feedback.hooks.ts @@ -0,0 +1,9 @@ +import requireAuth from '../../hooks/requireAuth'; +import signAuthority from '../../hooks/signAuthority'; + +export default { + before: { + create: [requireAuth, signAuthority] + } +}; + diff --git a/services/feedback/feedback.service.ts b/services/feedback/feedback.service.ts new file mode 100644 index 0000000..a15ede9 --- /dev/null +++ b/services/feedback/feedback.service.ts @@ -0,0 +1,13 @@ +import { Application } from '@feathersjs/express'; +import service from 'feathers-mongoose'; +import Model from '../../models/feedback/feedback.model'; + +import hooks from './feedback.hooks'; + +const FeebackService = service({ Model }); + +export default (app: Application): void => { + app.use('/feedback', FeebackService); + app.service('feedback').hooks(hooks); +}; + diff --git a/services/index.ts b/services/index.ts index 1763a17..e5ea703 100644 --- a/services/index.ts +++ b/services/index.ts @@ -5,6 +5,7 @@ import Profiles from './profiles/profiles.service'; import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; import Feed from './feed/feed.service'; +import Feedback from './feedback/feedback.service'; import tryAuthenticate from '../hooks/tryAuthenticate'; import logging from '../hooks/logging'; @@ -17,6 +18,7 @@ export default (app: Application): void => { app.configure(Profiles); app.configure(Votes); app.configure(Feed); + app.configure(Feedback); app.hooks({ before: { 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<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 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 diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts index 7d0b3ba..56e9000 100644 --- a/services/votes/votes.hooks.ts +++ b/services/votes/votes.hooks.ts @@ -1,15 +1,9 @@ -import { HookContext } from '@feathersjs/feathers'; import requireAuth from '../../hooks/requireAuth'; - -const addUserId = async (context: HookContext): Promise<HookContext> => { - const { params: { user } } = context; - context.data.userId = user._id; - return context; -}; +import signAuthority from '../../hooks/signAuthority'; export default { before: { - create: [requireAuth, addUserId] + create: [requireAuth, signAuthority] } }; |