Refactor subscription format
Place the actual push subscription data into a push property on the subscription so that other properties can be added to it.
This commit is contained in:
parent
b4934005ae
commit
abdcc83eb9
5 changed files with 29 additions and 11 deletions
|
@ -1,13 +1,17 @@
|
|||
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
||||
import { Subscription } from "~/shared/types/account";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
||||
const subscriptions = await readSubscriptions();
|
||||
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
|
||||
const existingIndex = subscriptions.findIndex(
|
||||
sub => sub.type === "push" && sub.push.endpoint === body.subscription.endpoint
|
||||
);
|
||||
const subscription: Subscription = { type: "push", push: body.subscription };
|
||||
if (existingIndex !== -1) {
|
||||
subscriptions[existingIndex] = body.subscription;
|
||||
subscriptions[existingIndex] = subscription;
|
||||
} else {
|
||||
subscriptions.push(body.subscription);
|
||||
subscriptions.push(subscription);
|
||||
}
|
||||
await writeSubscriptions(subscriptions);
|
||||
if (existingIndex !== -1) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue