owltide/server/api/subscribe.post.ts
Hornwitser 251e83f640
All checks were successful
/ build (push) Successful in 1m12s
/ deploy (push) Successful in 16s
Rename AcountSession to ServerSession
Start the work of clearly distingushing client side types, server side
types and types shared over the API by renaming "AccountSession" and
"Session" names used on the server to "ServerSession".
2025-06-09 16:51:05 +02:00

26 lines
896 B
TypeScript

import { readSubscriptions, writeSubscriptions } from "~/server/database";
import { Subscription } from "~/shared/types/account";
export default defineEventHandler(async (event) => {
const session = await requireServerSession(event);
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
const subscriptions = await readSubscriptions();
const existingIndex = subscriptions.findIndex(
sub => sub.type === "push" && sub.sessionId === session.id
);
const subscription: Subscription = {
type: "push",
sessionId: session.id,
push: body.subscription
};
if (existingIndex !== -1) {
subscriptions[existingIndex] = subscription;
} else {
subscriptions.push(subscription);
}
await writeSubscriptions(subscriptions);
if (existingIndex !== -1) {
return { message: "Existing subscription refreshed."};
}
return { message: "New subscription registered."};
})