-
Notifications
You must be signed in to change notification settings - Fork 273
[dev] [carhartlewis] lewis/comp-framework-editor #2355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0b93d38
feat(framework-editor): add control, policy, and requirement template…
carhartlewis 88b01ee
fix(control): drop documentTypes column from FrameworkEditorControlTe…
carhartlewis 2b4e0e0
feat(framework-editor): add documentTypes field to FrameworkEditorCon…
carhartlewis 1f173ff
feat(framework-editor): add Prisma schema and initial migration for F…
carhartlewis eedd924
feat(framework-editor): enhance services with conflict handling and n…
carhartlewis 129c247
refactor(framework-editor): update controllers to use PlatformAdminGu…
carhartlewis e911795
feat(framework-editor): enhance templates with frameworkId support in…
carhartlewis 39f27bc
feat(framework-editor): add linking functionality for controls, tasks…
carhartlewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
112 changes: 112 additions & 0 deletions
112
apps/api/src/framework-editor/control-template/control-template.controller.ts
This file contains hidden or 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,112 @@ | ||
| import { | ||
| Controller, | ||
| Get, | ||
| Post, | ||
| Patch, | ||
| Delete, | ||
| Body, | ||
| Param, | ||
| Query, | ||
| UseGuards, | ||
| UsePipes, | ||
| ValidationPipe, | ||
| } from '@nestjs/common'; | ||
| import { ApiTags } from '@nestjs/swagger'; | ||
| import { PlatformAdminGuard } from '../../auth/platform-admin.guard'; | ||
| import { CreateControlTemplateDto } from './dto/create-control-template.dto'; | ||
| import { UpdateControlTemplateDto } from './dto/update-control-template.dto'; | ||
| import { ControlTemplateService } from './control-template.service'; | ||
|
|
||
| @ApiTags('Framework Editor Control Templates') | ||
| @Controller({ path: 'framework-editor/control-template', version: '1' }) | ||
| @UseGuards(PlatformAdminGuard) | ||
| export class ControlTemplateController { | ||
| constructor(private readonly service: ControlTemplateService) {} | ||
|
|
||
| @Get() | ||
| async findAll( | ||
| @Query('take') take?: string, | ||
| @Query('skip') skip?: string, | ||
| @Query('frameworkId') frameworkId?: string, | ||
| ) { | ||
| const limit = Math.min(Number(take) || 500, 500); | ||
| const offset = Number(skip) || 0; | ||
| return this.service.findAll(limit, offset, frameworkId); | ||
| } | ||
|
|
||
| @Get(':id') | ||
| async findOne(@Param('id') id: string) { | ||
| return this.service.findById(id); | ||
| } | ||
|
|
||
| @Post() | ||
| @UsePipes(new ValidationPipe({ whitelist: true, transform: true })) | ||
| async create( | ||
| @Body() dto: CreateControlTemplateDto, | ||
| @Query('frameworkId') frameworkId?: string, | ||
| ) { | ||
| return this.service.create(dto, frameworkId); | ||
| } | ||
|
|
||
| @Patch(':id') | ||
| @UsePipes(new ValidationPipe({ whitelist: true, transform: true })) | ||
| async update( | ||
| @Param('id') id: string, | ||
| @Body() dto: UpdateControlTemplateDto, | ||
| ) { | ||
| return this.service.update(id, dto); | ||
| } | ||
|
|
||
| @Delete(':id') | ||
| async delete(@Param('id') id: string) { | ||
| return this.service.delete(id); | ||
| } | ||
|
|
||
| @Post(':id/requirements/:reqId') | ||
| async linkRequirement( | ||
| @Param('id') id: string, | ||
| @Param('reqId') reqId: string, | ||
| ) { | ||
| return this.service.linkRequirement(id, reqId); | ||
| } | ||
|
|
||
| @Delete(':id/requirements/:reqId') | ||
| async unlinkRequirement( | ||
| @Param('id') id: string, | ||
| @Param('reqId') reqId: string, | ||
| ) { | ||
| return this.service.unlinkRequirement(id, reqId); | ||
| } | ||
|
|
||
| @Post(':id/policy-templates/:ptId') | ||
| async linkPolicyTemplate( | ||
| @Param('id') id: string, | ||
| @Param('ptId') ptId: string, | ||
| ) { | ||
| return this.service.linkPolicyTemplate(id, ptId); | ||
| } | ||
|
|
||
| @Delete(':id/policy-templates/:ptId') | ||
| async unlinkPolicyTemplate( | ||
| @Param('id') id: string, | ||
| @Param('ptId') ptId: string, | ||
| ) { | ||
| return this.service.unlinkPolicyTemplate(id, ptId); | ||
| } | ||
|
|
||
| @Post(':id/task-templates/:ttId') | ||
| async linkTaskTemplate( | ||
| @Param('id') id: string, | ||
| @Param('ttId') ttId: string, | ||
| ) { | ||
| return this.service.linkTaskTemplate(id, ttId); | ||
| } | ||
|
|
||
| @Delete(':id/task-templates/:ttId') | ||
| async unlinkTaskTemplate( | ||
| @Param('id') id: string, | ||
| @Param('ttId') ttId: string, | ||
| ) { | ||
| return this.service.unlinkTaskTemplate(id, ttId); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
apps/api/src/framework-editor/control-template/control-template.module.ts
This file contains hidden or 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,12 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { AuthModule } from '../../auth/auth.module'; | ||
| import { ControlTemplateController } from './control-template.controller'; | ||
| import { ControlTemplateService } from './control-template.service'; | ||
|
|
||
| @Module({ | ||
| imports: [AuthModule], | ||
| controllers: [ControlTemplateController], | ||
| providers: [ControlTemplateService], | ||
| exports: [ControlTemplateService], | ||
| }) | ||
| export class ControlTemplateModule {} |
160 changes: 160 additions & 0 deletions
160
apps/api/src/framework-editor/control-template/control-template.service.ts
This file contains hidden or 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,160 @@ | ||
| import { Injectable, NotFoundException, ConflictException, Logger } from '@nestjs/common'; | ||
| import { db, Prisma } from '@trycompai/db'; | ||
| import type { EvidenceFormType } from '@trycompai/db'; | ||
| import { CreateControlTemplateDto } from './dto/create-control-template.dto'; | ||
| import { UpdateControlTemplateDto } from './dto/update-control-template.dto'; | ||
|
|
||
| @Injectable() | ||
| export class ControlTemplateService { | ||
| private readonly logger = new Logger(ControlTemplateService.name); | ||
|
|
||
| async findAll(take = 500, skip = 0, frameworkId?: string) { | ||
| return db.frameworkEditorControlTemplate.findMany({ | ||
| take, | ||
| skip, | ||
| orderBy: { createdAt: 'asc' }, | ||
| where: frameworkId | ||
| ? { requirements: { some: { frameworkId } } } | ||
| : undefined, | ||
| include: { | ||
| policyTemplates: { select: { id: true, name: true } }, | ||
| requirements: { | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| framework: { select: { name: true } }, | ||
| }, | ||
| }, | ||
| taskTemplates: { select: { id: true, name: true } }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| async findById(id: string) { | ||
| const ct = await db.frameworkEditorControlTemplate.findUnique({ | ||
| where: { id }, | ||
| include: { | ||
| policyTemplates: { select: { id: true, name: true } }, | ||
| requirements: { | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| framework: { select: { name: true } }, | ||
| }, | ||
| }, | ||
| taskTemplates: { select: { id: true, name: true } }, | ||
| }, | ||
| }); | ||
| if (!ct) throw new NotFoundException(`Control template ${id} not found`); | ||
| return ct; | ||
| } | ||
|
|
||
| async create(dto: CreateControlTemplateDto, frameworkId?: string) { | ||
| const requirementIds = frameworkId | ||
| ? await db.frameworkEditorRequirement | ||
| .findMany({ | ||
| where: { frameworkId }, | ||
| select: { id: true }, | ||
| }) | ||
| .then((reqs) => reqs.map((r) => ({ id: r.id }))) | ||
| : []; | ||
|
|
||
| const ct = await db.frameworkEditorControlTemplate.create({ | ||
| data: { | ||
| name: dto.name, | ||
| description: dto.description ?? '', | ||
| ...(dto.documentTypes && { | ||
| documentTypes: dto.documentTypes as EvidenceFormType[], | ||
| }), | ||
| ...(requirementIds.length > 0 && { | ||
| requirements: { connect: requirementIds }, | ||
| }), | ||
| }, | ||
| }); | ||
| this.logger.log(`Created control template: ${ct.name} (${ct.id})`); | ||
| return ct; | ||
| } | ||
|
|
||
| async update(id: string, dto: UpdateControlTemplateDto) { | ||
| await this.findById(id); | ||
| const updated = await db.frameworkEditorControlTemplate.update({ | ||
| where: { id }, | ||
| data: { | ||
| ...(dto.name !== undefined && { name: dto.name }), | ||
| ...(dto.description !== undefined && { description: dto.description }), | ||
| ...(dto.documentTypes !== undefined && { | ||
| documentTypes: dto.documentTypes as EvidenceFormType[], | ||
| }), | ||
| }, | ||
| }); | ||
| this.logger.log(`Updated control template: ${updated.name} (${id})`); | ||
| return updated; | ||
| } | ||
|
|
||
| async delete(id: string) { | ||
| await this.findById(id); | ||
| try { | ||
| await db.frameworkEditorControlTemplate.delete({ where: { id } }); | ||
| } catch (error) { | ||
| if ( | ||
| error instanceof Prisma.PrismaClientKnownRequestError && | ||
| error.code === 'P2003' | ||
| ) { | ||
| throw new ConflictException( | ||
| 'Cannot delete control template: it is referenced by existing controls', | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| this.logger.log(`Deleted control template ${id}`); | ||
| return { message: 'Control template deleted successfully' }; | ||
| } | ||
|
|
||
| async linkRequirement(controlId: string, requirementId: string) { | ||
| await db.frameworkEditorControlTemplate.update({ | ||
| where: { id: controlId }, | ||
| data: { requirements: { connect: { id: requirementId } } }, | ||
| }); | ||
| return { message: 'Requirement linked' }; | ||
| } | ||
|
|
||
| async unlinkRequirement(controlId: string, requirementId: string) { | ||
| await db.frameworkEditorControlTemplate.update({ | ||
| where: { id: controlId }, | ||
| data: { requirements: { disconnect: { id: requirementId } } }, | ||
| }); | ||
| return { message: 'Requirement unlinked' }; | ||
| } | ||
|
|
||
| async linkPolicyTemplate(controlId: string, policyTemplateId: string) { | ||
| await db.frameworkEditorControlTemplate.update({ | ||
| where: { id: controlId }, | ||
| data: { policyTemplates: { connect: { id: policyTemplateId } } }, | ||
| }); | ||
| return { message: 'Policy template linked' }; | ||
| } | ||
|
|
||
| async unlinkPolicyTemplate(controlId: string, policyTemplateId: string) { | ||
| await db.frameworkEditorControlTemplate.update({ | ||
| where: { id: controlId }, | ||
| data: { policyTemplates: { disconnect: { id: policyTemplateId } } }, | ||
| }); | ||
| return { message: 'Policy template unlinked' }; | ||
| } | ||
|
|
||
| async linkTaskTemplate(controlId: string, taskTemplateId: string) { | ||
| await db.frameworkEditorControlTemplate.update({ | ||
| where: { id: controlId }, | ||
| data: { taskTemplates: { connect: { id: taskTemplateId } } }, | ||
| }); | ||
| return { message: 'Task template linked' }; | ||
| } | ||
|
|
||
| async unlinkTaskTemplate(controlId: string, taskTemplateId: string) { | ||
| await db.frameworkEditorControlTemplate.update({ | ||
| where: { id: controlId }, | ||
| data: { taskTemplates: { disconnect: { id: taskTemplateId } } }, | ||
| }); | ||
| return { message: 'Task template unlinked' }; | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
apps/api/src/framework-editor/control-template/dto/create-control-template.dto.ts
This file contains hidden or 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,27 @@ | ||
| import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; | ||
| import { | ||
| IsString, | ||
| IsNotEmpty, | ||
| IsArray, | ||
| IsOptional, | ||
| MaxLength, | ||
| } from 'class-validator'; | ||
|
|
||
| export class CreateControlTemplateDto { | ||
| @ApiProperty({ example: 'Access Control Policy' }) | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(255) | ||
| name: string; | ||
|
|
||
| @ApiProperty({ example: 'Ensures access controls are properly managed' }) | ||
| @IsString() | ||
| @MaxLength(5000) | ||
| description: string; | ||
|
|
||
| @ApiPropertyOptional({ example: ['penetration-test', 'rbac-matrix'] }) | ||
| @IsArray() | ||
| @IsString({ each: true }) | ||
| @IsOptional() | ||
| documentTypes?: string[]; | ||
| } |
6 changes: 6 additions & 0 deletions
6
apps/api/src/framework-editor/control-template/dto/update-control-template.dto.ts
This file contains hidden or 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,6 @@ | ||
| import { PartialType } from '@nestjs/swagger'; | ||
| import { CreateControlTemplateDto } from './create-control-template.dto'; | ||
|
|
||
| export class UpdateControlTemplateDto extends PartialType( | ||
| CreateControlTemplateDto, | ||
| ) {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.