2024-06-06 10:26:34 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
|
|
import { Dexie, EntityTable, liveQuery } from 'Dexie';
|
|
|
|
|
import { SharedCalendarListItemOutputDTO, SharedCalendarListOutputDTO } from './model/sharedCalendarOutputDTO';
|
|
|
|
|
import { any, z } from 'zod';
|
|
|
|
|
import { err, ok } from 'neverthrow';
|
2024-06-06 15:13:03 +01:00
|
|
|
import { from } from 'rxjs';
|
2024-06-06 10:26:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const tableScharedCalendar = z.object({
|
|
|
|
|
wxUserId: z.number(),
|
|
|
|
|
wxFullName: z.string(),
|
|
|
|
|
wxeMail: z.string().email(),
|
|
|
|
|
role: z.string(),
|
|
|
|
|
roleId: z.number(),
|
|
|
|
|
shareType: z.number(),
|
|
|
|
|
date: z.string(),
|
|
|
|
|
})
|
|
|
|
|
export type TableSharedCalendar = z.infer<typeof tableScharedCalendar>
|
|
|
|
|
|
|
|
|
|
// Database declaration (move this to its own module also)
|
|
|
|
|
export const AgendaDataSource = new Dexie('AgendaDataSource') as Dexie & {
|
|
|
|
|
shareCalendar: EntityTable<TableSharedCalendar, 'wxUserId'>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AgendaDataSource.version(1).stores({
|
|
|
|
|
shareCalendar: 'wxUserId, wxFullName, wxeMail, role, roleId, shareType, startDate, endDate'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class AgendaLocalDataSourceService {
|
|
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
2024-06-06 15:13:03 +01:00
|
|
|
async bulkCreate(data: SharedCalendarListItemOutputDTO[]) {
|
2024-06-06 10:26:34 +01:00
|
|
|
// db.eve
|
|
|
|
|
try {
|
2024-06-06 15:13:03 +01:00
|
|
|
const result = await AgendaDataSource.shareCalendar.bulkAdd(data)
|
2024-06-06 10:26:34 +01:00
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 13:16:19 +01:00
|
|
|
async createCalendar(data: SharedCalendarListItemOutputDTO) {
|
|
|
|
|
// db.eve
|
|
|
|
|
try {
|
|
|
|
|
const result = await AgendaDataSource.shareCalendar.add(data)
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 10:26:34 +01:00
|
|
|
clearSharedCalendar() {
|
|
|
|
|
// db.eve
|
|
|
|
|
try {
|
|
|
|
|
const result = AgendaDataSource.shareCalendar.clear()
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
2024-06-06 15:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async geCalendars() {
|
|
|
|
|
return await AgendaDataSource.shareCalendar.toArray()
|
|
|
|
|
}
|
2024-06-06 10:26:34 +01:00
|
|
|
|
2024-06-06 15:13:03 +01:00
|
|
|
getShareCalendarItemsLive() {
|
|
|
|
|
return from(liveQuery( () => {
|
|
|
|
|
return AgendaDataSource.shareCalendar.toArray()
|
|
|
|
|
}))
|
2024-06-06 10:26:34 +01:00
|
|
|
}
|
|
|
|
|
}
|