Skip to content

Commit 1f72ac1

Browse files
authored
Initializing resend on the constructor of the RPC service (#221)
1 parent 806ac58 commit 1f72ac1

1 file changed

Lines changed: 45 additions & 37 deletions

File tree

workers/transactional_email_service/index.tsx

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@ import { createLogger } from "~/logging";
88

99
import { PurchaseOrderSuccessful } from "../../emails/templates/tickets/purchase-order-successful";
1010

11-
export default class EmailService extends WorkerEntrypoint<{
12-
RESEND_API_KEY: string;
13-
}> {
11+
type ENV = {
12+
RESEND_API_KEY: string | undefined;
13+
};
14+
export default class EmailService extends WorkerEntrypoint<ENV> {
1415
logger = createLogger("EmailService");
1516

17+
resend: Resend = new Resend("");
18+
constructor(ctx: ExecutionContext, env: ENV) {
19+
super(ctx, env);
20+
21+
if (!env.RESEND_API_KEY) {
22+
throw new Error("RESEND_API_KEY is required");
23+
}
24+
25+
this.resend = new Resend(env.RESEND_API_KEY);
26+
}
27+
1628
fetch() {
1729
return new Response("ok");
1830
}
@@ -46,42 +58,38 @@ export default class EmailService extends WorkerEntrypoint<{
4658
this.logger.info(
4759
`About to send purchase order email for ID: ${purchaseOrderId}`,
4860
);
49-
await sendTransactionalHTMLEmail(
50-
new Resend(this.env.RESEND_API_KEY),
51-
this.logger,
52-
{
53-
htmlContent: render(
54-
<PurchaseOrderSuccessful
55-
purchaseOrderId={purchaseOrderId}
56-
community={{
57-
name: communityInfo.name,
58-
// communityURL: "https://cdn.com",
59-
logoURL: communityInfo.logoImageSanityRef,
60-
}}
61-
eventName={eventInfo.name}
62-
place={{
63-
name: eventInfo.addressDescriptiveName,
64-
address: eventInfo.address,
65-
}}
66-
date={{
67-
start: eventInfo.startDateTime,
68-
end: eventInfo.endDateTime,
69-
}}
70-
/>,
71-
),
72-
to: [
73-
{
74-
name: purchaseOrder.user.name ?? purchaseOrder.user.username,
75-
email: purchaseOrder.user.email,
76-
},
77-
],
78-
from: {
79-
name: "CommunityOS",
80-
email: "contacto@communityos.io",
61+
await sendTransactionalHTMLEmail(this.resend, this.logger, {
62+
htmlContent: render(
63+
<PurchaseOrderSuccessful
64+
purchaseOrderId={purchaseOrderId}
65+
community={{
66+
name: communityInfo.name,
67+
// communityURL: "https://cdn.com",
68+
logoURL: communityInfo.logoImageSanityRef,
69+
}}
70+
eventName={eventInfo.name}
71+
place={{
72+
name: eventInfo.addressDescriptiveName,
73+
address: eventInfo.address,
74+
}}
75+
date={{
76+
start: eventInfo.startDateTime,
77+
end: eventInfo.endDateTime,
78+
}}
79+
/>,
80+
),
81+
to: [
82+
{
83+
name: purchaseOrder.user.name ?? purchaseOrder.user.username,
84+
email: purchaseOrder.user.email,
8185
},
82-
subject: "Tus tickets están listos 🎉",
86+
],
87+
from: {
88+
name: "CommunityOS",
89+
email: "contacto@communityos.io",
8390
},
84-
);
91+
subject: "Tus tickets están listos 🎉",
92+
});
8593
this.logger.info(`Sent purchase order email for ID ${purchaseOrderId}`);
8694
}
8795
}

0 commit comments

Comments
 (0)