diff --git a/src/app/pages/agenda/agenda.page.html b/src/app/pages/agenda/agenda.page.html index fe7a17da5..0dc0d4565 100644 --- a/src/app/pages/agenda/agenda.page.html +++ b/src/app/pages/agenda/agenda.page.html @@ -45,7 +45,7 @@ -
- - - + {{ selectedUserCalendar }} + + + + +
-
PR
-
MDGPR
-
Agenda do {{calendars.Fullname}}
-
- +
PR
+
MDGPR
+
Agenda do {{calendars.wxFullName}}
+
Minha agenda
-
{{ environment.agendaPR}}
-
{{ environment.agendaVP}}
-
Agenda do {{calendars.Fullname}}
-
Minha agenda
+
{{ environment.agendaPR}}
+
{{ environment.agendaVP}}
+
Agenda do {{calendars.wxFullName}}
+
Minha agenda
- + @@ -140,6 +140,8 @@ + +
-
+
{{ day.daysInfo.dayName }}
{{month.monthInfo.monthName}}
diff --git a/src/app/pages/agenda/agenda.page.ts b/src/app/pages/agenda/agenda.page.ts index e621e9902..2d0eb5503 100644 --- a/src/app/pages/agenda/agenda.page.ts +++ b/src/app/pages/agenda/agenda.page.ts @@ -35,6 +35,9 @@ import { environment } from 'src/environments/environment'; import { RoleIdService } from 'src/app/services/role-id.service' import { EventListStore } from 'src/app/models/agenda/AgendaEventList'; import { Cy } from 'cypress/enum' +import { Observable } from 'rxjs'; +import { TableSharedCalendar } from 'src/app/services/Repositorys/Agenda/agenda-local-data-source.service'; +import { map } from 'rxjs/operators'; @Component({ selector: 'app-agenda', templateUrl: './agenda.page.html', @@ -169,10 +172,12 @@ export class AgendaPage implements OnInit { SessionStore = SessionStore; environment = environment - CalendarName = '' + selectedUserCalendar: number | string = '' hasEventToday = false isSelectedDayHasEvent = true + sharedCalendar: Observable + constructor( @Inject(LOCALE_ID) private locale: string, private modalCtrl: ModalController, @@ -188,15 +193,25 @@ export class AgendaPage implements OnInit { public AgendaDataRepositoryService: AgendaDataRepositoryService, ) { - (async () => { - const result = await this.AgendaDataRepositoryService.getSharedCalendar() + this.AgendaDataRepositoryService.getSharedCalendar() + + // Define the role priorities + const rolePriorities: { [key: number]: number } = { + 100000014: 1, // Presidente da República + 100000011: 2, // Vice Presidente (example role ID) + // Add other roles with their priorities here + }; + + this.sharedCalendar = this.AgendaDataRepositoryService.getShareCalendarItemsLive().pipe( + map(data => data.sort((a, b) => { + console.log('Raw data:', data); // Debug line + const priorityA = rolePriorities[a.roleId] || Infinity; + const priorityB = rolePriorities[b.roleId] || Infinity; + return priorityA - priorityB; + })) + ) + - if(result.isOk()) { - console.log('111', result) - } else { - console.log('error') - } - })() this.dateAdapter.setLocale('es'); this.locale = 'pt' @@ -254,10 +269,10 @@ export class AgendaPage implements OnInit { this.weekToShow() - this.eventService.registerOnLoadCalendars(()=> { + this.eventService.registerOnLoadCalendars(async ()=> { this.setCalendarByDefault(true) - const selectedCalendarIds = this.getSelectedAgendaCalendars(); + const selectedCalendarIds = await this.getSelectedAgendaCalendars(); this.listToPresent = this.CalendarStore.getEventsByCalendarIds(selectedCalendarIds) this.reloadCalendar() @@ -283,8 +298,11 @@ export class AgendaPage implements OnInit { ngOnInit() { this.setCalendarByDefault(false) - const selectedCalendarIds = this.getSelectedAgendaCalendars(); - this.listToPresent = this.CalendarStore.getEventsByCalendarIds(selectedCalendarIds) + this.getSelectedAgendaCalendars().then((selectedCalendarIds) => { + + this.listToPresent = this.CalendarStore.getEventsByCalendarIds(selectedCalendarIds) + + }); setTimeout(() => { const pathname = window.location.pathname @@ -360,20 +378,18 @@ export class AgendaPage implements OnInit { }, 250) } - setCalendarByDefault(force) { - if (!this.CalendarName || force) { - const prObject = this.eventService.calendarNamesAry.find(e => e?.RoleId == 100000014) + async setCalendarByDefault(force) { + if (!this.selectedUserCalendar || force) { + const data = await this.AgendaDataRepositoryService.geCalendars() + + const prObject = data.find(e => e?.roleId == 100000014) if(prObject) { - console.log({prObject}) - this.CalendarName = prObject.Fullname - } else if (this.eventService.calendarNamesAry.includes('Meu calendario')) { - this.CalendarName = 'Meu calendario'; + this.selectedUserCalendar = prObject.wxUserId } else { - this.CalendarName = this.eventService.calendarNamesAry[0] + this.selectedUserCalendar = SessionStore.user.UserId } } - } //Go to the next view of the calendar month/week/day @@ -628,10 +644,10 @@ export class AgendaPage implements OnInit { removeDuplicatesByOwnerUserId = (array) => { const seen = new Set(); return array.filter(item => { - if (seen.has(item.OwnerUserId)) { + if (seen.has(item.wxUserId)) { return false; } else { - seen.add(item.OwnerUserId); + seen.add(item.wxUserId); return true; } }); @@ -646,9 +662,16 @@ export class AgendaPage implements OnInit { this.rangeStartDate = startTime this.rangeEndDate = endTime + + this.showLoader = true; - const selectedCalendarIds = this.removeDuplicatesByOwnerUserId(this.getSelectedAgendaCalendars()); + const selectedCalendarIds = await this.getSelectedAgendaCalendars() + + console.log({selectedCalendarIds}) + this.listToPresent = this.CalendarStore.getEventsByCalendarIds(selectedCalendarIds) + + console.log('listToPresent', this.listToPresent) // this.calendar.currentDate.setDate(1); this.updateEventListBox() @@ -663,8 +686,8 @@ export class AgendaPage implements OnInit { for (const selectedCalendar of selectedCalendarIds) { const response = await this.AgendaDataRepositoryService.EventList({ - userId: selectedCalendar.OwnerUserId, - calendarOwnerName: selectedCalendar['FullName'], + userId: selectedCalendar.wxUserId, + calendarOwnerName: selectedCalendar.wxFullName, endDate: endTime.toISOString(), startDate: startTime.toISOString() }) @@ -674,16 +697,16 @@ export class AgendaPage implements OnInit { if (SessionStore.user.Profile == 'PR') { label = "pr" - } else if (selectedCalendar.OwnerUserId == SessionStore.user.UserId as any) { + } else if (selectedCalendar.wxUserId == SessionStore.user.UserId as any) { label = 'md' } else { label = "pr" } - this.CalendarStore.removeRangeForCalendar(startTime, endTime, null , selectedCalendar.OwnerUserId as any) + this.CalendarStore.removeRangeForCalendar(startTime, endTime, null , selectedCalendar.wxUserId as any) let eventsList = response.value; - this.CalendarStore.pushEvent(eventsList, label, selectedCalendar.OwnerUserId); + this.CalendarStore.pushEvent(eventsList, label, selectedCalendar.wxUserId); this.listToPresent = this.CalendarStore.getEventsByCalendarIds(selectedCalendarIds) @@ -716,77 +739,33 @@ export class AgendaPage implements OnInit { selectedAgenda = {} - getSelectedAgendaCalendars(): any { + async getSelectedAgendaCalendars() { - if (this.CalendarName == 'PR+MDGPR') { + if (this.selectedUserCalendar == 'PR+MDGPR') { - const pr = this.SessionStore.user.SharedCalendars.filter(e => e.CalendarRoleId == this.RoleIdService.PRES.toString()) + const data = await this.AgendaDataRepositoryService.geCalendars() + + const prObject = data.find(e => e?.roleId == 100000014) return[{ ...this.eventService.calendarNamesType['Meu calendario'], - OwnerUserId: this.SessionStore.user.UserId + wxUserId: this.SessionStore.user.UserId }, { - ...pr[0] + ...prObject }] } else { - const calendar = this.eventService.calendarNamesType[this.CalendarName]; - let Oficial = calendar?.['OficialId'] - let Pessoal = calendar?.['PessoalId'] - - if (Oficial && Pessoal) { - - return [ - { - OwnerUserId: calendar.OwnerId, - CalendarId: Oficial, - OwnerId: calendar.OwnerId, - CalendarName: calendar.CalendarName, - FullName: calendar.FullName - }, - { - OwnerUserId: calendar.OwnerId, - OwnerId: calendar.OwnerId, - CalendarId: Pessoal, - CalendarName: calendar.CalendarName, - FullName: calendar.FullName - } - ] - - } else if (Oficial) { - try { - return [{ - OwnerUserId: calendar.OwnerId, - OwnerId: calendar.OwnerId, - CalendarId: Oficial, - CalendarName: calendar.CalendarName, - FullName: calendar.FullName - }] - } catch (error) { - console.log(error) - } - } else { - try { - return [{ - OwnerUserId: calendar.OwnerId, - OwnerId: calendar.OwnerId, - CalendarId: Pessoal, - CalendarName: calendar.CalendarName, - FullName: calendar.FullName - }] - } catch (error) { - console.log(error) - } - } + const data = await this.AgendaDataRepositoryService.geCalendars() + return [data.find(e => e?.wxUserId == this.selectedUserCalendar)] } } - updateEventListBox() { + async updateEventListBox() { - const selectedCalendarIds = this.removeDuplicatesByOwnerUserId(this.getSelectedAgendaCalendars()); + const selectedCalendarIds = await this.getSelectedAgendaCalendars() this.listToPresent = this.CalendarStore.getEventsByCalendarIds(selectedCalendarIds) const year = this.listBoxService.list(this.listToPresent, 'md', this.rangeStartDate, this.rangeEndDate, { segment: this.segment, selectedDate: this.eventSelectedDate }) 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 235197471..ff23afd6d 100644 --- a/src/app/services/Repositorys/Agenda/agenda-data-repository.service.ts +++ b/src/app/services/Repositorys/Agenda/agenda-data-repository.service.ts @@ -177,7 +177,7 @@ export class AgendaDataRepositoryService { if(result.isOk()) { await this.agendaLocalDataSourceService.clearSharedCalendar() if(result.value?.data) { - return await this.agendaLocalDataSourceService.create(result.value.data) + return await this.agendaLocalDataSourceService.bulkCreate(result.value.data) } else { return result } @@ -186,4 +186,12 @@ export class AgendaDataRepositoryService { } } + + getShareCalendarItemsLive() { + return this.agendaLocalDataSourceService.getShareCalendarItemsLive() + } + + async geCalendars() { + return await this.agendaLocalDataSourceService.geCalendars() + } } 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 index 67dfebe8c..6c8273eb9 100644 --- a/src/app/services/Repositorys/Agenda/agenda-local-data-source.service.ts +++ b/src/app/services/Repositorys/Agenda/agenda-local-data-source.service.ts @@ -4,6 +4,7 @@ import { Dexie, EntityTable, liveQuery } from 'Dexie'; import { SharedCalendarListItemOutputDTO, SharedCalendarListOutputDTO } from './model/sharedCalendarOutputDTO'; import { any, z } from 'zod'; import { err, ok } from 'neverthrow'; +import { from } from 'rxjs'; const tableScharedCalendar = z.object({ @@ -34,17 +35,16 @@ export class AgendaLocalDataSourceService { constructor() { } - async create(data: SharedCalendarListItemOutputDTO[]) { + async bulkCreate(data: SharedCalendarListItemOutputDTO[]) { // db.eve try { - const result = AgendaDataSource.shareCalendar.bulkAdd(data) + const result = await AgendaDataSource.shareCalendar.bulkAdd(data) return ok(result) } catch (e) { return err(false) } } - clearSharedCalendar() { // db.eve try { @@ -53,6 +53,15 @@ export class AgendaLocalDataSourceService { } catch (e) { return err(false) } + } + async geCalendars() { + return await AgendaDataSource.shareCalendar.toArray() + } + + getShareCalendarItemsLive() { + return from(liveQuery( () => { + return AgendaDataSource.shareCalendar.toArray() + })) } } diff --git a/src/app/services/agenda/list-box.service.ts b/src/app/services/agenda/list-box.service.ts index a07a2c310..5d880f711 100644 --- a/src/app/services/agenda/list-box.service.ts +++ b/src/app/services/agenda/list-box.service.ts @@ -75,6 +75,7 @@ export class ListBoxService { // daysObject[day] = object[day] // } + console.log('newStracture', newStracture) return this.display(newStracture, selectedDate).year diff --git a/src/app/services/role-id.service.ts b/src/app/services/role-id.service.ts index 3d34116ad..4465f316a 100644 --- a/src/app/services/role-id.service.ts +++ b/src/app/services/role-id.service.ts @@ -11,6 +11,7 @@ export class RoleIdService { * @description President role id */ PV: number = 100000011 + MD: number = 100000011 /** * @description Vice president role id */ diff --git a/src/app/store/calendar.service.ts b/src/app/store/calendar.service.ts index d1b0014c2..f7a226335 100644 --- a/src/app/store/calendar.service.ts +++ b/src/app/store/calendar.service.ts @@ -109,11 +109,11 @@ export class CalendarService { }, 10) } - getEventsByCalendarIds(ids:Object[]): EventListStore[] { + getEventsByCalendarIds(ids:any[]): EventListStore[] { let result = []; for (const calendar of ids) { - const events = this._eventSource.filter(e => e.CalendarId == calendar['OwnerUserId']) + const events = this._eventSource.filter(e => e.CalendarId == calendar.wxUserId ) result = result.concat(events) }