From e63122d84c65f30eb1e063504e7f16a05eb769f1 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 15:40:53 +0300 Subject: feat: create Feedback model --- models/feedback/feedback.model.ts | 5 +++++ models/feedback/feedback.schema.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 models/feedback/feedback.model.ts create mode 100644 models/feedback/feedback.schema.ts diff --git a/models/feedback/feedback.model.ts b/models/feedback/feedback.model.ts new file mode 100644 index 0000000..b2ec4b5 --- /dev/null +++ b/models/feedback/feedback.model.ts @@ -0,0 +1,5 @@ +import { Model, model, Types } from 'mongoose'; +import { FeedbackSchema, feedbackSchema } from './feedback.schema'; + +export default model>('Feedback', feedbackSchema); + diff --git a/models/feedback/feedback.schema.ts b/models/feedback/feedback.schema.ts new file mode 100644 index 0000000..a0844d6 --- /dev/null +++ b/models/feedback/feedback.schema.ts @@ -0,0 +1,28 @@ +import { Document, Schema, Types } from 'mongoose'; + +export interface FeedbackSchema extends Document { + contents: string; + authorId: string; + score: number; + version: string; + createdAt: Date; +} + +export const FeedbackSchema = new Schema({ + contents: String, + authorId: { + type: Types.ObjectId, + required: true, + ref: 'User' + }, + score: { + type: Number, + required: true + }, + version: { + type: String, + match: /^v\d+\.\d+\.\d+$/, + required: true + } +}, { timestamps: true }); + -- cgit v1.2.3 From 302a76985cef867f509a4180387cc45e934452d5 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 16:13:34 +0300 Subject: feat: improve votes --- hooks/signAuthority.ts | 8 ++++++++ models/votes/vote.model.ts | 2 +- models/votes/vote.schema.ts | 6 ++---- package-lock.json | 6 +++--- package.json | 2 +- services/votes/votes.hooks.ts | 10 ++-------- 6 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 hooks/signAuthority.ts diff --git a/hooks/signAuthority.ts b/hooks/signAuthority.ts new file mode 100644 index 0000000..b4d74b8 --- /dev/null +++ b/hooks/signAuthority.ts @@ -0,0 +1,8 @@ +import { HookContext } from '@feathersjs/feathers'; + +export default async (context: HookContext): Promise => { + const { params: { user } } = context; + context.data.authorId = user._id; + return context; +}; + diff --git a/models/votes/vote.model.ts b/models/votes/vote.model.ts index df2307e..bf2dcf6 100644 --- a/models/votes/vote.model.ts +++ b/models/votes/vote.model.ts @@ -1,7 +1,7 @@ import { Model, model } from 'mongoose'; import { VoteSchema, voteSchema } from './vote.schema'; -voteSchema.index({ pollId: 1, userId: 1 }, { unique: true }); // Unique together +voteSchema.index({ pollId: 1, authorId: 1 }, { unique: true }); // Unique together export default model>('Vote', voteSchema); diff --git a/models/votes/vote.schema.ts b/models/votes/vote.schema.ts index 63ba212..d8128b3 100644 --- a/models/votes/vote.schema.ts +++ b/models/votes/vote.schema.ts @@ -1,12 +1,10 @@ import { Document, Schema, Types } from 'mongoose'; import { Vote } from 'which-types'; -export interface VoteSchema extends Document, Omit { - password: string; -} +export interface VoteSchema extends Document, Omit {}; export const voteSchema = new Schema({ - userId: { + authorId: { type: Types.ObjectId, ref: 'user', required: true diff --git a/package-lock.json b/package-lock.json index c0621bf..7907c1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3093,9 +3093,9 @@ } }, "which-types": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.4.2.tgz", - "integrity": "sha512-nwcohvhH+VEA11cReLi/BgeuKHJYH7VM2BWe9OIX89CB+iaZ0+wb6oLFcIP6Vp6jw3k93yoPMe9pMBsOi4kj6w==" + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.5.1.tgz", + "integrity": "sha512-/WqGky5CFI1fiBFKSZz26aUjJ1F+OAnu2QiOIjFGH4V48tL6sdiDOVZsJaZJHXYa6p/y3vql7PdX+jadfI6kvw==" }, "word-wrap": { "version": "1.2.3", diff --git a/package.json b/package.json index c55255c..6f48dfa 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "mongoose": "^5.9.18", "ts-node": "^8.10.2", "typescript": "^3.9.5", - "which-types": "^1.4.2" + "which-types": "^1.5.1" }, "repository": { "type": "git", diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts index 7d0b3ba..56e9000 100644 --- a/services/votes/votes.hooks.ts +++ b/services/votes/votes.hooks.ts @@ -1,15 +1,9 @@ -import { HookContext } from '@feathersjs/feathers'; import requireAuth from '../../hooks/requireAuth'; - -const addUserId = async (context: HookContext): Promise => { - const { params: { user } } = context; - context.data.userId = user._id; - return context; -}; +import signAuthority from '../../hooks/signAuthority'; export default { before: { - create: [requireAuth, addUserId] + create: [requireAuth, signAuthority] } }; -- cgit v1.2.3 From 02b0da08155c6615a48b8d7f49648c19c1600020 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 16:18:20 +0300 Subject: feat: create feedback service --- models/feedback/feedback.schema.ts | 2 +- populateDb.ts | 13 ++++++++++++- services/feedback/feedback.hooks.ts | 10 ++++++++++ services/feedback/feedback.service.ts | 13 +++++++++++++ services/index.ts | 2 ++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 services/feedback/feedback.hooks.ts create mode 100644 services/feedback/feedback.service.ts diff --git a/models/feedback/feedback.schema.ts b/models/feedback/feedback.schema.ts index a0844d6..ea6f6e7 100644 --- a/models/feedback/feedback.schema.ts +++ b/models/feedback/feedback.schema.ts @@ -8,7 +8,7 @@ export interface FeedbackSchema extends Document { createdAt: Date; } -export const FeedbackSchema = new Schema({ +export const feedbackSchema = new Schema({ contents: String, authorId: { type: Types.ObjectId, diff --git a/populateDb.ts b/populateDb.ts index b3e46af..991c152 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -1,7 +1,7 @@ import mongoose from 'mongoose'; import bluebird from 'bluebird'; import _ from 'lodash'; -import { User, Poll, Vote } from 'which-types'; +import { User, Poll, Vote, Feedback } from 'which-types'; import app from './app'; @@ -71,6 +71,13 @@ const createVote = (userId: string, pollId: string): Promise => { }, { user: { _id: userId }, authenticated: true }); }; +const createFeedback = (userId: string): Promise => { + return app.service('feedback').create({ + version: 'v1.0.0', + score: _.sample([1, 2, 3, 4, 5]), + content: 'Absolutely amazing!' + }, { user: { _id: userId }, authenticated: true }); +}; const populate = async () => { const users = await bluebird.map(names, name => createUser(name)); @@ -80,6 +87,10 @@ const populate = async () => { return createPoll(user?._id || ''); }); + await bluebird.map(users, user => { + return createFeedback(user?._id || ''); + }); + await bluebird.map(users, user => { const pollsToVote = _.sampleSize(polls, _.random(0, POLLS_AMOUNT)); return bluebird.map(pollsToVote, poll => createVote(user?._id || '', poll?._id || '')); diff --git a/services/feedback/feedback.hooks.ts b/services/feedback/feedback.hooks.ts new file mode 100644 index 0000000..04f3af1 --- /dev/null +++ b/services/feedback/feedback.hooks.ts @@ -0,0 +1,10 @@ +import { HookContext } from '@feathersjs/feathers'; +import requireAuth from '../../hooks/requireAuth'; +import signAuthority from '../../hooks/signAuthority'; + +export default { + before: { + create: [requireAuth, signAuthority] + } +}; + diff --git a/services/feedback/feedback.service.ts b/services/feedback/feedback.service.ts new file mode 100644 index 0000000..a15ede9 --- /dev/null +++ b/services/feedback/feedback.service.ts @@ -0,0 +1,13 @@ +import { Application } from '@feathersjs/express'; +import service from 'feathers-mongoose'; +import Model from '../../models/feedback/feedback.model'; + +import hooks from './feedback.hooks'; + +const FeebackService = service({ Model }); + +export default (app: Application): void => { + app.use('/feedback', FeebackService); + app.service('feedback').hooks(hooks); +}; + diff --git a/services/index.ts b/services/index.ts index 1763a17..e5ea703 100644 --- a/services/index.ts +++ b/services/index.ts @@ -5,6 +5,7 @@ import Profiles from './profiles/profiles.service'; import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; import Feed from './feed/feed.service'; +import Feedback from './feedback/feedback.service'; import tryAuthenticate from '../hooks/tryAuthenticate'; import logging from '../hooks/logging'; @@ -17,6 +18,7 @@ export default (app: Application): void => { app.configure(Profiles); app.configure(Votes); app.configure(Feed); + app.configure(Feedback); app.hooks({ before: { -- cgit v1.2.3 From c3438eb4a7187773e2736dea765bb8281f7edf0e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 18:33:58 +0300 Subject: refactor: move convertPoll to polls hooks --- hooks/convertPoll.ts | 43 ------------------------------------------- services/feed/feed.hooks.ts | 4 ++-- services/polls/polls.hooks.ts | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 46 deletions(-) delete mode 100644 hooks/convertPoll.ts diff --git a/hooks/convertPoll.ts b/hooks/convertPoll.ts deleted file mode 100644 index 5e6f9f4..0000000 --- a/hooks/convertPoll.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { HookContext } from '@feathersjs/feathers'; -import { Types } from 'mongoose'; -import bluebird from 'bluebird'; -import _ from 'lodash'; -import { Poll } from 'which-types'; - -import { PollSchema } from '../models/polls/poll.schema'; -import VoteModel from '../models/votes/vote.model'; - - -export default async (context: HookContext): Promise => { - const { app, result, params: { user } } = context; - - const convert = async (poll: PollSchema): Promise => { - const author = await app.service('users').get(poll.authorId); - - const contents = await VoteModel.aggregate([ - { $match: { pollId: Types.ObjectId(poll._id) } }, - { $group: { _id: '$which', total: { $sum: 1 } } } - ]).then(groups => groups.reduce( - (acc, group) => _.set(acc, `${group._id}.votes`, group.total), - { left: { votes: 0 }, right: { votes: 0 } } - )); - - const userChoice = await VoteModel.findOne( - { pollId: poll._id, userId: user?._id } - ).then(vote => vote?.which); - - return _.merge( - _.omit(poll, ['authorId']), - { author, contents, userChoice } - ); - }; - - if (Array.isArray(result)) { - const polls = await bluebird.map(result, (poll: PollSchema) => convert(poll)); - context.result = _.compact(polls); - } else { - context.result = await convert(result); - } - return context; -}; - diff --git a/services/feed/feed.hooks.ts b/services/feed/feed.hooks.ts index 54f6d61..6bff8dc 100644 --- a/services/feed/feed.hooks.ts +++ b/services/feed/feed.hooks.ts @@ -7,7 +7,7 @@ const raiseNewVerifedPolls = async (context: HookContext): Promise // Raise unseen verified polls to the very top context.result = _.sortBy( context.result, - poll => !(poll.author.verified && !poll.userChoice) + poll => !(poll.author.verified && !poll.vote) ); return context; }; @@ -16,7 +16,7 @@ const lowerOldPolls = async (context: HookContext): Promise => { // Move all seen polls down context.result = _.sortBy( context.result, - poll => !!poll.userChoice + poll => !!poll.vote ); return context; }; diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index 77fcc7a..9f2183f 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,6 +1,47 @@ -import convertPoll from '../../hooks/convertPoll'; +import { HookContext } from '@feathersjs/feathers'; +import { Types } from 'mongoose'; +import bluebird from 'bluebird'; import _ from 'lodash'; +import { Poll } from 'which-types'; + +import { PollSchema } from '../../models/polls/poll.schema'; +import VoteModel from '../../models/votes/vote.model'; import sortByDate from '../../hooks/sortByDate'; + +const convertPoll = async (context: HookContext): Promise => { + const { app, result, params: { user } } = context; + + const convert = async (poll: PollSchema): Promise => { + const author = await app.service('users').get(poll.authorId); + + const contents = await VoteModel.aggregate([ + { $match: { pollId: Types.ObjectId(poll._id) } }, + { $group: { _id: '$which', total: { $sum: 1 } } } + ]).then(groups => groups.reduce( + (acc, group) => _.set(acc, `${group._id}.votes`, group.total), + { left: { votes: 0 }, right: { votes: 0 } } + )); + + const vote = await VoteModel.findOne( + { pollId: poll._id, authorId: user?._id } + ); + + return _.merge( + _.omit(poll, ['authorId']), + { author, contents, vote } + ); + }; + + if (Array.isArray(result)) { + const polls = await bluebird.map(result, (poll: PollSchema) => convert(poll)); + context.result = _.compact(polls); + } else { + context.result = await convert(result); + } + return context; +}; + + export default { before: { find: sortByDate -- cgit v1.2.3 From 00387d9ad38049db555cad7b5cb375167a108a6e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 18:56:14 +0300 Subject: style: fix linting errors --- models/feedback/feedback.model.ts | 2 +- models/votes/vote.schema.ts | 2 +- populateDb.ts | 7 ++++++- services/feedback/feedback.hooks.ts | 1 - 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/models/feedback/feedback.model.ts b/models/feedback/feedback.model.ts index b2ec4b5..793f8da 100644 --- a/models/feedback/feedback.model.ts +++ b/models/feedback/feedback.model.ts @@ -1,4 +1,4 @@ -import { Model, model, Types } from 'mongoose'; +import { Model, model } from 'mongoose'; import { FeedbackSchema, feedbackSchema } from './feedback.schema'; export default model>('Feedback', feedbackSchema); diff --git a/models/votes/vote.schema.ts b/models/votes/vote.schema.ts index d8128b3..72b196d 100644 --- a/models/votes/vote.schema.ts +++ b/models/votes/vote.schema.ts @@ -1,7 +1,7 @@ import { Document, Schema, Types } from 'mongoose'; import { Vote } from 'which-types'; -export interface VoteSchema extends Document, Omit {}; +export interface VoteSchema extends Document, Omit {} export const voteSchema = new Schema({ authorId: { diff --git a/populateDb.ts b/populateDb.ts index 991c152..e25005d 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -1,7 +1,12 @@ import mongoose from 'mongoose'; import bluebird from 'bluebird'; import _ from 'lodash'; -import { User, Poll, Vote, Feedback } from 'which-types'; +import { + User, + Poll, + Vote, + Feedback +} from 'which-types'; import app from './app'; diff --git a/services/feedback/feedback.hooks.ts b/services/feedback/feedback.hooks.ts index 04f3af1..56e9000 100644 --- a/services/feedback/feedback.hooks.ts +++ b/services/feedback/feedback.hooks.ts @@ -1,4 +1,3 @@ -import { HookContext } from '@feathersjs/feathers'; import requireAuth from '../../hooks/requireAuth'; import signAuthority from '../../hooks/signAuthority'; -- cgit v1.2.3 From 094384b580ab36feb7e23507aab36d174022ac0c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 18:57:31 +0300 Subject: feat: only allow feedback once per version --- models/feedback/feedback.model.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/models/feedback/feedback.model.ts b/models/feedback/feedback.model.ts index 793f8da..b21747b 100644 --- a/models/feedback/feedback.model.ts +++ b/models/feedback/feedback.model.ts @@ -1,5 +1,7 @@ import { Model, model } from 'mongoose'; import { FeedbackSchema, feedbackSchema } from './feedback.schema'; +feedbackSchema.index({ version: 1, authorId: 1 }, { unique: true }); // Unique together + export default model>('Feedback', feedbackSchema); -- cgit v1.2.3 From 29197dd3bc7e941707979b6c226e5f3b1a4cbbed Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 18:59:55 +0300 Subject: chore: update which-types --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7907c1b..255fef5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3093,9 +3093,9 @@ } }, "which-types": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.5.1.tgz", - "integrity": "sha512-/WqGky5CFI1fiBFKSZz26aUjJ1F+OAnu2QiOIjFGH4V48tL6sdiDOVZsJaZJHXYa6p/y3vql7PdX+jadfI6kvw==" + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.6.1.tgz", + "integrity": "sha512-uTCrp6+rbU48kyT9Z6upVo9CgGmiR50zFSwzDik8slE8oZ+0FC9SBEfJlfgADX/rFJVbIe8Vxsw4BsSxlL5Lsw==" }, "word-wrap": { "version": "1.2.3", diff --git a/package.json b/package.json index 6f48dfa..3c3342d 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "mongoose": "^5.9.18", "ts-node": "^8.10.2", "typescript": "^3.9.5", - "which-types": "^1.5.1" + "which-types": "^1.6.1" }, "repository": { "type": "git", -- cgit v1.2.3