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

[FEATURE]: Hide id from users when they vote on the cards #429

Merged
merged 2 commits into from
Sep 5, 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
46 changes: 46 additions & 0 deletions backend/src/libs/utils/boardVotesIdHidden.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ObjectId } from 'mongodb';
import { LeanDocument } from 'mongoose';

import Board, { BoardDocument } from 'modules/boards/schemas/board.schema';

import { hideText } from './hideText';

export const boardVotesIdHidden = (
input:
| LeanDocument<BoardDocument>
| LeanDocument<
Board & {
_id: ObjectId;
}
>,
userId: string
):
| LeanDocument<BoardDocument>
| LeanDocument<
Board & {
_id: ObjectId;
}
> => {
const columns = input.columns.map((column) => {
const cards = column.cards.map((card) => {
const items = card.items.map((item) => {
const votes = item.votes.map((vote) => {
if (String(userId) !== String(vote)) {
return hideText(String(vote));
}
return vote;
});
return { ...item, votes };
});
const votes = card.votes.map((vote) => {
if (String(userId) !== String(vote)) {
return hideText(String(vote));
}
return vote;
});
return { ...card, items, votes };
});
return { ...column, cards };
});
return { ...input, columns };
};
14 changes: 5 additions & 9 deletions backend/src/modules/boards/services/get.board.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ObjectId } from 'mongodb';
import { LeanDocument, Model } from 'mongoose';

import { BOARDS_NOT_FOUND } from 'libs/exceptions/messages';
import { boardVotesIdHidden } from 'libs/utils/boardVotesIdHidden';
import { hideText } from 'libs/utils/hideText';
import { CardItemDocument } from 'modules/cards/schemas/card.item.schema';
import { CardDocument } from 'modules/cards/schemas/card.schema';
Expand Down Expand Up @@ -203,7 +204,7 @@ export default class GetBoardServiceImpl implements GetBoardServiceInterface {
* @param userId Current Logged User
* @returns Array of Votes (filtered)
*/
private replaceVotes(input: LeanDocument<CardDocument | CardItemDocument>, userId: string) {
private filterVotes(input: LeanDocument<CardDocument | CardItemDocument>, userId: string) {
return (input.votes as UserDocument[]).filter((vote) => String(vote._id) === String(userId));
}

Expand Down Expand Up @@ -273,7 +274,7 @@ export default class GetBoardServiceImpl implements GetBoardServiceInterface {
}

if (hideVotes) {
votes = this.replaceVotes(input, userId);
votes = this.filterVotes(input, userId);
}

return {
Expand Down Expand Up @@ -305,28 +306,23 @@ export default class GetBoardServiceImpl implements GetBoardServiceInterface {
> {
const { hideCards = false, hideVotes = false, columns: boardColumns } = input;
// Columns
const columns = boardColumns.map((column) => {
input.columns = boardColumns.map((column) => {
const cards = column.cards.map((card) => {
const items = card.items.map((item) => {
return this.replaceCard(item, userId, hideCards, hideVotes);
});

return {
...this.replaceCard(card, userId, hideCards, hideVotes),
items
};
});

return {
...column,
cards
};
});

return {
...input,
columns
};
return boardVotesIdHidden(input, userId) as LeanDocument<Board & { _id: ObjectId }>;
}

async countBoards(userId: string) {
Expand Down
9 changes: 5 additions & 4 deletions backend/src/modules/votes/controller/votes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { SocketIdDto } from 'libs/swagger/dto/socket-id.swagger';
import { BadRequestResponse } from 'libs/swagger/errors/bad-request.swagger';
import { InternalServerErrorResponse } from 'libs/swagger/errors/internal-server-error.swagger';
import { UnauthorizedResponse } from 'libs/swagger/errors/unauthorized.swagger';
import { boardVotesIdHidden } from 'libs/utils/boardVotesIdHidden';
import SocketGateway from 'modules/socket/gateway/socket.gateway';

import BoardDto from '../../boards/dto/board.dto';
Expand Down Expand Up @@ -88,7 +89,7 @@ export default class VotesController {
if (!board) throw new BadRequestException(INSERT_FAILED);
this.socketService.sendUpdatedBoard(boardId, socketId);

return board;
return boardVotesIdHidden(board, request.user._id);
}

@ApiOperation({ summary: 'Add a vote to a specific card' })
Expand Down Expand Up @@ -127,7 +128,7 @@ export default class VotesController {
if (!board) throw new BadRequestException(INSERT_FAILED);
this.socketService.sendUpdatedBoard(boardId, socketId);

return board;
return boardVotesIdHidden(board, request.user._id);
}

@ApiOperation({ summary: 'Remove a vote from a specific card item' })
Expand Down Expand Up @@ -172,7 +173,7 @@ export default class VotesController {
if (!board) throw new BadRequestException(DELETE_FAILED);
this.socketService.sendUpdatedBoard(boardId, socketId);

return board;
return boardVotesIdHidden(board, request.user._id);
}

@ApiOperation({ summary: 'Remove a vote from a specific card' })
Expand Down Expand Up @@ -215,6 +216,6 @@ export default class VotesController {
if (!board) throw new BadRequestException(DELETE_FAILED);
this.socketService.sendUpdatedBoard(boardId, socketId);

return board;
return boardVotesIdHidden(board, request.user._id);
}
}
14 changes: 4 additions & 10 deletions backend/src/test/boards/get.board.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,10 @@ describe('GetBoardServiceImpl', () => {
{
items: [
{
votes: [
{ _id: 'any_id_1' },
{ _id: 'any_id_1' },
{ _id: 'any_id_2' },
{ _id: 'any_id_2' },
{ _id: 'any_id_3' }
]
votes: ['any_id_1', 'any_id_2']
}
],
votes: [{ _id: 'any_id_1' }, { _id: 'any_id_2' }]
votes: ['any_id_1']
}
]
},
Expand Down Expand Up @@ -98,10 +92,10 @@ describe('GetBoardServiceImpl', () => {
{
items: [
{
votes: [{ _id: 'any_id_1' }, { _id: 'any_id_1' }]
votes: []
}
],
votes: [{ _id: 'any_id_1' }]
votes: []
}
]
},
Expand Down