diff --git a/backend/src/modules/cards/services/delete.card.service.ts b/backend/src/modules/cards/services/delete.card.service.ts index c0feaa9c4..4aabd1688 100644 --- a/backend/src/modules/cards/services/delete.card.service.ts +++ b/backend/src/modules/cards/services/delete.card.service.ts @@ -1,6 +1,6 @@ import { Inject, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; -import { ClientSession, LeanDocument, Model, ObjectId } from 'mongoose'; +import { LeanDocument, Model, ObjectId } from 'mongoose'; import { UPDATE_FAILED } from 'libs/exceptions/messages'; import Board, { BoardDocument } from 'modules/boards/schemas/board.schema'; @@ -27,18 +27,14 @@ export default class DeleteCardServiceImpl implements DeleteCardService { async deletedVotesFromCardItem(boardId: string, cardItemId: string) { const getCardItem = await this.getCardService.getCardItemFromGroup(boardId, cardItemId); - if (!getCardItem) { throw Error(UPDATE_FAILED); } - if (getCardItem.votes?.length) { const promises = getCardItem.votes.map((voteUserId) => { return this.deleteVoteService.decrementVoteUser(boardId, voteUserId); }); - const results = await Promise.all(promises); - if (results.some((i) => i === null)) { throw Error(UPDATE_FAILED); } @@ -47,23 +43,18 @@ export default class DeleteCardServiceImpl implements DeleteCardService { async deletedVotesFromCard(boardId: string, cardId: string) { const getCard = await this.getCardService.getCardFromBoard(boardId, cardId); - if (!getCard) { throw Error(UPDATE_FAILED); } - if (getCard.votes?.length) { const promises = getCard.votes.map((voteUserId) => { return this.deleteVoteService.decrementVoteUser(boardId, voteUserId); }); - const results = await Promise.all(promises); - if (results.some((i) => i === null)) { throw Error(UPDATE_FAILED); } } - if (Array.isArray(getCard.items)) { const promises: Promise | null>[] = []; getCard.items.forEach(async (current) => { @@ -71,9 +62,7 @@ export default class DeleteCardServiceImpl implements DeleteCardService { promises.push(this.deleteVoteService.decrementVoteUser(boardId, currentVote)); }); }); - const results = await Promise.all(promises); - if (results.some((i) => i === null)) { throw Error(UPDATE_FAILED); } @@ -116,9 +105,10 @@ export default class DeleteCardServiceImpl implements DeleteCardService { cardId: string, newVotes: (LeanDocument | LeanDocument)[], newComments: LeanDocument[], - cardItems: LeanDocument[], - session: ClientSession + cardItems: LeanDocument[] ) { + const [{ text, createdBy }] = cardItems; + const board = await this.boardModel .findOneAndUpdate( { @@ -130,7 +120,9 @@ export default class DeleteCardServiceImpl implements DeleteCardService { 'columns.$.cards.$[card].items.$[cardItem].votes': newVotes, 'columns.$.cards.$[card].votes': [], 'columns.$.cards.$[card].items.$[cardItem].comments': newComments, - 'columns.$.cards.$[card].comments': [] + 'columns.$.cards.$[card].comments': [], + 'columns.$.cards.$[card].text': text, + 'columns.$.cards.$[card].createdBy': createdBy } }, { @@ -138,7 +130,6 @@ export default class DeleteCardServiceImpl implements DeleteCardService { new: true } ) - .session(session) .lean() .exec(); if (!board) throw Error(UPDATE_FAILED); @@ -149,15 +140,17 @@ export default class DeleteCardServiceImpl implements DeleteCardService { session.startTransaction(); try { await this.deletedVotesFromCardItem(boardId, cardItemId); - const card = await this.getCardService.getCardFromBoard(boardId, cardId); const cardItems = card?.items.filter((item) => item._id.toString() !== cardItemId); - if (card && cardItems?.length === 1 && (card.votes.length > 0 || card.comments.length > 0)) { + if ( + card && + cardItems?.length === 1 && + (card.votes.length >= 0 || card.comments.length >= 0) + ) { const newVotes = [...card.votes, ...cardItems[0].votes]; const newComments = [...card.comments, ...cardItems[0].comments]; - await this.refactorLastItem(boardId, cardId, newVotes, newComments, cardItems, session); + await this.refactorLastItem(boardId, cardId, newVotes, newComments, cardItems); } - const board = await this.boardModel .findOneAndUpdate( {