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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,24 +2,26 @@ import { Module } from '@nestjs/common';
|
||||
import { AccountsService } from './accounts.service';
|
||||
import { AccountsController } from './accounts.controller';
|
||||
import { PrismaModule } from '@/prisma/prisma.module';
|
||||
import { CacheService } from '../cache.service';
|
||||
import { CacheService } from '../database/redis/redis.service';
|
||||
import { UtilsModule } from '../utils/utils.module';
|
||||
import { RedisModule } from '../database/redis/redis.module';
|
||||
import {
|
||||
AuthControlGuard,
|
||||
EndpointControlGuard,
|
||||
} from '@/src/middleware/access-control.guard';
|
||||
import { SuperUsersService } from './superusers/superusers.service';
|
||||
import { UrlHandler } from '../utils/auth/urlHandler';
|
||||
import { UrlHandler } from '../utils/navigator/urlHandler';
|
||||
import { Navigator } from '@/src/utils/navigator/navigator';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, UtilsModule],
|
||||
imports: [PrismaModule, UtilsModule, RedisModule],
|
||||
providers: [
|
||||
AccountsService,
|
||||
CacheService,
|
||||
AuthControlGuard,
|
||||
EndpointControlGuard,
|
||||
SuperUsersService,
|
||||
UrlHandler,
|
||||
Navigator,
|
||||
],
|
||||
controllers: [AccountsController],
|
||||
})
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PaginationInfo } from '../utils/pagination-helper';
|
||||
import { SuperUsersService } from './superusers/superusers.service';
|
||||
|
||||
@Injectable()
|
||||
@@ -10,24 +9,7 @@ export class AccountsService {
|
||||
private superUsersService: SuperUsersService,
|
||||
) {
|
||||
this.mapper = {
|
||||
"j0adQOsJBR0xq24dxLKdDU9EQRmt4gzE05CmhA": superUsersService,
|
||||
"j0adQOsJBR0xq24dxLKdDU9EQRmt4gzE05CmhA": this.superUsersService,
|
||||
}
|
||||
}
|
||||
|
||||
getService(request: any) {
|
||||
const driveToken = request.driveToken
|
||||
const secondPartOfDriveToken = driveToken.split(":")[1]
|
||||
if (!secondPartOfDriveToken) { throw new Error('Drive token is missing or null') }
|
||||
return this.mapper[secondPartOfDriveToken];
|
||||
}
|
||||
|
||||
async supersUserFilter(query: any & { page?: number; pageSize?: number }): Promise<{ pagination: PaginationInfo; data: any[] }> {
|
||||
return this.superUsersService.filter(query);
|
||||
}
|
||||
|
||||
async infoEvents(userToken: string) {
|
||||
const relatedMapper = this.getService(userToken)
|
||||
if (!relatedMapper) { throw new Error(`No service found for user token: ${userToken}`) }
|
||||
return relatedMapper.infoEvents(userToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { PaginationHelper } from '@/src/utils/pagination-helper';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PaginationInfo } from '@/src/utils/pagination-helper';
|
||||
import { PrismaService } from '@/src/prisma.service';
|
||||
import { UrlHandler } from '@/src/utils/auth/urlHandler';
|
||||
import { UrlHandler } from '@/src/utils/navigator/urlHandler';
|
||||
|
||||
@Injectable()
|
||||
export class SuperUsersService {
|
||||
@@ -36,7 +36,6 @@ export class SuperUsersService {
|
||||
async infoEvents(userToken: string) { return Object.entries(this.events).filter(([key]) => key.endsWith(userToken)) }
|
||||
|
||||
async filter(query: any & { page?: number; pageSize?: number }): Promise<{ pagination: PaginationInfo; data: any[] }> {
|
||||
console.log("supersServiceFilter query", query)
|
||||
const result = await this.paginationHelper.findWithPagination(query, this.prisma.account_records);
|
||||
const { pagination, data } = result;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user