1- import { Controller } from '@nestjs/common'
1+ import { Controller , Get , HttpStatus , Param , Res } from '@nestjs/common'
22import { AbstractController } from '~/_common/abstracts/abstract.controller'
33import { ApiTags } from '@nestjs/swagger'
44import { PartialProjectionType } from '~/_common/types/partial-projection.type'
55import { AuditsService } from '~/core/audits/audits.service'
6+ import { UseRoles } from '~/_common/decorators/use-roles.decorator'
7+ import { AC_ACTIONS , AC_DEFAULT_POSSESSION } from '~/_common/types/ac-types'
8+ import { FilterOptions , SearchFilterOptions } from '~/_common/restools'
9+ import { Response } from 'express'
10+ import { ObjectIdValidationPipe } from '~/_common/pipes/object-id-validation.pipe'
11+ import { Types } from 'mongoose'
612
713/**
814 * Contrôleur pour la gestion des audits et de l'historique des enregistrements.
@@ -27,7 +33,24 @@ export class AuditsController extends AbstractController {
2733 * Configuration de la projection pour limiter les champs retournés.
2834 * Par défaut, tous les champs sont retournés (projection vide).
2935 */
30- protected static readonly projection : PartialProjectionType < any > = { }
36+ protected static readonly projection : PartialProjectionType < any > = {
37+ coll : 1 ,
38+ documentId : 1 ,
39+ op : 1 ,
40+ agent : 1 ,
41+ 'changes.path' : 1 ,
42+ 'changes.type' : 1 ,
43+ metadata : 1 ,
44+ }
45+
46+ protected static readonly detailProjection : PartialProjectionType < any > = {
47+ coll : 1 ,
48+ documentId : 1 ,
49+ op : 1 ,
50+ agent : 1 ,
51+ changes : 1 ,
52+ metadata : 1 ,
53+ }
3154
3255 /**
3356 * Constructeur du contrôleur AuditsController.
@@ -37,4 +60,66 @@ export class AuditsController extends AbstractController {
3760 public constructor ( private readonly _service : AuditsService ) {
3861 super ( )
3962 }
63+
64+ @Get ( )
65+ @UseRoles ( {
66+ resource : '/core/audits' ,
67+ action : AC_ACTIONS . READ ,
68+ possession : AC_DEFAULT_POSSESSION ,
69+ } )
70+ public async search (
71+ @Res ( ) res : Response ,
72+ @SearchFilterOptions ( ) searchFilterOptions : FilterOptions ,
73+ ) : Promise < Response > {
74+ const [ data , total ] = await this . _service . findAndCount ( { } , AuditsController . projection , searchFilterOptions )
75+ return res . status ( HttpStatus . OK ) . json ( {
76+ statusCode : HttpStatus . OK ,
77+ total,
78+ data,
79+ } )
80+ }
81+
82+ @Get ( ':coll/:documentId' )
83+ @UseRoles ( {
84+ resource : '/core/audits' ,
85+ action : AC_ACTIONS . READ ,
86+ possession : AC_DEFAULT_POSSESSION ,
87+ } )
88+ public async searchByDocumentId (
89+ @Param ( 'coll' ) coll : string ,
90+ @Param ( 'documentId' , ObjectIdValidationPipe ) documentId : Types . ObjectId ,
91+ @Res ( ) res : Response ,
92+ @SearchFilterOptions ( ) searchFilterOptions : FilterOptions ,
93+ ) : Promise < Response > {
94+ const [ data , total ] = await this . _service . findAndCount (
95+ {
96+ coll,
97+ documentId,
98+ } as any ,
99+ AuditsController . projection ,
100+ searchFilterOptions ,
101+ )
102+ return res . status ( HttpStatus . OK ) . json ( {
103+ statusCode : HttpStatus . OK ,
104+ total,
105+ data,
106+ } )
107+ }
108+
109+ @Get ( ':_id([0-9a-fA-F]{24})' )
110+ @UseRoles ( {
111+ resource : '/core/audits' ,
112+ action : AC_ACTIONS . READ ,
113+ possession : AC_DEFAULT_POSSESSION ,
114+ } )
115+ public async read (
116+ @Param ( '_id' , ObjectIdValidationPipe ) _id : Types . ObjectId ,
117+ @Res ( ) res : Response ,
118+ ) : Promise < Response > {
119+ const data = await this . _service . findById ( _id , AuditsController . detailProjection )
120+ return res . status ( HttpStatus . OK ) . json ( {
121+ statusCode : HttpStatus . OK ,
122+ data,
123+ } )
124+ }
40125}
0 commit comments