133 lines
3.9 KiB
TypeScript
133 lines
3.9 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
ParseUUIDPipe,
|
|
Post,
|
|
Query,
|
|
Res,
|
|
UploadedFile,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { ApiBody, ApiConsumes, ApiProduces } from '@nestjs/swagger';
|
|
import type { Response } from 'express';
|
|
import { SchemaPipe } from '../common/validation.pipe';
|
|
import {
|
|
CurrentPrincipal,
|
|
RequirePermission,
|
|
} from '../identity/access.decorator';
|
|
import { pageSchema, type PageInput } from '../identity/identity.schemas';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import {
|
|
MAX_DOCUMENT_BYTES,
|
|
type UploadedDocument,
|
|
} from './supplier-document-file';
|
|
import { SupplierDocumentStore } from './supplier-document.store';
|
|
import {
|
|
supplierDocumentSchema,
|
|
documentDecisionSchema,
|
|
type SupplierDocumentInput,
|
|
type DocumentDecisionInput,
|
|
} from './supplier-document.schemas';
|
|
|
|
@Controller('suppliers/:supplierId/documents')
|
|
export class SupplierDocumentsController {
|
|
constructor(private readonly documents: SupplierDocumentStore) {}
|
|
@Get()
|
|
@RequirePermission('supplier_document.read')
|
|
list(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('supplierId', ParseUUIDPipe) supplierId: string,
|
|
@Query(new SchemaPipe(pageSchema)) query: PageInput,
|
|
) {
|
|
return this.documents.list(actor, supplierId, query);
|
|
}
|
|
@Post()
|
|
@RequirePermission('supplier_document.manage')
|
|
create(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('supplierId', ParseUUIDPipe) supplierId: string,
|
|
@Body(new SchemaPipe(supplierDocumentSchema)) input: SupplierDocumentInput,
|
|
) {
|
|
return this.documents.create(actor, supplierId, input);
|
|
}
|
|
}
|
|
|
|
@Controller('supplier-documents')
|
|
export class SupplierDocumentController {
|
|
constructor(private readonly documents: SupplierDocumentStore) {}
|
|
@Get(':id')
|
|
@RequirePermission('supplier_document.read')
|
|
get(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
) {
|
|
return this.documents.find(actor, id);
|
|
}
|
|
@Post(':id/versions')
|
|
@RequirePermission('supplier_document.manage')
|
|
@ApiConsumes('multipart/form-data')
|
|
@ApiBody({
|
|
schema: {
|
|
type: 'object',
|
|
required: ['file'],
|
|
properties: { file: { type: 'string', format: 'binary' } },
|
|
},
|
|
})
|
|
@UseInterceptors(
|
|
FileInterceptor('file', {
|
|
limits: { fileSize: MAX_DOCUMENT_BYTES + 1, files: 1, fields: 0 },
|
|
}),
|
|
)
|
|
upload(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@UploadedFile() file?: UploadedDocument,
|
|
) {
|
|
return this.documents.addVersion(actor, id, file);
|
|
}
|
|
}
|
|
|
|
@Controller('supplier-document-versions')
|
|
export class SupplierDocumentVersionsController {
|
|
constructor(private readonly documents: SupplierDocumentStore) {}
|
|
@Get(':id')
|
|
@RequirePermission('supplier_document.read')
|
|
get(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
) {
|
|
return this.documents.findVersion(actor, id);
|
|
}
|
|
@Get(':id/download')
|
|
@RequirePermission('supplier_document.read')
|
|
@ApiProduces('application/pdf')
|
|
async download(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Res() response: Response,
|
|
) {
|
|
const file = await this.documents.download(actor, id);
|
|
response.setHeader('Content-Type', 'application/pdf');
|
|
response.setHeader(
|
|
'Content-Disposition',
|
|
`attachment; filename="${file.filename}"`,
|
|
);
|
|
response.setHeader('Content-Security-Policy', 'sandbox');
|
|
response.setHeader('X-Content-Type-Options', 'nosniff');
|
|
response.setHeader('Cache-Control', 'no-store');
|
|
response.send(file.content);
|
|
}
|
|
@Post(':id/decision')
|
|
@RequirePermission('supplier_document.validate')
|
|
decide(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body(new SchemaPipe(documentDecisionSchema)) input: DocumentDecisionInput,
|
|
) {
|
|
return this.documents.decide(actor, id, input);
|
|
}
|
|
}
|