@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
@@ -32,10 +32,14 @@ const audit_context_service_1 = require("./audit-context.service");
32
32
  const audit_strategy_service_1 = require("./audit-strategy.service");
33
33
  const multi_database_service_1 = require("./multi-database.service");
34
34
  const dto_1 = require("../../common/dto");
35
+ /**
36
+ * 实体审计服务
37
+ */
35
38
  let EntityAuditService = class EntityAuditService {
36
- constructor(auditLogRepository, transactionRepository, entityManager, contextService, multiDbService, auditStrategy = new audit_strategy_service_1.DefaultAuditStrategy(), config, auditConnectionName) {
39
+ constructor(auditLogRepository, transactionRepository, manualOperationRepository, entityManager, contextService, multiDbService, auditStrategy = new audit_strategy_service_1.DefaultAuditStrategy(), config, auditConnectionName) {
37
40
  this.auditLogRepository = auditLogRepository;
38
41
  this.transactionRepository = transactionRepository;
42
+ this.manualOperationRepository = manualOperationRepository;
39
43
  this.entityManager = entityManager;
40
44
  this.contextService = contextService;
41
45
  this.multiDbService = multiDbService;
@@ -43,22 +47,32 @@ let EntityAuditService = class EntityAuditService {
43
47
  this.config = config;
44
48
  this.auditConnectionName = auditConnectionName || 'default';
45
49
  }
50
+ /**
51
+ * 记录实体变更
52
+ */
46
53
  logEntityChange(entityType_1, entityId_1, operation_1, oldValue_1, newValue_1) {
47
54
  return __awaiter(this, arguments, void 0, function* (entityType, entityId, operation, oldValue, newValue, metadata = {}) {
48
55
  var _a, _b;
56
+ // 检查是否应该记录
49
57
  if (!this.auditStrategy.shouldRecord(entityType, operation)) {
50
58
  return null;
51
59
  }
60
+ // 获取记录策略
52
61
  const recordStrategy = this.auditStrategy.getRecordStrategy(entityType, operation);
53
62
  if (recordStrategy === enums_1.RecordStrategy.DISABLED) {
54
63
  return null;
55
64
  }
65
+ // 获取字段过滤器
56
66
  const fieldFilter = this.auditStrategy.getFieldFilter(entityType);
67
+ // 过滤和脱敏字段
57
68
  const filteredOldValue = this.filterAndMaskFields(oldValue, fieldFilter);
58
69
  const filteredNewValue = this.filterAndMaskFields(newValue, fieldFilter);
70
+ // 计算变更字段
59
71
  const changedFields = this.calculateChangedFields(filteredOldValue, filteredNewValue);
60
72
  const changedFieldPaths = this.calculateChangedFieldPaths(filteredOldValue, filteredNewValue);
73
+ // 获取上下文信息
61
74
  const context = yield this.contextService.getCurrentContext();
75
+ // 创建审计日志
62
76
  const auditLog = this.auditLogRepository.create({
63
77
  entityType,
64
78
  entityId,
@@ -77,17 +91,23 @@ let EntityAuditService = class EntityAuditService {
77
91
  ? yield this.generateHashChain(entityType, entityId, filteredNewValue)
78
92
  : undefined,
79
93
  });
94
+ // 保存变更日志
80
95
  const savedLog = yield this.auditLogRepository.save(auditLog);
81
96
  return savedLog;
82
97
  });
83
98
  }
99
+ /**
100
+ * 查询审计日志
101
+ */
84
102
  getAuditLogs(query) {
85
103
  return __awaiter(this, void 0, void 0, function* () {
104
+ // 应用默认值
86
105
  const page = query.page || 1;
87
106
  const limit = query.limit || 20;
88
107
  const sortBy = query.sortBy || 'createdAt';
89
108
  const sortOrder = query.sortOrder || 'DESC';
90
109
  const queryBuilder = this.auditLogRepository.createQueryBuilder('log');
110
+ // 应用过滤条件
91
111
  if (query.entityType) {
92
112
  queryBuilder.andWhere('log.entityType = :entityType', { entityType: query.entityType });
93
113
  }
@@ -111,7 +131,9 @@ let EntityAuditService = class EntityAuditService {
111
131
  search: `%${query.search}%`,
112
132
  });
113
133
  }
134
+ // 应用排序
114
135
  queryBuilder.orderBy(`log.${sortBy}`, sortOrder);
136
+ // 应用分页
115
137
  const skip = (page - 1) * limit;
116
138
  queryBuilder.skip(skip).take(limit);
117
139
  const [data, total] = yield queryBuilder.getManyAndCount();
@@ -124,6 +146,9 @@ let EntityAuditService = class EntityAuditService {
124
146
  return new dto_1.PageDto(data, pageMetaDto);
125
147
  });
126
148
  }
149
+ /**
150
+ * 比较实体差异
151
+ */
127
152
  compareEntities(entityType, entityId, fromLogId, toLogId) {
128
153
  return __awaiter(this, void 0, void 0, function* () {
129
154
  let fromData = {};
@@ -137,6 +162,7 @@ let EntityAuditService = class EntityAuditService {
137
162
  toData = (toLog === null || toLog === void 0 ? void 0 : toLog.newValue) || {};
138
163
  }
139
164
  else {
165
+ // 获取最新的日志
140
166
  const latestLog = yield this.auditLogRepository.findOne({
141
167
  where: { entityType, entityId },
142
168
  order: { createdAt: 'DESC' },
@@ -146,6 +172,9 @@ let EntityAuditService = class EntityAuditService {
146
172
  return this.compareSnapshotData(fromData, toData);
147
173
  });
148
174
  }
175
+ /**
176
+ * 预检查恢复操作
177
+ */
149
178
  preCheckRestore(entityType, entityId, auditLogId) {
150
179
  return __awaiter(this, void 0, void 0, function* () {
151
180
  const auditLog = yield this.auditLogRepository.findOne({ where: { id: auditLogId } });
@@ -163,6 +192,7 @@ let EntityAuditService = class EntityAuditService {
163
192
  warnings: [],
164
193
  };
165
194
  }
195
+ // 获取当前实体状态
166
196
  const repository = this.entityManager.getRepository(entityType);
167
197
  const currentEntity = yield repository.findOne({ where: { id: entityId } });
168
198
  if (!currentEntity) {
@@ -179,6 +209,7 @@ let EntityAuditService = class EntityAuditService {
179
209
  warnings: [],
180
210
  };
181
211
  }
212
+ // 检查冲突
182
213
  const conflicts = this.detectConflicts(currentEntity, auditLog.oldValue);
183
214
  return {
184
215
  canRestore: conflicts.length === 0,
@@ -187,6 +218,9 @@ let EntityAuditService = class EntityAuditService {
187
218
  };
188
219
  });
189
220
  }
221
+ /**
222
+ * 恢复实体
223
+ */
190
224
  restoreEntity(entityType_1, entityId_1, auditLogId_1) {
191
225
  return __awaiter(this, arguments, void 0, function* (entityType, entityId, auditLogId, options = {}) {
192
226
  const auditLog = yield this.auditLogRepository.findOne({ where: { id: auditLogId } });
@@ -196,6 +230,7 @@ let EntityAuditService = class EntityAuditService {
196
230
  message: '审计日志不存在',
197
231
  };
198
232
  }
233
+ // 预检查
199
234
  const preCheckResult = yield this.preCheckRestore(entityType, entityId, auditLogId);
200
235
  if (!preCheckResult.canRestore && !options.force) {
201
236
  return {
@@ -205,10 +240,13 @@ let EntityAuditService = class EntityAuditService {
205
240
  };
206
241
  }
207
242
  try {
243
+ // 获取指定的数据库连接
208
244
  const connectionName = options.connectionName || 'default';
209
245
  const dataSource = yield this.multiDbService.getDataSource(connectionName);
210
246
  const repository = dataSource.getRepository(entityType);
247
+ // 恢复实体
211
248
  const restoreData = Object.assign({}, auditLog.oldValue);
249
+ // 应用字段过滤
212
250
  if (options.includeFields && options.includeFields.length > 0) {
213
251
  Object.keys(restoreData).forEach((key) => {
214
252
  if (!options.includeFields.includes(key)) {
@@ -221,8 +259,10 @@ let EntityAuditService = class EntityAuditService {
221
259
  delete restoreData[field];
222
260
  });
223
261
  }
262
+ // 执行恢复
224
263
  if (!options.dryRun) {
225
264
  yield repository.update({ id: entityId }, restoreData);
265
+ // 记录恢复操作
226
266
  yield this.logEntityChange(entityType, entityId, enums_1.AuditOperation.RESTORE, auditLog.newValue, restoreData);
227
267
  }
228
268
  return {
@@ -239,9 +279,14 @@ let EntityAuditService = class EntityAuditService {
239
279
  }
240
280
  });
241
281
  }
282
+ /**
283
+ * 计算变更字段
284
+ */
242
285
  calculateChangedFields(oldValue, newValue) {
243
286
  const changedFields = [];
287
+ // 获取所有字段
244
288
  const allFields = new Set([...Object.keys(oldValue || {}), ...Object.keys(newValue || {})]);
289
+ // 比较每个字段
245
290
  for (const field of allFields) {
246
291
  const oldVal = oldValue === null || oldValue === void 0 ? void 0 : oldValue[field];
247
292
  const newVal = newValue === null || newValue === void 0 ? void 0 : newValue[field];
@@ -251,14 +296,22 @@ let EntityAuditService = class EntityAuditService {
251
296
  }
252
297
  return changedFields;
253
298
  }
299
+ /**
300
+ * 计算变更字段路径
301
+ */
254
302
  calculateChangedFieldPaths(oldValue, newValue) {
255
303
  const changedPaths = [];
304
+ // 使用深度比较获取变更路径
256
305
  const diff = this.deepDiff(oldValue, newValue);
306
+ // 提取路径
257
307
  for (const change of diff) {
258
308
  changedPaths.push(change.path.join('.'));
259
309
  }
260
310
  return changedPaths;
261
311
  }
312
+ /**
313
+ * 深度比较对象
314
+ */
262
315
  deepEqual(a, b) {
263
316
  if (a === b)
264
317
  return true;
@@ -282,13 +335,18 @@ let EntityAuditService = class EntityAuditService {
282
335
  }
283
336
  return true;
284
337
  }
338
+ /**
339
+ * 深度差异比较
340
+ */
285
341
  deepDiff(oldObj, newObj, path = []) {
286
342
  var _a;
287
343
  const changes = [];
288
344
  const maxDepth = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.maxDiffDepth) || 5;
345
+ // 防止过深
289
346
  if (path.length > maxDepth) {
290
347
  return changes;
291
348
  }
349
+ // 处理基本类型
292
350
  if (typeof oldObj !== 'object' || oldObj === null) {
293
351
  if (oldObj !== newObj) {
294
352
  changes.push({
@@ -300,6 +358,7 @@ let EntityAuditService = class EntityAuditService {
300
358
  }
301
359
  return changes;
302
360
  }
361
+ // 处理数组
303
362
  if (Array.isArray(oldObj)) {
304
363
  if (!Array.isArray(newObj)) {
305
364
  changes.push({
@@ -324,6 +383,7 @@ let EntityAuditService = class EntityAuditService {
324
383
  }
325
384
  return changes;
326
385
  }
386
+ // 处理对象
327
387
  const allKeys = new Set([...Object.keys(oldObj), ...Object.keys(newObj)]);
328
388
  for (const key of allKeys) {
329
389
  const oldVal = oldObj[key];
@@ -350,6 +410,9 @@ let EntityAuditService = class EntityAuditService {
350
410
  }
351
411
  return changes;
352
412
  }
413
+ /**
414
+ * 比较快照数据
415
+ */
353
416
  compareSnapshotData(fromData, toData) {
354
417
  const changes = this.deepDiff(fromData, toData);
355
418
  return {
@@ -361,6 +424,9 @@ let EntityAuditService = class EntityAuditService {
361
424
  },
362
425
  };
363
426
  }
427
+ /**
428
+ * 过滤和脱敏字段
429
+ */
364
430
  filterAndMaskFields(data, fieldFilter, path = []) {
365
431
  if (typeof data !== 'object' || data === null) {
366
432
  return data;
@@ -368,14 +434,17 @@ let EntityAuditService = class EntityAuditService {
368
434
  const result = {};
369
435
  for (const [key, value] of Object.entries(data)) {
370
436
  const currentPath = [...path, key];
437
+ // 检查是否应该包含此字段
371
438
  if (!fieldFilter.shouldIncludeField(key, currentPath)) {
372
439
  continue;
373
440
  }
441
+ // 检查是否应该脱敏此字段
374
442
  if (fieldFilter.shouldMaskField(key, currentPath)) {
375
443
  const maskingStrategy = fieldFilter.getMaskingStrategy(key, currentPath);
376
444
  result[key] = this.applyMasking(value, maskingStrategy);
377
445
  }
378
446
  else if (typeof value === 'object' && value !== null) {
447
+ // 递归处理嵌套对象
379
448
  result[key] = this.filterAndMaskFields(value, fieldFilter, currentPath);
380
449
  }
381
450
  else {
@@ -384,6 +453,9 @@ let EntityAuditService = class EntityAuditService {
384
453
  }
385
454
  return result;
386
455
  }
456
+ /**
457
+ * 应用脱敏策略
458
+ */
387
459
  applyMasking(value, strategy) {
388
460
  if (value == null)
389
461
  return value;
@@ -400,14 +472,23 @@ let EntityAuditService = class EntityAuditService {
400
472
  return '***MASKED***';
401
473
  }
402
474
  }
475
+ /**
476
+ * 哈希值
477
+ */
403
478
  hashValue(value) {
404
479
  const strValue = String(value);
405
480
  return (0, crypto_1.createHash)('sha256').update(strValue).digest('hex');
406
481
  }
482
+ /**
483
+ * 掩码值
484
+ */
407
485
  maskValue(value) {
408
486
  const strValue = String(value);
409
487
  return '*'.repeat(strValue.length);
410
488
  }
489
+ /**
490
+ * 部分掩码值
491
+ */
411
492
  partialMaskValue(value) {
412
493
  const strValue = String(value);
413
494
  if (strValue.length <= 4) {
@@ -417,14 +498,19 @@ let EntityAuditService = class EntityAuditService {
417
498
  '*'.repeat(strValue.length - 4) +
418
499
  strValue.substring(strValue.length - 2));
419
500
  }
501
+ /**
502
+ * 生成哈希链
503
+ */
420
504
  generateHashChain(entityType, entityId, data) {
421
505
  return __awaiter(this, void 0, void 0, function* () {
422
506
  var _a, _b, _c;
507
+ // 获取上一个审计记录的哈希
423
508
  const lastLog = yield this.auditLogRepository.findOne({
424
509
  where: { entityType, entityId },
425
510
  order: { createdAt: 'DESC' },
426
511
  });
427
512
  const previousHash = ((_a = lastLog === null || lastLog === void 0 ? void 0 : lastLog.hashChain) === null || _a === void 0 ? void 0 : _a.currentHash) || '';
513
+ // 计算当前数据的哈希
428
514
  const currentData = JSON.stringify(data);
429
515
  const hashAlgorithm = ((_c = (_b = this.config) === null || _b === void 0 ? void 0 : _b.security) === null || _c === void 0 ? void 0 : _c.hashAlgorithm) || 'sha256';
430
516
  const currentHash = (0, crypto_1.createHash)(hashAlgorithm).update(currentData + previousHash).digest('hex');
@@ -435,6 +521,9 @@ let EntityAuditService = class EntityAuditService {
435
521
  };
436
522
  });
437
523
  }
524
+ /**
525
+ * 生成描述
526
+ */
438
527
  generateDescription(operation, entityType, entityId, changedFields) {
439
528
  const operationText = {
440
529
  [enums_1.AuditOperation.CREATE]: '创建',
@@ -448,6 +537,9 @@ let EntityAuditService = class EntityAuditService {
448
537
  }
449
538
  return `${text} ${entityType}(${entityId})`;
450
539
  }
540
+ /**
541
+ * 检测冲突
542
+ */
451
543
  detectConflicts(currentEntity, targetValue) {
452
544
  const conflicts = [];
453
545
  for (const [key, targetVal] of Object.entries(targetValue)) {
@@ -463,20 +555,223 @@ let EntityAuditService = class EntityAuditService {
463
555
  }
464
556
  return conflicts;
465
557
  }
558
+ // ========== 新增方法:支持模板和手动操作 ==========
559
+ /**
560
+ * 增强的记录实体变更(支持模板和结构化变更详情)
561
+ * @param data 审计数据
562
+ * @returns 审计日志实体
563
+ */
564
+ logEntityChangeWithTemplate(data) {
565
+ return __awaiter(this, void 0, void 0, function* () {
566
+ var _a, _b, _c, _d, _e, _f, _g;
567
+ // 检查是否应该记录
568
+ if (!this.auditStrategy.shouldRecord(data.entityType, data.operation)) {
569
+ return null;
570
+ }
571
+ // 获取记录策略
572
+ const recordStrategy = this.auditStrategy.getRecordStrategy(data.entityType, data.operation);
573
+ if (recordStrategy === enums_1.RecordStrategy.DISABLED) {
574
+ return null;
575
+ }
576
+ // 获取字段过滤器
577
+ const fieldFilter = this.auditStrategy.getFieldFilter(data.entityType);
578
+ // 过滤和脱敏字段
579
+ const filteredOldValue = this.filterAndMaskFields(data.oldValue || {}, fieldFilter);
580
+ const filteredNewValue = this.filterAndMaskFields(data.newValue || {}, fieldFilter);
581
+ // 计算变更字段
582
+ const changedFields = data.changedFields || this.calculateChangedFields(filteredOldValue, filteredNewValue);
583
+ const changedFieldPaths = this.calculateChangedFieldPaths(filteredOldValue, filteredNewValue);
584
+ // 获取上下文信息
585
+ const context = yield this.contextService.getCurrentContext();
586
+ // 如果没有提供操作模板键,使用默认模板
587
+ const operationTemplateKey = data.operationTemplateKey || `${data.entityType}.${data.operation}`;
588
+ // 如果没有提供描述参数,使用实体数据
589
+ const descriptionParams = data.descriptionParams || Object.assign(Object.assign(Object.assign({}, filteredOldValue), filteredNewValue), { changedFields, changedFieldsCount: changedFields.length });
590
+ // 创建审计日志
591
+ const auditLog = this.auditLogRepository.create({
592
+ entityType: data.entityType,
593
+ entityId: data.entityId,
594
+ operation: data.operation,
595
+ oldValue: filteredOldValue,
596
+ newValue: filteredNewValue,
597
+ changedFields,
598
+ changedFieldPaths: changedFieldPaths.join(','),
599
+ userId: context.userId || ((_a = data.metadata) === null || _a === void 0 ? void 0 : _a.userId),
600
+ username: context.username || ((_b = data.metadata) === null || _b === void 0 ? void 0 : _b.username),
601
+ requestId: context.requestId || ((_c = data.metadata) === null || _c === void 0 ? void 0 : _c.requestId),
602
+ requestIp: context.requestIp || ((_d = data.metadata) === null || _d === void 0 ? void 0 : _d.requestIp),
603
+ userAgent: context.userAgent || ((_e = data.metadata) === null || _e === void 0 ? void 0 : _e.userAgent),
604
+ description: this.generateDescription(data.operation, data.entityType, data.entityId, changedFields),
605
+ hashChain: ((_g = (_f = this.config) === null || _f === void 0 ? void 0 : _f.security) === null || _g === void 0 ? void 0 : _g.hashChainEnabled)
606
+ ? yield this.generateHashChain(data.entityType, data.entityId, filteredNewValue)
607
+ : undefined,
608
+ // 新增字段
609
+ operationTemplateKey,
610
+ descriptionParams,
611
+ changeDetails: data.changeDetails,
612
+ rollbackActions: data.rollbackActions,
613
+ });
614
+ // 保存变更日志
615
+ const savedLog = yield this.auditLogRepository.save(auditLog);
616
+ return savedLog;
617
+ });
618
+ }
619
+ /**
620
+ * 记录手动操作
621
+ * @param data 手动操作数据
622
+ * @returns 手动操作日志实体
623
+ */
624
+ logManualOperation(data) {
625
+ return __awaiter(this, void 0, void 0, function* () {
626
+ // 获取上下文信息
627
+ const context = yield this.contextService.getCurrentContext();
628
+ const manualLog = this.manualOperationRepository.create({
629
+ transactionId: data.transactionId,
630
+ operationTemplateKey: data.operationTemplateKey,
631
+ descriptionParams: data.descriptionParams,
632
+ userId: data.userId || context.userId,
633
+ username: data.username || context.username,
634
+ requestIp: data.requestIp || context.requestIp,
635
+ rollbackActions: data.rollbackActions || [],
636
+ });
637
+ return yield this.manualOperationRepository.save(manualLog);
638
+ });
639
+ }
640
+ /**
641
+ * 在事务中执行操作
642
+ * @param fn 执行函数
643
+ * @param options 事务选项
644
+ * @returns 执���结果
645
+ */
646
+ withTransaction(fn, options) {
647
+ return __awaiter(this, void 0, void 0, function* () {
648
+ var _a;
649
+ // 获取上下文信息
650
+ const context = yield this.contextService.getCurrentContext();
651
+ // 创建事务
652
+ const transaction = this.transactionRepository.create({
653
+ description: ((_a = options === null || options === void 0 ? void 0 : options.descriptionParams) === null || _a === void 0 ? void 0 : _a.description) || 'Transaction',
654
+ status: 'PENDING',
655
+ entities: [],
656
+ userId: (options === null || options === void 0 ? void 0 : options.userId) || context.userId,
657
+ username: (options === null || options === void 0 ? void 0 : options.username) || context.username,
658
+ requestIp: (options === null || options === void 0 ? void 0 : options.requestIp) || context.requestIp,
659
+ operationTemplateKey: options === null || options === void 0 ? void 0 : options.operationTemplateKey,
660
+ descriptionParams: options === null || options === void 0 ? void 0 : options.descriptionParams,
661
+ metadata: options === null || options === void 0 ? void 0 : options.metadata,
662
+ });
663
+ const savedTransaction = yield this.transactionRepository.save(transaction);
664
+ const transactionId = savedTransaction.id;
665
+ try {
666
+ // 设置事务ID到上下文
667
+ yield this.contextService.setContext(Object.assign(Object.assign({}, context), { transactionId }));
668
+ // 执行操作
669
+ const result = yield fn(transactionId);
670
+ // 提交事务
671
+ yield this.transactionRepository.update(transactionId, {
672
+ status: 'COMMITTED',
673
+ completedAt: new Date(),
674
+ });
675
+ return result;
676
+ }
677
+ catch (error) {
678
+ // 回滚事务
679
+ yield this.transactionRepository.update(transactionId, {
680
+ status: 'ROLLED_BACK',
681
+ completedAt: new Date(),
682
+ });
683
+ throw error;
684
+ }
685
+ });
686
+ }
687
+ /**
688
+ * 获取当前事务ID
689
+ * @returns 当前事务ID或null
690
+ */
691
+ getCurrentTransactionId() {
692
+ return __awaiter(this, void 0, void 0, function* () {
693
+ const context = yield this.contextService.getCurrentContext();
694
+ return context.transactionId || null;
695
+ });
696
+ }
697
+ /**
698
+ * 设置当前事务ID
699
+ * @param transactionId 事务ID
700
+ */
701
+ setCurrentTransactionId(transactionId) {
702
+ return __awaiter(this, void 0, void 0, function* () {
703
+ const context = yield this.contextService.getCurrentContext();
704
+ yield this.contextService.setContext(Object.assign(Object.assign({}, context), { transactionId }));
705
+ });
706
+ }
707
+ /**
708
+ * 开始事务
709
+ * @param userId 用户ID
710
+ * @param userName 用户名
711
+ * @param ip IP地址
712
+ * @param operationTemplateKey 操作模板键
713
+ * @param descriptionParams 描述参数
714
+ * @returns 事务ID
715
+ */
716
+ beginTransaction(userId, userName, ip, operationTemplateKey, descriptionParams) {
717
+ return __awaiter(this, void 0, void 0, function* () {
718
+ // 获取上下文信息
719
+ const context = yield this.contextService.getCurrentContext();
720
+ // 创建事务
721
+ const transaction = this.transactionRepository.create({
722
+ description: (descriptionParams === null || descriptionParams === void 0 ? void 0 : descriptionParams.description) || 'Transaction',
723
+ status: 'pending',
724
+ entities: [],
725
+ userId: userId || context.userId,
726
+ username: userName || context.username,
727
+ requestIp: ip || context.requestIp,
728
+ operationTemplateKey,
729
+ descriptionParams,
730
+ });
731
+ const savedTransaction = yield this.transactionRepository.save(transaction);
732
+ return savedTransaction.id;
733
+ });
734
+ }
735
+ /**
736
+ * 提交事务
737
+ * @param transactionId 事务ID
738
+ */
739
+ commitTransaction(transactionId) {
740
+ return __awaiter(this, void 0, void 0, function* () {
741
+ yield this.transactionRepository.update(transactionId, {
742
+ status: 'committed',
743
+ completedAt: new Date(),
744
+ });
745
+ });
746
+ }
747
+ /**
748
+ * 回滚事务
749
+ * @param transactionId 事务ID
750
+ */
751
+ rollbackTransaction(transactionId) {
752
+ return __awaiter(this, void 0, void 0, function* () {
753
+ yield this.transactionRepository.update(transactionId, {
754
+ status: 'rolled_back',
755
+ completedAt: new Date(),
756
+ });
757
+ });
758
+ }
466
759
  };
467
760
  exports.EntityAuditService = EntityAuditService;
468
761
  exports.EntityAuditService = EntityAuditService = __decorate([
469
762
  (0, common_1.Injectable)(),
470
763
  __param(0, (0, typeorm_1.InjectRepository)(entities_1.EntityAuditLogEntity)),
471
764
  __param(1, (0, typeorm_1.InjectRepository)(entities_1.EntityTransactionEntity)),
472
- __param(2, (0, common_1.Inject)('AUDIT_ENTITY_MANAGER')),
473
- __param(5, (0, common_1.Optional)()),
474
- __param(5, (0, common_1.Inject)('AUDIT_STRATEGY')),
765
+ __param(2, (0, typeorm_1.InjectRepository)(entities_1.ManualOperationLogEntity)),
766
+ __param(3, (0, common_1.Inject)('AUDIT_ENTITY_MANAGER')),
475
767
  __param(6, (0, common_1.Optional)()),
476
- __param(6, (0, common_1.Inject)('AUDIT_CONFIG')),
768
+ __param(6, (0, common_1.Inject)('AUDIT_STRATEGY')),
477
769
  __param(7, (0, common_1.Optional)()),
478
- __param(7, (0, common_1.Inject)('AUDIT_CONNECTION_NAME')),
770
+ __param(7, (0, common_1.Inject)('AUDIT_CONFIG')),
771
+ __param(8, (0, common_1.Optional)()),
772
+ __param(8, (0, common_1.Inject)('AUDIT_CONNECTION_NAME')),
479
773
  __metadata("design:paramtypes", [typeorm_2.Repository,
774
+ typeorm_2.Repository,
480
775
  typeorm_2.Repository,
481
776
  typeorm_2.EntityManager,
482
777
  audit_context_service_1.AuditContextService,
@@ -3,3 +3,5 @@ export * from './audit-strategy.service';
3
3
  export * from './entity-audit.service';
4
4
  export * from './transaction-audit.service';
5
5
  export * from './multi-database.service';
6
+ export * from './operation-description.service';
7
+ export * from './manual-audit-log.service';
@@ -19,3 +19,5 @@ __exportStar(require("./audit-strategy.service"), exports);
19
19
  __exportStar(require("./entity-audit.service"), exports);
20
20
  __exportStar(require("./transaction-audit.service"), exports);
21
21
  __exportStar(require("./multi-database.service"), exports);
22
+ __exportStar(require("./operation-description.service"), exports);
23
+ __exportStar(require("./manual-audit-log.service"), exports);