From f4c4f2d4789880c0cfc956d08aab10b5a93ebcb1 Mon Sep 17 00:00:00 2001 From: ilyayudovin Date: Sun, 21 Jun 2020 13:10:05 +0300 Subject: feat: change votes type --- models/polls/poll.schema.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'models') diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index bc6d497..236011f 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -7,6 +7,7 @@ export interface ImageData { } export interface Poll { + _id: string; author: User; contents: { left: ImageData; @@ -14,14 +15,22 @@ export interface Poll { }; } -export interface PollSchema extends Document, Omit { - authorId: string; +export interface ImageDataSchema { + url: string; + votes: string[]; } +export interface PollSchema extends Document { + contents: { + left: ImageDataSchema; + right: ImageDataSchema; + }; + authorId: string; +} -const imageDataSchema = { +export const imageDataSchema = { url: String, - votes: Number + votes: [Types.ObjectId] }; export const pollSchema = new Schema({ -- cgit v1.2.3 From e99b51895afd532a529744396ecae87d47c68503 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 21 Jun 2020 14:29:59 +0300 Subject: feat: change hooks and install which-types --- models/polls/poll.schema.ts | 15 --------------- models/users/user.schema.ts | 7 +------ 2 files changed, 1 insertion(+), 21 deletions(-) (limited to 'models') 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; -- cgit v1.2.3 From 1af98079d43b8bf5da7a32870b7bb60e5f59e554 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 01:47:12 +0300 Subject: feat: add vote function to schema --- models/polls/poll.model.ts | 5 +++++ models/polls/poll.schema.ts | 1 + 2 files changed, 6 insertions(+) (limited to 'models') diff --git a/models/polls/poll.model.ts b/models/polls/poll.model.ts index 7f6be9a..6f3d088 100644 --- a/models/polls/poll.model.ts +++ b/models/polls/poll.model.ts @@ -1,5 +1,10 @@ import { Model, model } from 'mongoose'; import { PollSchema, pollSchema } from './poll.schema'; +pollSchema.methods.vote = function(userId: string, which: 'left' | 'right'): PollSchema { + this.contents[which].votes.push(userId); + return this.save(); +} + export default model>('Poll', pollSchema); diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index fd6751c..0caa6b2 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -11,6 +11,7 @@ export interface PollSchema extends Document { right: ImageDataSchema; }; authorId: string; + vote: (userId: string, which: 'left' | 'right') => void; } export const imageDataSchema = { -- cgit v1.2.3 From 4101b4ff7a5c328164fe74ebfa3c42b82f332c43 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 21:12:07 +0300 Subject: fix: get rid of poll.vote function --- models/polls/poll.model.ts | 5 ----- 1 file changed, 5 deletions(-) (limited to 'models') diff --git a/models/polls/poll.model.ts b/models/polls/poll.model.ts index 6f3d088..7f6be9a 100644 --- a/models/polls/poll.model.ts +++ b/models/polls/poll.model.ts @@ -1,10 +1,5 @@ import { Model, model } from 'mongoose'; import { PollSchema, pollSchema } from './poll.schema'; -pollSchema.methods.vote = function(userId: string, which: 'left' | 'right'): PollSchema { - this.contents[which].votes.push(userId); - return this.save(); -} - export default model>('Poll', pollSchema); -- cgit v1.2.3 From dbfb0d30f0e5cd44ea188d72ceb05acac2ac40d7 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 23 Jun 2020 22:50:59 +0300 Subject: feat: only allow voting once --- models/polls/poll.model.ts | 13 +++++++++++++ models/polls/poll.schema.ts | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'models') diff --git a/models/polls/poll.model.ts b/models/polls/poll.model.ts index 7f6be9a..ecd8ad8 100644 --- a/models/polls/poll.model.ts +++ b/models/polls/poll.model.ts @@ -1,5 +1,18 @@ import { Model, model } from 'mongoose'; import { PollSchema, pollSchema } from './poll.schema'; +pollSchema.methods.vote = function(userId: string, which: 'left' | 'right'): PollSchema { + const participants = ['left', 'right'].reduce((acc, option) => { + const { votes } = this.contents[option]; + return acc.concat(votes); + }, []); + + if (!participants.indexOf(userId) === -1) { + this.contents[which].votes.push(userId); + } + + return this.save(); +} + export default model>('Poll', pollSchema); diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index 0caa6b2..380c069 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -11,7 +11,7 @@ export interface PollSchema extends Document { right: ImageDataSchema; }; authorId: string; - vote: (userId: string, which: 'left' | 'right') => void; + vote: (userId: string, which: 'left' | 'right') => PollSchema; } export const imageDataSchema = { -- cgit v1.2.3 From b054bd6cf5be0eed0c17fbacec8bcc09cf013d44 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 23 Jun 2020 23:59:31 +0300 Subject: fix: check user correctly --- models/polls/poll.model.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'models') diff --git a/models/polls/poll.model.ts b/models/polls/poll.model.ts index ecd8ad8..11d0cfa 100644 --- a/models/polls/poll.model.ts +++ b/models/polls/poll.model.ts @@ -1,13 +1,14 @@ import { Model, model } from 'mongoose'; import { PollSchema, pollSchema } from './poll.schema'; +import { Types } from 'mongoose'; pollSchema.methods.vote = function(userId: string, which: 'left' | 'right'): PollSchema { - const participants = ['left', 'right'].reduce((acc, option) => { + const participants: Types.ObjectId[] = ['left', 'right'].reduce((acc, option) => { const { votes } = this.contents[option]; return acc.concat(votes); }, []); - if (!participants.indexOf(userId) === -1) { + if (!participants.some(user => user.equals(userId))) { this.contents[which].votes.push(userId); } -- cgit v1.2.3 From 75e527334e4e9a94b63704f87aa650c75f13891c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 24 Jun 2020 00:58:45 +0300 Subject: feat: migrate to latest which-types --- models/polls/poll.model.ts | 3 ++- models/polls/poll.schema.ts | 3 ++- models/users/user.schema.ts | 10 ++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'models') diff --git a/models/polls/poll.model.ts b/models/polls/poll.model.ts index 11d0cfa..6749e5c 100644 --- a/models/polls/poll.model.ts +++ b/models/polls/poll.model.ts @@ -1,8 +1,9 @@ import { Model, model } from 'mongoose'; import { PollSchema, pollSchema } from './poll.schema'; import { Types } from 'mongoose'; +import { Which } from 'which-types'; -pollSchema.methods.vote = function(userId: string, which: 'left' | 'right'): PollSchema { +pollSchema.methods.vote = function(userId: string, which: Which): PollSchema { const participants: Types.ObjectId[] = ['left', 'right'].reduce((acc, option) => { const { votes } = this.contents[option]; return acc.concat(votes); diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index 380c069..d495e4b 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -10,6 +10,7 @@ export interface PollSchema extends Document { left: ImageDataSchema; right: ImageDataSchema; }; + createdAt: Date; authorId: string; vote: (userId: string, which: 'left' | 'right') => PollSchema; } @@ -28,5 +29,5 @@ export const pollSchema = new Schema({ type: Types.ObjectId, ref: 'User' } -}); +}, { timestamps: true }); diff --git a/models/users/user.schema.ts b/models/users/user.schema.ts index 9030d61..ff6cbe9 100644 --- a/models/users/user.schema.ts +++ b/models/users/user.schema.ts @@ -1,19 +1,21 @@ import { Document, Schema } from 'mongoose'; import { User } from 'which-types'; -export interface UserSchema extends Document, User { +export interface UserSchema extends Document, Omit { password: string; } export const userSchema = new Schema({ - name: String, + username: String, password: String, + email: String, avatarUrl: { type: String, required: false }, age: { - type: Number + type: Number, + required: false } -}); +}, { timestamps: true }); -- cgit v1.2.3 From ffcb9d248228087ee05c7569784705b41d729aee Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 08:56:47 +0300 Subject: feat: create vote model --- models/polls/poll.schema.ts | 7 ++++++- models/votes/vote.model.ts | 5 +++++ models/votes/vote.schema.ts | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 models/votes/vote.model.ts create mode 100644 models/votes/vote.schema.ts (limited to 'models') diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index d495e4b..2f39aa1 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -17,7 +17,12 @@ export interface PollSchema extends Document { export const imageDataSchema = { url: String, - votes: [Types.ObjectId] + votes: [ + { + type: Types.ObjectId, + ref: 'vote' + } + ] }; export const pollSchema = new Schema({ diff --git a/models/votes/vote.model.ts b/models/votes/vote.model.ts new file mode 100644 index 0000000..1dbb146 --- /dev/null +++ b/models/votes/vote.model.ts @@ -0,0 +1,5 @@ +import { Model, model } from 'mongoose'; +import { VoteSchema, voteSchema } from './vote.schema'; + +export default model>('Vote', voteSchema); + diff --git a/models/votes/vote.schema.ts b/models/votes/vote.schema.ts new file mode 100644 index 0000000..36b38f2 --- /dev/null +++ b/models/votes/vote.schema.ts @@ -0,0 +1,22 @@ +import { Document, Schema, Types } from 'mongoose'; +import { Vote } from 'which-types'; + +export interface VoteSchema extends Document, Omit { + password: string; +} + +export const voteSchema = new Schema({ + userId: { + type: Types.ObjectId, + ref: 'user' + }, + pollId: { + type: Types.ObjectId, + ref: 'poll' + }, + which: { + type: String, + match: /left|right/g + } +}, { timestamps: true }); + -- cgit v1.2.3 From fc9cf3b8f77e9068fa51d614b0d0b120f7bf2440 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 09:03:46 +0300 Subject: feat!: construct VoteService based on VoteModel --- models/polls/poll.schema.ts | 7 ------- 1 file changed, 7 deletions(-) (limited to 'models') diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index 2f39aa1..4d1d762 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -2,7 +2,6 @@ import { Document, Schema, Types } from 'mongoose'; export interface ImageDataSchema { url: string; - votes: string[]; } export interface PollSchema extends Document { @@ -17,12 +16,6 @@ export interface PollSchema extends Document { export const imageDataSchema = { url: String, - votes: [ - { - type: Types.ObjectId, - ref: 'vote' - } - ] }; export const pollSchema = new Schema({ -- cgit v1.2.3 From 2e28a2555119c8958737e84fd2312e9930046242 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 09:48:39 +0300 Subject: feat: change match to enum in voteSchema --- models/votes/vote.schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'models') diff --git a/models/votes/vote.schema.ts b/models/votes/vote.schema.ts index 36b38f2..709e8bf 100644 --- a/models/votes/vote.schema.ts +++ b/models/votes/vote.schema.ts @@ -16,7 +16,7 @@ export const voteSchema = new Schema({ }, which: { type: String, - match: /left|right/g + enum: ['left', 'right'] } }, { timestamps: true }); -- cgit v1.2.3 From a58354a1d1bf967fc932723b9138d8c6322591d5 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 11:23:20 +0300 Subject: feat: validate fields in votes --- models/votes/vote.model.ts | 2 ++ models/votes/vote.schema.ts | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'models') diff --git a/models/votes/vote.model.ts b/models/votes/vote.model.ts index 1dbb146..df2307e 100644 --- a/models/votes/vote.model.ts +++ b/models/votes/vote.model.ts @@ -1,5 +1,7 @@ import { Model, model } from 'mongoose'; import { VoteSchema, voteSchema } from './vote.schema'; +voteSchema.index({ pollId: 1, userId: 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 709e8bf..63ba212 100644 --- a/models/votes/vote.schema.ts +++ b/models/votes/vote.schema.ts @@ -8,15 +8,18 @@ export interface VoteSchema extends Document, Omit { export const voteSchema = new Schema({ userId: { type: Types.ObjectId, - ref: 'user' + ref: 'user', + required: true }, pollId: { type: Types.ObjectId, - ref: 'poll' + ref: 'poll', + required: true }, which: { type: String, - enum: ['left', 'right'] + enum: ['left', 'right'], + required: true } }, { timestamps: true }); -- cgit v1.2.3