diff options
Diffstat (limited to 'services/feed/feed.hooks.ts')
-rw-r--r-- | services/feed/feed.hooks.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/services/feed/feed.hooks.ts b/services/feed/feed.hooks.ts new file mode 100644 index 0000000..54f6d61 --- /dev/null +++ b/services/feed/feed.hooks.ts @@ -0,0 +1,32 @@ +import _ from 'lodash'; +import { HookContext } from '@feathersjs/feathers'; +import { iff } from 'feathers-hooks-common'; +import isAuthenticated from '../../hooks/isAuthenticated'; + +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) + ); + return context; +}; + +const lowerOldPolls = async (context: HookContext): Promise<HookContext> => { + // Move all seen polls down + context.result = _.sortBy( + context.result, + poll => !!poll.userChoice + ); + return context; +}; + +export default { + after: { + find: [ + iff(isAuthenticated, raiseNewVerifedPolls), + iff(isAuthenticated, lowerOldPolls) + ] + } +}; + |