Skip to content
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

Implement endpoint for listing all protocols origins with translations #64

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/i18n/bg/origin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"PROTOCOL_TI_BROISH": "Ти Броиш",
"PROTOCOL_CIK": "ЦИК"
}
4 changes: 4 additions & 0 deletions src/i18n/en/origin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"PROTOCOL_TI_BROISH": "You Count",
"PROTOCOL_CIK": "CEC"
}
50 changes: 50 additions & 0 deletions src/protocols/api/protocols-origins.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Ability } from '@casl/ability';
import {
Controller,
Get,
HttpCode,
UsePipes,
ValidationPipe,
UseGuards,
} from '@nestjs/common';
import { string } from 'joi';
import { I18nService } from 'nestjs-i18n';
import { Action } from 'src/casl/action.enum';
import { CheckPolicies } from 'src/casl/check-policies.decorator';
import { PoliciesGuard } from 'src/casl/policies.guard';
import { Protocol, ProtocolOrigin } from '../entities/protocol.entity';
import { ProtocolDto } from './protocol.dto';

interface OriginResponse {
[key: string]: string;
}

@Controller('protocols/origins')
export class ProtocolsOriginsController {
constructor(private readonly i18n: I18nService) {}

@Get()
@HttpCode(200)
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: Ability) => ability.can(Action.Manage, Protocol))
@UsePipes(new ValidationPipe({ transform: true }))
async index(): Promise<OriginResponse[]> {
return await Promise.all(
Object.keys(ProtocolOrigin).map((originKey) =>
this.formatOrigin(originKey, ProtocolOrigin[originKey]),
),
);
}

private async formatOrigin(
originKey: string,
originValue: string,
): Promise<OriginResponse> {
return {
origin: originValue,
originLocalized: await this.i18n.translate(
`origin.PROTOCOL_${originKey}`,
),
} as OriginResponse;
}
}
2 changes: 2 additions & 0 deletions src/protocols/protocols.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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';
import { ProtocolsOriginsController } from './api/protocols-origins.controller';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { ProtocolsStatusesController } from './api/protocols-statuses.controller
],
controllers: [
ProtocolsStatusesController,
ProtocolsOriginsController,
ProtocolsController,
ProtocolAssigneesController,
],
Expand Down