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

fix: slack channels #992

Merged
merged 6 commits into from
Feb 2, 2023
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
9 changes: 8 additions & 1 deletion backend/src/modules/boards/dto/update-board.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { PartialType } from '@nestjs/mapped-types';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsOptional } from 'class-validator';
import BoardUser from '../schemas/board.user.schema';
import BoardDto from './board.dto';

export class UpdateBoardDto extends PartialType(BoardDto) {}
export class UpdateBoardDto extends PartialType(BoardDto) {
@ApiPropertyOptional({ type: BoardUser, isArray: true })
@IsOptional()
responsible?: BoardUser;
}
67 changes: 51 additions & 16 deletions backend/src/modules/boards/services/update.board.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
}

async update(boardId: string, boardData: UpdateBoardDto) {
const { responsible } = boardData;

const board = await this.boardModel.findById(boardId).exec();

if (!board) {
Expand All @@ -85,35 +87,62 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
const { isSubBoard } = board;

const currentResponsible = await this.getBoardResponsibleInfo(boardId);
const newResponsible: ResponsibleType = { id: currentResponsible?.id, email: '' };
const newResponsible: ResponsibleType = {
id: (responsible?.user as User)._id,
email: (responsible?.user as User).email
};

/**
* Validate if:
* - have users on request
* - is a sub-board
* - and the logged user isn't the current responsible
*/
if (isSubBoard && boardData.users) {
const boardUserFound = boardData.users?.find(
(userFound) => userFound.role === BoardRoles.RESPONSIBLE
).user as unknown as User;
if (boardData.users && currentResponsible.id !== newResponsible.id) {
if (isSubBoard) {
const promises = boardData.users
.filter((boardUser) =>
[getIdFromObjectId(String(currentResponsible?.id)), newResponsible.id].includes(
(boardUser.user as unknown as User)._id
)
)
.map((boardUser) => {
const typedBoardUser = boardUser.user as unknown as User;

return this.boardUserModel
.findOneAndUpdate(
{
user: typedBoardUser._id,
board: boardId
},
{
role: boardUser.role
}
)
.exec();
});
await Promise.all(promises);
}

const mainBoardId = await this.boardModel
.findOne({ dividedBoards: { $in: boardId } })
.select('_id')
.exec();

newResponsible.email = boardUserFound.email;
newResponsible.id = boardUserFound._id;
const promises = boardData.users
.filter((boardUser) =>
[getIdFromObjectId(String(currentResponsible?.id)), newResponsible.id].includes(
(boardUser.user as unknown as User)._id
)
)
.map(async (boardUser) => {
.map((boardUser) => {
const typedBoardUser = boardUser.user as unknown as User;

return this.boardUserModel
.findOneAndUpdate(
{
user: typedBoardUser._id,
board: boardId
board: mainBoardId
},
{
role: boardUser.role
Expand All @@ -124,6 +153,17 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
await Promise.all(promises);
}

/**
* Updates the board's settings fields
*
* */

board.title = boardData.title;
board.maxVotes = boardData.maxVotes;
board.hideCards = boardData.hideCards;
board.addCards = boardData.addCards;
board.hideVotes = boardData.hideVotes;

/**
* Only can change the maxVotes if:
* - new maxVotes not empty
Expand All @@ -148,12 +188,7 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
_id: boardId
},
{
maxVotes: boardData.maxVotes,
hideCards: boardData.hideCards,
addCards: boardData.addCards,
hideVotes: boardData.hideVotes,
title: boardData.title,
users: boardData.users
...board
},
{
new: true
Expand All @@ -164,7 +199,7 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa

if (
updatedBoard &&
String(currentResponsible?.id) !== newResponsible.id &&
currentResponsible.id !== newResponsible.id &&
board.slackChannelId &&
updatedBoard.slackEnable &&
updatedBoard.isSubBoard
Expand Down
4 changes: 2 additions & 2 deletions backend/src/modules/boards/utils/clean-board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const replaceCard = (
hideVotes: boolean
): Card | CardItem => {
let { text, comments, votes, createdBy } = input;
const { anonymous } = input;
const { anonymous, createdByTeam } = input;
const createdByAsUserDocument = createdBy as UserDocument;

if (hideCards && String(createdByAsUserDocument._id) !== String(userId)) {
Expand All @@ -64,7 +64,7 @@ export const replaceCard = (
comments = replaceComments(hideCards, createdByAsUserDocument, input.comments, userId);
}

if (anonymous) {
if (anonymous || createdByTeam) {
createdBy = replaceUser(createdByAsUserDocument, userId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class SlackCommunicationGateAdapter implements CommunicationGateAdapterIn
return { ok };
} catch (error) {
if (typeof error.data?.ok === 'boolean' && !error.data?.ok) {
this.logger.warn(error);
this.logger.error(error);

if (error.data.error === 'already_in_channel') {
return { ok: true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,21 @@ export class SlackCommunicationApplication implements CommunicationApplicationIn

errors.forEach((i) => this.logger.warn(i));

success.forEach(({ id: channelId, name: channelName }) => {
const board = teams.find((i) =>
channelName.includes(
`${i.normalName}${i.for === BoardRoles.RESPONSIBLE ? '-responsibles' : ''}`
)
);
return success.flatMap(({ id: channelId, name: channelName }) => {
const team = teams.find((i) => {
return String(channelName).includes(
`${i.normalName}${
i.for === BoardRoles.RESPONSIBLE ? '-responsibles' : ''
}-${month}-${year}`
);
});

if (board) {
board.channelId = channelId;
if (team) {
team.channelId = channelId;
}
});

return teams;
return team ?? [];
});
}

private async addSlackIdOnTeams(teams: TeamDto[]): Promise<TeamDto[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class CreateSchedulesService implements CreateSchedulesServiceInterface {

this.schedulerRegistry.addCronJob(String(boardId), job);
job.start();
this.logger.log(`Job created for ${boardId}`);
} catch (e) {
this.logger.error(e);
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/Board/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ const BoardSettings = ({
maxVotes,
columns: isRegularBoard ? updatedColumns : data.columns,
socketId,
responsible: data.users?.find((user) => user.role === BoardUserRoles.RESPONSIBLE),
},
{
onSuccess: () => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types/board/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ export type UpdateBoardType = {
isPublic: boolean;
columns?: (ColumnType | CreateColumn)[];
addCards: boolean;
responsible?: BoardUser;
};