aboutsummaryrefslogtreecommitdiff
path: root/services/feed/feed.hooks.ts
blob: 33c6e71e8a833bad2b5e10fbfbad24b71ea6a354 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import _ from 'lodash';
import { HookContext } from '@feathersjs/feathers';
import { iff, combine } 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),
    ]
  }
};