diff options
author | eug-vs <eug-vs@keemail.me> | 2020-06-10 19:47:48 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-06-10 19:47:48 +0300 |
commit | d40f6e6cf8150f9e112d0b5d8d7075c2fc90264b (patch) | |
tree | 7cdb3cb7b86e3333c15bbaefe14fe228cc93d023 | |
parent | 0915870f347eecbf5d1646fa732e2a73418960a6 (diff) | |
download | which-api-d40f6e6cf8150f9e112d0b5d8d7075c2fc90264b.tar.gz |
refactor: improve interfaces and expandAuthor
-rw-r--r-- | models/polls/poll.schema.ts | 9 | ||||
-rw-r--r-- | models/users/user.schema.ts | 4 | ||||
-rw-r--r-- | services/polls/polls.hooks.ts | 26 |
3 files changed, 24 insertions, 15 deletions
diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index 79036c3..ea91051 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -1,18 +1,23 @@ import { Document, Schema, Types } from 'mongoose'; +import { User } from '../users/user.schema' interface ImageData { url: string; votes: number; } -export interface PollSchema extends Document { - authorId: string; +export interface Poll { + author: User; contents: { left: ImageData; right: ImageData; }; } +export interface PollSchema extends Document, Omit<Poll, 'author'> { + authorId: string; +} + const imageDataSchema = { url: String, diff --git a/models/users/user.schema.ts b/models/users/user.schema.ts index 5292add..e39f667 100644 --- a/models/users/user.schema.ts +++ b/models/users/user.schema.ts @@ -1,11 +1,13 @@ import { Document, Schema } from "mongoose" -export interface UserSchema extends Document { +export interface User { name: string; avatarUrl?: string; age?: number; } +export interface UserSchema extends Document, User {} + export const userSchema = new Schema({ name: String, avatarUrl: { 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; }; |