forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.module.ts
More file actions
71 lines (65 loc) · 1.89 KB
/
Copy pathemail.module.ts
File metadata and controls
71 lines (65 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Module, Global, OnModuleInit, Logger } from "@nestjs/common";
import { BullModule } from "@nestjs/bull";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConfigModule } from "@nestjs/config";
import * as fs from "fs";
import * as path from "path";
import { EmailService } from "./email.service";
import { EmailProcessor } from "./email.processor";
import { EmailController } from "./email.controller";
import { User } from "../entities/user.entity";
export const EMAIL_TEMPLATE_FILES = [
"completed",
"confirmation",
"invitation",
"reminder",
] as const;
export const EMAIL_QUEUE_JOB_OPTIONS = {
attempts: 3,
backoff: {
type: "exponential" as const,
delay: 5000,
},
removeOnComplete: true,
removeOnFail: false,
};
@Global()
@Module({
imports: [
TypeOrmModule.forFeature([User]),
BullModule.registerQueue({
name: "email_queue",
defaultJobOptions: EMAIL_QUEUE_JOB_OPTIONS,
}),
BullModule.registerQueue({
name: "email_queue_dead_letter",
defaultJobOptions: {
attempts: 1,
removeOnComplete: false,
removeOnFail: false,
},
}),
],
controllers: [EmailController],
providers: [EmailService, EmailProcessor],
exports: [EmailService],
})
export class EmailModule implements OnModuleInit {
private readonly logger = new Logger(EmailModule.name);
onModuleInit(): void {
const templatesDir = path.join(__dirname, "templates");
for (const templateName of EMAIL_TEMPLATE_FILES) {
const templatePath = path.join(templatesDir, `${templateName}.hbs`);
try {
fs.accessSync(templatePath, fs.constants.R_OK);
} catch {
throw new Error(
`Email template missing at startup: ${templateName}.hbs (expected at ${templatePath})`,
);
}
}
this.logger.log(
`Validated ${EMAIL_TEMPLATE_FILES.length} email templates in ${templatesDir}`,
);
}
}