Bỏ qua, đến nội dung

@digiforce-nc/plugin-audit-logs

Plugin ghi lại nhật ký thao tác (audit trail) cho mọi hoạt động CRUD trong hệ thống — ai đã làm gì, khi nào, trên dữ liệu nào, giá trị cũ/mới.

Plugin này làm gì?

Mỗi khi người dùng tạo, sửa hoặc xóa bản ghi, plugin tự động ghi lại một audit log entry bao gồm: người thực hiện, thời gian, collection, action, và diff dữ liệu (before/after).

Ba nhiệm vụ chính

#Nhiệm vụChi tiết
1Ghi nhận tự độngGắn DB hooks (afterCreate/afterUpdate/afterDestroy) lên tất cả collection
2Lưu trữ diffTính toán giá trị cũ/mới cho từng field thay đổi, lưu vào auditChanges
3Giao diện tra cứuBlock audit logs trên page/popup, bộ lọc theo user/collection/action/thời gian

Kiến trúc

Luồng ghi audit log

API endpoints

EndpointPhương thứcMô tả
auditLogs:listGETDanh sách audit log, hỗ trợ filter/sort/paginate
auditLogs:getGETChi tiết một audit log entry
auditChanges:listGETDanh sách thay đổi field trong một audit log

Database - 2 bảng chính

BảngChứa gìVí dụ
auditLogsEntry chính: ai, làm gì, trên collection nào{ userId: 1, collectionName: 'posts', action: 'update' }
auditChangesChi tiết thay đổi từng field{ field: 'title', before: 'Draft', after: 'Published' }

Vòng đời plugin

DB Hooks - cơ chế ghi nhận

HookKhi nàoGhi nhận gì
afterCreateSau khi tạo bản ghiaction = create, before = null, after = giá trị mới
afterUpdateSau khi cập nhậtaction = update, before = giá trị cũ, after = giá trị mới
afterDestroySau khi xóaaction = destroy, before = giá trị cũ, after = null

Ví dụ sử dụng API

Truy vấn audit logs

typescript
const response = await agent.resource('auditLogs').list({
  filter: { collectionName: 'posts' },
  sort: ['-createdAt'],
  pageSize: 20,
});
const { data } = response.body;

Xem chi tiết thay đổi

typescript
const response = await agent.resource('auditChanges').list({
  filter: { auditLogId: 100 },
});
// [{ field: 'title', before: 'Draft', after: 'Published' }]

Lọc theo user và khoảng thời gian

typescript
const response = await agent.resource('auditLogs').list({
  filter: {
    userId: 5,
    createdAt: { $gte: '2024-01-01', $lte: '2024-12-31' },
  },
});

Thành phần client

Thành phầnMô tả
AuditLogsProviderContext provider cung cấp dữ liệu audit cho UI
AuditLogsBlockInitializerBlock initializer thêm audit logs block vào page/popup
AuditLogsTableBảng hiển thị danh sách audit log với filter, sort, paginate
AuditLogDetailChi tiết entry: diff before/after dạng visual

Dependencies

PackageVai trò
@digiforce-nc/serverServer framework — gắn hooks vào DB
@digiforce-nc/databaseDatabase ORM — collection definitions, hooks API
@digiforce-nc/clientClient framework — đăng ký UI components
@digiforce-nc/actionsAction context — lấy thông tin user, collection từ request

Mục lục chi tiết