2024-06-06 10:26:34 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
|
|
import { Dexie, EntityTable, liveQuery } from 'Dexie';
|
|
|
|
|
import { any, z } from 'zod';
|
|
|
|
|
import { err, ok } from 'neverthrow';
|
2024-06-06 15:13:03 +01:00
|
|
|
import { from } from 'rxjs';
|
2024-07-31 11:29:26 +01:00
|
|
|
import { SharedCalendarListItemOutputDTO } from '../dto/sharedCalendarOutputDTO';
|
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-07-02 14:15:06 +01:00
|
|
|
async clearAndAddRecords(data: SharedCalendarListItemOutputDTO[]) {
|
|
|
|
|
try {
|
|
|
|
|
await AgendaDataSource.transaction('rw', AgendaDataSource.shareCalendar, async () => {
|
|
|
|
|
// Clear existing records from myTable
|
|
|
|
|
await AgendaDataSource.shareCalendar.clear();
|
|
|
|
|
|
|
|
|
|
await AgendaDataSource.shareCalendar.bulkAdd(data);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
console.log('Clear and add operations completed within transaction.');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error performing transaction:', error, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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-18 12:03:38 +01:00
|
|
|
async removeCalendar(data: SharedCalendarListItemOutputDTO) {
|
|
|
|
|
// db.eve
|
|
|
|
|
try {
|
|
|
|
|
const result = await AgendaDataSource.shareCalendar.delete(data.wxUserId)
|
|
|
|
|
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
|
|
|
}
|
2024-06-21 13:45:06 +01:00
|
|
|
|
|
|
|
|
// New method to get calendars by wxUserId
|
|
|
|
|
async getCalendarByUserId(wxUserId: number) {
|
|
|
|
|
try {
|
|
|
|
|
const result = await AgendaDataSource.shareCalendar.get(wxUserId)
|
|
|
|
|
if(!result) {
|
|
|
|
|
const list = await AgendaDataSource.shareCalendar.toArray()
|
|
|
|
|
const found = list.find(e => e.wxUserId == wxUserId)
|
|
|
|
|
if(found) {
|
|
|
|
|
return ok(found)
|
|
|
|
|
} else {
|
|
|
|
|
return err('404')
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return ok(result)
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-06 10:26:34 +01:00
|
|
|
}
|