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 +++++ 1 file changed, 5 insertions(+) (limited to 'models/polls/poll.model.ts') 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); -- 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/polls/poll.model.ts') 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 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'models/polls/poll.model.ts') 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); -- 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/polls/poll.model.ts') 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 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'models/polls/poll.model.ts') 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); -- cgit v1.2.3