@nest-omni/core 4.1.3-1 → 4.1.3-11

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.
Files changed (249) hide show
  1. package/audit/audit.module.d.ts +10 -0
  2. package/audit/audit.module.js +39 -1
  3. package/audit/controllers/audit.controller.d.ts +24 -0
  4. package/audit/controllers/audit.controller.js +24 -0
  5. package/audit/decorators/audit-controller.decorator.d.ts +9 -1
  6. package/audit/decorators/audit-controller.decorator.js +11 -2
  7. package/audit/decorators/audit-operation.decorator.d.ts +45 -0
  8. package/audit/decorators/audit-operation.decorator.js +49 -0
  9. package/audit/decorators/entity-audit.decorator.d.ts +76 -1
  10. package/audit/decorators/entity-audit.decorator.js +135 -3
  11. package/audit/decorators/index.d.ts +1 -0
  12. package/audit/decorators/index.js +1 -0
  13. package/audit/dto/audit-log-query.dto.d.ts +3 -0
  14. package/audit/dto/audit-log-query.dto.js +3 -0
  15. package/audit/dto/begin-transaction.dto.d.ts +3 -0
  16. package/audit/dto/begin-transaction.dto.js +3 -0
  17. package/audit/dto/compare-entities.dto.d.ts +3 -0
  18. package/audit/dto/compare-entities.dto.js +3 -0
  19. package/audit/dto/pre-check-restore.dto.d.ts +3 -0
  20. package/audit/dto/pre-check-restore.dto.js +3 -0
  21. package/audit/dto/restore-entity.dto.d.ts +3 -0
  22. package/audit/dto/restore-entity.dto.js +3 -0
  23. package/audit/entities/entity-audit-log.entity.d.ts +8 -0
  24. package/audit/entities/entity-audit-log.entity.js +33 -1
  25. package/audit/entities/entity-transaction.entity.d.ts +10 -0
  26. package/audit/entities/entity-transaction.entity.js +33 -1
  27. package/audit/entities/index.d.ts +2 -0
  28. package/audit/entities/index.js +2 -0
  29. package/audit/entities/manual-operation-log.entity.d.ts +4 -0
  30. package/audit/entities/manual-operation-log.entity.js +4 -0
  31. package/audit/entities/operation-template.entity.d.ts +4 -0
  32. package/audit/entities/operation-template.entity.js +4 -0
  33. package/audit/enums/audit.enums.d.ts +45 -5
  34. package/audit/enums/audit.enums.js +47 -4
  35. package/audit/index.d.ts +3 -1
  36. package/audit/index.js +30 -1
  37. package/audit/interceptors/audit.interceptor.d.ts +15 -0
  38. package/audit/interceptors/audit.interceptor.js +23 -1
  39. package/audit/interfaces/audit.interfaces.d.ts +182 -2
  40. package/audit/services/audit-context.service.d.ts +15 -0
  41. package/audit/services/audit-context.service.js +15 -0
  42. package/audit/services/audit-strategy.service.d.ts +6 -0
  43. package/audit/services/audit-strategy.service.js +13 -0
  44. package/audit/services/entity-audit.service.d.ts +129 -3
  45. package/audit/services/entity-audit.service.js +301 -6
  46. package/audit/services/index.d.ts +2 -0
  47. package/audit/services/index.js +2 -0
  48. package/audit/services/manual-audit-log.service.d.ts +124 -0
  49. package/audit/services/manual-audit-log.service.js +138 -0
  50. package/audit/services/multi-database.service.d.ts +12 -0
  51. package/audit/services/multi-database.service.js +12 -0
  52. package/audit/services/operation-description.service.d.ts +59 -0
  53. package/audit/services/operation-description.service.js +76 -2
  54. package/audit/services/transaction-audit.service.d.ts +30 -0
  55. package/audit/services/transaction-audit.service.js +47 -0
  56. package/audit/subscribers/entity-audit.subscriber.d.ts +15 -0
  57. package/audit/subscribers/entity-audit.subscriber.js +29 -1
  58. package/cache/cache-metrics.service.d.ts +67 -0
  59. package/cache/cache-metrics.service.js +68 -4
  60. package/cache/cache-serialization.service.d.ts +31 -0
  61. package/cache/cache-serialization.service.js +25 -0
  62. package/cache/cache.constants.d.ts +9 -0
  63. package/cache/cache.constants.js +9 -0
  64. package/cache/cache.health.d.ts +26 -0
  65. package/cache/cache.health.js +30 -0
  66. package/cache/cache.module.d.ts +86 -0
  67. package/cache/cache.module.js +71 -0
  68. package/cache/cache.service.d.ts +140 -0
  69. package/cache/cache.service.js +157 -0
  70. package/cache/cache.warmup.service.d.ts +39 -0
  71. package/cache/cache.warmup.service.js +32 -0
  72. package/cache/decorators/cache-evict.decorator.d.ts +47 -0
  73. package/cache/decorators/cache-evict.decorator.js +56 -0
  74. package/cache/decorators/cache-put.decorator.d.ts +34 -0
  75. package/cache/decorators/cache-put.decorator.js +39 -0
  76. package/cache/decorators/cacheable.decorator.d.ts +40 -0
  77. package/cache/decorators/cacheable.decorator.js +55 -0
  78. package/cache/dependencies/callback.dependency.d.ts +33 -0
  79. package/cache/dependencies/callback.dependency.js +39 -1
  80. package/cache/dependencies/chain.dependency.d.ts +28 -0
  81. package/cache/dependencies/chain.dependency.js +34 -0
  82. package/cache/dependencies/db.dependency.d.ts +45 -0
  83. package/cache/dependencies/db.dependency.js +48 -1
  84. package/cache/dependencies/file.dependency.d.ts +32 -0
  85. package/cache/dependencies/file.dependency.js +34 -0
  86. package/cache/dependencies/tag.dependency.d.ts +36 -0
  87. package/cache/dependencies/tag.dependency.js +36 -0
  88. package/cache/dependencies/time.dependency.d.ts +43 -0
  89. package/cache/dependencies/time.dependency.js +43 -0
  90. package/cache/examples/basic-usage.d.ts +15 -0
  91. package/cache/examples/basic-usage.js +62 -8
  92. package/cache/index.js +9 -0
  93. package/cache/interfaces/cache-dependency.interface.d.ts +53 -0
  94. package/cache/interfaces/cache-options.interface.d.ts +81 -0
  95. package/cache/interfaces/cache-options.interface.js +6 -0
  96. package/cache/interfaces/cache-provider.interface.d.ts +78 -0
  97. package/cache/providers/base-cache.provider.d.ts +14 -0
  98. package/cache/providers/base-cache.provider.js +16 -0
  99. package/cache/providers/cls-cache.provider.d.ts +20 -0
  100. package/cache/providers/cls-cache.provider.js +28 -0
  101. package/cache/providers/memory-cache.provider.d.ts +23 -0
  102. package/cache/providers/memory-cache.provider.js +26 -0
  103. package/cache/providers/redis-cache.provider.d.ts +26 -0
  104. package/cache/providers/redis-cache.provider.js +29 -0
  105. package/cache/utils/dependency-manager.util.d.ts +52 -0
  106. package/cache/utils/dependency-manager.util.js +59 -0
  107. package/cache/utils/key-generator.util.d.ts +42 -0
  108. package/cache/utils/key-generator.util.js +53 -1
  109. package/common/abstract.entity.d.ts +14 -0
  110. package/common/abstract.entity.js +14 -0
  111. package/common/boilerplate.polyfill.d.ts +142 -4
  112. package/common/boilerplate.polyfill.js +24 -100
  113. package/common/dto/dto-container.d.ts +16 -0
  114. package/common/dto/dto-container.js +20 -0
  115. package/common/dto/dto-decorators.d.ts +18 -0
  116. package/common/dto/dto-decorators.js +14 -0
  117. package/common/dto/dto-extensions.d.ts +11 -0
  118. package/common/dto/dto-extensions.js +9 -0
  119. package/common/dto/dto-service-accessor.d.ts +17 -0
  120. package/common/dto/dto-service-accessor.js +18 -0
  121. package/common/dto/dto-transformer.d.ts +12 -0
  122. package/common/dto/dto-transformer.js +9 -0
  123. package/common/dto/index.js +2 -0
  124. package/common/examples/paginate-and-map.example.d.ts +6 -0
  125. package/common/examples/paginate-and-map.example.js +26 -0
  126. package/common/utils.d.ts +15 -0
  127. package/common/utils.js +15 -0
  128. package/constants/language-code.js +1 -0
  129. package/decorators/field.decorators.js +8 -1
  130. package/decorators/property.decorators.js +1 -0
  131. package/decorators/public-route.decorator.js +1 -0
  132. package/decorators/transform.decorators.d.ts +27 -0
  133. package/decorators/transform.decorators.js +29 -0
  134. package/decorators/translate.decorator.js +1 -0
  135. package/decorators/user.decorator.js +1 -0
  136. package/decorators/validator.decorators.d.ts +8 -18
  137. package/decorators/validator.decorators.js +22 -190
  138. package/filters/constraint-errors.js +1 -0
  139. package/helpers/common.helper.d.ts +13 -0
  140. package/helpers/common.helper.js +13 -0
  141. package/http-client/config/http-client.config.d.ts +15 -0
  142. package/http-client/config/http-client.config.js +25 -9
  143. package/http-client/decorators/http-client.decorators.d.ts +63 -0
  144. package/http-client/decorators/http-client.decorators.js +71 -3
  145. package/http-client/entities/http-log.entity.d.ts +229 -0
  146. package/http-client/entities/http-log.entity.js +6 -1
  147. package/http-client/errors/http-client.errors.d.ts +57 -0
  148. package/http-client/errors/http-client.errors.js +58 -0
  149. package/http-client/examples/advanced-usage.example.d.ts +41 -0
  150. package/http-client/examples/advanced-usage.example.js +68 -24
  151. package/http-client/examples/auth-with-waiting-lock.example.d.ts +31 -0
  152. package/http-client/examples/auth-with-waiting-lock.example.js +52 -5
  153. package/http-client/examples/basic-usage.example.d.ts +60 -0
  154. package/http-client/examples/basic-usage.example.js +60 -0
  155. package/http-client/examples/multi-api-configuration.example.d.ts +60 -0
  156. package/http-client/examples/multi-api-configuration.example.js +76 -5
  157. package/http-client/http-client.module.d.ts +13 -0
  158. package/http-client/http-client.module.js +20 -5
  159. package/http-client/index.js +8 -0
  160. package/http-client/interfaces/api-client-config.interface.d.ts +125 -0
  161. package/http-client/interfaces/api-client-config.interface.js +3 -0
  162. package/http-client/interfaces/http-client-config.interface.d.ts +60 -0
  163. package/http-client/services/api-client-registry.service.d.ts +57 -0
  164. package/http-client/services/api-client-registry.service.js +84 -1
  165. package/http-client/services/cache.service.d.ts +52 -0
  166. package/http-client/services/cache.service.js +72 -3
  167. package/http-client/services/circuit-breaker.service.d.ts +46 -0
  168. package/http-client/services/circuit-breaker.service.js +52 -0
  169. package/http-client/services/http-client.service.d.ts +67 -0
  170. package/http-client/services/http-client.service.js +105 -4
  171. package/http-client/services/http-log-query.service.d.ts +83 -0
  172. package/http-client/services/http-log-query.service.js +122 -1
  173. package/http-client/services/http-replay.service.d.ts +101 -0
  174. package/http-client/services/http-replay.service.js +86 -0
  175. package/http-client/services/log-cleanup.service.d.ts +63 -0
  176. package/http-client/services/log-cleanup.service.js +54 -2
  177. package/http-client/services/logging.service.d.ts +40 -0
  178. package/http-client/services/logging.service.js +53 -0
  179. package/http-client/utils/call-stack-extractor.util.d.ts +37 -0
  180. package/http-client/utils/call-stack-extractor.util.js +48 -0
  181. package/http-client/utils/context-extractor.util.d.ts +49 -0
  182. package/http-client/utils/context-extractor.util.js +52 -0
  183. package/http-client/utils/curl-generator.util.d.ts +21 -0
  184. package/http-client/utils/curl-generator.util.js +44 -3
  185. package/http-client/utils/request-id.util.d.ts +18 -0
  186. package/http-client/utils/request-id.util.js +20 -0
  187. package/http-client/utils/retry-recorder.util.d.ts +42 -0
  188. package/http-client/utils/retry-recorder.util.js +44 -0
  189. package/i18n/en_US/validation.json +2 -1
  190. package/i18n/zh_CN/validation.json +2 -1
  191. package/index.js +8 -0
  192. package/interceptors/translation-interceptor.service.js +5 -0
  193. package/package.json +1 -1
  194. package/providers/context.provider.js +2 -0
  195. package/providers/generator.provider.d.ts +4 -0
  196. package/providers/generator.provider.js +4 -0
  197. package/redis-lock/comprehensive-lock-cleanup.service.d.ts +94 -0
  198. package/redis-lock/comprehensive-lock-cleanup.service.js +253 -0
  199. package/redis-lock/examples/lock-strategy.examples.d.ts +89 -0
  200. package/redis-lock/examples/lock-strategy.examples.js +130 -15
  201. package/redis-lock/index.d.ts +2 -0
  202. package/redis-lock/index.js +8 -1
  203. package/redis-lock/lock-heartbeat.service.d.ts +78 -0
  204. package/redis-lock/lock-heartbeat.service.js +222 -0
  205. package/redis-lock/redis-lock.decorator.d.ts +101 -0
  206. package/redis-lock/redis-lock.decorator.js +120 -0
  207. package/redis-lock/redis-lock.module.d.ts +66 -0
  208. package/redis-lock/redis-lock.module.js +175 -70
  209. package/redis-lock/redis-lock.service.d.ts +278 -0
  210. package/redis-lock/redis-lock.service.js +282 -12
  211. package/setup/bootstrap.setup.js +20 -0
  212. package/setup/mode.setup.d.ts +44 -0
  213. package/setup/mode.setup.js +44 -0
  214. package/setup/schedule.decorator.d.ts +227 -0
  215. package/setup/schedule.decorator.js +235 -12
  216. package/setup/worker.decorator.d.ts +86 -0
  217. package/setup/worker.decorator.js +88 -0
  218. package/shared/serviceRegistryModule.js +27 -14
  219. package/shared/services/api-config.service.d.ts +3 -0
  220. package/shared/services/api-config.service.js +20 -9
  221. package/validator-json/decorators.d.ts +17 -0
  222. package/validator-json/decorators.js +17 -2
  223. package/validator-json/default.d.ts +6 -0
  224. package/validator-json/default.js +30 -2
  225. package/validator-json/defaultConverters.js +1 -0
  226. package/validator-json/options.d.ts +23 -0
  227. package/validators/common-validators.d.ts +143 -0
  228. package/validators/common-validators.js +249 -0
  229. package/validators/custom-validate.examples.d.ts +96 -0
  230. package/validators/custom-validate.examples.js +400 -0
  231. package/validators/custom-validate.validator.d.ts +134 -0
  232. package/validators/custom-validate.validator.js +214 -0
  233. package/validators/index.d.ts +2 -0
  234. package/validators/index.js +2 -0
  235. package/validators/is-exists.validator.d.ts +18 -4
  236. package/validators/is-exists.validator.js +67 -6
  237. package/validators/is-unique.validator.d.ts +32 -5
  238. package/validators/is-unique.validator.js +99 -17
  239. package/validators/skip-empty.validator.d.ts +5 -0
  240. package/validators/skip-empty.validator.js +5 -0
  241. package/vault/interfaces/vault-options.interface.d.ts +9 -0
  242. package/vault/vault-config.loader.d.ts +30 -0
  243. package/vault/vault-config.loader.js +48 -1
  244. package/vault/vault-config.service.d.ts +53 -0
  245. package/vault/vault-config.service.js +57 -0
  246. package/vault/vault.module.d.ts +4 -0
  247. package/vault/vault.module.js +4 -0
  248. package/decorators/examples/validation-decorators.example.d.ts +0 -69
  249. package/decorators/examples/validation-decorators.example.js +0 -331
