-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathmail.controller.js
More file actions
147 lines (124 loc) · 4.35 KB
/
Copy pathmail.controller.js
File metadata and controls
147 lines (124 loc) · 4.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { Resend } = require("resend");
const { z } = require("zod");
const { Project, decrypt, redis, sendMailSchema } = require("@urbackend/common");
const {
getMonthKey,
getEndOfMonthTtlSeconds,
getMonthlyMailLimit,
} = require("../utils/mailLimit");
const DEFAULT_FROM = process.env.EMAIL_FROM || "urBackend <urbackend@apps.bitbros.in>";
const getMailCountKey = (projectId, monthKey) =>
`project:mail:count:${projectId}:${monthKey}`;
const loadProjectMailConfig = async (projectId) => {
return Project.findById(projectId)
.select("+resendApiKey.encrypted +resendApiKey.iv +resendApiKey.tag resendFromEmail")
.lean();
};
const reserveMonthlyMailSlot = async (projectId, limit) => {
if (redis.status !== "ready") {
const err = new Error("Mail service unavailable. Redis is not ready.");
err.statusCode = 503;
throw err;
}
const now = new Date();
const monthKey = getMonthKey(now);
const ttlSeconds = getEndOfMonthTtlSeconds(now);
const key = getMailCountKey(projectId, monthKey);
const luaScript = `
local current = redis.call("INCR", KEYS[1])
if current == 1 then
redis.call("EXPIRE", KEYS[1], ARGV[1])
end
return current
`;
const count = await redis.eval(luaScript, 1, key, ttlSeconds);
if (count > limit) {
await redis.decr(key);
const err = new Error("Monthly mail limit exceeded.");
err.statusCode = 429;
err.limit = limit;
throw err;
}
return { count, key };
};
module.exports.sendMail = async (req, res) => {
let consumedQuotaKey = null;
try {
if (req.keyRole !== "secret") {
return res.status(403).json({
success: false,
data: {},
message: "Forbidden. This action requires a Secret Key (sk_live_...).",
});
}
const { to, subject, html, text } = sendMailSchema.parse(req.body || {});
const projectId = req.project?._id;
if (!projectId) {
return res.status(401).json({ success: false, data: {}, message: "Project context missing." });
}
const project = await loadProjectMailConfig(projectId);
if (!project) {
return res.status(404).json({ success: false, data: {}, message: "Project not found." });
}
const encryptedByokKey =
project.resendApiKey && typeof project.resendApiKey === "object" && Object.keys(project.resendApiKey).length > 0
? project.resendApiKey
: null;
const decryptedByokKey = encryptedByokKey ? decrypt(encryptedByokKey) : null;
const usingByok = typeof decryptedByokKey === "string" && decryptedByokKey.trim().length > 0;
const clientKey = usingByok
? decryptedByokKey.trim()
: process.env.RESEND_API_KEY_2 || process.env.RESEND_API_KEY;
if (!clientKey) {
return res.status(500).json({ success: false, data: {}, message: "Resend API key is not configured." });
}
const limit = getMonthlyMailLimit(req.project);
const { count, key } = await reserveMonthlyMailSlot(projectId, limit);
consumedQuotaKey = key;
const resend = new Resend(clientKey);
let fromAddress = DEFAULT_FROM;
if (usingByok) {
fromAddress = project.resendFromEmail && project.resendFromEmail.trim()
? project.resendFromEmail.trim()
: "onboarding@resend.dev";
}
const payload = {
from: fromAddress,
to,
subject,
};
if (typeof html === "string" && html.trim()) payload.html = html;
if (typeof text === "string" && text.trim()) payload.text = text;
const { data, error } = await resend.emails.send(payload);
if (error) {
throw new Error(error.message || "Failed to send mail.");
}
return res.status(200).json({
success: true,
data: {
id: data?.id || null,
provider: usingByok ? "byok" : "default",
monthlyUsage: count,
monthlyLimit: limit,
},
message: "Mail sent successfully.",
});
} catch (err) {
if (consumedQuotaKey) {
await redis.decr(consumedQuotaKey).catch(() => {});
}
if (err instanceof z.ZodError) {
return res.status(400).json({
success: false,
data: {},
message: err.issues?.[0]?.message || "Invalid mail payload.",
});
}
return res.status(err.statusCode || 500).json({
success: false,
data: {},
message: err.message || "Failed to send mail.",
...(typeof err.limit === "number" ? { limit: err.limit } : {}),
});
}
};