Skip to content

Commit 566a1a5

Browse files
committed
chore: just a test script
1 parent 009bc4c commit 566a1a5

9 files changed

Lines changed: 2910 additions & 0 deletions

File tree

test/PrismaConnector.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PrismaPg } from "@prisma/adapter-pg";
2+
import { PrismaClient } from "generated/prisma/client";
3+
import { config } from "src/config/app.config";
4+
5+
export class PrismaConnector {
6+
private readonly adapter: PrismaPg;
7+
public readonly prisma: PrismaClient;
8+
9+
constructor() {
10+
this.adapter = new PrismaPg({
11+
connectionString: config.db.url,
12+
});
13+
this.prisma = new PrismaClient({ adapter: this.adapter });
14+
}
15+
}

test/mail/MailConnector.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { render } from "@react-email/render";
2+
import * as nodemailer from "nodemailer";
3+
import { config } from "src/config/app.config";
4+
import AnnouncementEmail from "src/core/email/templates/AnnouncementEmail";
5+
6+
export abstract class MailConnector {
7+
private transporter: nodemailer.Transporter;
8+
9+
constructor() {
10+
this.transporter = nodemailer.createTransport({
11+
host: config.email.nodemailer.host,
12+
port: config.email.nodemailer.port,
13+
secure: config.email.nodemailer.secure,
14+
auth: {
15+
user: config.email.nodemailer.user,
16+
pass: config.email.nodemailer.pass,
17+
},
18+
});
19+
}
20+
21+
protected async sendMail(to: string, subject: string, html: string) {
22+
const info = await this.transporter.sendMail({
23+
from: config.email.nodemailer.from,
24+
to: to,
25+
subject: subject,
26+
html: html,
27+
});
28+
29+
console.log(`Email sent to ${to} (MsgID: ${info.messageId})`);
30+
return { success: true, messageId: info.messageId };
31+
}
32+
33+
protected handleError(type: string, email: string, error: any) {
34+
console.error(`Failed to send ${type} email to ${email}`, error.stack);
35+
throw error;
36+
}
37+
}

test/mail/SendMail.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { render } from "@react-email/render";
4+
import AnnouncementEmail from "src/core/email/templates/AnnouncementEmail";
5+
import { PrismaConnector } from "test/PrismaConnector";
6+
import { MailConnector } from "./MailConnector";
7+
8+
class SendMail extends MailConnector {
9+
private db: PrismaConnector;
10+
11+
constructor() {
12+
super();
13+
this.db = new PrismaConnector();
14+
}
15+
async sendAnnouncement(email: string, name: string) {
16+
try {
17+
const html = await render(AnnouncementEmail({ name }));
18+
return await this.sendMail(email, "ประกาศผลการคัดเลือก ComCamp 37", html);
19+
} catch (error) {
20+
this.handleError("announcement", email, error);
21+
}
22+
}
23+
24+
async run() {
25+
interface EmailList {
26+
email: string;
27+
name: string;
28+
}
29+
const loadEmailJson = fs.readFileSync(path.join(__dirname, "./emails.json"), "utf8");
30+
const emails: EmailList[] = JSON.parse(loadEmailJson);
31+
32+
for (const { email, name } of emails) {
33+
try {
34+
await this.sendAnnouncement(email, name);
35+
36+
const readlast = fs.readFileSync(path.join(__dirname, "./log/success.json"), "utf8");
37+
const toJson: string[] = JSON.parse(readlast);
38+
toJson.push(email);
39+
fs.writeFileSync(path.join(__dirname, "./log/success.json"), JSON.stringify(toJson, null, 2), "utf8");
40+
console.log(`Success : ${name} - ${email}`);
41+
} catch (e) {
42+
const readlast = fs.readFileSync(path.join(__dirname, "./log/error.json"), "utf8");
43+
const toJson: string[] = JSON.parse(readlast);
44+
toJson.push(email);
45+
fs.writeFileSync(path.join(__dirname, "./log/error.json"), JSON.stringify(toJson, null, 2), "utf8");
46+
47+
console.log(`Send Email Error: ${name} - ${email}`, e);
48+
}
49+
}
50+
}
51+
}
52+
53+
new SendMail().run();

0 commit comments

Comments
 (0)