feat(cloud): sync subscriber preferences on unsubscribe

This commit is contained in:
Henry
2026-08-03 11:53:29 +01:00
parent d6aa82e070
commit b4e9dd3811
6 changed files with 87 additions and 1 deletions

View File

@@ -27,6 +27,7 @@
"@kan/api": "workspace:*",
"@kan/auth": "workspace:*",
"@kan/db": "workspace:^",
"@kan/email": "workspace:^",
"@kan/logger": "workspace:^",
"@kan/shared": "workspace:^",
"@lingui/babel-preset-react": "^2.9.2",

View File

@@ -5,9 +5,13 @@ import { z } from "zod";
import { withApiLogging } from "@kan/api/utils/apiLogging";
import { withRateLimit } from "@kan/api/utils/rateLimit";
import { updateSubscriberPreferences } from "@kan/email";
import { createLogger } from "@kan/logger";
import { env } from "~/env";
const log = createLogger("unsubscribe");
const requestSchema = z.object({
token: z.string().min(1),
});
@@ -84,6 +88,7 @@ export default withRateLimit(
const novu = new Novu({ secretKey: env.NOVU_API_KEY });
let novuFailed = false;
try {
await novu.subscribers.preferences.update(
{
@@ -94,6 +99,18 @@ export default withRateLimit(
payload.subscriberId,
);
} catch (error) {
novuFailed = true;
}
try {
await updateSubscriberPreferences(payload.subscriberId, {
email: false,
});
} catch (error) {
log.error({ err: error }, "Error updating subscriber preferences");
}
if (novuFailed) {
return res.status(502).json({
success: false,
error: