Skip to content

Commit

Permalink
[FEATURE]: User vote count limit on a specific board verification (#414)
Browse files Browse the repository at this point in the history
* test: vote conditions

* test: vote conditions

* test: backend conditions, frontend remove conditions

* feat: delete comments

* feat: change variable name

* feat: pr suggestions

* fix: pr suggestions

* fix: change await functions

* feat: pr suggestions

* fix: addingvotes implementation

Co-authored-by: Francisco Morgado <f.morgado@kigroup.de>
  • Loading branch information
CatiaBarroco-xgeeks and f-morgado authored Aug 25, 2022
1 parent 81b10ed commit 55dc6dd
Showing 1 changed file with 76 additions and 47 deletions.
123 changes: 76 additions & 47 deletions backend/src/modules/votes/services/create.vote.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import { UPDATE_FAILED } from 'libs/exceptions/messages';
import Board, { BoardDocument } from 'modules/boards/schemas/board.schema';
import BoardUser, { BoardUserDocument } from 'modules/boards/schemas/board.user.schema';

Expand All @@ -15,8 +16,24 @@ export default class CreateVoteServiceImpl implements CreateVoteService {
private boardUserModel: Model<BoardUserDocument>
) {}

incrementVoteUser(boardId: string, userId: string) {
return this.boardUserModel
private async canUserVote(boardId: string, userId: string): Promise<boolean> {
const board = await this.boardModel.findById(boardId).exec();
if (!board) {
throw new NotFoundException('Board not found!');
}
if (board.maxVotes === null || board.maxVotes === undefined) {
return true;
}
const maxVotes = Number(board.maxVotes);

const boardUserFound = await this.boardUserModel
.findOne({ board: boardId, user: userId })
.exec();
return boardUserFound?.votesCount ? boardUserFound.votesCount + 1 <= maxVotes : false;
}

private async incrementVoteUser(boardId: string, userId: string) {
const boardUser = await this.boardUserModel
.findOneAndUpdate(
{
user: userId,
Expand All @@ -28,55 +45,67 @@ export default class CreateVoteServiceImpl implements CreateVoteService {
)
.lean()
.exec();
if (!boardUser) throw new BadRequestException(UPDATE_FAILED);
return boardUser;
}

addVoteToCard(boardId: string, cardId: string, userId: string, cardItemId: string) {
this.incrementVoteUser(boardId, userId);
return this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards.items._id': cardItemId
},
{
$push: {
'columns.$.cards.$[c].items.$[i].votes': userId
async addVoteToCard(boardId: string, cardId: string, userId: string, cardItemId: string) {
const canUserVote = await this.canUserVote(boardId, userId);
if (canUserVote) {
await this.incrementVoteUser(boardId, userId);
const board = await this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards.items._id': cardItemId
},
$inc: { totalUsedVotes: 1 }
},
{
arrayFilters: [{ 'c._id': cardId }, { 'i._id': cardItemId }],
new: true
}
)
.populate({
path: 'users',
select: 'user role votesCount -board',
populate: { path: 'user', select: 'firstName lastName _id' }
})
.lean()
.exec();
{
$push: {
'columns.$.cards.$[c].items.$[i].votes': userId
},
$inc: { totalUsedVotes: 1 }
},
{
arrayFilters: [{ 'c._id': cardId }, { 'i._id': cardItemId }],
new: true
}
)
.populate({
path: 'users',
select: 'user role votesCount -board',
populate: { path: 'user', select: 'firstName lastName _id' }
})
.lean()
.exec();
return board;
}
throw new BadRequestException('Error adding a vote');
}

addVoteToCardGroup(boardId: string, cardId: string, userId: string) {
this.incrementVoteUser(boardId, userId);
return this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards._id': cardId
},
{
$push: {
'columns.$.cards.$[c].votes': userId
async addVoteToCardGroup(boardId: string, cardId: string, userId: string) {
const canUserVote = await this.canUserVote(boardId, userId);
if (canUserVote) {
await this.incrementVoteUser(boardId, userId);
const board = await this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards._id': cardId
},
{
$push: {
'columns.$.cards.$[c].votes': userId
}
},
{
arrayFilters: [{ 'c._id': cardId }],
new: true
}
},
{
arrayFilters: [{ 'c._id': cardId }],
new: true
}
)
.lean()
.exec();
)
.lean()
.exec();
return board;
}
throw new BadRequestException('Error adding a vote');
}
}

0 comments on commit 55dc6dd

Please sign in to comment.