Skip to content

Commit 6380921

Browse files
fix: don't fail request if there is an error sending email
1 parent cdfa04a commit 6380921

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/cohorts/cohorts.service.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BadRequestException, Injectable } from '@nestjs/common';
1+
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
33
import { Cohort } from '@/entities/cohort.entity';
44
import { Repository } from 'typeorm';
@@ -26,10 +26,11 @@ import { CohortWaitlist } from '@/entities/cohort-waitlist.entity';
2626
import { APITask } from '@/entities/api-task.entity';
2727
import { TaskType } from '@/task-processor/task.enums';
2828
import { MailService } from '@/mail/mail.service';
29-
import { formatDate } from '@/utils/data.utils';
3029

3130
@Injectable()
3231
export class CohortsService {
32+
private readonly logger = new Logger(CohortsService.name);
33+
3334
private readonly masteringBitcoinDiscordRoleId: string;
3435
private readonly learningBitcoinFromCommandLineDiscordRoleId: string;
3536
private readonly programmingBitcoinDiscordRoleId: string;
@@ -444,14 +445,21 @@ export class CohortsService {
444445
user.name || user.discordGlobalName || user.discordUserName;
445446
const classroomUrl = cohort.weeks[0]?.classroomUrl || undefined;
446447

447-
await this.mailService.sendCohortJoiningConfirmationEmail(
448-
user.email,
449-
userName,
450-
cohort.type,
451-
cohort.startDate,
452-
cohort.endDate,
453-
classroomUrl,
454-
);
448+
try {
449+
await this.mailService.sendCohortJoiningConfirmationEmail(
450+
user.email,
451+
userName,
452+
cohort.type,
453+
cohort.startDate,
454+
cohort.endDate,
455+
classroomUrl,
456+
);
457+
} catch (error) {
458+
this.logger.error(
459+
`Failed to send cohort joining confirmation email to user ${user.id}: ${error.message}`,
460+
error.stack,
461+
);
462+
}
455463
}
456464

457465
async joinCohortWaitlist(
@@ -482,12 +490,19 @@ export class CohortsService {
482490

483491
await this.cohortWaitlistRepository.save(waitlistEntry);
484492

485-
// Send welcome email to the user
486-
await this.mailService.sendWelcomeToWaitlistEmail(
487-
user.email,
488-
user.name || user.discordGlobalName || user.discordUserName,
489-
body.type,
490-
);
493+
try {
494+
// Send welcome email to the user
495+
await this.mailService.sendWelcomeToWaitlistEmail(
496+
user.email,
497+
user.name || user.discordGlobalName || user.discordUserName,
498+
body.type,
499+
);
500+
} catch (error) {
501+
this.logger.error(
502+
`Failed to send welcome to waitlist email to user ${user.id}: ${error.message}`,
503+
error.stack,
504+
);
505+
}
491506
}
492507

493508
async getUserWaitlist(user: User): Promise<UserCohortWaitlistResponseDto> {

0 commit comments

Comments
 (0)