Files
doneit-web/src/app/module/notification/data/datasource/local-notification.service.ts
T
2024-12-16 12:04:02 +01:00

148 lines
3.8 KiB
TypeScript

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 {
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)
}
}
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;
});
// 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);
} 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()
.then(notifications => {
return notifications.filter(notification => notification.viewDate == null)
})
}))
}
getNotificationLiveCount() {
return from(liveQuery( () => {
return NotificationDataSource.notification
.toArray()
.then(notifications => {
return notifications.filter(notification => notification.viewDate == null).length
})
}))
}
async getNotification() {
return NotificationDataSource.notification.toArray()
}
clear() {
return NotificationDataSource.notification.clear()
}
}