gathergate 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +212 -2
- package/client/dist/assets/geist-mono-cyrillic-400-normal-C51Di1Mf.woff2 +0 -0
- package/client/dist/assets/geist-mono-cyrillic-400-normal-CPJFfJgk.woff +0 -0
- package/client/dist/assets/geist-mono-cyrillic-500-normal-D2cXvDHF.woff2 +0 -0
- package/client/dist/assets/geist-mono-cyrillic-500-normal-DfuvdVgn.woff +0 -0
- package/client/dist/assets/geist-mono-cyrillic-600-normal-Dfv53e6N.woff2 +0 -0
- package/client/dist/assets/geist-mono-cyrillic-600-normal-DzvMIgFZ.woff +0 -0
- package/client/dist/assets/geist-mono-latin-400-normal-B2yvC1Cq.woff +0 -0
- package/client/dist/assets/geist-mono-latin-400-normal-DTRLJnHl.woff2 +0 -0
- package/client/dist/assets/geist-mono-latin-500-normal-D-GG86Jb.woff +0 -0
- package/client/dist/assets/geist-mono-latin-500-normal-YINYabwD.woff2 +0 -0
- package/client/dist/assets/geist-mono-latin-600-normal-V-KvD_pi.woff2 +0 -0
- package/client/dist/assets/geist-mono-latin-600-normal-bZn07FKM.woff +0 -0
- package/client/dist/assets/geist-mono-latin-ext-400-normal-CfzLURNc.woff +0 -0
- package/client/dist/assets/geist-mono-latin-ext-400-normal-DI-rJ0UV.woff2 +0 -0
- package/client/dist/assets/geist-mono-latin-ext-500-normal-Buglb9-a.woff2 +0 -0
- package/client/dist/assets/geist-mono-latin-ext-500-normal-CrWEgvU_.woff +0 -0
- package/client/dist/assets/geist-mono-latin-ext-600-normal-B7ybHiTt.woff +0 -0
- package/client/dist/assets/geist-mono-latin-ext-600-normal-TphwrhF8.woff2 +0 -0
- package/client/dist/assets/geist-mono-symbols2-400-normal-DjbiTEEA.woff +0 -0
- package/client/dist/assets/geist-mono-symbols2-500-normal-CmnCPGa0.woff +0 -0
- package/client/dist/assets/geist-mono-symbols2-600-normal-D7I4D0GJ.woff +0 -0
- package/client/dist/assets/geist-mono-vietnamese-400-normal-CZEPLOgu.woff +0 -0
- package/client/dist/assets/geist-mono-vietnamese-500-normal-Dx9-epDo.woff +0 -0
- package/client/dist/assets/geist-mono-vietnamese-600-normal-BLL01lwa.woff +0 -0
- package/client/dist/assets/index-BWbZAfMJ.js +80 -0
- package/client/dist/assets/index-ZnG2njjK.css +2 -0
- package/client/dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/client/dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/client/dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/client/dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/client/dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/client/dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/client/dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/client/dist/favicon.svg +12 -0
- package/client/dist/index.html +16 -0
- package/client/dist/site.webmanifest +16 -0
- package/dist/adapters/email.js +125 -0
- package/dist/adapters/storage.js +106 -0
- package/dist/app.js +2126 -0
- package/dist/config.js +106 -0
- package/dist/db/client.js +294 -0
- package/dist/db/queries.js +898 -0
- package/dist/emails/templates.js +221 -0
- package/dist/index.js +77 -0
- package/dist/jobs/chaser.js +81 -0
- package/dist/jobs/demo.js +329 -0
- package/dist/jobs/purge.js +22 -0
- package/dist/jobs/webhook.js +153 -0
- package/dist/security/auth.js +69 -0
- package/dist/security/crypto.js +91 -0
- package/dist/security/magic-bytes.js +131 -0
- package/dist/security/rate-limit.js +72 -0
- package/dist/security/ssrf.js +181 -0
- package/dist/templates/presets.js +341 -0
- package/dist/validators/index.js +52 -0
- package/dist/validators/packs/au.js +108 -0
- package/dist/validators/packs/eu.js +85 -0
- package/dist/validators/packs/generic.js +57 -0
- package/dist/validators/packs/types.js +1 -0
- package/dist/validators/packs/uk.js +58 -0
- package/dist/validators/packs/us.js +82 -0
- package/package.json +55 -4
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
export function emailBrand(ws, baseUrl) {
|
|
2
|
+
return {
|
|
3
|
+
name: ws.name,
|
|
4
|
+
color: ws.brand_color,
|
|
5
|
+
// Email clients block data: URIs, so logos are served from a public route.
|
|
6
|
+
logoUrl: ws.logo_b64 ? `${baseUrl}/api/public/workspaces/${ws.id}/logo` : null,
|
|
7
|
+
supportEmail: ws.support_email,
|
|
8
|
+
supportPhone: ws.support_phone,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
const DEFAULT_ACCENT = "#0F5243";
|
|
12
|
+
const INK = "#11181C";
|
|
13
|
+
const MUTED = "#5B6B66";
|
|
14
|
+
const HAIRLINE = "#E2E7E3";
|
|
15
|
+
const CANVAS = "#F8F9F7";
|
|
16
|
+
export function escapeHtml(value) {
|
|
17
|
+
return value
|
|
18
|
+
.replace(/&/g, "&")
|
|
19
|
+
.replace(/</g, "<")
|
|
20
|
+
.replace(/>/g, ">")
|
|
21
|
+
.replace(/"/g, """)
|
|
22
|
+
.replace(/'/g, "'");
|
|
23
|
+
}
|
|
24
|
+
export function isHexColor(value) {
|
|
25
|
+
return typeof value === "string" && /^#[0-9a-fA-F]{6}$/.test(value);
|
|
26
|
+
}
|
|
27
|
+
function luminance(hex) {
|
|
28
|
+
const channel = (i) => {
|
|
29
|
+
const c = parseInt(hex.slice(i, i + 2), 16) / 255;
|
|
30
|
+
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
31
|
+
};
|
|
32
|
+
return 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5);
|
|
33
|
+
}
|
|
34
|
+
/** WCAG contrast ratio between two #RRGGBB colours. */
|
|
35
|
+
export function contrastRatio(a, b) {
|
|
36
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
37
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
38
|
+
}
|
|
39
|
+
/** Picks white or ink text, whichever reads better on the accent. */
|
|
40
|
+
export function readableTextOn(hex) {
|
|
41
|
+
return contrastRatio(hex, "#FFFFFF") >= contrastRatio(hex, INK) ? "#FFFFFF" : INK;
|
|
42
|
+
}
|
|
43
|
+
function layout(params) {
|
|
44
|
+
const accent = isHexColor(params.brand.color) ? params.brand.color : DEFAULT_ACCENT;
|
|
45
|
+
const onAccent = readableTextOn(accent);
|
|
46
|
+
const name = escapeHtml(params.brand.name);
|
|
47
|
+
const header = params.brand.logoUrl
|
|
48
|
+
? `<img src="${escapeHtml(params.brand.logoUrl)}" alt="${name}" height="32" style="display:block;height:32px;max-width:200px;border:0;">`
|
|
49
|
+
: `<span style="font-size:15px;font-weight:600;color:${INK};letter-spacing:-0.01em;">${name}</span>`;
|
|
50
|
+
const cta = params.cta
|
|
51
|
+
? `<table role="presentation" cellpadding="0" cellspacing="0" style="margin:28px 0 8px;">
|
|
52
|
+
<tr><td style="border-radius:8px;background:${accent};">
|
|
53
|
+
<a href="${escapeHtml(params.cta.url)}" style="display:inline-block;padding:12px 22px;font-size:15px;font-weight:600;color:${onAccent};text-decoration:none;border-radius:8px;">${escapeHtml(params.cta.label)}</a>
|
|
54
|
+
</td></tr>
|
|
55
|
+
</table>
|
|
56
|
+
<p style="margin:12px 0 0;font-size:12px;line-height:18px;color:${MUTED};">Button not working? Paste this link into your browser:<br><a href="${escapeHtml(params.cta.url)}" style="color:${MUTED};word-break:break-all;">${escapeHtml(params.cta.url)}</a></p>`
|
|
57
|
+
: "";
|
|
58
|
+
const support = [params.brand.supportEmail, params.brand.supportPhone].filter(Boolean).map((v) => escapeHtml(v));
|
|
59
|
+
const supportLine = support.length
|
|
60
|
+
? `Questions? Contact ${name} at ${support.join(" or ")}.`
|
|
61
|
+
: `Questions? Reply to ${name} directly.`;
|
|
62
|
+
return `<!doctype html>
|
|
63
|
+
<html lang="en">
|
|
64
|
+
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escapeHtml(params.heading)}</title></head>
|
|
65
|
+
<body style="margin:0;padding:0;background:${CANVAS};font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;">
|
|
66
|
+
<span style="display:none;max-height:0;overflow:hidden;opacity:0;">${escapeHtml(params.preheader)}</span>
|
|
67
|
+
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:${CANVAS};">
|
|
68
|
+
<tr><td align="center" style="padding:32px 16px;">
|
|
69
|
+
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="max-width:560px;">
|
|
70
|
+
<tr><td style="padding:0 4px 20px;">${header}</td></tr>
|
|
71
|
+
<tr><td style="background:#FFFFFF;border:1px solid ${HAIRLINE};border-radius:12px;padding:32px;">
|
|
72
|
+
<h1 style="margin:0 0 16px;font-size:20px;line-height:28px;font-weight:600;color:${INK};letter-spacing:-0.01em;">${escapeHtml(params.heading)}</h1>
|
|
73
|
+
${params.bodyHtml}
|
|
74
|
+
${cta}
|
|
75
|
+
</td></tr>
|
|
76
|
+
<tr><td style="padding:20px 4px 0;font-size:12px;line-height:18px;color:${MUTED};">
|
|
77
|
+
${supportLine}<br>No account or password needed. Files are encrypted when they arrive.
|
|
78
|
+
</td></tr>
|
|
79
|
+
</table>
|
|
80
|
+
</td></tr>
|
|
81
|
+
</table>
|
|
82
|
+
</body>
|
|
83
|
+
</html>`;
|
|
84
|
+
}
|
|
85
|
+
const p = (html) => `<p style="margin:0 0 14px;font-size:15px;line-height:24px;color:${INK};">${html}</p>`;
|
|
86
|
+
function formatDue(dueAt) {
|
|
87
|
+
if (!dueAt)
|
|
88
|
+
return null;
|
|
89
|
+
const d = new Date(dueAt);
|
|
90
|
+
if (Number.isNaN(d.getTime()))
|
|
91
|
+
return null;
|
|
92
|
+
return d.toLocaleDateString("en-AU", { weekday: "long", day: "numeric", month: "long", timeZone: "UTC" });
|
|
93
|
+
}
|
|
94
|
+
export function inviteEmail(params) {
|
|
95
|
+
const due = formatDue(params.dueAt);
|
|
96
|
+
const subject = params.subject || `${params.brand.name} needs a few documents from you`;
|
|
97
|
+
const items = `${params.itemCount} item${params.itemCount === 1 ? "" : "s"}`;
|
|
98
|
+
const text = [
|
|
99
|
+
`Hi ${params.clientName},`,
|
|
100
|
+
"",
|
|
101
|
+
`${params.brand.name} has asked for ${items} for "${params.title}".`,
|
|
102
|
+
...(params.welcomeNote ? ["", params.welcomeNote] : []),
|
|
103
|
+
...(due ? ["", `Please complete it by ${due}.`] : []),
|
|
104
|
+
"",
|
|
105
|
+
"Open your secure checklist (no account or password needed):",
|
|
106
|
+
params.portalUrl,
|
|
107
|
+
].join("\n");
|
|
108
|
+
const html = layout({
|
|
109
|
+
brand: params.brand,
|
|
110
|
+
preheader: `${items} requested for ${params.title}`,
|
|
111
|
+
heading: params.title,
|
|
112
|
+
bodyHtml: [
|
|
113
|
+
p(`Hi ${escapeHtml(params.clientName)},`),
|
|
114
|
+
p(`${escapeHtml(params.brand.name)} has asked for ${items}. Upload everything in one place, at your own pace. Your progress saves as you go.`),
|
|
115
|
+
params.welcomeNote
|
|
116
|
+
? `<p style="margin:0 0 14px;padding:12px 14px;border-left:3px solid ${HAIRLINE};font-size:14px;line-height:22px;color:${MUTED};white-space:pre-line;">${escapeHtml(params.welcomeNote)}</p>`
|
|
117
|
+
: "",
|
|
118
|
+
due ? p(`Please complete it by <strong>${escapeHtml(due)}</strong>.`) : "",
|
|
119
|
+
].join(""),
|
|
120
|
+
cta: { label: "Open checklist", url: params.portalUrl },
|
|
121
|
+
});
|
|
122
|
+
return { subject, text, html };
|
|
123
|
+
}
|
|
124
|
+
export function otpEmail(params) {
|
|
125
|
+
const subject = `${params.code} is your verification code`;
|
|
126
|
+
const text = [
|
|
127
|
+
`Hi ${params.clientName},`,
|
|
128
|
+
"",
|
|
129
|
+
`Use this code to open "${params.title}" from ${params.brand.name}:`,
|
|
130
|
+
"",
|
|
131
|
+
params.code,
|
|
132
|
+
"",
|
|
133
|
+
"It expires in 15 minutes. If you did not request it, you can ignore this email.",
|
|
134
|
+
].join("\n");
|
|
135
|
+
const html = layout({
|
|
136
|
+
brand: params.brand,
|
|
137
|
+
preheader: `Your code is ${params.code}`,
|
|
138
|
+
heading: "Your verification code",
|
|
139
|
+
bodyHtml: [
|
|
140
|
+
p(`Use this code to open <strong>${escapeHtml(params.title)}</strong>.`),
|
|
141
|
+
`<p style="margin:20px 0;font-family:'SFMono-Regular',Menlo,Consolas,monospace;font-size:30px;line-height:36px;font-weight:600;letter-spacing:8px;color:${INK};">${escapeHtml(params.code)}</p>`,
|
|
142
|
+
p(`<span style="color:${MUTED};font-size:13px;">It expires in 15 minutes. If you did not request it, you can ignore this email.</span>`),
|
|
143
|
+
].join(""),
|
|
144
|
+
});
|
|
145
|
+
return { subject, text, html };
|
|
146
|
+
}
|
|
147
|
+
export function revisionEmail(params) {
|
|
148
|
+
const subject = `One item needs another look: ${params.itemLabel}`;
|
|
149
|
+
const text = [
|
|
150
|
+
`Hi ${params.clientName},`,
|
|
151
|
+
"",
|
|
152
|
+
`${params.brand.name} reviewed "${params.itemLabel}" on "${params.title}" and asked for an update:`,
|
|
153
|
+
"",
|
|
154
|
+
params.note,
|
|
155
|
+
"",
|
|
156
|
+
"Everything else you sent is saved. Open your checklist to update this item:",
|
|
157
|
+
params.portalUrl,
|
|
158
|
+
].join("\n");
|
|
159
|
+
const html = layout({
|
|
160
|
+
brand: params.brand,
|
|
161
|
+
preheader: `Update needed for ${params.itemLabel}`,
|
|
162
|
+
heading: "One item needs another look",
|
|
163
|
+
bodyHtml: [
|
|
164
|
+
p(`Hi ${escapeHtml(params.clientName)},`),
|
|
165
|
+
p(`${escapeHtml(params.brand.name)} reviewed <strong>${escapeHtml(params.itemLabel)}</strong> and asked for an update:`),
|
|
166
|
+
`<p style="margin:0 0 14px;padding:12px 14px;background:#FFF8EB;border:1px solid #F5D9A8;border-radius:8px;font-size:14px;line-height:22px;color:${INK};white-space:pre-line;">${escapeHtml(params.note)}</p>`,
|
|
167
|
+
p("Everything else you sent is saved."),
|
|
168
|
+
].join(""),
|
|
169
|
+
cta: { label: "Update item", url: params.portalUrl },
|
|
170
|
+
});
|
|
171
|
+
return { subject, text, html };
|
|
172
|
+
}
|
|
173
|
+
export function reminderEmail(params) {
|
|
174
|
+
const count = params.outstanding.length;
|
|
175
|
+
const subject = `Reminder: ${count} item${count === 1 ? "" : "s"} left for ${params.title}`;
|
|
176
|
+
const text = [
|
|
177
|
+
`Hi ${params.clientName},`,
|
|
178
|
+
"",
|
|
179
|
+
`A quick reminder from ${params.brand.name}. Still needed for "${params.title}":`,
|
|
180
|
+
"",
|
|
181
|
+
...params.outstanding.map((i) => ` - ${i.label}${i.needsRevision ? " (needs update)" : ""}${i.note ? `: ${i.note}` : ""}`),
|
|
182
|
+
"",
|
|
183
|
+
"Open your secure checklist (no account or password needed):",
|
|
184
|
+
params.portalUrl,
|
|
185
|
+
].join("\n");
|
|
186
|
+
const rows = params.outstanding
|
|
187
|
+
.map((i) => `<tr><td style="padding:10px 0;border-top:1px solid ${HAIRLINE};font-size:14px;line-height:20px;color:${INK};">
|
|
188
|
+
${escapeHtml(i.label)}${i.needsRevision ? ` <span style="color:#B45309;font-size:12px;font-weight:600;">Needs update</span>` : ""}
|
|
189
|
+
${i.note ? `<br><span style="color:${MUTED};font-size:13px;">${escapeHtml(i.note)}</span>` : ""}
|
|
190
|
+
</td></tr>`)
|
|
191
|
+
.join("");
|
|
192
|
+
const html = layout({
|
|
193
|
+
brand: params.brand,
|
|
194
|
+
preheader: `${count} item${count === 1 ? "" : "s"} still needed`,
|
|
195
|
+
heading: `${count} item${count === 1 ? "" : "s"} left`,
|
|
196
|
+
bodyHtml: [
|
|
197
|
+
p(`Hi ${escapeHtml(params.clientName)}, a quick reminder from ${escapeHtml(params.brand.name)}. Still needed for <strong>${escapeHtml(params.title)}</strong>:`),
|
|
198
|
+
`<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin:4px 0 0;border-bottom:1px solid ${HAIRLINE};">${rows}</table>`,
|
|
199
|
+
].join(""),
|
|
200
|
+
cta: { label: "Continue checklist", url: params.portalUrl },
|
|
201
|
+
});
|
|
202
|
+
return { subject, text, html };
|
|
203
|
+
}
|
|
204
|
+
export function testDeliveryEmail(params) {
|
|
205
|
+
const subject = `${params.brand.name} test email`;
|
|
206
|
+
const text = [
|
|
207
|
+
`This is a test email from ${params.brand.name} via GatherGate (${params.backendLabel}).`,
|
|
208
|
+
"",
|
|
209
|
+
"Your outbound email settings are working.",
|
|
210
|
+
].join("\n");
|
|
211
|
+
const html = layout({
|
|
212
|
+
brand: params.brand,
|
|
213
|
+
preheader: `Test email from ${params.brand.name}`,
|
|
214
|
+
heading: "Outbound email is connected",
|
|
215
|
+
bodyHtml: [
|
|
216
|
+
p(`This is a test message sent from <strong>${escapeHtml(params.brand.name)}</strong> using <strong>${escapeHtml(params.backendLabel)}</strong>.`),
|
|
217
|
+
p(`<span style="color:${MUTED};font-size:13px;">Client portal invites, 6-digit verification codes, revision notices and scheduled reminders use this configuration.</span>`),
|
|
218
|
+
].join(""),
|
|
219
|
+
});
|
|
220
|
+
return { subject, text, html };
|
|
221
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { serve } from "@hono/node-server";
|
|
3
|
+
import { createEmailAdapter } from "./adapters/email.js";
|
|
4
|
+
import { createStorageAdapter } from "./adapters/storage.js";
|
|
5
|
+
import { createApp } from "./app.js";
|
|
6
|
+
import { loadConfig } from "./config.js";
|
|
7
|
+
import { openDatabase } from "./db/client.js";
|
|
8
|
+
import { runReminderSweep } from "./jobs/chaser.js";
|
|
9
|
+
import { runPurgeSweep } from "./jobs/purge.js";
|
|
10
|
+
const config = loadConfig();
|
|
11
|
+
if (config.masterKeySource === "file") {
|
|
12
|
+
console.warn(`[GatherGate] MASTER_KEY not set in environment; using auto-generated key at ${config.masterKeyFilePath}. Move MASTER_KEY to an environment variable before production use.`);
|
|
13
|
+
}
|
|
14
|
+
const db = openDatabase(config.dbPath);
|
|
15
|
+
const storage = createStorageAdapter(config);
|
|
16
|
+
const email = createEmailAdapter(config);
|
|
17
|
+
const { app, store } = createApp({ config, db, storage, email });
|
|
18
|
+
const server = serve({
|
|
19
|
+
fetch: app.fetch,
|
|
20
|
+
hostname: config.host,
|
|
21
|
+
port: config.port,
|
|
22
|
+
}, (info) => {
|
|
23
|
+
console.log(`GatherGate listening on http://${info.address}:${info.port} (base: ${config.baseUrl}, storage: ${config.storageBackend}, email: ${config.emailBackend})`);
|
|
24
|
+
});
|
|
25
|
+
let cronTimer;
|
|
26
|
+
let cronInFlight = false;
|
|
27
|
+
if (config.cronIntervalMinutes > 0) {
|
|
28
|
+
const runJobs = async () => {
|
|
29
|
+
if (cronInFlight)
|
|
30
|
+
return;
|
|
31
|
+
cronInFlight = true;
|
|
32
|
+
try {
|
|
33
|
+
let sent = 0;
|
|
34
|
+
let purged = { requestsPurged: 0, filesDeleted: 0 };
|
|
35
|
+
try {
|
|
36
|
+
sent = await runReminderSweep({
|
|
37
|
+
store,
|
|
38
|
+
email,
|
|
39
|
+
baseUrl: config.baseUrl,
|
|
40
|
+
masterKey: config.masterKey,
|
|
41
|
+
config,
|
|
42
|
+
saveToOutbox: config.emailBackend === "console",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
console.error("[GatherGate Cron] reminder sweep failed:", err);
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
purged = await runPurgeSweep({ store, storage });
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
console.error("[GatherGate Cron] purge sweep failed:", err);
|
|
53
|
+
}
|
|
54
|
+
if (sent > 0 || purged.requestsPurged > 0) {
|
|
55
|
+
console.log(`[GatherGate Cron] reminders_sent=${sent} requests_purged=${purged.requestsPurged} files_deleted=${purged.filesDeleted}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
cronInFlight = false;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
cronTimer = setInterval(runJobs, config.cronIntervalMinutes * 60_000);
|
|
63
|
+
cronTimer.unref();
|
|
64
|
+
}
|
|
65
|
+
for (const sig of ["SIGINT", "SIGTERM"]) {
|
|
66
|
+
process.on(sig, () => {
|
|
67
|
+
if (cronTimer)
|
|
68
|
+
clearInterval(cronTimer);
|
|
69
|
+
server.close(() => {
|
|
70
|
+
db.close();
|
|
71
|
+
process.exit(0);
|
|
72
|
+
});
|
|
73
|
+
if ("closeAllConnections" in server && typeof server.closeAllConnections === "function") {
|
|
74
|
+
server.closeAllConnections();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createEmailAdapterFromOptions, resolveWorkspaceEmail } from "../adapters/email.js";
|
|
2
|
+
import { emailBrand, reminderEmail } from "../emails/templates.js";
|
|
3
|
+
import { decryptField } from "../security/crypto.js";
|
|
4
|
+
import { assertPublicHostname } from "../security/ssrf.js";
|
|
5
|
+
export async function runReminderSweep(params) {
|
|
6
|
+
const nowIso = params.nowIso ?? new Date().toISOString();
|
|
7
|
+
const due = params.store.listDueReminders(nowIso);
|
|
8
|
+
let sentCount = 0;
|
|
9
|
+
for (const req of due) {
|
|
10
|
+
const items = params.store.listRequestItems(req.id);
|
|
11
|
+
const requiredItems = items.filter((i) => i.required === 1);
|
|
12
|
+
const checkSet = new Set((requiredItems.length > 0 ? requiredItems : items).map((i) => i.id));
|
|
13
|
+
const outstanding = items.filter((i) => (checkSet.has(i.id) || i.status === "rejected") && (i.status === "pending" || i.status === "rejected"));
|
|
14
|
+
if (outstanding.length === 0) {
|
|
15
|
+
params.store.clearNextReminder(req.id);
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const workspace = params.store.getWorkspaceById(req.workspace_id);
|
|
19
|
+
if (!workspace)
|
|
20
|
+
continue;
|
|
21
|
+
try {
|
|
22
|
+
const magicToken = decryptField(params.masterKey, req.token_encrypted, req.id);
|
|
23
|
+
const message = reminderEmail({
|
|
24
|
+
brand: emailBrand(workspace, params.baseUrl),
|
|
25
|
+
clientName: req.client_name,
|
|
26
|
+
title: req.title,
|
|
27
|
+
portalUrl: `${params.baseUrl}/c/${magicToken}`,
|
|
28
|
+
outstanding: outstanding.map((i) => ({
|
|
29
|
+
label: i.label,
|
|
30
|
+
needsRevision: i.status === "rejected",
|
|
31
|
+
note: i.review_note,
|
|
32
|
+
})),
|
|
33
|
+
});
|
|
34
|
+
const resolved = params.config
|
|
35
|
+
? resolveWorkspaceEmail(workspace, params.config, params.masterKey)
|
|
36
|
+
: null;
|
|
37
|
+
if (params.config?.multiTenant &&
|
|
38
|
+
resolved &&
|
|
39
|
+
resolved.source === "workspace" &&
|
|
40
|
+
resolved.backend === "smtp" &&
|
|
41
|
+
resolved.smtp?.host) {
|
|
42
|
+
const dnsErr = await assertPublicHostname(resolved.smtp.host, params.lookupImpl);
|
|
43
|
+
if (dnsErr) {
|
|
44
|
+
throw new Error("SMTP host must resolve to a public address in multi-tenant mode.");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const adapter = resolved && resolved.source === "workspace"
|
|
48
|
+
? createEmailAdapterFromOptions(resolved)
|
|
49
|
+
: params.email;
|
|
50
|
+
await adapter.send({
|
|
51
|
+
to: req.client_email,
|
|
52
|
+
replyTo: resolved?.replyTo || undefined,
|
|
53
|
+
...message,
|
|
54
|
+
});
|
|
55
|
+
const shouldSaveOutbox = resolved ? resolved.backend === "console" : Boolean(params.saveToOutbox);
|
|
56
|
+
if (shouldSaveOutbox) {
|
|
57
|
+
params.store.saveOutboxEmail({
|
|
58
|
+
workspaceId: req.workspace_id,
|
|
59
|
+
requestId: req.id,
|
|
60
|
+
recipient: req.client_email,
|
|
61
|
+
subject: message.subject,
|
|
62
|
+
textBody: message.text,
|
|
63
|
+
htmlBody: message.html,
|
|
64
|
+
kind: "reminder",
|
|
65
|
+
isDemo: req.is_demo === 1,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
params.store.recordReminderSent(req.id, nowIso);
|
|
69
|
+
sentCount += 1;
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
console.error(`[GatherGate Cron] reminder failed for request ${req.id}:`, err);
|
|
73
|
+
params.store.logAudit(req.workspace_id, req.id, "system", "reminder.failed", {
|
|
74
|
+
error: err instanceof Error ? err.message : String(err),
|
|
75
|
+
});
|
|
76
|
+
const retryAt = new Date(Date.parse(nowIso) + 3600_000).toISOString();
|
|
77
|
+
params.store.deferReminderAfterError(req.id, retryAt);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return sentCount;
|
|
81
|
+
}
|