diff options
-rw-r--r-- | app.ts | 2 | ||||
-rw-r--r-- | hooks/convertPoll.ts (renamed from hooks/expandAuthor.ts) | 32 | ||||
-rw-r--r-- | models/polls/poll.schema.ts | 15 | ||||
-rw-r--r-- | models/users/user.schema.ts | 7 | ||||
-rw-r--r-- | package-lock.json | 5 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | populateDb.ts | 4 | ||||
-rw-r--r-- | services/auth/auth.service.ts | 2 | ||||
-rw-r--r-- | services/polls/polls.hooks.ts | 10 | ||||
-rw-r--r-- | services/profiles/profiles.hooks.ts | 6 |
10 files changed, 35 insertions, 51 deletions
@@ -1,7 +1,7 @@ import feathers from '@feathersjs/feathers'; import express from '@feathersjs/express'; import socketio from '@feathersjs/socketio'; -import configuration from '@feathersjs/configuration' +import configuration from '@feathersjs/configuration'; import '@feathersjs/transport-commons'; import cors from 'cors'; diff --git a/hooks/expandAuthor.ts b/hooks/convertPoll.ts index 5993839..62ddea1 100644 --- a/hooks/expandAuthor.ts +++ b/hooks/convertPoll.ts @@ -1,9 +1,9 @@ import { HookContext } from '@feathersjs/feathers'; import bluebird from 'bluebird'; import _ from 'lodash'; +import { Poll, User } from 'which-types'; -import { Poll, PollSchema } from '../models/polls/poll.schema'; -import { User } from '../models/users/user.schema'; +import { PollSchema } from '../models/polls/poll.schema'; import UserModel from '../models/users/user.model'; const convertPoll = async (poll: PollSchema): Promise<Poll | null> => { @@ -11,20 +11,20 @@ const convertPoll = async (poll: PollSchema): Promise<Poll | null> => { .lean<User>() .exec() .then((author: User | null): Poll | null => { - return author && { - _id: poll._id, - author, - contents: { - left: { - url: poll.contents.left.url, - votes: poll.contents.left.votes.length - }, - right: { - url: poll.contents.right.url, - votes: poll.contents.right.votes.length + return author && _.merge( + _.omit(poll, ['authorId']), + { + author, + contents: { + left: { + votes: poll.contents.left.votes.length + }, + right: { + votes: poll.contents.right.votes.length + } } } - }; + ); }) .catch(err => { console.error(err); @@ -32,12 +32,12 @@ const convertPoll = async (poll: PollSchema): Promise<Poll | null> => { }); }; -export const expandAuthorHook = async (context: HookContext): Promise<HookContext> => { +export const convertPollHook = async (context: HookContext): Promise<HookContext> => { context.result = await convertPoll(context.result); return context; }; -export const expandAuthorManyHook = async (context: HookContext): Promise<HookContext> => { +export const convertPollManyHook = async (context: HookContext): Promise<HookContext> => { const polls = await bluebird.map(context.result, (poll: PollSchema) => convertPoll(poll)); context.result = _.compact(polls); return context; diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index 236011f..fd6751c 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -1,19 +1,4 @@ import { Document, Schema, Types } from 'mongoose'; -import { User } from '../users/user.schema'; - -export interface ImageData { - url: string; - votes: number; -} - -export interface Poll { - _id: string; - author: User; - contents: { - left: ImageData; - right: ImageData; - }; -} export interface ImageDataSchema { url: string; diff --git a/models/users/user.schema.ts b/models/users/user.schema.ts index fd7d1e1..9030d61 100644 --- a/models/users/user.schema.ts +++ b/models/users/user.schema.ts @@ -1,10 +1,5 @@ import { Document, Schema } from 'mongoose'; - -export interface User { - name: string; - avatarUrl?: string; - age?: number; -} +import { User } from 'which-types'; export interface UserSchema extends Document, User { password: string; diff --git a/package-lock.json b/package-lock.json index 771797b..6f78e7c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3050,6 +3050,11 @@ "isexe": "^2.0.0" } }, + "which-types": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.0.3.tgz", + "integrity": "sha512-anON+pR6+GeT+aCRhxK0Vj3AQvo6COSOG1zPy24VDxXnxluCvK5tNmlV+1Oe771lOjl0z3LEvc1B7YL5xYtFqQ==" + }, "word-wrap": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", diff --git a/package.json b/package.json index 7116faa..7b27466 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "cors": "^2.8.5", "feathers-mongoose": "^8.3.0", "lodash": "^4.17.15", - "mongoose": "^5.9.18" + "mongoose": "^5.9.18", + "which-types": "^1.0.3" }, "repository": { "type": "git", diff --git a/populateDb.ts b/populateDb.ts index ae42d86..a666427 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -29,9 +29,7 @@ const names: string[] = [ ]; - const createPoll = (authorId: string, generateImageData:()=> ImageDataSchema): Promise<PollSchema> => { - return app.service('polls').create({ contents: { left: generateImageData(), @@ -60,7 +58,7 @@ const populate = async () => { await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { const sampleUser = _.sample(users); - return createPoll(sampleUser?._id,generateImageData); + return createPoll(sampleUser?._id, generateImageData); }); }; diff --git a/services/auth/auth.service.ts b/services/auth/auth.service.ts index 42846b0..826357c 100644 --- a/services/auth/auth.service.ts +++ b/services/auth/auth.service.ts @@ -1,6 +1,6 @@ import { AuthenticationService, - JWTStrategy + JWTStrategy } from '@feathersjs/authentication'; import { LocalStrategy } from '@feathersjs/authentication-local'; import { Application } from '@feathersjs/express'; diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index 0637320..a914cd0 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,12 +1,12 @@ import { - expandAuthorHook, - expandAuthorManyHook -} from '../../hooks/expandAuthor'; + convertPollHook, + convertPollManyHook +} from '../../hooks/convertPoll'; export default { after: { - get: [expandAuthorHook], - find: [expandAuthorManyHook] + get: [convertPollHook], + find: [convertPollManyHook] } }; diff --git a/services/profiles/profiles.hooks.ts b/services/profiles/profiles.hooks.ts index bb05d94..4447bee 100644 --- a/services/profiles/profiles.hooks.ts +++ b/services/profiles/profiles.hooks.ts @@ -1,10 +1,10 @@ import { - expandAuthorManyHook -} from '../../hooks/expandAuthor'; + convertPollManyHook +} from '../../hooks/convertPoll'; export default { after: { - get: [expandAuthorManyHook] + get: [convertPollManyHook] } }; |