Skip to content

Commit 676c722

Browse files
committed
refactor: unite convertPoll hooks
1 parent 4101b4f commit 676c722

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

hooks/convertPoll.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,37 @@ import _ from 'lodash';
44
import { Poll, User } from 'which-types';
55

66
import { PollSchema } from '../models/polls/poll.schema';
7-
import UserModel from '../models/users/user.model';
87

9-
const convertPoll = async (poll: PollSchema): Promise<Poll | null> => {
10-
return UserModel.findById(poll.authorId)
11-
.lean<User>()
12-
.exec()
13-
.then((author: User | null): Poll | null => {
14-
return author && _.merge(
15-
_.omit(poll, ['authorId']),
16-
{
17-
author,
18-
contents: {
19-
left: {
20-
votes: poll.contents.left.votes.length
21-
},
22-
right: {
23-
votes: poll.contents.right.votes.length
8+
9+
export default async (context: HookContext): Promise<HookContext> => {
10+
const { app, result } = context;
11+
12+
const convert = async (poll: PollSchema): Promise<Poll | null> => {
13+
return app.service('users').get(poll.authorId)
14+
.then((author: User | null): Poll | null => {
15+
return author && _.merge(
16+
_.omit(poll, ['authorId']),
17+
{
18+
author,
19+
contents: {
20+
left: {
21+
votes: poll.contents.left.votes.length
22+
},
23+
right: {
24+
votes: poll.contents.right.votes.length
25+
}
2426
}
2527
}
26-
}
27-
);
28-
})
29-
.catch(err => {
30-
console.error(err);
31-
return err;
32-
});
33-
};
34-
35-
export const convertPollHook = async (context: HookContext): Promise<HookContext> => {
36-
context.result = await convertPoll(context.result);
37-
return context;
38-
};
28+
);
29+
});
30+
};
3931

40-
export const convertPollManyHook = async (context: HookContext): Promise<HookContext> => {
41-
const polls = await bluebird.map(context.result, (poll: PollSchema) => convertPoll(poll));
42-
context.result = _.compact(polls);
32+
if (Array.isArray(result)) {
33+
const polls = await bluebird.map(result, (poll: PollSchema) => convert(poll));
34+
context.result = _.compact(polls);
35+
} else {
36+
context.result = await convert(result);
37+
}
4338
return context;
4439
};
4540

services/polls/polls.hooks.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import {
2-
convertPollHook,
3-
convertPollManyHook
4-
} from '../../hooks/convertPoll';
1+
import convertPoll from '../../hooks/convertPoll';
52

63
export default {
74
after: {
8-
get: [convertPollHook],
9-
find: [convertPollManyHook]
5+
all: [convertPoll],
106
}
117
};
128

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import {
2-
convertPollManyHook
3-
} from '../../hooks/convertPoll';
1+
import convertPoll from '../../hooks/convertPoll';
42

53
export default {
64
after: {
7-
get: [convertPollManyHook]
5+
all: [convertPoll]
86
}
97
};
108

services/votes/votes.hooks.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import {
2-
convertPollHook
3-
} from '../../hooks/convertPoll';
4-
51
import { authenticate } from '@feathersjs/authentication';
2+
import convertPoll from '../../hooks/convertPoll';
63

74
export default {
85
before: {
96
create: [authenticate('jwt')]
107
},
118
after: {
12-
all: [convertPollHook]
9+
all: [convertPoll]
1310
}
1411
};
1512

0 commit comments

Comments
 (0)