From 1c4c438ec12bd0af59fc3b64cd8f07857625d0bc Mon Sep 17 00:00:00 2001 From: Haralan Dobrev Date: Mon, 28 Jun 2021 06:54:49 +0300 Subject: [PATCH 1/2] List protocols statuses in an endpoint for admin filters --- src/casl/casl-ability.factory.ts | 1 + .../api/protocols-statuses.controller.ts | 34 +++++++++++++++++++ src/protocols/protocols.module.ts | 7 +++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/protocols/api/protocols-statuses.controller.ts diff --git a/src/casl/casl-ability.factory.ts b/src/casl/casl-ability.factory.ts index 55c7f083..03e76d44 100644 --- a/src/casl/casl-ability.factory.ts +++ b/src/casl/casl-ability.factory.ts @@ -81,6 +81,7 @@ export class CaslAbilityFactory { // read access to everything can(Action.Read, 'all'); can([Action.Manage], User); + can([Action.Manage], Protocol); can([Action.Create, Action.Update, Action.Publish], Post); // Delete only unpublished posts can(Action.Delete, Post, { publishAt: { $eq: null } }); diff --git a/src/protocols/api/protocols-statuses.controller.ts b/src/protocols/api/protocols-statuses.controller.ts new file mode 100644 index 00000000..cfc86900 --- /dev/null +++ b/src/protocols/api/protocols-statuses.controller.ts @@ -0,0 +1,34 @@ +import { Ability } from '@casl/ability'; +import { + Controller, + Get, + HttpCode, + UsePipes, + ValidationPipe, + UseGuards, +} from '@nestjs/common'; +import { Action } from 'src/casl/action.enum'; +import { CheckPolicies } from 'src/casl/check-policies.decorator'; +import { PoliciesGuard } from 'src/casl/policies.guard'; +import { Protocol, ProtocolStatus } from '../entities/protocol.entity'; +import { ProtocolDto } from './protocol.dto'; + +@Controller('protocols/statuses') +export class ProtocolsStatusesController { + @Get() + @HttpCode(200) + @UseGuards(PoliciesGuard) + @CheckPolicies((ability: Ability) => ability.can(Action.Manage, Protocol)) + @UsePipes(new ValidationPipe({ transform: true })) + index(): ProtocolDto[] { + const result: ProtocolDto[] = []; + + Object.values(ProtocolStatus).forEach((status: ProtocolStatus) => { + const v = new ProtocolDto(); + v.status = status; + result.push(v); + }); + + return result; + } +} diff --git a/src/protocols/protocols.module.ts b/src/protocols/protocols.module.ts index f2d424fd..1b230f79 100644 --- a/src/protocols/protocols.module.ts +++ b/src/protocols/protocols.module.ts @@ -10,6 +10,7 @@ import { ProtocolsRepository } from './entities/protocols.repository'; import { Violation } from '../violations/entities/violation.entity'; import { ViolationsModule } from '../violations/violations.module'; import { SectionsModule } from 'src/sections/sections.module'; +import { ProtocolsStatusesController } from './api/protocols-statuses.controller'; @Module({ imports: [ @@ -20,7 +21,11 @@ import { SectionsModule } from 'src/sections/sections.module'; SectionsModule, ViolationsModule, ], - controllers: [ProtocolsController, ProtocolAssigneesController], + controllers: [ + ProtocolsStatusesController, + ProtocolsController, + ProtocolAssigneesController, + ], providers: [ProtocolsRepository], exports: [ProtocolsRepository], }) From 4bc4b8f02a79d1d71fce6d46def5ca8cd62ed2dd Mon Sep 17 00:00:00 2001 From: Haralan Dobrev Date: Mon, 28 Jun 2021 06:55:02 +0300 Subject: [PATCH 2/2] Remove unused protocols status --- src/protocols/entities/protocol.entity.ts | 1 - src/protocols/entities/protocols.repository.ts | 4 ---- 2 files changed, 5 deletions(-) diff --git a/src/protocols/entities/protocol.entity.ts b/src/protocols/entities/protocol.entity.ts index 46252a0f..843012ad 100644 --- a/src/protocols/entities/protocol.entity.ts +++ b/src/protocols/entities/protocol.entity.ts @@ -27,7 +27,6 @@ export enum ProtocolStatus { REPLACED = 'replaced', READY = 'ready', APPROVED = 'approved', - PUBLISHING = 'publishing', PUBLISHED = 'published', } diff --git a/src/protocols/entities/protocols.repository.ts b/src/protocols/entities/protocols.repository.ts index 47c051a0..a0bcee68 100644 --- a/src/protocols/entities/protocols.repository.ts +++ b/src/protocols/entities/protocols.repository.ts @@ -212,10 +212,6 @@ export class ProtocolsRepository { return qb.getMany(); } - async markProtocolsAsPublishing(protocols: Protocol[]): Promise { - this.markProtocolsAs(ProtocolStatus.PUBLISHING, protocols); - } - async markProtocolsAsPublished(protocols: Protocol[]): Promise { this.markProtocolsAs(ProtocolStatus.PUBLISHED, protocols); }