import { Body, Injectable, NotFoundException } from '@nestjs/common'; import { MongoService } from '@/src/database/mongo/mongo.service'; import { PrismaService } from '@/src/prisma.service'; import { EventsGetterValidator, EventsSetterValidator } from '@/src/navigator/events/dtoValidator'; import { Document } from 'mongodb'; type SearchType = 'value' | 'key' | 'both'; @Injectable() export class EventsService { // const result = await eventsService.getEventsOccupants({ // collectionName: 'Events:Build-UUIDv4', // regexKey: 'qt5P0xoeThjNT9EuWfwBgxsntHY5ydRtKFr1pgKGcgxx' // }); // const result = await eventsService.getEventsOccupants({ // collectionName: 'Events:Build-UUIDv4', // regexKey: 'e6hewIe7YqbQZHO3', // searchType: 'key' // }); // const result = await eventsService.getEventsOccupants({ // collectionName: 'Events:Build-UUIDv4', // regexKey: 'e6hewIe7YqbQZHO3', // searchType: 'both' // }); constructor(private mongoService: MongoService, private prisma: PrismaService) { } seperator = "/" private async getBuildUUID(uuid: string) { console.log('uuid', uuid) const livingSpace = await this.prisma.build_living_space.findFirstOrThrow({ where: { uu_id: uuid }, select: { people: { select: { users: { select: { uu_id: true } } } }, build_parts: { select: { build: { select: { uu_id: true } } } } } }); const userUUID = livingSpace.people.users[0].uu_id const buildUUID = livingSpace.build_parts.build.uu_id return { userUUID, buildUUID } } private async getCompanyUUID(uuid: string) { const employee = await this.prisma.employees.findFirstOrThrow({ where: { uu_id: uuid }, select: { people: { select: { users: { select: { uu_id: true } } } }, staff: { select: { duties: { select: { company_uu_id: true } } } } } }); const userUUID = employee.people?.users[0].uu_id const companyUUID = employee.staff?.duties.company_uu_id return { userUUID, companyUUID } } private validateCollectionName(collectionName: string) { if (!collectionName) { throw new NotFoundException('Collection name is required') } } private async setupMongoCollection(collectionName: string, buildUUID: string) { await this.mongoService.set(collectionName); await this.mongoService.set(`EVENTS${this.seperator}${buildUUID}`); } private escapeRegex(text: string): string { return text.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); } async getAllEventsOccupants(userTypeToken: string): Promise { await this.mongoService.set(`Events`) return await this.mongoService.findManyKeyWhere(userTypeToken) || null; } async getAllEventsEmployees(userTypeToken: string): Promise { await this.mongoService.set(`Events`) return await this.mongoService.findManyKeyWhere(userTypeToken) || null; } async getEventsOccupants(livingSpaceUUID: string) { const eventsObject = {} const { userUUID, buildUUID } = await this.getBuildUUID(livingSpaceUUID); const collectionKey = `Events/${buildUUID}` this.validateCollectionName(collectionKey); await this.mongoService.set(collectionKey) const eventsResponse = await this.mongoService.findOne({ [userUUID]: { $exists: true } }); if (eventsResponse && typeof eventsResponse === 'object') { const mapOfEvents = eventsResponse[userUUID]; if (mapOfEvents && typeof mapOfEvents === 'object') { const userTypeTokenKey = Object.keys(mapOfEvents)[0]; const userTypeTokenValue = mapOfEvents[userTypeTokenKey]; if (userTypeTokenValue && typeof userTypeTokenValue === 'object') { for (const siteUrlTokenKey of Object.keys(userTypeTokenValue)) { const siteUrlTokenValue = userTypeTokenValue[siteUrlTokenKey]; eventsObject[`${siteUrlTokenKey}:${userTypeTokenKey}`] = siteUrlTokenValue } } } } return eventsObject } async getEventsEmployees(employeeUUID: string) { const eventsObject = {} const companyUUID = await this.getCompanyUUID(employeeUUID); if (!companyUUID) { throw new NotFoundException('Company not found') } await this.mongoService.set(`EVENTS${this.seperator}${companyUUID}`); const eventsResponse = await this.mongoService.findOne({ [employeeUUID]: { $exists: true } }); if (eventsResponse && typeof eventsResponse === 'object') { const mapOfEvents = eventsResponse[employeeUUID]; if (mapOfEvents && typeof mapOfEvents === 'object') { const userTypeTokenKey = Object.keys(mapOfEvents)[0]; const userTypeTokenValue = mapOfEvents[userTypeTokenKey]; if (userTypeTokenValue && typeof userTypeTokenValue === 'object') { for (const siteUrlTokenKey of Object.keys(userTypeTokenValue)) { const siteUrlTokenValue = userTypeTokenValue[siteUrlTokenKey]; eventsObject[`${siteUrlTokenKey}:${userTypeTokenKey}`] = siteUrlTokenValue } } } } return eventsObject } private async setSavedEventToMapper(data: any, useruuid: string) { await this.mongoService.set(`MAP${this.seperator}EVENTS`); const events = await this.mongoService.findOrCreate({ uuid: `EVENTS${this.seperator}${useruuid}:${data.uuid}`, data }); } private async deleteSavedEventFromMapper(data: any, useruuid: string) { await this.mongoService.set(`MAP${this.seperator}EVENTS`); const events = await this.mongoService.deleteMany({ uuid: `EVENTS${this.seperator}${useruuid}:${data.uuid}` }); return events; } async setEventsEmployees(@Body() body: EventsSetterValidator) { const companyUUID = await this.getCompanyUUID(body.userUUID); if (!companyUUID) { throw new NotFoundException('Company not found') } await this.mongoService.set(`EVENTS${this.seperator}${companyUUID}`); const events = await this.mongoService.findOrCreate(body.event); // await this.setSavedEventToMapper(events, body.dutyUUID); return events; } async setEventsOccupants(@Body() body: EventsSetterValidator) { const buildUUID = await this.getBuildUUID(body.userUUID); if (!buildUUID) { throw new NotFoundException('Build not found') } await this.mongoService.set(`EVENTS${this.seperator}${buildUUID}`); const events = await this.mongoService.findOrCreate(body.event); return events; } async setEvents(events: any, serviceName: string) { await this.mongoService.set(`Events`); for (const [key, value] of Object.entries(events)) { const description = (value as Array)[0].endpoint || ""; console.log(`Setting events for ${serviceName} ${description} is carried to nosql database store.`) await this.mongoService.deleteMany({ [key]: { $exists: true } }); await this.mongoService.create({ [key]: value }); } } async deleteEventsEmployees(@Body() body: EventsGetterValidator) { const companyUUID = await this.getCompanyUUID(body.dutyUUID); if (!companyUUID) { throw new NotFoundException('Company not found') } await this.mongoService.set(`EVENTS${this.seperator}${companyUUID}`); const events = await this.mongoService.deleteMany({ uuid: { $regex: body.regexKey, $options: 'i' } }); return events; } async deleteEventsOccupants(@Body() body: EventsGetterValidator) { const buildUUID = await this.getBuildUUID(body.dutyUUID); if (!buildUUID) { throw new NotFoundException('Build not found') } await this.mongoService.set(`EVENTS${this.seperator}${buildUUID}`); const events = await this.mongoService.deleteMany({ uuid: { $regex: body.regexKey, $options: 'i' } }); return events; } }