updated event controllers and service event mtach tested
This commit is contained in:
@@ -8,53 +8,36 @@ import {
|
||||
Body,
|
||||
HttpCode,
|
||||
UseGuards,
|
||||
ForbiddenException,
|
||||
NotFoundException,
|
||||
Req,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { AccountsService } from './accounts.service';
|
||||
import { AuthControlGuard, EndpointControlGuard } from '../middleware/access-control.guard';
|
||||
import { RedisHandlers } from '../utils/auth/redisHandlers';
|
||||
import { AuthControlGuard, EndpointControlGuard } from '@/src/middleware/access-control.guard';
|
||||
import { Navigator } from '@/src/utils/navigator/navigator';
|
||||
|
||||
@Controller('accounts')
|
||||
export class AccountsController {
|
||||
|
||||
constructor(private accountsService: AccountsService, private redisHandler: RedisHandlers) { }
|
||||
constructor(
|
||||
private accountsService: AccountsService,
|
||||
private navigator: Navigator
|
||||
) { }
|
||||
|
||||
@Get('events')
|
||||
@HttpCode(200)
|
||||
@UseGuards(AuthControlGuard)
|
||||
async getEvents(@Query() query: any) {
|
||||
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.`);
|
||||
}
|
||||
if (!userToken) { throw new NotFoundException('User token is missing or null') }
|
||||
const events = await this.navigator.getInfos(this.accountsService, userToken)
|
||||
return { events, message: "Events fetched successfully" };
|
||||
}
|
||||
|
||||
@Post('filter')
|
||||
@HttpCode(200)
|
||||
@UseGuards(AuthControlGuard, EndpointControlGuard)
|
||||
async filterAccounts(@Body() query: any, @Req() req: any) {
|
||||
// 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 {
|
||||
// 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) { throw new ForbiddenException(`This user is not allowed to access this endpoint. Please contact your system administrator.`) }
|
||||
return await this.navigator.getFunction(req, this.accountsService.mapper, query)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user