2025-03-05 18:41:47 +01:00
|
|
|
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
2025-02-28 15:32:03 +01:00
|
|
|
|
2025-03-05 15:36:50 +01:00
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
2025-03-05 18:41:47 +01:00
|
|
|
const subscriptions = await readSubscriptions();
|
2025-03-07 12:30:39 +01:00
|
|
|
const existingIndex = subscriptions.findIndex(
|
|
|
|
sub => sub.type === "push" && sub.push.endpoint === body.subscription.endpoint
|
|
|
|
);
|
2025-02-28 15:32:03 +01:00
|
|
|
if (existingIndex !== -1) {
|
|
|
|
subscriptions.splice(existingIndex, 1);
|
|
|
|
} else {
|
2025-03-05 15:36:50 +01:00
|
|
|
return { message: "No subscription registered."};
|
2025-02-28 15:32:03 +01:00
|
|
|
}
|
2025-03-05 18:41:47 +01:00
|
|
|
await writeSubscriptions(subscriptions);
|
2025-03-05 15:36:50 +01:00
|
|
|
return { message: "Existing subscription removed."};
|
|
|
|
})
|