Files
doneit-web/src/app/module/notification/data/notification-repository.service.ts
T
2024-06-27 16:53:45 +01:00

84 lines
2.6 KiB
TypeScript

import { Injectable } from '@angular/core';
import { NotificationInputDTO } from './dto/NotificationInputDTO';
import { RemoteNotificationService } from './datasource/remote-notification.service'
import { FirebasePushNotificationService } from './datasource/firebase-push-notification.service'
import { LocalNotificationService } from './datasource/local-notification.service'
import { SessionStore } from 'src/app/store/session.service';
import { NotificationListMapper } from '../domain/mapper/notificationListMapper';
import { NotificationListChanges } from './async/changes/notificationListChange';
import { NotificationTable } from './infra/db/notification.db';
@Injectable({
providedIn: 'root'
})
export class NotificationRepositoryService {
constructor(
private RemoteNotificationService: RemoteNotificationService,
private FirebasePushNotificationService: FirebasePushNotificationService,
private LocalNotificationService: LocalNotificationService
) {
this.FirebasePushNotificationService.onReceiveForeground(async (data)=> {
console.log('FirebasePushNotificationService', data)
this.init()
})
this.init()
}
async init() {
const result = await this.getNotification({
PageNumber: "1",
PageSize: "50",
userId: SessionStore.user.UserId.toString()
})
if(result.isOk()) {
console.log('notification-list', result.value.data.result)
if(result.value.data.result.length >= 1) {
const localList = await this.LocalNotificationService.getNotification()
const serverList = NotificationListMapper(result.value)
const { insert, update } = NotificationListChanges(localList, serverList)
console.log({insert, update})
this.LocalNotificationService.addNotifications(insert)
this.LocalNotificationService.updateNotifications(update)
// this.LocalNotificationService.addNotifications.
} else {
this.LocalNotificationService.clear()
}
}
}
async getNotification(queryParameter: NotificationInputDTO) {
return await this.RemoteNotificationService.getNotification(queryParameter)
}
getNotificationLive() {
return this.LocalNotificationService.getNotificationLive()
}
async notificationStatus(item: NotificationTable) {
await this.RemoteNotificationService.notificationStatus(item.notificationId)
item.status = true
this.LocalNotificationService.updateNotification(item)
this
this.init()
return
}
async localNotificationStatus(item: NotificationTable) {
item.status = true
this.LocalNotificationService.updateNotification(item)
this.init()
return
}
}