* helper * Add script to move notification settings * wip, removal of NotificationSettings * event name * iteration * test * test * Remove last of NotificationSettings model * refactor * More fixes * snapshots * Change emails to class instances for type safety * test * docs * Update migration for self-hosted * tsc
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import Router from "koa-router";
|
|
import { NotificationEventType } from "@shared/types";
|
|
import env from "@server/env";
|
|
import { transaction } from "@server/middlewares/transaction";
|
|
import validate from "@server/middlewares/validate";
|
|
import { User } from "@server/models";
|
|
import NotificationSettingsHelper from "@server/models/helpers/NotificationSettingsHelper";
|
|
import { APIContext } from "@server/types";
|
|
import * as T from "./schema";
|
|
|
|
const router = new Router();
|
|
|
|
const handleUnsubscribe = async (
|
|
ctx: APIContext<T.NotificationsUnsubscribeReq>
|
|
) => {
|
|
const eventType = (ctx.input.body.eventType ??
|
|
ctx.input.query.eventType) as NotificationEventType;
|
|
const userId = (ctx.input.body.userId ?? ctx.input.query.userId) as string;
|
|
const token = (ctx.input.body.token ?? ctx.input.query.token) as string;
|
|
|
|
const user = await User.scope("withTeam").findByPk(userId, {
|
|
rejectOnEmpty: true,
|
|
});
|
|
const unsubscribeToken = NotificationSettingsHelper.unsubscribeToken(
|
|
user,
|
|
eventType
|
|
);
|
|
|
|
if (unsubscribeToken !== token) {
|
|
ctx.redirect(`${env.URL}?notice=invalid-auth`);
|
|
return;
|
|
}
|
|
|
|
user.setNotificationEventType(eventType, false);
|
|
await user.save();
|
|
ctx.redirect(`${user.team.url}/settings/notifications?success`);
|
|
};
|
|
|
|
router.get(
|
|
"notifications.unsubscribe",
|
|
validate(T.NotificationsUnsubscribeSchema),
|
|
transaction(),
|
|
handleUnsubscribe
|
|
);
|
|
router.post(
|
|
"notifications.unsubscribe",
|
|
validate(T.NotificationsUnsubscribeSchema),
|
|
transaction(),
|
|
handleUnsubscribe
|
|
);
|
|
|
|
export default router;
|