-
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: 🔥 [EXL-74] support configure rule without enabling
support configure rule without enabling
- Loading branch information
tal-rofe
committed
Nov 15, 2022
1 parent
add4dd9
commit cca4c1d
Showing
45 changed files
with
842 additions
and
280 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
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
26 changes: 26 additions & 0 deletions
26
apps/backend/src/modules/user/modules/rules/classes/configure-missing.dto.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,26 @@ | ||
import { ApiProperty, ApiResponseProperty } from '@nestjs/swagger'; | ||
import type { IConfigureMissingRuleDto, IConfigureMissingRuleResponseData } from '@exlint-dashboard/common'; | ||
import { IsArray, IsString } from 'class-validator'; | ||
import type { Rule } from '@prisma/client'; | ||
|
||
export class ConfigureMissingRuleDto implements IConfigureMissingRuleDto { | ||
@ApiProperty({ type: String, description: 'The new rule name to configure', example: 'yazif-array' }) | ||
@IsString() | ||
readonly name!: Rule['name']; | ||
|
||
@ApiProperty({ | ||
type: Array, | ||
description: 'The configuration to apply a rule with', | ||
example: ['error'], | ||
}) | ||
@IsArray() | ||
readonly configuration!: IConfigureMissingRuleDto['configuration']; | ||
} | ||
|
||
export class ConfigureMissingRuleResponse implements IConfigureMissingRuleResponseData { | ||
@ApiResponseProperty({ | ||
type: String, | ||
example: '62e5362119bea07115434f4a', | ||
}) | ||
public id!: string; | ||
} |
18 changes: 18 additions & 0 deletions
18
apps/backend/src/modules/user/modules/rules/classes/enable-missing.dto.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,18 @@ | ||
import { ApiProperty, ApiResponseProperty } from '@nestjs/swagger'; | ||
import type { IEnableMissingRuleDto, IEnableMissingRuleResponseData } from '@exlint-dashboard/common'; | ||
import { IsString } from 'class-validator'; | ||
import type { Rule } from '@prisma/client'; | ||
|
||
export class EnableMissingDto implements IEnableMissingRuleDto { | ||
@ApiProperty({ type: String, description: 'The rule name to enable', example: 'yazif-array' }) | ||
@IsString() | ||
readonly name!: Rule['name']; | ||
} | ||
|
||
export class EnableMissingResponse implements IEnableMissingRuleResponseData { | ||
@ApiResponseProperty({ | ||
type: String, | ||
example: '62e5362119bea07115434f4a', | ||
}) | ||
public id!: string; | ||
} |
18 changes: 0 additions & 18 deletions
18
apps/backend/src/modules/user/modules/rules/classes/enable-rule.dto.ts
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
apps/backend/src/modules/user/modules/rules/classes/update-configuration.dto.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,13 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsArray } from 'class-validator'; | ||
import type { IUpdateRuleConfiguratinoDto } from '@exlint-dashboard/common'; | ||
|
||
export class UpdateRuleConfigurationDto implements IUpdateRuleConfiguratinoDto { | ||
@ApiProperty({ | ||
type: Array, | ||
description: 'The configuration to update a rule with', | ||
example: ['error'], | ||
}) | ||
@IsArray() | ||
readonly configuration!: IUpdateRuleConfiguratinoDto['configuration']; | ||
} |
3 changes: 3 additions & 0 deletions
3
apps/backend/src/modules/user/modules/rules/commands/contracts/disable.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 DisableContract { | ||
constructor(public readonly ruleId: string) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
apps/backend/src/modules/user/modules/rules/commands/contracts/enable-exist.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 EnableExistContract { | ||
constructor(public readonly ruleId: string) {} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ackend/src/modules/user/modules/rules/commands/contracts/update-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,5 @@ | ||
import type { Prisma } from '@prisma/client'; | ||
|
||
export class UpdateConfigurationContract { | ||
constructor(public readonly ruleId: string, public readonly configuration: Prisma.JsonArray) {} | ||
} |
14 changes: 14 additions & 0 deletions
14
apps/backend/src/modules/user/modules/rules/commands/handlers/disable.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 { DBRuleService } from '@/modules/database/rule.service'; | ||
|
||
import { DisableContract } from '../contracts/disable.contract'; | ||
|
||
@CommandHandler(DisableContract) | ||
export class DisableHandler implements ICommandHandler<DisableContract> { | ||
constructor(private readonly dbRuleService: DBRuleService) {} | ||
|
||
async execute(contract: DisableContract) { | ||
await this.dbRuleService.disableRule(contract.ruleId); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
apps/backend/src/modules/user/modules/rules/commands/handlers/enable-exist.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 { DBRuleService } from '@/modules/database/rule.service'; | ||
|
||
import { EnableExistContract } from '../contracts/enable-exist.contract'; | ||
|
||
@CommandHandler(EnableExistContract) | ||
export class EnableExistHandler implements ICommandHandler<EnableExistContract> { | ||
constructor(private readonly dbRuleService: DBRuleService) {} | ||
|
||
async execute(contract: EnableExistContract) { | ||
await this.dbRuleService.enableExistRule(contract.ruleId); | ||
} | ||
} |
10 changes: 9 additions & 1 deletion
10
apps/backend/src/modules/user/modules/rules/commands/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,11 @@ | ||
import { DeleteHandler } from './delete.handler'; | ||
import { DisableHandler } from './disable.handler'; | ||
import { EnableExistHandler } from './enable-exist.handler'; | ||
import { UpdateConfigurationHandler } from './update-configuration.handler'; | ||
|
||
export const CommandHandlers = [DeleteHandler]; | ||
export const CommandHandlers = [ | ||
DeleteHandler, | ||
EnableExistHandler, | ||
DisableHandler, | ||
UpdateConfigurationHandler, | ||
]; |
14 changes: 14 additions & 0 deletions
14
.../backend/src/modules/user/modules/rules/commands/handlers/update-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 { DBRuleService } from '@/modules/database/rule.service'; | ||
|
||
import { UpdateConfigurationContract } from '../contracts/update-configuration.contract'; | ||
|
||
@CommandHandler(UpdateConfigurationContract) | ||
export class UpdateConfigurationHandler implements ICommandHandler<UpdateConfigurationContract> { | ||
constructor(private readonly dbRuleService: DBRuleService) {} | ||
|
||
async execute(contract: UpdateConfigurationContract) { | ||
await this.dbRuleService.updateRuleConfiguration(contract.ruleId, contract.configuration); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
apps/backend/src/modules/user/modules/rules/configure-missing.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,72 @@ | ||
import { Body, Controller, HttpCode, HttpStatus, Logger, Param, Post, UseGuards } from '@nestjs/common'; | ||
import { QueryBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiBearerAuth, | ||
ApiCreatedResponse, | ||
ApiInternalServerErrorResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
import { PolicyLibrary } from '@prisma/client'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
import { Library } from '@/decorators/library.decorator'; | ||
import { RuleablePolicyGuard } from '@/guards/ruleable-policy.guard'; | ||
|
||
import Routes from './rules.routes'; | ||
import { EnableMissingResponse } from './classes/enable-missing.dto'; | ||
import { ConfigureMissingRuleDto, type ConfigureMissingRuleResponse } from './classes/configure-missing.dto'; | ||
import { ConfigureMissingContract } from './queries/contracts/configure-missing.contract'; | ||
|
||
@ApiTags('Rules') | ||
@Controller(Routes.CONTROLLER) | ||
export class ConfigureMissingController { | ||
private readonly logger = new Logger(ConfigureMissingController.name); | ||
|
||
constructor(private readonly queryBus: QueryBus) {} | ||
|
||
@ApiOperation({ description: 'Configure a missing rule for a policy' }) | ||
@ApiBearerAuth('access-token') | ||
@ApiCreatedResponse({ | ||
description: 'If successfully configured the missing rule', | ||
type: EnableMissingResponse, | ||
}) | ||
@ApiBadRequestResponse({ description: "If rule name is invalid or policy's library has no rules" }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid, or policy does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to configure the missing rule' }) | ||
@UseGuards(RuleablePolicyGuard) | ||
@Post(Routes.CONFIGURE_MISSING) | ||
@HttpCode(HttpStatus.CREATED) | ||
public async configureMissing( | ||
@CurrentUserId() userId: string, | ||
@Param('policy_id') policyId: string, | ||
@Body() configureMissingRuleDto: ConfigureMissingRuleDto, | ||
@Library() policyLibrary: PolicyLibrary, | ||
): Promise<ConfigureMissingRuleResponse> { | ||
this.logger.log( | ||
`Will try to configure a missing rule for a policy with an ID: "${policyId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
const createdRuleId = await this.queryBus.execute< | ||
ConfigureMissingContract, | ||
ConfigureMissingRuleResponse['id'] | ||
>( | ||
new ConfigureMissingContract( | ||
policyId, | ||
configureMissingRuleDto.name, | ||
configureMissingRuleDto.configuration, | ||
policyLibrary, | ||
), | ||
); | ||
|
||
this.logger.log( | ||
`Successfully configured a missing rule with an ID: "${createdRuleId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
return { id: createdRuleId }; | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
apps/backend/src/modules/user/modules/rules/disable.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,46 @@ | ||
import { Controller, HttpCode, HttpStatus, Logger, Param, Patch, UseGuards } from '@nestjs/common'; | ||
import { CommandBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './rules.routes'; | ||
import { BelongingRuleGuard } from './guards/belonging-rule.guard'; | ||
import { DisableContract } from './commands/contracts/disable.contract'; | ||
|
||
@ApiTags('Rules') | ||
@Controller(Routes.CONTROLLER) | ||
export class DisableController { | ||
private readonly logger = new Logger(DisableController.name); | ||
|
||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
@ApiOperation({ description: 'Disable a rule with provided identifier' }) | ||
@ApiBearerAuth('access-token') | ||
@ApiOkResponse({ description: 'If successfully disabled the rule' }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid, or rule does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to disable the rule' }) | ||
@UseGuards(BelongingRuleGuard) | ||
@Patch(Routes.DISABLE_RULE) | ||
@HttpCode(HttpStatus.OK) | ||
public async disable(@CurrentUserId() userId: string, @Param('rule_id') ruleId: string): Promise<void> { | ||
this.logger.log( | ||
`Will try to disable a rule with an ID: "${ruleId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
await this.commandBus.execute<DisableContract, void>(new DisableContract(ruleId)); | ||
|
||
this.logger.log( | ||
`Successfully disabled a rule with an ID: "${ruleId}" for a user with an ID: "${userId}"`, | ||
); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
apps/backend/src/modules/user/modules/rules/enable-exist.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,49 @@ | ||
import { Controller, HttpCode, HttpStatus, Logger, Param, Patch, UseGuards } from '@nestjs/common'; | ||
import { CommandBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
import { RuleablePolicyGuard } from '@/guards/ruleable-policy.guard'; | ||
|
||
import Routes from './rules.routes'; | ||
import { EnableExistContract } from './commands/contracts/enable-exist.contract'; | ||
|
||
@ApiTags('Rules') | ||
@Controller(Routes.CONTROLLER) | ||
export class EnableExistController { | ||
private readonly logger = new Logger(EnableExistController.name); | ||
|
||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
@ApiOperation({ description: 'Enable an exist rule for a policy' }) | ||
@ApiBearerAuth('access-token') | ||
@ApiOkResponse({ description: 'If successfully enabled the rule' }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid, or rule does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to enable the rule' }) | ||
@UseGuards(RuleablePolicyGuard) | ||
@Patch(Routes.ENABLE_EXIST_RULE) | ||
@HttpCode(HttpStatus.OK) | ||
public async enableRule( | ||
@CurrentUserId() userId: string, | ||
@Param('rule_id') ruleId: string, | ||
): Promise<void> { | ||
this.logger.log( | ||
`Will try to enable a rule with an ID: "${ruleId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
await this.commandBus.execute<EnableExistContract, void>(new EnableExistContract(ruleId)); | ||
|
||
this.logger.log( | ||
`Successfully enabled a rule with an ID: "${ruleId}" for a user with an ID: "${userId}"`, | ||
); | ||
} | ||
} |
Oops, something went wrong.