updated accounts service navigator
This commit is contained in:
@@ -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.`) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
EndpointControlGuard,
|
||||
} from '@/src/middleware/access-control.guard';
|
||||
import { SuperUsersService } from './superusers/superusers.service';
|
||||
import { UrlHandler } from '../utils/auth/urlHandler';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, UtilsModule],
|
||||
@@ -18,16 +19,11 @@ import { SuperUsersService } from './superusers/superusers.service';
|
||||
AuthControlGuard,
|
||||
EndpointControlGuard,
|
||||
SuperUsersService,
|
||||
UrlHandler,
|
||||
],
|
||||
controllers: [AccountsController],
|
||||
})
|
||||
export class AccountsModule {
|
||||
constructor(
|
||||
private accountsService: AccountsService,
|
||||
) { }
|
||||
constructor() { }
|
||||
|
||||
async onModuleInit() {
|
||||
const accountEvents = await this.accountsService.infoEvents();
|
||||
console.dir(accountEvents, { depth: null });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,80 +1,33 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PaginationInfo } from '../utils/pagination-helper';
|
||||
import { SuperUsersService } from './superusers/superusers.service';
|
||||
import crypto from 'crypto';
|
||||
|
||||
@Injectable()
|
||||
export class AccountsService {
|
||||
mapper: any
|
||||
|
||||
constructor(
|
||||
private superUsersService: SuperUsersService,
|
||||
) { }
|
||||
events = {
|
||||
"/accounts/filter:GQKQshahQhGm8HYy4O4Tgx": [
|
||||
{
|
||||
"key": "s8OnSnHoQfyfuDk7A1XRww",
|
||||
"description": "Super Users Filter",
|
||||
"isDefault": true,
|
||||
"query": { "query": true, "page": false, "pageSize": false },
|
||||
"token": "GQKQshahQhGm8HYy4O4Tgx",
|
||||
"pages": ["accounts"]
|
||||
// "type": "EMP",
|
||||
// "fr": "SuperUserEmployee",
|
||||
}
|
||||
],
|
||||
"/accounts/read:GQKQshahQhGm8HYy4O4Tgx": [
|
||||
{
|
||||
"key": "s8OnSnHoQfyfuDk7A1XRww",
|
||||
"description": "Super Users Read",
|
||||
"isDefault": true,
|
||||
"query": { "query": true, "page": false, "pageSize": false },
|
||||
"token": "GQKQshahQhGm8HYy4O4Tgx",
|
||||
"pages": ["accounts"]
|
||||
// "type": "EMP",
|
||||
// "fr": "SuperUserEmployee",
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
createSecureKeyWithoutLib(url: string) {
|
||||
const subString = crypto.createHash('sha256').update(url).digest().toString('base64').substring(0, 16)
|
||||
return subString.replace(/=/g, 'E').replace(/-/g, 'M').replace(/_/g, 'N').replace(/\+/g, 'P').replace(/\//g, 'Q')
|
||||
}
|
||||
|
||||
async infoEvents(urlRetriever: string | null = null, functionRetriever: string | null = null) {
|
||||
const events = this.events;
|
||||
if (urlRetriever && !functionRetriever) {
|
||||
if (events[urlRetriever]) {
|
||||
return [[urlRetriever, events[urlRetriever]]];
|
||||
}
|
||||
return [];
|
||||
} else if (urlRetriever && functionRetriever) {
|
||||
if (events[urlRetriever] && events[urlRetriever][functionRetriever]) {
|
||||
return [[urlRetriever, { [functionRetriever]: events[urlRetriever][functionRetriever] }]];
|
||||
}
|
||||
return [];
|
||||
} else if (!urlRetriever && functionRetriever) {
|
||||
const filteredEvents: [string, any][] = [];
|
||||
Object.entries(events).forEach(([url, urlEvents]) => {
|
||||
if (urlEvents[functionRetriever]) {
|
||||
filteredEvents.push([url, { [functionRetriever]: urlEvents[functionRetriever] }]);
|
||||
}
|
||||
});
|
||||
return filteredEvents;
|
||||
} else {
|
||||
return Object.entries(events);
|
||||
) {
|
||||
this.mapper = {
|
||||
"j0adQOsJBR0xq24dxLKdDU9EQRmt4gzE05CmhA": superUsersService,
|
||||
}
|
||||
}
|
||||
|
||||
async getEvents() {
|
||||
return {
|
||||
"/accounts/filter:GQKQshahQhGm8HYy4O4Tgx:s8OnSnHoQfyfuDk7A1XRww": (query: any) => this.supersUserFilter(query),
|
||||
"/accounts/read:a5b6d9c716f409a7004a:tcc116f409a7004a": (query: any) => this.supersUserFilter(query)
|
||||
};
|
||||
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,14 +2,39 @@ 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';
|
||||
|
||||
@Injectable()
|
||||
export class SuperUsersService {
|
||||
userToken: string = "j0adQOsJBR0xq24dxLKdDU9EQRmt4gzE05CmhA"
|
||||
constructor(
|
||||
private paginationHelper: PaginationHelper,
|
||||
private prisma: PrismaService,
|
||||
private urlHandler: UrlHandler,
|
||||
) { }
|
||||
|
||||
events = {
|
||||
"e6hewIe7YqbQZHO3:j0adQOsJBR0xq24dxLKdDU9EQRmt4gzE05CmhA": [
|
||||
{
|
||||
"key": "qt5P0xoeThjNT9EuWfwBgxsntHY5ydRtKFr1pgKGcgxx",
|
||||
"endpoint": "/accounts/filter:POST",
|
||||
"eToken": "e6hewIe7YqbQZHO3",
|
||||
"token": "j0adQOsJBR0xq24dxLKdDU9EQRmt4gzE05CmhA",
|
||||
"description": "Super Users Account Filter",
|
||||
"isDefault": true,
|
||||
"query": { "query": true, "page": false, "pageSize": false },
|
||||
"pages": []
|
||||
}
|
||||
]
|
||||
};
|
||||
mapper = {
|
||||
"e6hewIe7YqbQZHO3:j0adQOsJBR0xq24dxLKdDU9EQRmt4gzE05CmhA:qt5P0xoeThjNT9EuWfwBgxsntHY5ydRtKFr1pgKGcgxx": (query: any) => this.filter(query),
|
||||
}
|
||||
|
||||
async getEvents() { return this.urlHandler.getEvents(this.events, this.mapper) }
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user