Skip to content

Commit

Permalink
fix: pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
CatiaBarroco-xgeeks committed Aug 22, 2022
1 parent c87ae14 commit 262cd0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
23 changes: 11 additions & 12 deletions backend/src/modules/votes/services/create.vote.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

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

private async voteEnable(boardId: string, userId: string): Promise<boolean> {
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!');
}
const maxVotes = board?.maxVotes as Number;
const maxVotes = Number(board?.maxVotes);

// userFound
const userFound = await this.boardUserModel.find({ board: boardId, user: userId });
const voteAllowed = userFound[0].votesCount + 1 <= maxVotes;
const userCanVote = maxVotes === null || voteAllowed;
return userCanVote;
return maxVotes === null || voteAllowed;
}

async incrementVoteUser(boardId: string, userId: string) {
private async incrementVoteUser(boardId: string, userId: string) {
const boardUser = this.boardUserModel
.findOneAndUpdate(
{
Expand All @@ -48,8 +47,8 @@ export default class CreateVoteServiceImpl implements CreateVoteService {
}

async addVoteToCard(boardId: string, cardId: string, userId: string, cardItemId: string) {
const voteEnable = await this.voteEnable(boardId, userId);
if (voteEnable) {
const canUserVote = await this.canUserVote(boardId, userId);
if (canUserVote) {
await this.incrementVoteUser(boardId, userId);
return this.boardModel
.findOneAndUpdate(
Expand All @@ -76,12 +75,12 @@ export default class CreateVoteServiceImpl implements CreateVoteService {
.lean()
.exec();
}
return null;
throw new BadRequestException('Error adding a vote');
}

async addVoteToCardGroup(boardId: string, cardId: string, userId: string) {
const voteEnable = await this.voteEnable(boardId, userId);
if (voteEnable) {
const canUserVote = await this.canUserVote(boardId, userId);
if (canUserVote) {
await this.incrementVoteUser(boardId, userId);
return this.boardModel
.findOneAndUpdate(
Expand All @@ -102,6 +101,6 @@ export default class CreateVoteServiceImpl implements CreateVoteService {
.lean()
.exec();
}
return null;
throw new BadRequestException('Error adding a vote');
}
}
1 change: 0 additions & 1 deletion frontend/src/components/Board/Card/CardFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useMemo } from 'react';

import { styled } from 'styles/stitches/stitches.config';
Expand Down

0 comments on commit 262cd0f

Please sign in to comment.