Files
doneit-web/src/app/services/Repositorys/Agenda/agenda-data-repository.service.ts
T
2024-06-07 13:37:06 +01:00

221 lines
6.7 KiB
TypeScript

import { Injectable } from '@angular/core';
import { AgendaDataService } from './agenda-data.service';
import { map } from 'rxjs/operators';
import { ListEventMapper } from './mapper/EventListMapper';
import { EventMapper } from './mapper/EventDetailsMapper';
import { Utils } from './utils';
import { Event } from 'src/app/models/event.model';
import { SessionStore } from 'src/app/store/session.service';
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'
})
export class AgendaDataRepositoryService {
constructor(
private agendaDataService: AgendaDataService,
private utils: Utils,
private agendaLocalDataSourceService: AgendaLocalDataSourceService
) {}
createOwnCalendar() {
const currentUserCalendar = {
wxUserId: SessionStore.user.UserId,
wxFullName: SessionStore.user.FullName,
wxeMail: SessionStore.user.Email,
role: SessionStore.user.RoleDescription,
roleId: SessionStore.user.RoleID,
shareType: 0,
date: '',
}
return this.agendaLocalDataSourceService.createCalendar(currentUserCalendar)
}
async getEventById(id: string) {
try {
const result = await this.agendaDataService.getEvent(id).pipe(
map((response) => {
console.log('Response',response.data)
console.log('Output',EventMapper.toDomain(response.data))
return EventMapper.toDomain(response.data)
})
).toPromise()
return ok (result)
} catch (e) {
return err(e as HttpErrorResponse)
}
}
async getEventToApproveById(id: string) {
try {
const result = await this.agendaDataService.getEvent(id).pipe(
map((response) => {
return EventToApproveDetailsMapper.toDomain(response.data)
})
).toPromise()
return ok (result)
} catch (e) {
return err(e as HttpErrorResponse)
}
}
async EventList({userId, startDate , endDate , status= 2, category= null, type= null, calendarOwnerName = ''}) {
try {
const result = await this.agendaDataService.getEvents(userId, startDate, endDate, status, category, type).pipe(
map((response) => {
return ListEventMapper.toDomain(response.data, calendarOwnerName, userId)
}
)).toPromise()
return ok (result)
} catch (e) {
return err(e as HttpErrorResponse)
}
}
async eventToApproveList({userId, startDate = null, endDate = null, status = 0, category= null, type= null, calendarOwnerName = ''}) {
try {
const result = await this.agendaDataService.getEvents(userId, startDate = null, endDate = null, status, category= null, type= null).pipe(
map((response) => {
return EventListToApproveMapper.toDomain(response.data, calendarOwnerName, userId)
}
)).toPromise()
return ok (result)
} catch (e) {
return err(e as HttpErrorResponse)
}
}
createEvent(eventData: Event, CalendarName, documents) {
console.log(eventData)
let eventInput = {
userId: this.utils.selectedCalendarUserId(CalendarName, eventData) as any,
ownerType: this.utils.selectedCalendarOwner(CalendarName),
subject: eventData.Subject,
body: eventData.Body.Text,
location: eventData.Location,
startDate: eventData.StartDate.toISOString(),
endDate: eventData.EndDate.toISOString(),
type: this.utils.calendarTypeSeleted(eventData.Category),
category: this.utils.calendarCategorySeleted(eventData.CalendarName),
attendees: this.utils.attendeesAdded(eventData.Attendees),
attachments: this.utils.documentAdded(documents),
recurrence: {
frequency: 0,
occurrences: 0,
},
organizerId: SessionStore.user.UserId,
isAllDayEvent: eventData.IsAllDayEvent,
}
return this.agendaDataService.createEvent(eventInput)
}
updateEvent(eventId, eventData) {
console.log(eventData.StartDate)
console.log(eventData.EndDate)
let eventInput = {
subject: eventData.Subject,
body: eventData.Body.Text || eventData.Body,
location: eventData.Location,
startDate: eventData.StartDate,
endDate: eventData.EndDate,
isAllDayEvent: eventData.IsAllDayEvent,
updateAllEvents: false,
recurrence: {
frequency: 0,
occurrences: 0
}
}
return this.agendaDataService.updateEvent(eventId, eventInput)
}
addEventAttendee(id,attendeeData,) {
console.log(attendeeData)
console.log(this.utils.attendeesEdit(attendeeData))
return this.agendaDataService.addEventAttendee(id,{ attendees: this.utils.attendeesAdded(attendeeData) });
}
addEventAttachment(id,attachmentData) {
console.log(attachmentData)
console.log('post attachment',this.utils.documentAdded(attachmentData))
return this.agendaDataService.addEventAttachment(id,{ attachments: this.utils.documentAdded(attachmentData) });
}
deleteEvent(eventId) {
return this.agendaDataService.deleteEvent(eventId, false)
}
removeEventAttachment(eventId,attachmentData) {
return this.agendaDataService.removeEventAttachment(eventId,attachmentData);
}
async deleteEvent1(eventId) {
try {
const result = await this.agendaDataService.deleteEvent(eventId,false).toPromise()
return ok (result)
} catch (e) {
return err(e as HttpErrorResponse)
}
}
eventToaprovalStatus(eventId, status) {
let statusObject = {
status: this.utils.statusEventAproval(status),
comment: ""
}
return this.agendaDataService.updateEventStatus(eventId,statusObject)
}
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()) {
if(result.value?.data) {
await this.agendaLocalDataSourceService.clearSharedCalendar()
await this.createOwnCalendar()
return await this.agendaLocalDataSourceService.bulkCreate(result.value.data)
} else {
await this.createOwnCalendar()
return result
}
} else {
}
}
async clearSharedCalendar() {
return await this.agendaLocalDataSourceService.clearSharedCalendar()
}
getShareCalendarItemsLive() {
return this.agendaLocalDataSourceService.getShareCalendarItemsLive()
}
async geCalendars() {
return await this.agendaLocalDataSourceService.geCalendars()
}
}