mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
87 lines
2.1 KiB
TypeScript
87 lines
2.1 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.bulkUpdate(data.map(e => ({
|
||
|
|
key: e.notificationId,
|
||
|
|
changes: { status: e.status }
|
||
|
|
})))
|
||
|
|
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) {
|
||
|
|
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()
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
async getNotification() {
|
||
|
|
return NotificationDataSource.notification.toArray()
|
||
|
|
}
|
||
|
|
|
||
|
|
clear() {
|
||
|
|
return NotificationDataSource.notification.clear()
|
||
|
|
}
|
||
|
|
}
|