Skip to content

Commit

Permalink
refactor: board settings, update packages (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro authored Dec 19, 2022
1 parent 21c9128 commit f5aef78
Show file tree
Hide file tree
Showing 17 changed files with 10,946 additions and 8,994 deletions.
126 changes: 53 additions & 73 deletions backend/src/modules/votes/services/create.vote.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,35 @@ export default class CreateVoteServiceImpl implements CreateVoteService {
const canUserVote = await this.canUserVote(boardId, userId);

if (canUserVote) {
const dbSession = await this.boardModel.db.startSession();
dbSession.startTransaction();
try {
await this.incrementVoteUser(boardId, userId);
const board = await this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards.items._id': cardItemId
await this.incrementVoteUser(boardId, userId);
const board = await this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards.items._id': cardItemId
},
{
$push: {
'columns.$.cards.$[c].items.$[i].votes': userId
},
{
$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();

if (!board) throw Error(UPDATE_FAILED);
await dbSession.commitTransaction();

return board;
} catch (error) {
await dbSession.abortTransaction();
} finally {
await dbSession.endSession();
}
$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();

if (!board) throw Error(UPDATE_FAILED);

return board;
}
throw new BadRequestException('Error adding a vote');
}
Expand All @@ -104,43 +95,32 @@ export default class CreateVoteServiceImpl implements CreateVoteService {
const canUserVote = await this.canUserVote(boardId, userId);

if (canUserVote) {
const dbSession = await this.boardModel.db.startSession();
dbSession.startTransaction();
try {
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
await this.incrementVoteUser(boardId, userId);
const board = await this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards._id': cardId
},
{
$push: {
'columns.$.cards.$[c].votes': userId
}
)
.populate({
path: 'users',
select: 'user role votesCount -board',
populate: { path: 'user', select: 'firstName lastName _id' }
})
.lean()
.exec();

await dbSession.commitTransaction();

return board;
} catch (error) {
await dbSession.abortTransaction();
throw error;
} finally {
await dbSession.endSession();
}
},
{
arrayFilters: [{ 'c._id': cardId }],
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');
}
Expand Down
121 changes: 50 additions & 71 deletions backend/src/modules/votes/services/delete.vote.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,30 @@ export default class DeleteVoteServiceImpl implements DeleteVoteService {

votes.splice(voteIndex, 1);

const dbSession = await this.boardModel.db.startSession();
dbSession.startTransaction();
try {
await this.decrementVoteUser(boardId, userId);
const board = await this.boardModel.findOneAndUpdate(
{
_id: boardId,
'columns.cards.items._id': cardItemId
},
{
$set: {
'columns.$.cards.$[c].items.$[i].votes': votes
}
},
{
arrayFilters: [{ 'c._id': cardId }, { 'i._id': cardItemId }],
new: true,
session: dbSession
await this.decrementVoteUser(boardId, userId);
const board = await this.boardModel.findOneAndUpdate(
{
_id: boardId,
'columns.cards.items._id': cardItemId
},
{
$set: {
'columns.$.cards.$[c].items.$[i].votes': votes
}
);

if (!board) throw Error(UPDATE_FAILED);
await dbSession.commitTransaction();

return await board.populate({
path: 'users',
select: 'user role votesCount -board',
populate: { path: 'user', select: 'firstName lastName _id' }
});
} catch (error) {
await dbSession.abortTransaction();
throw error;
} finally {
await dbSession.endSession();
}
},
{
arrayFilters: [{ 'c._id': cardId }, { 'i._id': cardItemId }],
new: true
}
);

if (!board) throw Error(UPDATE_FAILED);

return await board.populate({
path: 'users',
select: 'user role votesCount -board',
populate: { path: 'user', select: 'firstName lastName _id' }
});
}
throw new BadRequestException('Error removing a vote');
}
Expand Down Expand Up @@ -143,44 +132,34 @@ export default class DeleteVoteServiceImpl implements DeleteVoteService {
if (voteIndex === -1) return null;
newVotes.splice(voteIndex, 1);

const dbSession = await this.boardModel.db.startSession();
dbSession.startTransaction();
try {
await this.decrementVoteUser(boardId, userId);
const board = await this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards._id': cardId
},
{
$set: {
'columns.$.cards.$[c].votes': newVotes
}
},
{
arrayFilters: [{ 'c._id': cardId }],
new: true
await this.decrementVoteUser(boardId, userId);
const board = await this.boardModel
.findOneAndUpdate(
{
_id: boardId,
'columns.cards._id': cardId
},
{
$set: {
'columns.$.cards.$[c].votes': newVotes
}
)
.populate({
path: 'users',
select: 'user role votesCount -board',
populate: { path: 'user', select: 'firstName lastName _id' }
})
.lean()
.exec();

if (!board) throw Error(UPDATE_FAILED);
await dbSession.commitTransaction();

return board;
} catch (error) {
await dbSession.abortTransaction();
throw error;
} finally {
await dbSession.endSession();
}
},
{
arrayFilters: [{ 'c._id': cardId }],
new: true
}
)
.populate({
path: 'users',
select: 'user role votesCount -board',
populate: { path: 'user', select: 'firstName lastName _id' }
})
.lean()
.exec();

if (!board) throw Error(UPDATE_FAILED);

return board;
}
throw new BadRequestException('Error removing a vote');
}
Expand Down
Loading

0 comments on commit f5aef78

Please sign in to comment.