Skip to content

Commit 9acfef4

Browse files
authored
feat(mentorship): limit allowed open requests (#238)
1 parent 92315c4 commit 9acfef4

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

src/modules/mentorships/__tests__/mentorships.controller.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,24 @@ describe('modules/mentorships/MentorshipsController', () => {
125125
).rejects.toThrow(BadRequestException);
126126
});
127127

128+
it('should return a 400 error if the user exceeded the allowed open requests', async () => {
129+
mentorshipsService.getOpenRequestsCount = jest.fn(() =>
130+
Promise.resolve(5),
131+
);
132+
133+
await expect(
134+
mentorshipsController.applyForMentorship(
135+
<Request>request,
136+
mentorId,
137+
mentorship,
138+
),
139+
).rejects.toThrow(BadRequestException);
140+
});
141+
128142
it('should return a successful response', async () => {
143+
mentorshipsService.getOpenRequestsCount = jest.fn(() =>
144+
Promise.resolve(3),
145+
);
129146
const data = await mentorshipsController.applyForMentorship(
130147
<Request>request,
131148
mentorId,

src/modules/mentorships/mentorships.controller.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import { FindOneParams } from '../common/dto/findOneParams.dto';
3838
import { MentorshipUpdatePayload } from './dto/mentorshipUpdatePayload.dto';
3939
import { mentorshipsToDtos } from './mentorshipsToDto';
4040

41+
const ALLOWED_OPEN_MENTORSHIPS = 5;
42+
4143
@ApiUseTags('/mentorships')
4244
@Controller('mentorships')
4345
export class MentorshipsController {
@@ -85,6 +87,15 @@ export class MentorshipsController {
8587
throw new BadRequestException('A mentorship request already exists');
8688
}
8789

90+
const openMentorships = await this.mentorshipsService.getOpenRequestsCount(
91+
current._id,
92+
);
93+
if (openMentorships >= ALLOWED_OPEN_MENTORSHIPS) {
94+
throw new BadRequestException(
95+
'You reached the maximum of open requests. Please wait a little longer for responses or cancel some of the requests',
96+
);
97+
}
98+
8899
await this.mentorshipsService.createMentorship({
89100
mentor: mentor._id,
90101
mentee: current._id,
@@ -239,7 +250,7 @@ export class MentorshipsController {
239250
? `https://coding-coach.slack.com/team/${slack.id}`
240251
: `mailto:${currentUser.email}`;
241252

242-
const openRequests = await this.mentorshipsService.getOpenRequests(
253+
const openRequests = await this.mentorshipsService.getOpenRequestsCount(
243254
mentee._id,
244255
);
245256

src/modules/mentorships/mentorships.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class MentorshipsService {
101101
* Count open mentorship requests of a user
102102
* @param userId
103103
*/
104-
getOpenRequests(userId: string): Promise<number> {
104+
getOpenRequestsCount(userId: string): Promise<number> {
105105
if (isObjectId(userId)) {
106106
return this.mentorshipModel
107107
.countDocuments({

test/api/mentorships.e2e-spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,47 @@ describe('Mentorships', () => {
287287
});
288288
});
289289
});
290+
291+
describe('POST /mentorships/:mentorId/apply', () => {
292+
it('returns a status code of 400 if user has reached the maximum of allowed open requests', async () => {
293+
const sendMentorshipRequest = (mentor, mentee) => {
294+
const token = getToken(mentee);
295+
const field = '1'.repeat(31);
296+
297+
return request(server)
298+
.post(`/mentorships/${mentor._id}/apply`)
299+
.set('Authorization', `Bearer ${token}`)
300+
.send({
301+
expectation: field,
302+
message: field,
303+
background: field,
304+
});
305+
};
306+
307+
const roles = [Role.MENTOR];
308+
const [mentee, ...mentors] = await Promise.all([
309+
createUser(),
310+
createUser({ roles }),
311+
createUser({ roles }),
312+
createUser({ roles }),
313+
createUser({ roles }),
314+
createUser({ roles }),
315+
createUser({ roles }),
316+
]);
317+
318+
const [lastMentor] = mentors.splice(-1);
319+
await Promise.all(
320+
mentors.map((mentor) =>
321+
sendMentorshipRequest(mentor, mentee).expect({ success: true }),
322+
),
323+
);
324+
const {
325+
body: { message },
326+
} = await sendMentorshipRequest(lastMentor, mentee).expect(400);
327+
328+
expect(message).toBe(
329+
'You reached the maximum of open requests. Please wait a little longer for responses or cancel some of the requests',
330+
);
331+
});
332+
});
290333
});

0 commit comments

Comments
 (0)