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: board settings, update packages #713

Merged
merged 3 commits into from
Dec 19, 2022
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
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