Skip to content

Commit 6bca42a

Browse files
Cátia AntunesNuno Caseiro
andauthored
fix: slack channels (#992)
Co-authored-by: Nuno Caseiro <[email protected]>
1 parent e3b1212 commit 6bca42a

File tree

8 files changed

+77
-30
lines changed

8 files changed

+77
-30
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import { PartialType } from '@nestjs/mapped-types';
2+
import { ApiPropertyOptional } from '@nestjs/swagger';
3+
import { IsOptional } from 'class-validator';
4+
import BoardUser from '../schemas/board.user.schema';
25
import BoardDto from './board.dto';
36

4-
export class UpdateBoardDto extends PartialType(BoardDto) {}
7+
export class UpdateBoardDto extends PartialType(BoardDto) {
8+
@ApiPropertyOptional({ type: BoardUser, isArray: true })
9+
@IsOptional()
10+
responsible?: BoardUser;
11+
}

backend/src/modules/boards/services/update.board.service.ts

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
7575
}
7676

7777
async update(boardId: string, boardData: UpdateBoardDto) {
78+
const { responsible } = boardData;
79+
7880
const board = await this.boardModel.findById(boardId).exec();
7981

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

8789
const currentResponsible = await this.getBoardResponsibleInfo(boardId);
88-
const newResponsible: ResponsibleType = { id: currentResponsible?.id, email: '' };
90+
const newResponsible: ResponsibleType = {
91+
id: (responsible?.user as User)._id,
92+
email: (responsible?.user as User).email
93+
};
8994

9095
/**
9196
* Validate if:
9297
* - have users on request
9398
* - is a sub-board
9499
* - and the logged user isn't the current responsible
95100
*/
96-
if (isSubBoard && boardData.users) {
97-
const boardUserFound = boardData.users?.find(
98-
(userFound) => userFound.role === BoardRoles.RESPONSIBLE
99-
).user as unknown as User;
101+
if (boardData.users && currentResponsible.id !== newResponsible.id) {
102+
if (isSubBoard) {
103+
const promises = boardData.users
104+
.filter((boardUser) =>
105+
[getIdFromObjectId(String(currentResponsible?.id)), newResponsible.id].includes(
106+
(boardUser.user as unknown as User)._id
107+
)
108+
)
109+
.map((boardUser) => {
110+
const typedBoardUser = boardUser.user as unknown as User;
111+
112+
return this.boardUserModel
113+
.findOneAndUpdate(
114+
{
115+
user: typedBoardUser._id,
116+
board: boardId
117+
},
118+
{
119+
role: boardUser.role
120+
}
121+
)
122+
.exec();
123+
});
124+
await Promise.all(promises);
125+
}
126+
127+
const mainBoardId = await this.boardModel
128+
.findOne({ dividedBoards: { $in: boardId } })
129+
.select('_id')
130+
.exec();
100131

101-
newResponsible.email = boardUserFound.email;
102-
newResponsible.id = boardUserFound._id;
103132
const promises = boardData.users
104133
.filter((boardUser) =>
105134
[getIdFromObjectId(String(currentResponsible?.id)), newResponsible.id].includes(
106135
(boardUser.user as unknown as User)._id
107136
)
108137
)
109-
.map(async (boardUser) => {
138+
.map((boardUser) => {
110139
const typedBoardUser = boardUser.user as unknown as User;
111140

112141
return this.boardUserModel
113142
.findOneAndUpdate(
114143
{
115144
user: typedBoardUser._id,
116-
board: boardId
145+
board: mainBoardId
117146
},
118147
{
119148
role: boardUser.role
@@ -124,6 +153,17 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
124153
await Promise.all(promises);
125154
}
126155

156+
/**
157+
* Updates the board's settings fields
158+
*
159+
* */
160+
161+
board.title = boardData.title;
162+
board.maxVotes = boardData.maxVotes;
163+
board.hideCards = boardData.hideCards;
164+
board.addCards = boardData.addCards;
165+
board.hideVotes = boardData.hideVotes;
166+
127167
/**
128168
* Only can change the maxVotes if:
129169
* - new maxVotes not empty
@@ -148,12 +188,7 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
148188
_id: boardId
149189
},
150190
{
151-
maxVotes: boardData.maxVotes,
152-
hideCards: boardData.hideCards,
153-
addCards: boardData.addCards,
154-
hideVotes: boardData.hideVotes,
155-
title: boardData.title,
156-
users: boardData.users
191+
...board
157192
},
158193
{
159194
new: true
@@ -164,7 +199,7 @@ export default class UpdateBoardServiceImpl implements UpdateBoardServiceInterfa
164199

165200
if (
166201
updatedBoard &&
167-
String(currentResponsible?.id) !== newResponsible.id &&
202+
currentResponsible.id !== newResponsible.id &&
168203
board.slackChannelId &&
169204
updatedBoard.slackEnable &&
170205
updatedBoard.isSubBoard

backend/src/modules/boards/utils/clean-board.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const replaceCard = (
5252
hideVotes: boolean
5353
): Card | CardItem => {
5454
let { text, comments, votes, createdBy } = input;
55-
const { anonymous } = input;
55+
const { anonymous, createdByTeam } = input;
5656
const createdByAsUserDocument = createdBy as UserDocument;
5757

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

67-
if (anonymous) {
67+
if (anonymous || createdByTeam) {
6868
createdBy = replaceUser(createdByAsUserDocument, userId);
6969
}
7070

backend/src/modules/communication/adapters/slack-communication-gate.adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class SlackCommunicationGateAdapter implements CommunicationGateAdapterIn
100100
return { ok };
101101
} catch (error) {
102102
if (typeof error.data?.ok === 'boolean' && !error.data?.ok) {
103-
this.logger.warn(error);
103+
this.logger.error(error);
104104

105105
if (error.data.error === 'already_in_channel') {
106106
return { ok: true };

backend/src/modules/communication/applications/slack-communication.application.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,21 @@ export class SlackCommunicationApplication implements CommunicationApplicationIn
131131

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

134-
success.forEach(({ id: channelId, name: channelName }) => {
135-
const board = teams.find((i) =>
136-
channelName.includes(
137-
`${i.normalName}${i.for === BoardRoles.RESPONSIBLE ? '-responsibles' : ''}`
138-
)
139-
);
134+
return success.flatMap(({ id: channelId, name: channelName }) => {
135+
const team = teams.find((i) => {
136+
return String(channelName).includes(
137+
`${i.normalName}${
138+
i.for === BoardRoles.RESPONSIBLE ? '-responsibles' : ''
139+
}-${month}-${year}`
140+
);
141+
});
140142

141-
if (board) {
142-
board.channelId = channelId;
143+
if (team) {
144+
team.channelId = channelId;
143145
}
144-
});
145146

146-
return teams;
147+
return team ?? [];
148+
});
147149
}
148150

149151
private async addSlackIdOnTeams(teams: TeamDto[]): Promise<TeamDto[]> {

backend/src/modules/schedules/services/create.schedules.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class CreateSchedulesService implements CreateSchedulesServiceInterface {
9999

100100
this.schedulerRegistry.addCronJob(String(boardId), job);
101101
job.start();
102+
this.logger.log(`Job created for ${boardId}`);
102103
} catch (e) {
103104
this.logger.error(e);
104105
}

frontend/src/components/Board/Settings/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ const BoardSettings = ({
266266
maxVotes,
267267
columns: isRegularBoard ? updatedColumns : data.columns,
268268
socketId,
269+
responsible: data.users?.find((user) => user.role === BoardUserRoles.RESPONSIBLE),
269270
},
270271
{
271272
onSuccess: () => {

frontend/src/types/board/board.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ export type UpdateBoardType = {
8787
isPublic: boolean;
8888
columns?: (ColumnType | CreateColumn)[];
8989
addCards: boolean;
90+
responsible?: BoardUser;
9091
};

0 commit comments

Comments
 (0)