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

[BUG] Deleting merged cards #442

Merged
merged 3 commits into from
Sep 14, 2022
Merged
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
33 changes: 13 additions & 20 deletions backend/src/modules/cards/services/delete.card.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand All @@ -47,33 +43,26 @@ 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<LeanDocument<BoardUserDocument> | null>[] = [];
getCard.items.forEach(async (current) => {
current.votes.forEach(async (currentVote) => {
promises.push(this.deleteVoteService.decrementVoteUser(boardId, currentVote));
});
});

const results = await Promise.all(promises);

if (results.some((i) => i === null)) {
throw Error(UPDATE_FAILED);
}
Expand Down Expand Up @@ -116,9 +105,10 @@ export default class DeleteCardServiceImpl implements DeleteCardService {
cardId: string,
newVotes: (LeanDocument<User> | LeanDocument<ObjectId>)[],
newComments: LeanDocument<CommentDocument>[],
cardItems: LeanDocument<CardItemDocument>[],
session: ClientSession
cardItems: LeanDocument<CardItemDocument>[]
) {
const [{ text, createdBy }] = cardItems;

const board = await this.boardModel
.findOneAndUpdate(
{
Expand All @@ -130,15 +120,16 @@ 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
}
},
{
arrayFilters: [{ 'card._id': cardId }, { 'cardItem._id': cardItems[0]._id }],
new: true
}
)
.session(session)
.lean()
.exec();
if (!board) throw Error(UPDATE_FAILED);
Expand All @@ -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(
{
Expand Down