@@ -1,4 +1,7 @@
1
- import { AuditOperation, MaskingStrategy, RecordStrategy } from '../enums/audit.enums';
1
+ import { AuditOperation, MaskingStrategy, RecordStrategy, RollbackActionType, ChangeType } from '../enums/audit.enums';
2
+ /**
3
+ * 审计上下文
4
+ */
2
5
  export interface AuditContext {
3
6
  userId?: string;
4
7
  username?: string;
@@ -9,16 +12,25 @@ export interface AuditContext {
9
12
  transactionId?: string;
10
13
  metadata?: Record<string, any>;
11
14
  }
15
+ /**
16
+ * 审计策略接口
17
+ */
12
18
  export interface IAuditStrategy {
13
19
  shouldRecord(entityType: string, operation: AuditOperation): boolean;
14
20
  getRecordStrategy(entityType: string, operation: AuditOperation): RecordStrategy;
15
21
  getFieldFilter(entityType: string): IFieldFilter;
16
22
  }
23
+ /**
24
+ * 字段过滤器接口
25
+ */
17
26
  export interface IFieldFilter {
18
27
  shouldIncludeField(fieldName: string, path: string[]): boolean;
19
28
  shouldMaskField(fieldName: string, path: string[]): boolean;
20
29
  getMaskingStrategy(fieldName: string, path: string[]): MaskingStrategy;
21
30
  }
31
+ /**
32
+ * 实体差异
33
+ */
22
34
  export interface EntityDifference {
23
35
  changes: Array<{
24
36
  path: string[];
@@ -32,6 +44,9 @@ export interface EntityDifference {
32
44
  changed: number;
33
45
  };
34
46
  }
47
+ /**
48
+ * 恢复选项
49
+ */
35
50
  export interface RestoreOptions {
36
51
  force?: boolean;
37
52
  dryRun?: boolean;
@@ -40,6 +55,9 @@ export interface RestoreOptions {
40
55
  skipDependencies?: boolean;
41
56
  connectionName?: string;
42
57
  }
58
+ /**
59
+ * 恢复结果
60
+ */
43
61
  export interface RestoreResult {
44
62
  success: boolean;
45
63
  message?: string;
@@ -54,6 +72,9 @@ export interface RestoreResult {
54
72
  entityId: string;
55
73
  }>;
56
74
  }
75
+ /**
76
+ * 预检查结果
77
+ */
57
78
  export interface PreCheckResult {
58
79
  canRestore: boolean;
59
80
  conflicts: Array<{
@@ -64,6 +85,9 @@ export interface PreCheckResult {
64
85
  }>;
65
86
  warnings: string[];
66
87
  }
88
+ /**
89
+ * 批量操作结果
90
+ */
67
91
  export interface BatchOperationResult {
68
92
  total: number;
69
93
  success: number;
@@ -74,6 +98,9 @@ export interface BatchOperationResult {
74
98
  }>;
75
99
  results: any[];
76
100
  }
101
+ /**
102
+ * 审计配置
103
+ */
77
104
  export interface AuditConfig {
78
105
  enabled?: boolean;
79
106
  defaultStrategy?: RecordStrategy;
@@ -94,26 +121,179 @@ export interface AuditConfig {
94
121
  };
95
122
  };
96
123
  }
124
+ /**
125
+ * 实体级别审计配置
126
+ */
127
+ /**
128
+ * 实体审计配置
129
+ *
130
+ * @description
131
+ * 支持基础审计配置
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * @EntityAudit({
136
+ * enabled: true,
137
+ * excludeFields: ['password'],
138
+ * maskFields: ['email', 'phone'],
139
+ * templateKey: 'user',
140
+ * })
141
+ * ```
142
+ */
97
143
  export interface EntityAuditConfig {
98
144
  enabled?: boolean;
99
145
  strategy?: RecordStrategy;
100
146
  includeFields?: string[];
101
147
  excludeFields?: string[];
102
148
  maskFields?: string[];
149
+ /**
150
+ * 操作模板键前缀
151
+ * @example 'user' -> 生成 'user.create', 'user.update', 'user.delete'
152
+ */
153
+ templateKey?: string;
154
+ /**
155
+ * @deprecated 使用 maskFields 代替
156
+ * 敏感字段列表(别名,用于兼容旧版本)
157
+ */
158
+ sensitiveFields?: string[];
103
159
  }
160
+ /**
161
+ * 操作级别审计配置
162
+ */
104
163
  export interface OperationAuditConfig {
105
164
  enabled?: boolean;
106
165
  strategy?: RecordStrategy;
107
166
  }
167
+ /**
168
+ * 字段显示选项
169
+ */
108
170
  export interface FieldDisplayOptions {
109
- displayName?: string;
171
+ /**
172
+ * 字段标签(支持多语言)
173
+ * @example
174
+ * // 单语言
175
+ * label: '姓名'
176
+ *
177
+ * // 多语言
178
+ * label: { zh: '姓名', en: 'Name' }
179
+ */
180
+ label?: string | Record<string, string>;
181
+ /**
182
+ * 字段值的多语言标签映射
183
+ * @example
184
+ * valueLabels: {
185
+ * active: { zh: '激活', en: 'Active' },
186
+ * inactive: { zh: '未激活', en: 'Inactive' }
187
+ * }
188
+ */
189
+ valueLabels?: Record<string, Record<string, string>>;
110
190
  sensitive?: boolean;
111
191
  formatter?: (value: any) => string;
112
192
  }
193
+ /**
194
+ * 控制器审计选项
195
+ */
113
196
  export interface ControllerAuditOptions {
114
197
  enabled?: boolean;
115
198
  operations?: AuditOperation[];
116
199
  }
200
+ /**
201
+ * 方法审计选项
202
+ */
117
203
  export interface MethodAuditOptions {
118
204
  enabled?: boolean;
119
205
  }
206
+ /**
207
+ * 变更详情
208
+ */
209
+ export interface ChangeDetail {
210
+ fieldName: string;
211
+ fieldLabels?: Record<string, string>;
212
+ oldValue: any;
213
+ newValue: any;
214
+ displayOldValue?: Record<string, string>;
215
+ displayNewValue?: Record<string, string>;
216
+ changeType: ChangeType;
217
+ }
218
+ /**
219
+ * 回滚操作
220
+ */
221
+ export interface RollbackAction {
222
+ type: RollbackActionType;
223
+ action: string;
224
+ params: Record<string, any>;
225
+ order: number;
226
+ handler?: string;
227
+ }
228
+ /**
229
+ * 操作模板配置(用于数据库存储)
230
+ */
231
+ export interface OperationTemplateData {
232
+ key: string;
233
+ entityName: string;
234
+ operation: AuditOperation;
235
+ nameTemplates: Record<string, string>;
236
+ descriptionTemplates: Record<string, string>;
237
+ fieldLabels?: Record<string, Record<string, string>>;
238
+ valueDisplays?: Record<string, Record<string, Record<string, string>>>;
239
+ }
240
+ /**
241
+ * 手动操作日志数据
242
+ */
243
+ export interface ManualOperationData {
244
+ transactionId: string;
245
+ operationTemplateKey: string;
246
+ descriptionParams: Record<string, any>;
247
+ userId?: string;
248
+ username?: string;
249
+ requestIp?: string;
250
+ rollbackActions?: RollbackAction[];
251
+ }
252
+ /**
253
+ * 手动日志记录选项(支持三种模式)
254
+ */
255
+ export interface ManualLogOptions {
256
+ templateKey?: string;
257
+ descriptionParams?: Record<string, any>;
258
+ description?: string | Record<string, string>;
259
+ descriptionTemplate?: Record<string, string>;
260
+ userId?: string;
261
+ username?: string;
262
+ requestIp?: string;
263
+ rollbackActions?: RollbackAction[];
264
+ transactionId?: string;
265
+ autoCreateTransaction?: boolean;
266
+ }
267
+ /**
268
+ * 方法审计选项(支持模板和回滚)
269
+ */
270
+ export interface AuditOperationOptions {
271
+ templateKey: string;
272
+ descriptionParams?: (args: any[], result: any, context: any) => Record<string, any>;
273
+ rollbackActions?: (args: any[], result: any, context: any) => RollbackAction[];
274
+ autoTransaction?: boolean;
275
+ }
276
+ /**
277
+ * 增强的实体审计配置(支持多语言)
278
+ */
279
+ export interface EnhancedEntityAuditConfig extends EntityAuditConfig {
280
+ templateKey?: string;
281
+ fieldLabels?: Record<string, Record<string, string>>;
282
+ valueDisplays?: Record<string, Record<string, Record<string, string>>>;
283
+ }
284
+ /**
285
+ * 事务描述结果
286
+ */
287
+ export interface TransactionDescription {
288
+ operationName: string;
289
+ description: string;
290
+ changes: Array<{
291
+ entityName?: string;
292
+ entityId?: string;
293
+ operation?: AuditOperation;
294
+ type?: string;
295
+ description: string;
296
+ details: any;
297
+ createdAt: Date;
298
+ }>;
299
+ }
@@ -1,10 +1,25 @@
1
1
  import { ClsService } from 'nestjs-cls';
2
2
  import { AuditContext } from '../interfaces';
3
+ /**
4
+ * 审计上下文服务
5
+ */
3
6
  export declare class AuditContextService {
4
7
  private readonly cls;
5
8
  constructor(cls: ClsService);
9
+ /**
10
+ * 获取当前上下文
11
+ */
6
12
  getCurrentContext(): Promise<AuditContext>;
13
+ /**
14
+ * 设置上下文
15
+ */
7
16
  setContext(context: Partial<AuditContext>): void;
17
+ /**
18
+ * 清除上下文
19
+ */
8
20
  clearContext(): void;
21
+ /**
22
+ * 获取默认上下文
23
+ */
9
24
  private getDefaultContext;
10
25
  }
@@ -21,22 +21,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.AuditContextService = void 0;
22
22
  const common_1 = require("@nestjs/common");
23
23
  const nestjs_cls_1 = require("nestjs-cls");
24
+ /**
25
+ * 审计上下文服务
26
+ */
24
27
  let AuditContextService = class AuditContextService {
25
28
  constructor(cls) {
26
29
  this.cls = cls;
27
30
  }
31
+ /**
32
+ * 获取当前上下文
33
+ */
28
34
  getCurrentContext() {
29
35
  return __awaiter(this, void 0, void 0, function* () {
30
36
  return this.cls.get('auditContext') || this.getDefaultContext();
31
37
  });
32
38
  }
39
+ /**
40
+ * 设置上下文
41
+ */
33
42
  setContext(context) {
34
43
  const currentContext = this.cls.get('auditContext') || this.getDefaultContext();
35
44
  this.cls.set('auditContext', Object.assign(Object.assign({}, currentContext), context));
36
45
  }
46
+ /**
47
+ * 清除上下文
48
+ */
37
49
  clearContext() {
38
50
  this.cls.set('auditContext', undefined);
39
51
  }
52
+ /**
53
+ * 获取默认上下文
54
+ */
40
55
  getDefaultContext() {
41
56
  return {
42
57
  userId: 'system',
@@ -1,5 +1,8 @@
1
1
  import { IAuditStrategy, IFieldFilter, AuditConfig } from '../interfaces';
2
2
  import { AuditOperation, RecordStrategy, MaskingStrategy } from '../enums';
3
+ /**
4
+ * 默认字段过滤器
5
+ */
3
6
  export declare class DefaultFieldFilter implements IFieldFilter {
4
7
  private readonly includeFields?;
5
8
  private readonly excludeFields?;
@@ -10,6 +13,9 @@ export declare class DefaultFieldFilter implements IFieldFilter {
10
13
  getMaskingStrategy(fieldName: string, path: string[]): MaskingStrategy;
11
14
  private matchPattern;
12
15
  }
16
+ /**
17
+ * 默认审计策略
18
+ */
13
19
  export declare class DefaultAuditStrategy implements IAuditStrategy {
14
20
  private readonly config?;
15
21
  constructor(config?: AuditConfig);
@@ -15,6 +15,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.DefaultAuditStrategy = exports.DefaultFieldFilter = void 0;
16
16
  const common_1 = require("@nestjs/common");
17
17
  const enums_1 = require("../enums");
18
+ /**
19
+ * 默认字段过滤器
20
+ */
18
21
  class DefaultFieldFilter {
19
22
  constructor(includeFields, excludeFields, maskFields) {
20
23
  this.includeFields = includeFields;
@@ -23,9 +26,11 @@ class DefaultFieldFilter {
23
26
  }
24
27
  shouldIncludeField(fieldName, path) {
25
28
  const fullPath = [...path, fieldName].join('.');
29
+ // 如果指定了包含字段,则只记录这些字段
26
30
  if (this.includeFields && this.includeFields.length > 0) {
27
31
  return this.includeFields.some((pattern) => this.matchPattern(fullPath, pattern));
28
32
  }
33
+ // 如果指定了排除字段,则排除这些字段
29
34
  if (this.excludeFields && this.excludeFields.length > 0) {
30
35
  return !this.excludeFields.some((pattern) => this.matchPattern(fullPath, pattern));
31
36
  }
@@ -40,6 +45,7 @@ class DefaultFieldFilter {
40
45
  return enums_1.MaskingStrategy.MASK;
41
46
  }
42
47
  matchPattern(path, pattern) {
48
+ // 支持通配符匹配,如 user.address.*
43
49
  if (pattern.endsWith('*')) {
44
50
  return path.startsWith(pattern.slice(0, -1));
45
51
  }
@@ -47,18 +53,24 @@ class DefaultFieldFilter {
47
53
  }
48
54
  }
49
55
  exports.DefaultFieldFilter = DefaultFieldFilter;
56
+ /**
57
+ * 默认审计策略
58
+ */
50
59
  let DefaultAuditStrategy = class DefaultAuditStrategy {
51
60
  constructor(config) {
52
61
  this.config = config;
53
62
  }
54
63
  shouldRecord(entityType, operation) {
55
64
  var _a, _b, _c, _d, _e;
65
+ // 检查全局配置
56
66
  const globalEnabled = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.enabled) !== false;
57
67
  if (!globalEnabled)
58
68
  return false;
69
+ // 检查实体级别配置
59
70
  const entityConfig = (_c = (_b = this.config) === null || _b === void 0 ? void 0 : _b.entities) === null || _c === void 0 ? void 0 : _c[entityType];
60
71
  if (entityConfig && entityConfig.enabled === false)
61
72
  return false;
73
+ // 检查操作级别配置
62
74
  const operationConfig = (_e = (_d = this.config) === null || _d === void 0 ? void 0 : _d.operations) === null || _e === void 0 ? void 0 : _e[operation];
63
75
  if (operationConfig && operationConfig.enabled === false)
64
76
  return false;
@@ -66,6 +78,7 @@ let DefaultAuditStrategy = class DefaultAuditStrategy {
66
78
  }
67
79
  getRecordStrategy(entityType, operation) {
68
80
  var _a, _b, _c, _d, _e, _f, _g;
81
+ // 优先级: 实体级别 > 操作级别 > 全局默认
69
82
  const entityStrategy = (_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.entities) === null || _b === void 0 ? void 0 : _b[entityType]) === null || _c === void 0 ? void 0 : _c.strategy;
70
83
  if (entityStrategy)
71
84
  return entityStrategy;
@@ -1,37 +1,163 @@
1
1
  import { Repository, EntityManager } from 'typeorm';
2
- import { EntityAuditLogEntity, EntityTransactionEntity } from '../entities';
2
+ import { EntityAuditLogEntity, EntityTransactionEntity, ManualOperationLogEntity } from '../entities';
3
3
  import { AuditOperation } from '../enums';
4
- import { AuditConfig, IAuditStrategy, EntityDifference, RestoreOptions, RestoreResult, PreCheckResult } from '../interfaces';
4
+ import { AuditConfig, IAuditStrategy, EntityDifference, RestoreOptions, RestoreResult, PreCheckResult, ManualOperationData, ChangeDetail, RollbackAction } from '../interfaces';
5
5
  import { AuditContextService } from './audit-context.service';
6
6
  import { MultiDatabaseService } from './multi-database.service';
7
7
  import { PageDto } from '../../common/dto';
8
8
  import { AuditLogQueryDto } from '../dto';
9
+ /**
10
+ * 实体审计服务
11
+ */
9
12
  export declare class EntityAuditService {
10
13
  private readonly auditLogRepository;
11
14
  private readonly transactionRepository;
15
+ private readonly manualOperationRepository;
12
16
  private readonly entityManager;
13
17
  private readonly contextService;
14
18
  private readonly multiDbService;
15
19
  private readonly auditStrategy;
16
20
  private readonly config?;
17
21
  private readonly auditConnectionName;
18
- constructor(auditLogRepository: Repository<EntityAuditLogEntity>, transactionRepository: Repository<EntityTransactionEntity>, entityManager: EntityManager, contextService: AuditContextService, multiDbService: MultiDatabaseService, auditStrategy?: IAuditStrategy, config?: AuditConfig, auditConnectionName?: string);
22
+ constructor(auditLogRepository: Repository<EntityAuditLogEntity>, transactionRepository: Repository<EntityTransactionEntity>, manualOperationRepository: Repository<ManualOperationLogEntity>, entityManager: EntityManager, contextService: AuditContextService, multiDbService: MultiDatabaseService, auditStrategy?: IAuditStrategy, config?: AuditConfig, auditConnectionName?: string);
23
+ /**
24
+ * 记录实体变更
25
+ */
19
26
  logEntityChange(entityType: string, entityId: string, operation: AuditOperation, oldValue: Record<string, any>, newValue: Record<string, any>, metadata?: Record<string, any>): Promise<EntityAuditLogEntity | null>;
27
+ /**
28
+ * 查询审计日志
29
+ */
20
30
  getAuditLogs(query: AuditLogQueryDto): Promise<PageDto<EntityAuditLogEntity>>;
31
+ /**
32
+ * 比较实体差异
33
+ */
21
34
  compareEntities(entityType: string, entityId: string, fromLogId?: string, toLogId?: string): Promise<EntityDifference>;
35
+ /**
36
+ * 预检查恢复操作
37
+ */
22
38
  preCheckRestore(entityType: string, entityId: string, auditLogId: string): Promise<PreCheckResult>;
39
+ /**
40
+ * 恢复实体
41
+ */
23
42
  restoreEntity(entityType: string, entityId: string, auditLogId: string, options?: RestoreOptions): Promise<RestoreResult>;
43
+ /**
44
+ * 计算变更字段
45
+ */
24
46
  private calculateChangedFields;
47
+ /**
48
+ * 计算变更字段路径
49
+ */
25
50
  private calculateChangedFieldPaths;
51
+ /**
52
+ * 深度比较对象
53
+ */
26
54
  private deepEqual;
55
+ /**
56
+ * 深度差异比较
57
+ */
27
58
  private deepDiff;
59
+ /**
60
+ * 比较快照数据
61
+ */
28
62
  private compareSnapshotData;
63
+ /**
64
+ * 过滤和脱敏字段
65
+ */
29
66
  private filterAndMaskFields;
67
+ /**
68
+ * 应用脱敏策略
69
+ */
30
70
  private applyMasking;
71
+ /**
72
+ * 哈希值
73
+ */
31
74
  private hashValue;
75
+ /**
76
+ * 掩码值
77
+ */
32
78
  private maskValue;
79
+ /**
80
+ * 部分掩码值
81
+ */
33
82
  private partialMaskValue;
83
+ /**
84
+ * 生成哈希链
85
+ */
34
86
  private generateHashChain;
87
+ /**
88
+ * 生成描述
89
+ */
35
90
  private generateDescription;
91
+ /**
92
+ * 检测冲突
93
+ */
36
94
  private detectConflicts;
95
+ /**
96
+ * 增强的记录实体变更(支持模板和结构化变更详情)
97
+ * @param data 审计数据
98
+ * @returns 审计日志实体
99
+ */
100
+ logEntityChangeWithTemplate(data: {
101
+ entityType: string;
102
+ entityId: string;
103
+ operation: AuditOperation;
104
+ oldValue?: Record<string, any>;
105
+ newValue?: Record<string, any>;
106
+ changedFields?: string[];
107
+ operationTemplateKey?: string;
108
+ descriptionParams?: Record<string, any>;
109
+ changeDetails?: ChangeDetail[];
110
+ rollbackActions?: RollbackAction[];
111
+ metadata?: Record<string, any>;
112
+ }): Promise<EntityAuditLogEntity | null>;
113
+ /**
114
+ * 记录手动操作
115
+ * @param data 手动操作数据
116
+ * @returns 手动操作日志实体
117
+ */
118
+ logManualOperation(data: ManualOperationData): Promise<ManualOperationLogEntity>;
119
+ /**
120
+ * 在事务中执行操作
121
+ * @param fn 执行函数
122
+ * @param options 事务选项
123
+ * @returns 执���结果
124
+ */
125
+ withTransaction<T>(fn: (transactionId: string) => Promise<T>, options?: {
126
+ userId?: string;
127
+ username?: string;
128
+ requestIp?: string;
129
+ operationTemplateKey?: string;
130
+ descriptionParams?: Record<string, any>;
131
+ metadata?: Record<string, any>;
132
+ }): Promise<T>;
133
+ /**
134
+ * 获取当前事务ID
135
+ * @returns 当前事务ID或null
136
+ */
137
+ getCurrentTransactionId(): Promise<string | null>;
138
+ /**
139
+ * 设置当前事务ID
140
+ * @param transactionId 事务ID
141
+ */
142
+ setCurrentTransactionId(transactionId: string | null): Promise<void>;
143
+ /**
144
+ * 开始事务
145
+ * @param userId 用户ID
146
+ * @param userName 用户名
147
+ * @param ip IP地址
148
+ * @param operationTemplateKey 操作模板键
149
+ * @param descriptionParams 描述参数
150
+ * @returns 事务ID
151
+ */
152
+ beginTransaction(userId?: string, userName?: string, ip?: string, operationTemplateKey?: string, descriptionParams?: Record<string, any>): Promise<string>;
153
+ /**
154
+ * 提交事务
155
+ * @param transactionId 事务ID
156
+ */
157
+ commitTransaction(transactionId: string): Promise<void>;
158
+ /**
159
+ * 回滚事务
160
+ * @param transactionId 事务ID
161
+ */
162
+ rollbackTransaction(transactionId: string): Promise<void>;
37
163
  }