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
})
}))
}