diff --git a/src/modules/notifications/services/notifications.service.ts b/src/modules/notifications/services/notifications.service.ts index 5a24c3e..16fea92 100755 --- a/src/modules/notifications/services/notifications.service.ts +++ b/src/modules/notifications/services/notifications.service.ts @@ -42,8 +42,10 @@ export class NotificationsService { }); await em.persistAndFlush(notification); + await em.commit(); + if (user.pushTokens && user.pushTokens.length > 0) { - await this.sendPushNotificationToUserDevices(user.id, user.pushTokens, { + this.sendPushNotificationAsync(user.id, user.pushTokens, { title: createNotificationDto.title, body: createNotificationDto.message, notification_click: { @@ -52,8 +54,6 @@ export class NotificationsService { }); } - await em.commit(); - return notification; } catch (error) { this.logger.error("error in createNotification", error); @@ -64,6 +64,14 @@ export class NotificationsService { //=============================================== + private sendPushNotificationAsync(userId: string, userTokens: string[], message: INajvaNotificationMessage) { + this.sendPushNotificationToUserDevices(userId, userTokens, message).catch((error) => { + this.logger.error(`Failed to send push notification asynchronously for user ${userId}:`, error); + }); + } + + //=============================================== + async sendPushNotificationToUserDevices(userId: string, userTokens: string[], message: INajvaNotificationMessage) { try { const result = await this.najvaPushService.sendNotificationToMultipleUsers(userTokens, message);