@goplusvn/core 0.1.54 → 0.1.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,6 +40,12 @@ GENERATED — đừng sửa tay) và tạo thư mục migration
40
40
  thái DB. Runtime: `@goerp/core/cron` (`configureCronManager`) + trang admin
41
41
  `system/pages/system-jobs-page` (app cung cấp API, mẫu vinhhoa
42
42
  /api/admin/system/jobs).
43
+ - `audit-logs` — bảng `audit_logs` cho nhật ký thay đổi dữ liệu. Runtime:
44
+ `@goerp/core/audit` (`createAuditExtension` chộp tự động create/update/delete
45
+ ở tầng Prisma, `logEntityAction` ghi hành động nghiệp vụ, `withAuditContext`
46
+ đưa actor xuống) + trang admin `system/pages/system-audit-page`. YÊU CẦU
47
+ back-relation `auditLogs AuditLog[]` trên model `User` — xem
48
+ `features/audit-logs/README.md`.
43
49
  - `error-logs` — bảng `error_logs` cho hệ ghi lỗi server
44
50
  (`createErrorLogger`/`buildServerError` ở `errors/server-error`) + trang
45
51
  admin `system/pages/error-logs-page` (app cung cấp API, mẫu vinhhoa
@@ -0,0 +1,18 @@
1
+ # Feature: audit-logs
2
+
3
+ Bảng `audit_logs` cho `@goerp/core/audit`: extension Prisma
4
+ (`createAuditExtension`) chộp tự động create/update/delete, `logEntityAction`
5
+ ghi hành động nghiệp vụ, trang admin `SystemAuditPage` đọc.
6
+
7
+ **Yêu cầu phía app (Prisma bắt buộc 2 chiều relation):** model `User` phải khai
8
+
9
+ ```prisma
10
+ auditLogs AuditLog[]
11
+ ```
12
+
13
+ **Bảng người dùng phải tên `users`** — SQL neo FK `audit_logs.user_id → users.id`.
14
+
15
+ **Trước khi bật extension, xem lại `skipModels`:** mặc định core đã bỏ qua bảng
16
+ hạ tầng (audit/auth/log/task/notification). App còn bảng ghi dày kiểu hàng đợi
17
+ tin nhắn thì khai thêm, không thì nhật ký nghiệp vụ bị rác nhấn chìm — đo ở
18
+ vinhhoa: 67% số hàng audit 7 ngày là từ 1 bảng hàng đợi ZNS.
@@ -0,0 +1,33 @@
1
+ -- goerp feature: audit-logs — bước 0001 (idempotent). Bảng `audit_logs` cho
2
+ -- @goerp/core/audit: extension Prisma chộp create/update/delete tự động +
3
+ -- logEntityAction ghi hành động nghiệp vụ, và trang admin SystemAuditPage đọc.
4
+ -- Tên index/constraint giữ đúng bản Prisma sinh ra để app đã có bảng (vinhhoa)
5
+ -- chạy lại là no-op, không đẻ index trùng.
6
+ CREATE TABLE IF NOT EXISTS "audit_logs" (
7
+ "id" TEXT NOT NULL,
8
+ "user_id" TEXT,
9
+ "action" TEXT NOT NULL,
10
+ "resource" TEXT NOT NULL,
11
+ "resource_id" TEXT,
12
+ "old_data" JSONB,
13
+ "new_data" JSONB,
14
+ "ip_address" TEXT,
15
+ "user_agent" TEXT,
16
+ "description" TEXT,
17
+ "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
18
+
19
+ CONSTRAINT "audit_logs_pkey" PRIMARY KEY ("id")
20
+ );
21
+
22
+ CREATE INDEX IF NOT EXISTS "audit_logs_user_id_idx" ON "audit_logs"("user_id");
23
+ CREATE INDEX IF NOT EXISTS "audit_logs_action_idx" ON "audit_logs"("action");
24
+ CREATE INDEX IF NOT EXISTS "audit_logs_resource_idx" ON "audit_logs"("resource");
25
+ CREATE INDEX IF NOT EXISTS "audit_logs_created_at_idx" ON "audit_logs"("created_at");
26
+
27
+ -- FK: PG không có ADD CONSTRAINT IF NOT EXISTS → DO-block kiểm tra pg_constraint.
28
+ DO $$ BEGIN
29
+ IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'audit_logs_user_id_fkey') THEN
30
+ ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_user_id_fkey"
31
+ FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
32
+ END IF;
33
+ END $$;
@@ -0,0 +1,20 @@
1
+ model AuditLog {
2
+ id String @id @default(cuid()) @map("id")
3
+ userId String? @map("user_id")
4
+ action String @map("action")
5
+ resource String @map("resource")
6
+ resourceId String? @map("resource_id")
7
+ oldData Json? @map("old_data")
8
+ newData Json? @map("new_data")
9
+ ipAddress String? @map("ip_address")
10
+ userAgent String? @map("user_agent")
11
+ description String? @map("description")
12
+ createdAt DateTime @default(now()) @map("created_at")
13
+ user User? @relation(fields: [userId], references: [id])
14
+
15
+ @@index([userId])
16
+ @@index([action])
17
+ @@index([resource])
18
+ @@index([createdAt])
19
+ @@map("audit_logs")
20
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@goplusvn/core",
3
3
  "description": "GoPlusVN Platform Kit - ERP kernel: layout, RBAC, CRUD, multi-tenant, system pages",
4
- "version": "0.1.54",
4
+ "version": "0.1.55",
5
5
  "private": false,
6
6
  "publishConfig": {
7
7
  "registry": "https://registry.npmjs.org",
@@ -81,7 +81,9 @@
81
81
  "./system/pages/system-category-page": "./src/system/pages/system-category-page.tsx",
82
82
  "./system/pages/error-logs-page": "./src/system/pages/error-logs-page.tsx",
83
83
  "./system/pages/system-jobs-page": "./src/system/pages/system-jobs-page.tsx",
84
+ "./system/pages/system-audit-page": "./src/system/pages/system-audit-page.tsx",
84
85
  "./cron": "./src/cron/index.ts",
86
+ "./audit": "./src/audit/index.ts",
85
87
  "./rbac/role-service": "./src/rbac/role-service.ts",
86
88
  "./rbac/resource-service": "./src/rbac/resource-service.ts",
87
89
  "./infrastructure/cron/cron-manager": "./src/infrastructure/cron/cron-manager.ts",
@@ -0,0 +1,174 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ import { getAuditContext, withAuditContext } from "../audit-context";
4
+ import { resolveAuditActor } from "../audit-actor";
5
+ import { logEntityAction } from "../entity-audit";
6
+
7
+ describe("withAuditContext / getAuditContext", () => {
8
+ it("ngoài context trả undefined (extension tự biết là không có actor)", () => {
9
+ expect(getAuditContext()).toBeUndefined();
10
+ });
11
+
12
+ it("giữ context xuyên qua await", async () => {
13
+ await withAuditContext(
14
+ { userId: "user-1", userName: "Nguyễn Văn A" },
15
+ async () => {
16
+ await Promise.resolve();
17
+ expect(getAuditContext()).toEqual({
18
+ userId: "user-1",
19
+ userName: "Nguyễn Văn A",
20
+ });
21
+ },
22
+ );
23
+ });
24
+
25
+ it("lồng nhau: thoát context trong thì context ngoài trở lại", async () => {
26
+ await withAuditContext({ userId: "outer" }, async () => {
27
+ expect(getAuditContext()?.userId).toBe("outer");
28
+ await withAuditContext({ userId: "inner" }, async () => {
29
+ expect(getAuditContext()?.userId).toBe("inner");
30
+ });
31
+ expect(getAuditContext()?.userId).toBe("outer");
32
+ });
33
+ });
34
+
35
+ it("trả về giá trị của fn và không nuốt lỗi", async () => {
36
+ await expect(
37
+ withAuditContext({ userId: "u" }, async () => 42),
38
+ ).resolves.toBe(42);
39
+ await expect(
40
+ withAuditContext({ userId: "u" }, async () => {
41
+ throw new Error("test error");
42
+ }),
43
+ ).rejects.toThrow("test error");
44
+ });
45
+ });
46
+
47
+ describe("resolveAuditActor", () => {
48
+ it("phiên thường: lấy id + tên người dùng", async () => {
49
+ await expect(
50
+ resolveAuditActor({
51
+ user: { id: "u1", name: "Kế toán", email: "kt@x.vn" },
52
+ }),
53
+ ).resolves.toEqual({ userId: "u1", userName: "Kế toán" });
54
+ });
55
+
56
+ it("không có tên thì rơi về email", async () => {
57
+ await expect(
58
+ resolveAuditActor({ user: { id: "u1", name: null, email: "kt@x.vn" } }),
59
+ ).resolves.toEqual({ userId: "u1", userName: "kt@x.vn" });
60
+ });
61
+
62
+ it("mạo danh: audit quy về ADMIN THẬT, không phải người bị mạo danh", async () => {
63
+ const resolveName = vi.fn().mockResolvedValue("Quản trị");
64
+
65
+ await expect(
66
+ resolveAuditActor(
67
+ { user: { id: "u-nv", name: "Nhân viên" }, impersonatedBy: "u-admin" },
68
+ resolveName,
69
+ ),
70
+ ).resolves.toEqual({
71
+ userId: "u-admin",
72
+ userName: "Quản trị (mạo danh Nhân viên)",
73
+ });
74
+ expect(resolveName).toHaveBeenCalledWith("u-admin");
75
+ });
76
+
77
+ it("tra tên admin hỏng vẫn ghi được actor", async () => {
78
+ await expect(
79
+ resolveAuditActor(
80
+ { user: { id: "u-nv", name: "Nhân viên" }, impersonatedBy: "u-admin" },
81
+ async () => {
82
+ throw new Error("DB down");
83
+ },
84
+ ),
85
+ ).resolves.toEqual({
86
+ userId: "u-admin",
87
+ userName: "Admin (mạo danh Nhân viên)",
88
+ });
89
+ });
90
+ });
91
+
92
+ describe("logEntityAction", () => {
93
+ const makeClient = () => ({
94
+ auditLog: { create: vi.fn().mockResolvedValue({ id: "a1" }) },
95
+ });
96
+
97
+ it("ghi qua ĐÚNG client được truyền (tx → rollback cùng nghiệp vụ)", async () => {
98
+ const tx = makeClient();
99
+ await logEntityAction({
100
+ resource: "sales-order",
101
+ resourceId: "so-1",
102
+ action: "confirm-payment",
103
+ description: "Xác nhận thanh toán",
104
+ userId: "u1",
105
+ userName: "Kế toán",
106
+ referenceNumber: "DH001",
107
+ newData: { amount: 500000 },
108
+ client: tx,
109
+ });
110
+
111
+ expect(tx.auditLog.create).toHaveBeenCalledWith({
112
+ data: {
113
+ action: "confirm-payment",
114
+ resource: "sales-order",
115
+ resourceId: "so-1",
116
+ userId: "u1",
117
+ description: "[Kế toán] Xác nhận thanh toán",
118
+ oldData: undefined,
119
+ newData: { amount: 500000, orderNumber: "DH001" },
120
+ },
121
+ });
122
+ });
123
+
124
+ it("không có userName thì mô tả giữ nguyên, không có tiền tố []", async () => {
125
+ const client = makeClient();
126
+ await logEntityAction({
127
+ resource: "sales-order",
128
+ resourceId: "so-1",
129
+ action: "cancel",
130
+ description: "Huỷ đơn",
131
+ client,
132
+ });
133
+
134
+ expect(client.auditLog.create.mock.calls[0][0].data.description).toBe(
135
+ "Huỷ đơn",
136
+ );
137
+ });
138
+
139
+ it("chỉ có số chứng từ vẫn nhét được vào newData để tra cứu", async () => {
140
+ const client = makeClient();
141
+ await logEntityAction({
142
+ resource: "purchase-order",
143
+ resourceId: "po-1",
144
+ action: "receive",
145
+ description: "Nhận hàng",
146
+ referenceNumber: "PN001",
147
+ client,
148
+ });
149
+
150
+ expect(client.auditLog.create.mock.calls[0][0].data.newData).toEqual({
151
+ orderNumber: "PN001",
152
+ });
153
+ });
154
+
155
+ it("audit hỏng KHÔNG throw ra nghiệp vụ", async () => {
156
+ const client = {
157
+ auditLog: { create: vi.fn().mockRejectedValue(new Error("DB fail")) },
158
+ };
159
+ const onError = vi.fn();
160
+
161
+ await expect(
162
+ logEntityAction({
163
+ resource: "sales-order",
164
+ resourceId: "so-1",
165
+ action: "cancel",
166
+ description: "Huỷ đơn",
167
+ client,
168
+ onError,
169
+ }),
170
+ ).resolves.toBeUndefined();
171
+
172
+ expect(onError).toHaveBeenCalledWith(expect.any(Error));
173
+ });
174
+ });