From 74a365b3cf2743d435828b1e5dd8785acf5fff7f Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Thu, 6 Jun 2024 10:26:34 +0100 Subject: [PATCH] add endpoint to get sharedCalendar --- src/app/pages/agenda/agenda.page.ts | 13 ++++- .../agenda-data-repository.service.spec.ts | 16 ----- .../Agenda/agenda-data-repository.service.ts | 21 ++++++- .../Agenda/agenda-data.service.spec.ts | 16 ----- .../Repositorys/Agenda/agenda-data.service.ts | 16 ++++- .../agenda-local-data-source.service.ts | 58 +++++++++++++++++++ .../Agenda/model/sharedCalendarOutputDTO.ts | 20 +++++++ src/app/services/agenda/list-box.service.ts | 1 - .../api-validate-schema.decorator.ts | 46 +++++++++++++++ 9 files changed, 169 insertions(+), 38 deletions(-) delete mode 100644 src/app/services/Repositorys/Agenda/agenda-data-repository.service.spec.ts delete mode 100644 src/app/services/Repositorys/Agenda/agenda-data.service.spec.ts create mode 100644 src/app/services/Repositorys/Agenda/agenda-local-data-source.service.ts create mode 100644 src/app/services/Repositorys/Agenda/model/sharedCalendarOutputDTO.ts create mode 100644 src/app/services/decorator/api-validate-schema.decorator.ts diff --git a/src/app/pages/agenda/agenda.page.ts b/src/app/pages/agenda/agenda.page.ts index 0d14e613f..e621e9902 100644 --- a/src/app/pages/agenda/agenda.page.ts +++ b/src/app/pages/agenda/agenda.page.ts @@ -185,9 +185,19 @@ export class AgendaPage implements OnInit { public ThemeService: ThemeService, public p: PermissionService, public RoleIdService: RoleIdService, - public AgendaDataRepositoryService: AgendaDataRepositoryService + public AgendaDataRepositoryService: AgendaDataRepositoryService, ) { + (async () => { + const result = await this.AgendaDataRepositoryService.getSharedCalendar() + + if(result.isOk()) { + console.log('111', result) + } else { + console.log('error') + } + })() + this.dateAdapter.setLocale('es'); this.locale = 'pt' @@ -779,7 +789,6 @@ export class AgendaPage implements OnInit { const selectedCalendarIds = this.removeDuplicatesByOwnerUserId(this.getSelectedAgendaCalendars()); this.listToPresent = this.CalendarStore.getEventsByCalendarIds(selectedCalendarIds) - console.log('this.listToPresent', JSON.stringify(this.listToPresent) ) const year = this.listBoxService.list(this.listToPresent, 'md', this.rangeStartDate, this.rangeEndDate, { segment: this.segment, selectedDate: this.eventSelectedDate }) this.TimelineMDList = year diff --git a/src/app/services/Repositorys/Agenda/agenda-data-repository.service.spec.ts b/src/app/services/Repositorys/Agenda/agenda-data-repository.service.spec.ts deleted file mode 100644 index ec3612d98..000000000 --- a/src/app/services/Repositorys/Agenda/agenda-data-repository.service.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; - -import { AgendaDataRepositoryService } from './agenda-data-repository.service'; - -describe('AgendaDataRepositoryService', () => { - let service: AgendaDataRepositoryService; - - beforeEach(() => { - TestBed.configureTestingModule({}); - service = TestBed.inject(AgendaDataRepositoryService); - }); - - it('should be created', () => { - expect(service).toBeTruthy(); - }); -}); diff --git a/src/app/services/Repositorys/Agenda/agenda-data-repository.service.ts b/src/app/services/Repositorys/Agenda/agenda-data-repository.service.ts index 5580f587e..235197471 100644 --- a/src/app/services/Repositorys/Agenda/agenda-data-repository.service.ts +++ b/src/app/services/Repositorys/Agenda/agenda-data-repository.service.ts @@ -10,6 +10,7 @@ import { EventListToApproveMapper } from './mapper/eventToApproveListMapper'; import { err, ok } from 'neverthrow'; import { HttpErrorResponse } from '@angular/common/http'; import { EventToApproveDetailsMapper } from './mapper/EventToApproveDetailsMapper'; +import { AgendaLocalDataSourceService } from './agenda-local-data-source.service'; @Injectable({ providedIn: 'root' @@ -18,7 +19,8 @@ export class AgendaDataRepositoryService { constructor( private agendaDataService: AgendaDataService, - private utils: Utils + private utils: Utils, + private agendaLocalDataSourceService: AgendaLocalDataSourceService ) { } async getEventById(id: string) { @@ -167,4 +169,21 @@ export class AgendaDataRepositoryService { getDocumentAttachments(applicationId,userId,subject,pageNumber,pageSize) { return this.agendaDataService.getDocumentAttachment(applicationId,userId,subject,pageNumber,pageSize) } + + async getSharedCalendar() { + + const result = await this.agendaDataService.getSharedCalendar() + + if(result.isOk()) { + await this.agendaLocalDataSourceService.clearSharedCalendar() + if(result.value?.data) { + return await this.agendaLocalDataSourceService.create(result.value.data) + } else { + return result + } + } else { + + } + + } } diff --git a/src/app/services/Repositorys/Agenda/agenda-data.service.spec.ts b/src/app/services/Repositorys/Agenda/agenda-data.service.spec.ts deleted file mode 100644 index 243cb2dde..000000000 --- a/src/app/services/Repositorys/Agenda/agenda-data.service.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; - -import { AgendaDataService } from './agenda-data.service'; - -describe('AgendaDataService', () => { - let service: AgendaDataService; - - beforeEach(() => { - TestBed.configureTestingModule({}); - service = TestBed.inject(AgendaDataService); - }); - - it('should be created', () => { - expect(service).toBeTruthy(); - }); -}); diff --git a/src/app/services/Repositorys/Agenda/agenda-data.service.ts b/src/app/services/Repositorys/Agenda/agenda-data.service.ts index 3931f05b1..0f63f907e 100644 --- a/src/app/services/Repositorys/Agenda/agenda-data.service.ts +++ b/src/app/services/Repositorys/Agenda/agenda-data.service.ts @@ -1,8 +1,11 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; -import { EventOutputDTO } from './model/eventDTOOutput'; import { EventInputDTO } from './model/eventInputDTO'; +import { SessionStore } from 'src/app/store/session.service'; +import { SharedCalendarListOutputDTO, SharedCalendarListOutputDTOSchema } from './model/sharedCalendarOutputDTO'; +import { HttpService } from '../../http.service'; +import { APIReturn } from '../../decorator/api-validate-schema.decorator'; @Injectable({ providedIn: 'root' @@ -11,7 +14,10 @@ import { EventInputDTO } from './model/eventInputDTO'; export class AgendaDataService { private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2'; // Your base URL - constructor(private http: HttpClient) { } + constructor( + private http: HttpClient, + private httpService: HttpService + ) { } getContacts(value: string): Observable { const params = new HttpParams().set('value', value); @@ -115,4 +121,10 @@ export class AgendaDataService { .set('PageSize', PageSize); return this.http.get(`${this.baseUrl}/Documents/Attachments${aplicationId}`, {params}); } + + + @APIReturn(SharedCalendarListOutputDTOSchema) + async getSharedCalendar() { + return await this.httpService.get(`${this.baseUrl}/Users/id/ShareCalendar?id=${SessionStore.user.UserId}`); + } } diff --git a/src/app/services/Repositorys/Agenda/agenda-local-data-source.service.ts b/src/app/services/Repositorys/Agenda/agenda-local-data-source.service.ts new file mode 100644 index 000000000..67dfebe8c --- /dev/null +++ b/src/app/services/Repositorys/Agenda/agenda-local-data-source.service.ts @@ -0,0 +1,58 @@ +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'; + + +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 + +// Database declaration (move this to its own module also) +export const AgendaDataSource = new Dexie('AgendaDataSource') as Dexie & { + shareCalendar: EntityTable; +}; + + +AgendaDataSource.version(1).stores({ + shareCalendar: 'wxUserId, wxFullName, wxeMail, role, roleId, shareType, startDate, endDate' +}); + +@Injectable({ + providedIn: 'root' +}) +export class AgendaLocalDataSourceService { + + constructor() { } + + async create(data: SharedCalendarListItemOutputDTO[]) { + // db.eve + try { + const result = AgendaDataSource.shareCalendar.bulkAdd(data) + return ok(result) + } catch (e) { + return err(false) + } + } + + + clearSharedCalendar() { + // db.eve + try { + const result = AgendaDataSource.shareCalendar.clear() + return ok(result) + } catch (e) { + return err(false) + } + + } +} diff --git a/src/app/services/Repositorys/Agenda/model/sharedCalendarOutputDTO.ts b/src/app/services/Repositorys/Agenda/model/sharedCalendarOutputDTO.ts new file mode 100644 index 000000000..69b875d1e --- /dev/null +++ b/src/app/services/Repositorys/Agenda/model/sharedCalendarOutputDTO.ts @@ -0,0 +1,20 @@ +import { z } from "zod"; + +const SharedCalendarListItemOutputDTOSchema = 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 const SharedCalendarListOutputDTOSchema = z.object({ + success: z.boolean(), + message: z.string(), + data: z.array(SharedCalendarListItemOutputDTOSchema), +}).nullable(); + +export type SharedCalendarListItemOutputDTO = z.infer; +export type SharedCalendarListOutputDTO = z.infer; diff --git a/src/app/services/agenda/list-box.service.ts b/src/app/services/agenda/list-box.service.ts index 5d880f711..a07a2c310 100644 --- a/src/app/services/agenda/list-box.service.ts +++ b/src/app/services/agenda/list-box.service.ts @@ -75,7 +75,6 @@ export class ListBoxService { // daysObject[day] = object[day] // } - console.log('newStracture', newStracture) return this.display(newStracture, selectedDate).year diff --git a/src/app/services/decorator/api-validate-schema.decorator.ts b/src/app/services/decorator/api-validate-schema.decorator.ts new file mode 100644 index 000000000..a9c9f5536 --- /dev/null +++ b/src/app/services/decorator/api-validate-schema.decorator.ts @@ -0,0 +1,46 @@ +import { HttpErrorResponse } from '@angular/common/http'; +import { Result, err } from 'neverthrow'; +import { z, ZodError } from 'zod'; +import * as Sentry from '@sentry/capacitor'; + +export function APIReturn(schema: z.ZodTypeAny) { + return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { + const originalMethod = descriptor.value; + + descriptor.value = async function (...args: any[]) { + const result: Result = await originalMethod.apply(this, args); + + if(result.isOk()) { + try { + // Validate the result using the provided schema + schema.parse(result.value); + + } catch (error) { + if (error instanceof ZodError) { + // If validation fails, throw an error with the details + // + console.log('unexpected data structure') + // Capture the Zod validation error with additional context + Sentry.withScope((scope) => { + scope.setTag('APIReturn', 'user'); + scope.setContext('data', { data: result.value }); + Sentry.captureException(error); + }); + console.error('Validation failed:', error.errors); + + } else { + console.log('failed to setup the connection successful') + // Throw any other unexpected errors + throw error; + } + } + } else { + console.log() + // result.error. + } + + return result; + } + return descriptor; + } +}