Skip to content

Commit 3095417

Browse files
committed
feat: implement audits controller with search and detail retrieval endpoints, and add audits tab in identities table
1 parent 36f4967 commit 3095417

File tree

3 files changed

+557
-2
lines changed

3 files changed

+557
-2
lines changed

apps/api/src/core/audits/audits.controller.ts

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { Controller } from '@nestjs/common'
1+
import { Controller, Get, HttpStatus, Param, Res } from '@nestjs/common'
22
import { AbstractController } from '~/_common/abstracts/abstract.controller'
33
import { ApiTags } from '@nestjs/swagger'
44
import { PartialProjectionType } from '~/_common/types/partial-projection.type'
55
import { 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
}

apps/web/src/pages/identities/table.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ export default defineNuxtComponent({
192192
action: (i) => navigateToTab(`/identities/table/${i._id}`),
193193
condition: () => hasPermission('/management/identities', 'read'),
194194
},
195+
{
196+
name: 'audits',
197+
icon: 'mdi-clipboard-text-clock',
198+
label: 'Historique des changements',
199+
action: (i) => navigateToTab(`/identities/table/${i._id}/audits`),
200+
condition: () => hasPermission('/core/audits', 'read'),
201+
},
195202
{
196203
name: 'jobs',
197204
icon: 'mdi-book-clock',

0 commit comments

Comments
 (0)