mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 19:44:55 +00:00
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { DeviceNotificationRepository } from '@app/common/modules/device/repositories';
|
|
import { OneSignalService } from './onesignal.service';
|
|
|
|
@Injectable()
|
|
export class DeviceMessagesService {
|
|
constructor(
|
|
private readonly deviceNotificationRepository: DeviceNotificationRepository,
|
|
private readonly oneSignalService: OneSignalService,
|
|
) {}
|
|
|
|
async getDevicesUserNotifications(deviceTuyaUuid: string, bizData: any) {
|
|
try {
|
|
// Retrieve notifications for the specified device
|
|
const notifications = await this.deviceNotificationRepository.find({
|
|
where: {
|
|
device: {
|
|
deviceTuyaUuid,
|
|
},
|
|
},
|
|
relations: ['user', 'user.userNotification'],
|
|
});
|
|
|
|
// If notifications are found, send them
|
|
if (notifications) {
|
|
await this.sendNotifications(notifications, bizData);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching device notifications:', error);
|
|
}
|
|
}
|
|
|
|
private async sendNotifications(notifications: any[], bizData: any) {
|
|
const notificationPromises = [];
|
|
|
|
// Iterate over each notification and its associated user notifications
|
|
notifications.forEach((notification) => {
|
|
notification.user.userNotification.forEach((userNotification) => {
|
|
// Queue notification sending without awaiting
|
|
notificationPromises.push(
|
|
this.oneSignalService.sendNotification(
|
|
JSON.stringify(bizData),
|
|
'device-status',
|
|
[userNotification.subscriptionUuid],
|
|
),
|
|
);
|
|
});
|
|
});
|
|
|
|
// Wait for all notification sending operations to complete
|
|
await Promise.all(notificationPromises);
|
|
}
|
|
}
|