145 lines
4.9 KiB
TypeScript
145 lines
4.9 KiB
TypeScript
|
||
const webPush = require('web-push')
|
||
|
||
import common from '../../src/helpers/common';
|
||
|
||
//generate and store VAPID keys in .env.local if not already done
|
||
if (!process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY || !process.env.WEB_PUSH_PRIVATE_KEY) {
|
||
const { publicKey, privateKey } = webPush.generateVAPIDKeys()
|
||
console.log('VAPID keys generated:')
|
||
console.log('Public key:', publicKey)
|
||
console.log('Private key:', privateKey)
|
||
console.log('Store these keys in your .env.local file:')
|
||
console.log('NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY=', publicKey)
|
||
console.log('WEB_PUSH_PRIVATE_KEY=', privateKey)
|
||
process.exit(0)
|
||
}
|
||
|
||
webPush.setVapidDetails(
|
||
`mailto:${process.env.WEB_PUSH_EMAIL}`,
|
||
process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY,
|
||
process.env.WEB_PUSH_PRIVATE_KEY
|
||
)
|
||
|
||
const Notification = async (req, res) => {
|
||
if (req.method == 'GET') {
|
||
res.statusCode = 200
|
||
res.setHeader('Allow', 'POST')
|
||
// send the public key in the response headers
|
||
//res.setHeader('Content-Type', 'text/plain')
|
||
res.end(process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY)
|
||
res.end()
|
||
}
|
||
if (req.method == 'PUT') {
|
||
// store the subscription object in the database
|
||
// publisher.pushSubscription = subscription
|
||
const prisma = common.getPrismaClient();
|
||
const { subscription, id } = req.body
|
||
const publisher = await prisma.publisher.update({
|
||
where: { id },
|
||
data: { pushSubscription: subscription }
|
||
})
|
||
console.log('Subscription for publisher', id, 'updated:', subscription)
|
||
res.statusCode = 200
|
||
res.end()
|
||
}
|
||
if (req.method == 'DELETE') {
|
||
// remove the subscription object from the database
|
||
// publisher.pushSubscription = null
|
||
const prisma = common.getPrismaClient();
|
||
const { id } = req.body
|
||
const publisher = await prisma.publisher.update({
|
||
where: { id },
|
||
data: { pushSubscription: null }
|
||
})
|
||
console.log('Subscription for publisher', id, 'deleted')
|
||
res.statusCode = 200
|
||
res.end()
|
||
}
|
||
|
||
|
||
if (req.method == 'POST') {//title = "ССС", message = "Ще получите уведомление по този начин.")
|
||
const { subscription, id, broadcast, title = 'ССОМ', message = 'Ще получавате уведомления така.', actions } = req.body
|
||
if (broadcast) {
|
||
await broadcastPush(title, message)
|
||
res.statusCode = 200
|
||
res.end()
|
||
return
|
||
}
|
||
else if (id) {
|
||
await sendPush(id, title, message)
|
||
res.statusCode = 200
|
||
res.end()
|
||
return
|
||
} else if (subscription) {
|
||
await webPush
|
||
.sendNotification(
|
||
subscription,
|
||
JSON.stringify({ title, message, actions })
|
||
)
|
||
.then(response => {
|
||
res.writeHead(response.statusCode, response.headers).end(response.body)
|
||
})
|
||
.catch(err => {
|
||
if ('statusCode' in err) {
|
||
res.writeHead(err.statusCode, err.headers).end(err.body)
|
||
} else {
|
||
console.error(err)
|
||
res.statusCode = 500
|
||
res.end()
|
||
}
|
||
})
|
||
}
|
||
} else {
|
||
res.statusCode = 405
|
||
res.end()
|
||
}
|
||
}
|
||
|
||
export default Notification
|
||
|
||
//export pushNotification(userId or email) for use in other files
|
||
export const sendPush = async (id, title, message) => {
|
||
const prisma = common.getPrismaClient();
|
||
const publisher = await prisma.publisher.findUnique({
|
||
where: { id }
|
||
})
|
||
if (!publisher.pushSubscription) {
|
||
console.log('No push subscription found for publisher', id)
|
||
return
|
||
}
|
||
|
||
await webPush
|
||
.sendNotification(
|
||
publisher.pushSubscription,
|
||
JSON.stringify({ title, message })
|
||
)
|
||
.then(response => {
|
||
console.log('Push notification sent to publisher', id)
|
||
})
|
||
.catch(err => {
|
||
console.error('Error sending push notification to publisher', id, ':', err)
|
||
})
|
||
}
|
||
//export breoadcastNotification for use in other files
|
||
export const broadcastPush = async (title, message) => {
|
||
const prisma = common.getPrismaClient();
|
||
const publishers = await prisma.publisher.findMany({
|
||
where: { pushSubscription: { not: null } }
|
||
})
|
||
|
||
for (const publisher of publishers) {
|
||
await webPush
|
||
.sendNotification(
|
||
publisher.pushSubscription,
|
||
JSON.stringify({ title, message })
|
||
)
|
||
.then(response => {
|
||
console.log('Push notification sent to publisher', publisher.id)
|
||
})
|
||
.catch(err => {
|
||
console.error('Error sending push notification to publisher', publisher.id, ':', err)
|
||
})
|
||
}
|
||
}
|