import { Body, Controller, Get, Param, Post, Query, Req, UseGuards } from '@nestjs/common';
import { AuthGuard } from '../../../common/auth/auth.guard';
import { OrganizationContextGuard, type TenantScopedRequest } from '../../../common/auth/organization-context.guard';
import { PermissionGuard } from '../../../common/auth/permission.guard';
import { RequirePermissions } from '../../../common/auth/permissions.decorator';
import { KnowledgeService } from '../application/knowledge.service';
import { KnowledgeReadModelService } from '../read-models/knowledge-read-model.service';
import { ZodValidationPipe } from '../../../common/validation/zod-validation.pipe';
import { knowledgeDraftSchema,knowledgeAcknowledgeSchema } from '../../../common/validation/critical-write.schemas';
import type { z } from 'zod';
type KnowledgeDraftBody = z.infer<typeof knowledgeDraftSchema>;
type KnowledgeAcknowledgeBody = z.infer<typeof knowledgeAcknowledgeSchema>;
@Controller('/knowledge')
@UseGuards(AuthGuard, OrganizationContextGuard, PermissionGuard)
export class KnowledgeController {
constructor(
private readonly knowledge: KnowledgeService,
private readonly readModel: KnowledgeReadModelService,
) {}
@Get('/:articleId')
@RequirePermissions('knowledge.read')
get(
@Req() req: TenantScopedRequest,
@Param('articleId') articleId: string,
@Query('employeeId') employeeId: string,
@Query('locale') locale?: string,
) {
return this.readModel.getPublished({
organizationId: req.organizationId!,
articleId,
employeeId,
locale: locale ?? req.auth?.locale ?? 'en-GB',
});
}
@Post()
@RequirePermissions('knowledge.manage')
create(@Req() req: TenantScopedRequest, @Body(new ZodValidationPipe(knowledgeDraftSchema)) body:KnowledgeDraftBody) {
return this.knowledge.createDraft({
organizationId: req.organizationId!,
slug: body.slug,
title: body.title,
bodyMarkdown: body.bodyMarkdown,
articleType: body.articleType,
sourceLocale: body.sourceLocale,
createdBy: req.auth?.userId,
});
}
@Post('/versions/:versionId/publish')
@RequirePermissions('knowledge.publish')
publish(@Req() req: TenantScopedRequest, @Param('versionId') versionId: string) {
return this.knowledge.publish(req.organizationId!, versionId);
}
@Post('/versions/:versionId/acknowledge')
@RequirePermissions('knowledge.acknowledge')
acknowledge(
@Req() req: TenantScopedRequest,
@Param('versionId') versionId: string,
@Body(new ZodValidationPipe(knowledgeAcknowledgeSchema)) body:KnowledgeAcknowledgeBody,
) {
return this.knowledge.acknowledge({
organizationId: req.organizationId!,
articleVersionId: versionId,
employeeId: body.employeeId,
localeShown: body.localeShown,
});
}
}
