push subscriptions managment
This commit is contained in:
@ -35,11 +35,26 @@ const Notification = async (req, res) => {
|
||||
// publisher.pushSubscription = subscription
|
||||
const prisma = common.getPrismaClient();
|
||||
const { subscription, id } = req.body
|
||||
const publisher = await prisma.publisher.update({
|
||||
const publisher = await prisma.publisher.findUnique({
|
||||
where: { id },
|
||||
data: { pushSubscription: subscription }
|
||||
})
|
||||
select: { pushSubscription: true }
|
||||
});
|
||||
|
||||
let subscriptions = Array.isArray(publisher.pushSubscription) ? publisher.pushSubscription : (publisher.pushSubscription ? [publisher.pushSubscription] : []);
|
||||
const index = subscriptions.findIndex(sub => sub.endpoint === subscription.endpoint);
|
||||
|
||||
if (index !== -1) {
|
||||
subscriptions[index] = subscription; // Update existing subscription
|
||||
} else {
|
||||
subscriptions.push(subscription); // Add new subscription
|
||||
}
|
||||
|
||||
await prisma.publisher.update({
|
||||
where: { id },
|
||||
data: { pushSubscription: subscriptions }
|
||||
});
|
||||
console.log('Subscription for publisher', id, 'updated:', subscription)
|
||||
res.send({ subs: subscriptions.length })
|
||||
res.statusCode = 200
|
||||
res.end()
|
||||
}
|
||||
@ -47,12 +62,31 @@ const Notification = async (req, res) => {
|
||||
// remove the subscription object from the database
|
||||
// publisher.pushSubscription = null
|
||||
const prisma = common.getPrismaClient();
|
||||
const { id } = req.body
|
||||
const publisher = await prisma.publisher.update({
|
||||
const { subscriptionId, id } = req.body;
|
||||
|
||||
const publisher = await prisma.publisher.findUnique({
|
||||
where: { id },
|
||||
data: { pushSubscription: null }
|
||||
})
|
||||
select: { pushSubscription: true }
|
||||
});
|
||||
|
||||
let subscriptions = Array.isArray(publisher.pushSubscription) ? publisher.pushSubscription : (publisher.pushSubscription ? [publisher.pushSubscription] : []);
|
||||
try {
|
||||
subscriptions = subscriptions.filter(sub => sub.endpoint !== subscriptionId);
|
||||
|
||||
await prisma.publisher.update({
|
||||
where: { id },
|
||||
data: { pushSubscription: subscriptions }
|
||||
});
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
await prisma.publisher.update({
|
||||
where: { id },
|
||||
data: { pushSubscription: null }
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Subscription for publisher', id, 'deleted')
|
||||
res.send({ subs: subscriptions.length })
|
||||
res.statusCode = 200
|
||||
res.end()
|
||||
}
|
||||
|
Reference in New Issue
Block a user