swadhinbiswas Complete email templates routing and analytics APIs · 2/20/2026
1c01425
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
import type { APIRoute } from "astro";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { and, eq, isNull } from "drizzle-orm";
import { z } from "zod";
import { badRequest, success, unauthorized } from "@/lib/api";
import { withErrorHandler } from "@/lib/errors";
import { getDatabase, schema } from "@/db";
const EVENT_TYPES = [
"mention",
"assign",
"review_request",
"review_submitted",
"pr_approved",
"pr_changes_requested",
"pr_merged",
"pr_closed",
"comment",
"ci_passed",
"ci_failed",
"merge_queue",
"stack_update",
"ai_review",
"star",
"watching",
] as const;
const updateSchema = z.object({
routes: z.record(z.boolean()),
});
function generateId(): string {
return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
}
export const GET: APIRoute = withErrorHandler(async ({ locals }) => {
const user = locals.user;
if (!user) return unauthorized();
const db = getDatabase() as NodePgDatabase<typeof schema>;
const prefs = await db.query.notificationPreferences.findMany({
where: and(
eq(schema.notificationPreferences.userId, user.id),
isNull(schema.notificationPreferences.repositoryId),
),
});
const routing = Object.fromEntries(
EVENT_TYPES.map((eventType) => {
const pref = prefs.find((item) => item.eventType === eventType);
return [eventType, pref?.emailEnabled ?? true];
}),
);
return success({
eventTypes: EVENT_TYPES,
routing,
});
});
export const POST: APIRoute = withErrorHandler(async ({ locals, request }) => {
const user = locals.user;
if (!user) return unauthorized();
const body = await request.json().catch(() => null);
const parsed = updateSchema.safeParse(body || {});
if (!parsed.success) {
return badRequest(parsed.error.issues[0]?.message || "Invalid routing payload");
}
const db = getDatabase() as NodePgDatabase<typeof schema>;
for (const [eventType, enabled] of Object.entries(parsed.data.routes)) {
if (!(EVENT_TYPES as readonly string[]).includes(eventType)) {
return badRequest(`Unsupported event type: ${eventType}`);
}
const existing = await db.query.notificationPreferences.findFirst({
where: and(
eq(schema.notificationPreferences.userId, user.id),
eq(schema.notificationPreferences.eventType, eventType),
isNull(schema.notificationPreferences.repositoryId),
),
});
if (existing) {
await db
.update(schema.notificationPreferences)
.set({
emailEnabled: enabled,
updatedAt: new Date(),
})
.where(eq(schema.notificationPreferences.id, existing.id));
} else {
await db.insert(schema.notificationPreferences).values({
id: generateId(),
userId: user.id,
eventType,
emailEnabled: enabled,
slackEnabled: false,
inAppEnabled: true,
browserPushEnabled: false,
createdAt: new Date(),
updatedAt: new Date(),
});
}
}
return success({
updated: Object.keys(parsed.data.routes).length,
});
});