Skip to content

Commit

Permalink
feat: 🔥 support policy config api
Browse files Browse the repository at this point in the history
support policy config api
  • Loading branch information
tal-rofe committed Jul 29, 2022
1 parent de95499 commit 3a2079a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 4 deletions.
7 changes: 7 additions & 0 deletions apps/backend/src/modules/database/inline-policy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ export class DBInlinePolicyService {
data: { rules: rulesWithoutRule },
});
}

public getConfiguration(inlinePolicyId: string) {
return this.prisma.inlinePolicy.findFirst({
where: { id: inlinePolicyId },
select: { configuration: true },
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Controller, Get, HttpCode, HttpStatus, Logger, Param, UseGuards } from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';
import type { Prisma } from '@prisma/client';

import { CurrentUserId } from '@/decorators/current-user-id.decorator';

import Routes from './inline-policies.routes';
import { BelongingInlinePolicyGuard } from './guards/belonging-inline-policy.guard';
import type { IGetConfigurationResponse } from './interfaces/responses';
import { GetConfigurationContract } from './queries/contracts/get-configuration.contract';

@Controller(Routes.CONTROLLER)
export class GetConfigurationController {
private readonly logger = new Logger(GetConfigurationController.name);

constructor(private readonly queryBus: QueryBus) {}

@UseGuards(BelongingInlinePolicyGuard)
@Get(Routes.GET_CONFIGURATION)
@HttpCode(HttpStatus.OK)
public async getConfiguration(
@CurrentUserId() userId: string,
@Param('policy_id') policyId: string,
): Promise<IGetConfigurationResponse> {
this.logger.log(
`Will try to fetch policy configuration belongs to use with an Id: "${userId}" with policy Id: "${policyId}"`,
);

const policyConfiguration = await this.queryBus.execute<GetConfigurationContract, Prisma.JsonValue>(
new GetConfigurationContract(policyId),
);

this.logger.log(
`Successfully got policy configuration belongs to user with an Id: "${userId}" with policy Id: "${policyId}"`,
);

return {
configuration: policyConfiguration,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UpdateConfigurationController } from './update-configuration.controller
import { AddRuleController } from './add-rule.controller';
import { RemoveRuleController } from './remove-rule.controller';
import { EventHandlers } from './events/handlers';
import { GetConfigurationController } from './get-configuration.controller';

@Module({
imports: [CqrsModule],
Expand All @@ -21,6 +22,7 @@ import { EventHandlers } from './events/handlers';
UpdateConfigurationController,
AddRuleController,
RemoveRuleController,
GetConfigurationController,
],
providers: [
BelongingGroupGuard,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const Routes = {
CONTROLLER: 'inline-policies',
CREATE: 'create/:group_id',
DELETE: 'delete/:policy_id',
UPDATE_CONFIGURATION: 'update-configuration/:policy_id',
CREATE: ':group_id',
DELETE: ':policy_id',
UPDATE_CONFIGURATION: ':policy_id',
ADD_RULE: 'add-rule/:policy_id',
EDIT_RULE: 'edit-rule/:policy_id',
REMOVE_RULE: 'remove-rule/:policy_id',
GET_CONFIGURATION: ':policy_id',
};

export default Routes;
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import type { Prisma } from '@prisma/client';

export interface ICreateInlinePolicy {
policyId: string;
}

export interface IGetConfigurationResponse {
configuration: Prisma.JsonValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class GetConfigurationContract {
constructor(public readonly policyId: string) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs';

import { DBInlinePolicyService } from '@/modules/database/inline-policy.service';

import { GetConfigurationContract } from '../contracts/get-configuration.contract';

@CommandHandler(GetConfigurationContract)
export class GetConfigurationHandler implements ICommandHandler<GetConfigurationContract> {
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {}

execute(contract: GetConfigurationContract) {
return this.dbInlinePolicyService.getConfiguration(contract.policyId);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CreateInlineHandler } from './create-inline.handler';
import { GetConfigurationHandler } from './get-configuration.handler';

export const QueryHandlers = [CreateInlineHandler];
export const QueryHandlers = [CreateInlineHandler, GetConfigurationHandler];

0 comments on commit 3a2079a

Please sign in to comment.