2024-06-27 16:53:45 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { NotificationDataSource, NotificationTable, NotificationTableSchema } from '../infra/db/notification.db';
|
|
|
|
|
import { err, ok } from 'neverthrow';
|
|
|
|
|
import { from } from 'rxjs';
|
|
|
|
|
import { liveQuery } from 'Dexie';
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class LocalNotificationService {
|
|
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async addNotification(data: NotificationTable) {
|
|
|
|
|
// db.eve
|
|
|
|
|
try {
|
|
|
|
|
const result = await NotificationDataSource.notification.add(data)
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async updateNotification(data: NotificationTable) {
|
|
|
|
|
// db.eve
|
|
|
|
|
try {
|
|
|
|
|
const result = await NotificationDataSource.notification.update(data.notificationId, data)
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateNotifications(data: NotificationTable[]) {
|
|
|
|
|
// db.eve
|
|
|
|
|
try {
|
2024-06-28 07:44:43 +01:00
|
|
|
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))
|
2024-06-27 16:53:45 +01:00
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addNotifications(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;
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-28 07:44:43 +01:00
|
|
|
// 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;
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-27 16:53:45 +01:00
|
|
|
// Add valid notifications to the database
|
|
|
|
|
if (validNotifications.length > 0) {
|
|
|
|
|
await NotificationDataSource.notification.bulkAdd(validNotifications);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('No valid notifications to add.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log({failed})
|
|
|
|
|
|
|
|
|
|
return ok(failed)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getNotificationLive() {
|
|
|
|
|
return from(liveQuery( () => {
|
|
|
|
|
return NotificationDataSource.notification.orderBy('createdAt').reverse().toArray()
|
2024-06-28 07:44:43 +01:00
|
|
|
.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
|
|
|
|
|
})
|
2024-06-27 16:53:45 +01:00
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getNotification() {
|
|
|
|
|
return NotificationDataSource.notification.toArray()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
|
return NotificationDataSource.notification.clear()
|
|
|
|
|
}
|
|
|
|
|
}
|