74 lines
4.2 KiB
TypeScript
74 lines
4.2 KiB
TypeScript
import { Controller, Post, NotFoundException, Body } from '@nestjs/common';
|
|
import { PrismaService } from '@/src/prisma.service';
|
|
import { EventsService } from '@/src/navigator/events/events.service';
|
|
import { MongoService } from '@/src/database/mongo/mongo.service';
|
|
import { UrlHandler } from '@/src/utils/navigator/urlHandler';
|
|
import { eventSetValidator, eventGetValidator } from './dtoValidator';
|
|
import { PagesService } from './pages/pages.service';
|
|
|
|
const tokens = { employeeTypeToken: "L9wBdwV9OlxsLAgh", occupantTypeToken: "j0adQOsJBR0xq24d" }
|
|
|
|
@Controller('navigator')
|
|
export class NavigatorController {
|
|
constructor(
|
|
private prismaService: PrismaService,
|
|
private eventService: EventsService,
|
|
private mongoService: MongoService,
|
|
private urlHandler: UrlHandler,
|
|
private pagesService: PagesService
|
|
) { }
|
|
|
|
@Post('event/set')
|
|
async setEvent(@Body() body: eventSetValidator) {
|
|
const user = await this.prismaService.users.findFirst({ where: { uu_id: body.usersUUID }, include: { people: true } });
|
|
if (!user) { throw new NotFoundException('User not found') }
|
|
const userType = await this.prismaService.user_types.findFirstOrThrow({ where: { token: body.event.token } })
|
|
const person = user.people[0]
|
|
const people2userType = await this.prismaService.employees.findFirstOrThrow({ where: { uu_id: person.uu_id, staff: { user_type_uu_id: userType.uu_id } } })
|
|
if (!people2userType) { throw new NotFoundException('User type not found') }
|
|
if (userType.type_token == tokens.employeeTypeToken) { await this.eventService.setEventsEmployees({ event: body.event, userUUID: body.usersUUID }) }
|
|
else if (userType.type_token == tokens.occupantTypeToken) { await this.eventService.setEventsOccupants({ event: body.event, userUUID: body.usersUUID }) }
|
|
else { throw new NotFoundException('User type not found') }
|
|
return body.event;
|
|
}
|
|
|
|
@Post('event/get')
|
|
async getEvent(@Body() body: eventGetValidator) {
|
|
const { typeToken, usersUUID } = body
|
|
const userType = await this.prismaService.user_types.findFirstOrThrow({ where: { token: typeToken } })
|
|
if (userType.type_token == tokens.employeeTypeToken) {
|
|
const allEvents = await this.eventService.getAllEventsEmployees(typeToken);
|
|
if (!allEvents) { throw new NotFoundException('Events not found') }
|
|
const selectedEvents = await this.eventService.getEventsEmployees(usersUUID);
|
|
const selectedEventsKeys = Object.values(selectedEvents || {}).map((value: any) => value.key) || [];
|
|
for (const event of allEvents) { if (selectedEventsKeys.includes(event.key)) { event.isSelected = true } else { event.isSelected = false } }
|
|
return { events: allEvents }
|
|
}
|
|
else if (userType.type_token == tokens.occupantTypeToken) {
|
|
const allEvents = await this.eventService.getAllEventsOccupants(typeToken);
|
|
if (!allEvents) { throw new NotFoundException('Events not found') }
|
|
const selectedEvents = await this.eventService.getEventsOccupants(usersUUID);
|
|
const selectedEventsKeys = Object.values(selectedEvents || {}).map((value: any) => value.key) || [];
|
|
for (const event of allEvents) { if (selectedEventsKeys.includes(event.key)) { event.isSelected = true } else { event.isSelected = false } }
|
|
return { events: allEvents }
|
|
} else { throw new NotFoundException('User type not found') }
|
|
}
|
|
|
|
@Post('page/set')
|
|
async setPage(@Body() body: { usersUUID: string, usersToken: string, url: string, page: Record<string, any> }) {
|
|
return await this.pagesService.setPageViaToken(body.usersUUID, body.usersToken, body.url, body.page)
|
|
}
|
|
|
|
@Post('page/get')
|
|
async getPage(@Body() body: { usersUUID: string, token: string, url?: string, skip?: number, limit?: number }) {
|
|
const pages = await this.pagesService.getPageViaToken(body.usersUUID, body.token, body.url, body.skip, body.limit)
|
|
return { pages }
|
|
}
|
|
|
|
@Post('page/configure')
|
|
async setPages(@Body() body: { chunkIndex: number; chunkCount: number; data: Record<string, any> }) {
|
|
return await this.pagesService.configurePages(body.data, body.chunkIndex, body.chunkCount)
|
|
}
|
|
|
|
}
|