-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🔥 support policy config api
support policy config api
- Loading branch information
tal-rofe
committed
Jul 29, 2022
1 parent
de95499
commit 3a2079a
Showing
8 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
apps/backend/src/modules/user/modules/inline-policies/get-configuration.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
apps/backend/src/modules/user/modules/inline-policies/inline-policies.routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
6 changes: 6 additions & 0 deletions
6
apps/backend/src/modules/user/modules/inline-policies/interfaces/responses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
3 changes: 3 additions & 0 deletions
3
.../src/modules/user/modules/inline-policies/queries/contracts/get-configuration.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class GetConfigurationContract { | ||
constructor(public readonly policyId: string) {} | ||
} |
14 changes: 14 additions & 0 deletions
14
...nd/src/modules/user/modules/inline-policies/queries/handlers/get-configuration.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
apps/backend/src/modules/user/modules/inline-policies/queries/handlers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |