updated accounts service navigator

This commit is contained in:
2025-08-03 18:23:26 +03:00
parent 1b87dee60d
commit aa8f0b8f31
24 changed files with 398 additions and 211 deletions

View File

@@ -14,38 +14,47 @@ import {
} from '@nestjs/common';
import { AccountsService } from './accounts.service';
import { AuthControlGuard, EndpointControlGuard } from '../middleware/access-control.guard';
import { RedisHandlers } from '../utils/auth/redisHandlers';
@Controller('accounts')
export class AccountsController {
constructor(private accountsService: AccountsService) { }
constructor(private accountsService: AccountsService, private redisHandler: RedisHandlers) { }
@Get('events')
@HttpCode(200)
@UseGuards(AuthControlGuard, EndpointControlGuard)
@UseGuards(AuthControlGuard)
async getEvents(@Query() query: any) {
const { url, func } = query;
const events = await this.accountsService.infoEvents(url, func);
return {
events,
message: "Events fetched successfully",
const { userToken } = query;
const events = await this.accountsService.infoEvents(userToken)
try {
return { events, message: "Events fetched successfully" };
} catch (error) {
console.error('Error getting events:', error);
throw new ForbiddenException(`Error retrieving events. Please contact your system administrator.`);
}
}
@Post('filter')
@HttpCode(200)
@UseGuards(AuthControlGuard, EndpointControlGuard)
async filterAccounts(@Body() query: any, @Req() req: any) {
const driveToken = req.driveToken
const redirectToService = await this.accountsService.getEvents();
console.log('redirectToService', redirectToService);
// Get request drive token from acess control guard and retrieve related Service
const relatedService = this.accountsService.getService(req)
if (!relatedService) { throw new Error(`No service found for drive token: ${req.driveToken}`) }
try {
const functionToCall = redirectToService[driveToken];
// Get function mapper from related
if (!relatedService.mapper) { throw new Error(`Mapper in ${relatedService.constructor.name} is missing or null`) }
// Get redis select token object from redis
const selectObject = await this.redisHandler.getSelectFromRedis(req);
if (!selectObject) { throw new Error(`Select object is missing or null`) }
if (!selectObject.value.events) { throw new Error(`Events in select object is missing or null`) }
const eventKey = Object.entries(selectObject.value.events).filter((key) => key.includes(req.driveToken))[0]
if (!eventKey) { throw new Error(`No event is registered for this user ${req.driveToken}`) }
// Get function to call from related service mapper
const functionToCall = relatedService.mapper[eventKey.join(":")];
if (!functionToCall || typeof functionToCall !== 'function') { throw new Error(`No function found for drive token: ${req.driveToken}`); }
return await functionToCall(query);
} catch (error) {
console.error('Error redirecting to service:', error);
throw new ForbiddenException(`This user is not allowed to access this endpoint. Please contact your system administrator.`);
}
} catch (error) { throw new ForbiddenException(`This user is not allowed to access this endpoint. Please contact your system administrator.`) }
}
}