1- import {
1+ import {
22 BadRequestException ,
3+ ConflictException ,
34 Controller ,
5+ Get ,
6+ NotFoundException ,
7+ Param ,
48 Post ,
59 Req ,
610 Res ,
@@ -20,6 +24,8 @@ import { DocumentsService } from './documents.service';
2024import { DocumentStatus } from './entities/document.entity' ;
2125import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard' ;
2226import { User } from '../users/entities/user.entity' ;
27+ import { QueueService } from '../queue/queue.service' ;
28+ import { VerificationService } from '../verification/verification.service' ;
2329
2430const ALLOWED_MIME_TYPES = [ 'application/pdf' , 'image/png' , 'image/jpeg' ] ;
2531const MAX_FILE_SIZE_BYTES = 20 * 1024 * 1024 ;
@@ -39,6 +45,8 @@ export class DocumentsController {
3945 constructor (
4046 private readonly documentsService : DocumentsService ,
4147 private readonly configService : ConfigService ,
48+ private readonly queueService : QueueService ,
49+ private readonly verificationService : VerificationService ,
4250 ) { }
4351
4452 @Post ( 'upload' )
@@ -74,7 +82,7 @@ export class DocumentsController {
7482 await fs . mkdir ( uploadDir , { recursive : true } ) ;
7583
7684 const extension = extname ( file . originalname ) || '' ;
77- const filename = $ { fileHash} ;
85+ const filename = ` ${ fileHash } ${ extension } ` ;
7886 const targetPath = join ( uploadDir , filename ) ;
7987 await fs . writeFile ( targetPath , file . buffer ) ;
8088
@@ -88,6 +96,43 @@ export class DocumentsController {
8896 status : DocumentStatus . PENDING ,
8997 } ) ;
9098
91- return res . status ( 201 ) . send ( document ) ;
99+ await this . queueService . enqueueAnalyze ( document . id ) ;
100+ return res . status ( 202 ) . send ( document ) ;
101+ }
102+
103+ @Post ( ':id/verify' )
104+ @UseGuards ( JwtAuthGuard )
105+ async verifyDocument ( @Param ( 'id' ) id : string , @Res ( ) res : Response ) {
106+ const document = await this . documentsService . findById ( id ) ;
107+ if ( ! document ) {
108+ throw new NotFoundException ( 'Document not found' ) ;
109+ }
110+
111+ if ( document . status === DocumentStatus . VERIFIED ) {
112+ throw new ConflictException ( 'Document has already been verified' ) ;
113+ }
114+
115+ await this . queueService . enqueueAnchor ( document . id ) ;
116+
117+ return res . status ( 202 ) . json ( {
118+ message : 'Verification queued' ,
119+ documentId : document . id ,
120+ } ) ;
121+ }
122+
123+ @Get ( ':id/verification' )
124+ @UseGuards ( JwtAuthGuard )
125+ async getVerification ( @Param ( 'id' ) id : string ) {
126+ const document = await this . documentsService . findById ( id ) ;
127+ if ( ! document ) {
128+ throw new NotFoundException ( 'Document not found' ) ;
129+ }
130+
131+ const record = await this . verificationService . findLatestByDocument ( id ) ;
132+ if ( ! record ) {
133+ throw new NotFoundException ( 'No verification record found for this document' ) ;
134+ }
135+
136+ return record ;
92137 }
93138}
0 commit comments