Skip to content

Commit 2355c58

Browse files
Added logger + fixed web push notifs.
1 parent 6a6e779 commit 2355c58

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/lib/locations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import prisma from './db'
2+
import { logger } from './logger'
23

34
// Function definition with types
45
async function createLocation(
@@ -61,7 +62,7 @@ async function createLocation(
6162
},
6263
})
6364

64-
console.log('Location created:', location)
65+
logger.info('Location created:', location)
6566
}
6667

6768
// Example usage
@@ -97,7 +98,7 @@ const geoAddressData: GeoAddressData = {
9798
}
9899

99100
createLocation(subscriptionId, locationData, geoAddressData)
100-
.catch((e) => console.error(e))
101+
.catch((e) => logger.error(e))
101102
.finally(async () => {
102103
await prisma.$disconnect()
103104
})

src/lib/notifications.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import { messaging } from './firebase' // Adjust as needed
66
import { Subscription } from '@prisma/client'
77
import { JsonValue } from '@prisma/client/runtime/library'
88
import prisma from '@/lib/db'
9+
import { logger } from '@/lib/logger'
10+
11+
// Initialize VAPID keys for web push notifications
12+
webpush.setVapidDetails(
13+
'mailto:your-email@example.com',
14+
process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY!,
15+
process.env.VAPID_PRIVATE_KEY!
16+
)
917

1018
/**
1119
* Sends a push notification to a subscription using either web push (VAPID) or FCM.
@@ -22,7 +30,7 @@ export async function sendNotification(
2230
notificationPayload: NotificationPayload
2331
): Promise<void> {
2432
try {
25-
console.log('Sending notification to subscription:', subscription)
33+
logger.info('Sending notification to subscription:', subscription)
2634

2735
// Handle browser-based web push notifications.
2836
if (subscription.type === 'web') {
@@ -82,7 +90,7 @@ export async function sendNotification(
8290

8391
// Send the FCM message using Firebase.
8492
const response = await messaging.send(fcmMessage)
85-
console.log('FCM response:', response)
93+
logger.info('FCM response:', response)
8694
}
8795
} catch (error) {
8896
/**
@@ -93,9 +101,9 @@ export async function sendNotification(
93101
// Status code 410 indicates that the subscription is no longer valid.
94102
if (error.statusCode === 410) {
95103
await prisma.subscription.delete({ where: { id: subscription.id } })
96-
console.log(`Subscription with id ${subscription.id} removed due to expiration.`)
104+
logger.info(`Subscription with id ${subscription.id} removed due to expiration.`)
97105
} else {
98-
console.log(
106+
logger.info(
99107
`Failed to send notification to subscription id ${subscription.id}:`,
100108
error.statusCode,
101109
error.body
@@ -108,12 +116,12 @@ export async function sendNotification(
108116
error.code === 'messaging/registration-token-not-registered'
109117
) {
110118
await prisma.subscription.delete({ where: { id: subscription.id } })
111-
console.log(`Removed invalid FCM token for subscription id ${subscription.id}.`)
119+
logger.info(`Removed invalid FCM token for subscription id ${subscription.id}.`)
112120
} else {
113-
console.log(`Failed to send FCM notification to subscription id ${subscription.id}:`, error)
121+
logger.info(`Failed to send FCM notification to subscription id ${subscription.id}:`, error)
114122
}
115123
} else {
116-
console.log(
124+
logger.info(
117125
`An error occurred while sending notification to subscription id ${subscription.id}:`,
118126
error
119127
)

0 commit comments

Comments
 (0)