aboutsummaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-06-10 19:47:48 +0300
committereug-vs <eug-vs@keemail.me>2020-06-10 19:47:48 +0300
commitd40f6e6cf8150f9e112d0b5d8d7075c2fc90264b (patch)
tree7cdb3cb7b86e3333c15bbaefe14fe228cc93d023 /services
parent0915870f347eecbf5d1646fa732e2a73418960a6 (diff)
downloadwhich-api-d40f6e6cf8150f9e112d0b5d8d7075c2fc90264b.tar.gz
refactor: improve interfaces and expandAuthor
Diffstat (limited to 'services')
-rw-r--r--services/polls/polls.hooks.ts26
1 files changed, 14 insertions, 12 deletions
diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts
index ff6a83f..fc5fa45 100644
--- a/services/polls/polls.hooks.ts
+++ b/services/polls/polls.hooks.ts
@@ -2,20 +2,22 @@ import { HookContext } from '@feathersjs/feathers';
import bluebird from 'bluebird';
import _ from 'lodash';
-import { PollSchema } from '../../models/polls/poll.schema';
-import { UserSchema } from '../../models/users/user.schema';
+import { Poll, PollSchema } from '../../models/polls/poll.schema';
+import { User } from '../../models/users/user.schema';
import UserModel from '../../models/users/user.model';
-interface Poll extends Omit<PollSchema, 'authorId'> {
- author: UserSchema;
-}
-
const expandAuthor = async (poll: PollSchema): Promise<Poll | null> => {
- return UserModel.findById(poll.authorId).then((author: UserSchema | null): Poll | null => {
- if (author) return _.merge(_.omit(poll, 'authorId'), { author });
- return 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;
+ });
};
const expandAuthorHook = async (context: HookContext): Promise<HookContext> => {
@@ -24,8 +26,8 @@ const expandAuthorHook = async (context: HookContext): Promise<HookContext> => {
};
const expandAuthorManyHook = async (context: HookContext): Promise<HookContext> => {
- context.result = await bluebird.map(context.result, (poll: any) => expandAuthor(poll));
- console.log(context.result);
+ const polls = await bluebird.map(context.result, (poll: any) => expandAuthor(poll));
+ context.result = _.compact(polls);
return context;
};