Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add usecases for deletevoteservice #1390

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
340 changes: 265 additions & 75 deletions backend/src/modules/votes/applications/card-group-vote.use-case.spec.ts

Large diffs are not rendered by default.

212 changes: 210 additions & 2 deletions backend/src/modules/votes/applications/card-group-vote.use-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import { UseCase } from 'src/libs/interfaces/use-case.interface';
import { CreateVoteServiceInterface } from '../interfaces/services/create.vote.service.interface';
import { UpdateBoardUserServiceInterface } from 'src/modules/boardUsers/interfaces/services/update.board.user.service.interface';
import { InsertFailedException } from 'src/libs/exceptions/insertFailedBadRequestException';
import { INSERT_VOTE_FAILED } from 'src/libs/exceptions/messages';
import { DELETE_VOTE_FAILED, INSERT_VOTE_FAILED } from 'src/libs/exceptions/messages';
import { WRITE_LOCK_ERROR } from 'src/libs/constants/database';
import { VoteRepositoryInterface } from '../interfaces/repositories/vote.repository.interface';
import CardGroupVoteUseCaseDto from '../dto/useCase/card-group-vote.use-case.dto';
import { DeleteVoteServiceInterface } from '../interfaces/services/delete.vote.service.interface';
import isEmpty from 'src/libs/utils/isEmpty';
import Card from 'src/modules/cards/entities/card.schema';
import { DeleteFailedException } from 'src/libs/exceptions/deleteFailedBadRequestException';
import { arrayIdToString } from 'src/libs/utils/arrayIdToString';
import { getVotesFromCardItem } from '../utils/getVotesFromCardItem';

@Injectable()
export class CardGroupVoteUseCase implements UseCase<CardGroupVoteUseCaseDto, void> {
Expand All @@ -27,7 +32,7 @@ export class CardGroupVoteUseCase implements UseCase<CardGroupVoteUseCaseDto, vo

async execute({ boardId, cardId, userId, count, completionHandler }: CardGroupVoteUseCaseDto) {
if (count < 0) {
await this.deleteVoteService.deleteVoteFromCardGroup(boardId, cardId, userId, count);
await this.deleteVoteFromCardGroup(boardId, cardId, userId, count);
} else {
await this.addVoteToCardGroupAndUser(boardId, userId, count, cardId);
}
Expand Down Expand Up @@ -94,4 +99,207 @@ export class CardGroupVoteUseCase implements UseCase<CardGroupVoteUseCaseDto, vo
}
}
}

private async deleteVoteFromCardGroup(
boardId: string,
cardId: string,
userId: string,
count: number,
retryCount?: number
) {
await this.deleteVoteService.canUserDeleteVote(boardId, userId, count, cardId);

let currentCount = Math.abs(count);

const card = await this.deleteVoteService.getCardFromBoard(boardId, cardId);

const mappedVotes = card.votes as string[];

const userVotes = mappedVotes.filter((vote) => vote.toString() === userId.toString());

await this.updateBoardUserService.startTransaction();
await this.voteRepository.startTransaction();

try {
const withSession = true;

if (!isEmpty(userVotes)) {
currentCount = await this.deleteCardGroupAndUserVotes(
mappedVotes,
userVotes,
boardId,
cardId,
userId,
count,
currentCount,
withSession,
retryCount
);
}

if (!isEmpty(currentCount)) {
await this.deleteCardItemAndUserVotes(
currentCount,
boardId,
card,
userId,
withSession,
retryCount
);
}
await this.updateBoardUserService.commitTransaction();
await this.voteRepository.commitTransaction();
} catch (e) {
this.logger.error(e);
throw new DeleteFailedException(DELETE_VOTE_FAILED);
} finally {
await this.updateBoardUserService.endSession();
await this.voteRepository.endSession();
}
}

private async deleteCardItemAndUserVotes(
currentCount: number,
boardId: string,
card: Card,
userId: string,
withSession: boolean,
retryCount?: number
) {
let items = card.items;

while (currentCount > 0) {
const item = items.find(({ votes: itemVotes }) =>
arrayIdToString(itemVotes as string[]).includes(userId.toString())
);

if (!item) {
throw new DeleteFailedException(DELETE_VOTE_FAILED);
}

const votesOfUser = (item.votes as string[]).filter(
(vote) => vote.toString() === userId.toString()
);

const itemVotesToReduce =
votesOfUser.length / currentCount >= 1 ? currentCount : votesOfUser.length;

await this.deleteVoteFromCardItemOnCardGroup(
boardId,
card._id,
userId,
String(item._id),
-itemVotesToReduce,
withSession,
retryCount
);

currentCount -= itemVotesToReduce;
items = items.filter((card) => String(card._id) !== String(item._id));
}
}

private async deleteVoteFromCardItemOnCardGroup(
boardId: string,
cardId: string,
userId: string,
cardItemId: string,
count: number,
withSession: boolean,
retryCount?: number
) {
const cardItem = await this.deleteVoteService.getCardItemFromBoard(boardId, cardId, cardItemId);

const votes = getVotesFromCardItem(cardItem.votes as string[], String(userId), count);

let retryCountOperation = retryCount ?? 0;

try {
await this.deleteVoteService.removeVotesFromCardItem(
boardId,
cardItemId,
votes,
cardId,
withSession
);
await this.deleteVoteService.decrementVoteUser(boardId, userId, count, withSession);
} catch (e) {
this.logger.error(e);

await this.updateBoardUserService.abortTransaction();
await this.voteRepository.abortTransaction();

if (e.code === WRITE_LOCK_ERROR && retryCountOperation < 5) {
retryCountOperation++;
await this.updateBoardUserService.endSession();
await this.voteRepository.endSession();

await this.deleteVoteFromCardGroup(boardId, cardId, userId, count, retryCountOperation);
} else {
throw new DeleteFailedException(DELETE_VOTE_FAILED);
}
}
}

private async deleteCardGroupAndUserVotes(
votes: string[],
userVotes: string[],
boardId: string,
cardId: string,
userId: string,
count: number,
currentCount: number,
withSession: boolean,
retryCount?: number
) {
let mappedVotes = votes.filter((vote) => vote.toString() !== userId.toString());

const votesToReduce = userVotes.length / currentCount >= 1 ? currentCount : userVotes.length;

userVotes.splice(0, Math.abs(votesToReduce));

mappedVotes = mappedVotes.concat(userVotes);

let retryCountOperation = retryCount ?? 0;

try {
await this.removeVotesFromCardGroup(boardId, mappedVotes, cardId, withSession);
await this.deleteVoteService.decrementVoteUser(boardId, userId, -votesToReduce, withSession);

currentCount -= Math.abs(votesToReduce);

if (currentCount === 0) return;

return currentCount;
} catch (e) {
this.logger.error('error', e);
await this.updateBoardUserService.abortTransaction();
await this.voteRepository.abortTransaction();

if (e.code === WRITE_LOCK_ERROR && retryCountOperation < 5) {
retryCountOperation++;
await this.updateBoardUserService.endSession();
await this.voteRepository.endSession();
await this.deleteVoteFromCardGroup(boardId, cardId, userId, count, retryCountOperation);
} else {
throw new DeleteFailedException(DELETE_VOTE_FAILED);
}
}
}

private async removeVotesFromCardGroup(
boardId: string,
mappedVotes: string[],
cardId: string,
withSession?: boolean
) {
const updatedBoard = await this.voteRepository.removeVotesFromCard(
boardId,
mappedVotes,
cardId,
withSession
);

if (!updatedBoard) throw new DeleteFailedException(DELETE_VOTE_FAILED);
}
}
Loading