fix notification

This commit is contained in:
Peter Maquiran
2024-06-28 07:44:43 +01:00
parent ac0ead4885
commit 0281db0d83
18 changed files with 243 additions and 218 deletions
@@ -36,10 +36,18 @@ export class LocalNotificationService {
async updateNotifications(data: NotificationTable[]) {
// db.eve
try {
const result = await NotificationDataSource.notification.bulkUpdate(data.map(e => ({
key: e.notificationId,
changes: { status: e.status }
})))
const result = await NotificationDataSource.notification.bulkDelete(data.map(e => e.notificationId))
return ok(result)
} catch (e) {
return err(false)
}
}
async deleteNotifications(data: NotificationTable[]) {
// db.eve
try {
const result = await NotificationDataSource.notification.bulkDelete(data.map(e => e.notificationId))
return ok(result)
} catch (e) {
return err(false)
@@ -58,6 +66,46 @@ export class LocalNotificationService {
return result.success;
});
// Add valid notifications to the database
if (validNotifications.length > 0) {
} else {
console.log('No valid notifications to add.');
}
console.log({failed})
return ok(failed)
}
async clearAndAddRecords(notifications: NotificationTable[]) {
try {
await NotificationDataSource.transaction('rw', NotificationDataSource.notification, async () => {
// Clear existing records from myTable
await NotificationDataSource.notification.clear();
await NotificationDataSource.notification.bulkAdd(notifications);
});
console.log('Clear and add operations completed within transaction.');
} catch (error) {
console.error('Error performing transaction:', error, notifications);
}
}
async addRemove(notifications: NotificationTable[]) {
// Validate each notification
const failed = []
const validNotifications = notifications.filter(notification => {
const result = NotificationTableSchema.safeParse(notification);
if(!result.success) {
failed.push(notification)
}
return result.success;
});
// Add valid notifications to the database
if (validNotifications.length > 0) {
await NotificationDataSource.notification.bulkAdd(validNotifications);
@@ -73,6 +121,19 @@ export class LocalNotificationService {
getNotificationLive() {
return from(liveQuery( () => {
return NotificationDataSource.notification.orderBy('createdAt').reverse().toArray()
.then(notifications => {
return notifications.filter(notification => notification.status === false)
})
}))
}
getNotificationLiveCount() {
return from(liveQuery( () => {
return NotificationDataSource.notification
.toArray()
.then(notifications => {
return notifications.filter(notification => notification.status === false).length
})
}))
}
@@ -21,5 +21,5 @@ export const NotificationDataSource = new Dexie('NotificationDataSource') as Dex
};
NotificationDataSource.version(1).stores({
notification: 'notificationId, title, service, object, idObject, folderId, dateInit, dateEnd, location, createdAt'
notification: 'notificationId, title, service, object, idObject, folderId, dateInit, dateEnd, location, createdAt, status'
});
@@ -5,7 +5,6 @@ import { FirebasePushNotificationService } from './datasource/firebase-push-noti
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({
@@ -27,32 +26,33 @@ export class NotificationRepositoryService {
})
this.init()
if(SessionStore.user.UserId) {
this.init()
}
}
async init() {
const result = await this.getNotification({
PageNumber: "1",
PageSize: "50",
PageSize: "100",
userId: SessionStore.user.UserId.toString()
})
if(result.isOk()) {
console.log('notification-list', result.value.data.result)
// console.log('notification-list', result.value.data.result)
const serverListFiltered = result.value.data.result.filter( e => e.body != 'Foi editado um evento na vossa agenda' && e.body != 'Foi apagado um evento na vossa agenda')
result.value.data.result = serverListFiltered
if(result.value.data.result.length >= 1) {
const localList = await this.LocalNotificationService.getNotification()
const serverList = NotificationListMapper(result.value)
const { insert, update } = NotificationListChanges(localList, serverList)
// const localList = await this.LocalNotificationService.getNotification()
// const serverList = NotificationListMapper(result.value)
// const { insert, update, remove } = NotificationListChanges(localList, serverList)
console.log({insert, update})
this.LocalNotificationService.addNotifications(insert)
this.LocalNotificationService.updateNotifications(update)
// this.LocalNotificationService.addNotifications.
this.LocalNotificationService.clearAndAddRecords(NotificationListMapper(result.value))
} else {
this.LocalNotificationService.clear()
}
@@ -68,6 +68,10 @@ export class NotificationRepositoryService {
return this.LocalNotificationService.getNotificationLive()
}
getNotificationLiveCount() {
return this.LocalNotificationService.getNotificationLiveCount()
}
async notificationStatus(item: NotificationTable) {
await this.RemoteNotificationService.notificationStatus(item.notificationId)
item.status = true
@@ -92,4 +96,9 @@ export class NotificationRepositoryService {
this.init()
return
}
clearData() {
return this.LocalNotificationService.clear()
}
}