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".
26 lines
896 B
TypeScript
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."};
|
|
})
|