Skip to content

Commit 88e97a4

Browse files
authored
Merge pull request #16 from which-ecosystem/feedback
Feedback endpoint & schema updates
2 parents 8baf96b + 29197dd commit 88e97a4

File tree

15 files changed

+137
-64
lines changed

15 files changed

+137
-64
lines changed

hooks/convertPoll.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

hooks/signAuthority.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HookContext } from '@feathersjs/feathers';
2+
3+
export default async (context: HookContext): Promise<HookContext> => {
4+
const { params: { user } } = context;
5+
context.data.authorId = user._id;
6+
return context;
7+
};
8+

models/feedback/feedback.model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Model, model } from 'mongoose';
2+
import { FeedbackSchema, feedbackSchema } from './feedback.schema';
3+
4+
feedbackSchema.index({ version: 1, authorId: 1 }, { unique: true }); // Unique together
5+
6+
export default model<FeedbackSchema, Model<FeedbackSchema>>('Feedback', feedbackSchema);
7+

models/feedback/feedback.schema.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Document, Schema, Types } from 'mongoose';
2+
3+
export interface FeedbackSchema extends Document {
4+
contents: string;
5+
authorId: string;
6+
score: number;
7+
version: string;
8+
createdAt: Date;
9+
}
10+
11+
export const feedbackSchema = new Schema({
12+
contents: String,
13+
authorId: {
14+
type: Types.ObjectId,
15+
required: true,
16+
ref: 'User'
17+
},
18+
score: {
19+
type: Number,
20+
required: true
21+
},
22+
version: {
23+
type: String,
24+
match: /^v\d+\.\d+\.\d+$/,
25+
required: true
26+
}
27+
}, { timestamps: true });
28+

models/votes/vote.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Model, model } from 'mongoose';
22
import { VoteSchema, voteSchema } from './vote.schema';
33

4-
voteSchema.index({ pollId: 1, userId: 1 }, { unique: true }); // Unique together
4+
voteSchema.index({ pollId: 1, authorId: 1 }, { unique: true }); // Unique together
55

66
export default model<VoteSchema, Model<VoteSchema>>('Vote', voteSchema);
77

models/votes/vote.schema.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { Document, Schema, Types } from 'mongoose';
22
import { Vote } from 'which-types';
33

4-
export interface VoteSchema extends Document, Omit<Vote, '_id'> {
5-
password: string;
6-
}
4+
export interface VoteSchema extends Document, Omit<Vote, '_id'> {}
75

86
export const voteSchema = new Schema({
9-
userId: {
7+
authorId: {
108
type: Types.ObjectId,
119
ref: 'user',
1210
required: true

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"mongoose": "^5.9.18",
3434
"ts-node": "^8.10.2",
3535
"typescript": "^3.9.5",
36-
"which-types": "^1.4.2"
36+
"which-types": "^1.6.1"
3737
},
3838
"repository": {
3939
"type": "git",

populateDb.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import mongoose from 'mongoose';
22
import bluebird from 'bluebird';
33
import _ from 'lodash';
4-
import { User, Poll, Vote } from 'which-types';
4+
import {
5+
User,
6+
Poll,
7+
Vote,
8+
Feedback
9+
} from 'which-types';
510

611
import app from './app';
712

@@ -71,6 +76,13 @@ const createVote = (userId: string, pollId: string): Promise<Vote> => {
7176
}, { user: { _id: userId }, authenticated: true });
7277
};
7378

79+
const createFeedback = (userId: string): Promise<Feedback> => {
80+
return app.service('feedback').create({
81+
version: 'v1.0.0',
82+
score: _.sample([1, 2, 3, 4, 5]),
83+
content: 'Absolutely amazing!'
84+
}, { user: { _id: userId }, authenticated: true });
85+
};
7486

7587
const populate = async () => {
7688
const users = await bluebird.map(names, name => createUser(name));
@@ -80,6 +92,10 @@ const populate = async () => {
8092
return createPoll(user?._id || '');
8193
});
8294

95+
await bluebird.map(users, user => {
96+
return createFeedback(user?._id || '');
97+
});
98+
8399
await bluebird.map(users, user => {
84100
const pollsToVote = _.sampleSize(polls, _.random(0, POLLS_AMOUNT));
85101
return bluebird.map(pollsToVote, poll => createVote(user?._id || '', poll?._id || ''));

services/feed/feed.hooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const raiseNewVerifedPolls = async (context: HookContext): Promise<HookContext>
77
// Raise unseen verified polls to the very top
88
context.result = _.sortBy(
99
context.result,
10-
poll => !(poll.author.verified && !poll.userChoice)
10+
poll => !(poll.author.verified && !poll.vote)
1111
);
1212
return context;
1313
};
@@ -16,7 +16,7 @@ const lowerOldPolls = async (context: HookContext): Promise<HookContext> => {
1616
// Move all seen polls down
1717
context.result = _.sortBy(
1818
context.result,
19-
poll => !!poll.userChoice
19+
poll => !!poll.vote
2020
);
2121
return context;
2222
};

0 commit comments

Comments
 (0)