@@ -64,7 +64,7 @@ export async function subscribeUser(subscription) {
6464
6565 await userProfiles . updateOne (
6666 { user_id : userId } ,
67- { $set : { "userData.pwa_subscription " : subscription } } ,
67+ { $addToSet : { "userData.pwa_subscriptions " : subscription } } ,
6868 { upsert : true }
6969 )
7070
@@ -79,7 +79,7 @@ export async function subscribeUser(subscription) {
7979 }
8080}
8181
82- export async function unsubscribeUser ( ) {
82+ export async function unsubscribeUser ( endpoint ) {
8383 const session = await auth0 . getSession ( )
8484 if ( ! session ?. user ) {
8585 throw new Error ( "Not authenticated" )
@@ -92,10 +92,12 @@ export async function unsubscribeUser() {
9292
9393 await userProfiles . updateOne (
9494 { user_id : userId } ,
95- { $unset : { "userData.pwa_subscription " : "" } }
95+ { $pull : { "userData.pwa_subscriptions " : { endpoint : endpoint } } }
9696 )
9797
98- console . log ( `[Actions] Subscription removed for user: ${ userId } ` )
98+ console . log (
99+ `[Actions] Subscription with endpoint ${ endpoint } removed for user: ${ userId } `
100+ )
99101 return { success : true }
100102 } catch ( error ) {
101103 console . error ( "[Actions] Error removing subscription:" , error )
@@ -132,39 +134,64 @@ export async function sendNotificationToCurrentUser(payload) {
132134 . collection ( "user_profiles" )
133135 . findOne (
134136 { user_id : userId } ,
135- { projection : { "userData.pwa_subscription " : 1 } }
137+ { projection : { "userData.pwa_subscriptions " : 1 } }
136138 )
137139
138- const subscription = userProfile ?. userData ?. pwa_subscription
140+ const subscriptions = userProfile ?. userData ?. pwa_subscriptions
139141
140- if ( ! subscription ) {
142+ if (
143+ ! subscriptions ||
144+ ! Array . isArray ( subscriptions ) ||
145+ subscriptions . length === 0
146+ ) {
141147 console . log (
142148 `[Actions] No push subscription found for user ${ userId } .`
143149 )
144150 return { success : false , error : "No subscription found for user." }
145151 }
146152
147- await webpush . sendNotification ( subscription , JSON . stringify ( payload ) )
148-
149- console . log (
150- `[Actions] Push notification sent successfully to user ${ userId } .`
151- )
152- return { success : true }
153+ let successCount = 0
154+ const promises = subscriptions . map ( ( subscription ) => {
155+ return webpush
156+ . sendNotification ( subscription , JSON . stringify ( payload ) )
157+ . then ( ( ) => {
158+ successCount ++
159+ console . log (
160+ `[Actions] Push notification sent successfully to endpoint for user ${ userId } .`
161+ )
162+ } )
163+ . catch ( async ( error ) => {
164+ console . error (
165+ `[Actions] Error sending push notification to an endpoint for user ${ userId } :` ,
166+ error . statusCode
167+ )
168+ if ( error . statusCode === 410 || error . statusCode === 404 ) {
169+ console . log (
170+ `[Actions] Subscription for user ${ userId } is invalid. Removing from DB.`
171+ )
172+ await unsubscribeUser ( subscription . endpoint )
173+ }
174+ } )
175+ } )
176+
177+ await Promise . all ( promises )
178+
179+ if ( successCount > 0 ) {
180+ return {
181+ success : true ,
182+ message : `Sent notifications to ${ successCount } of ${ subscriptions . length } devices.`
183+ }
184+ } else {
185+ return {
186+ success : false ,
187+ error : "Failed to send notifications to any device."
188+ }
189+ }
153190 } catch ( error ) {
154191 console . error (
155- `[Actions] Error sending push notification to user ${ userId } :` ,
192+ `[Actions] General error sending push notifications to user ${ userId } :` ,
156193 error
157194 )
158-
159- // If the subscription is expired or invalid, the push service returns an error (e.g., 410 Gone).
160- // We should handle this by removing the invalid subscription from the database.
161- if ( error . statusCode === 410 || error . statusCode === 404 ) {
162- console . log (
163- `[Actions] Subscription for user ${ userId } is invalid. Removing from DB.`
164- )
165- await unsubscribeUser ( )
166- }
167-
168- return { success : false , error : "Failed to send push notification." }
195+ return { success : false , error : "A general error occurred." }
169196 }
170197}
0 commit comments