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

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 (175) hide show
  1. package/audit/audit.module.js +17 -0
  2. package/audit/controllers/audit.controller.d.ts +64 -0
  3. package/audit/controllers/audit.controller.js +50 -0
  4. package/audit/decorators/audit-action.decorator.d.ts +74 -0
  5. package/audit/decorators/audit-action.decorator.js +42 -0
  6. package/audit/decorators/entity-audit.decorator.d.ts +10 -1
  7. package/audit/decorators/entity-audit.decorator.js +34 -16
  8. package/audit/decorators/index.d.ts +1 -0
  9. package/audit/decorators/index.js +1 -0
  10. package/audit/entities/audit-action-summary.entity.d.ts +23 -0
  11. package/audit/entities/audit-action-summary.entity.js +101 -0
  12. package/audit/entities/entity-audit-log.entity.d.ts +3 -0
  13. package/audit/entities/entity-audit-log.entity.js +25 -2
  14. package/audit/entities/entity-transaction.entity.d.ts +3 -4
  15. package/audit/entities/entity-transaction.entity.js +10 -3
  16. package/audit/entities/index.d.ts +1 -0
  17. package/audit/entities/index.js +1 -0
  18. package/audit/entities/manual-operation-log.entity.js +8 -1
  19. package/audit/enums/audit.enums.d.ts +1 -10
  20. package/audit/enums/audit.enums.js +7 -17
  21. package/audit/index.d.ts +2 -1
  22. package/audit/index.js +5 -1
  23. package/audit/interceptors/audit-action.interceptor.d.ts +38 -0
  24. package/audit/interceptors/audit-action.interceptor.js +215 -0
  25. package/audit/interceptors/index.d.ts +1 -0
  26. package/audit/interceptors/index.js +1 -0
  27. package/audit/interfaces/audit.interfaces.d.ts +10 -5
  28. package/audit/services/audit-action.service.d.ts +141 -0
  29. package/audit/services/audit-action.service.js +244 -0
  30. package/audit/services/audit-context.service.d.ts +82 -0
  31. package/audit/services/audit-context.service.js +170 -0
  32. package/audit/services/entity-audit.service.d.ts +104 -3
  33. package/audit/services/entity-audit.service.js +306 -9
  34. package/audit/services/index.d.ts +1 -0
  35. package/audit/services/index.js +1 -0
  36. package/audit/services/manual-audit-log.service.d.ts +24 -23
  37. package/audit/services/manual-audit-log.service.js +32 -53
  38. package/audit/services/operation-description.service.d.ts +13 -3
  39. package/audit/services/operation-description.service.js +161 -24
  40. package/audit/services/transaction-audit.service.js +3 -3
  41. package/audit/subscribers/entity-audit.subscriber.d.ts +4 -0
  42. package/audit/subscribers/entity-audit.subscriber.js +47 -0
  43. package/file-upload/controllers/file-access.controller.d.ts +23 -0
  44. package/file-upload/controllers/file-access.controller.js +128 -0
  45. package/file-upload/decorators/csv-data.decorator.d.ts +44 -0
  46. package/file-upload/decorators/csv-data.decorator.js +131 -0
  47. package/file-upload/decorators/excel-data.decorator.d.ts +44 -0
  48. package/file-upload/decorators/excel-data.decorator.js +125 -0
  49. package/file-upload/decorators/file-upload.decorator.d.ts +83 -0
  50. package/file-upload/decorators/file-upload.decorator.js +172 -0
  51. package/file-upload/decorators/index.d.ts +4 -0
  52. package/file-upload/decorators/index.js +20 -0
  53. package/file-upload/decorators/process.decorator.d.ts +40 -0
  54. package/file-upload/decorators/process.decorator.js +52 -0
  55. package/file-upload/dto/create-file.dto.d.ts +24 -0
  56. package/file-upload/dto/create-file.dto.js +112 -0
  57. package/file-upload/dto/find-files.dto.d.ts +15 -0
  58. package/file-upload/dto/find-files.dto.js +76 -0
  59. package/file-upload/dto/index.d.ts +4 -0
  60. package/file-upload/dto/index.js +20 -0
  61. package/file-upload/dto/pagination.dto.d.ts +7 -0
  62. package/file-upload/dto/pagination.dto.js +39 -0
  63. package/file-upload/dto/update-file.dto.d.ts +16 -0
  64. package/file-upload/dto/update-file.dto.js +71 -0
  65. package/file-upload/entities/file-metadata.entity.d.ts +22 -0
  66. package/file-upload/entities/file-metadata.entity.js +84 -0
  67. package/file-upload/entities/file.entity.d.ts +129 -0
  68. package/file-upload/entities/file.entity.js +384 -0
  69. package/file-upload/entities/index.d.ts +2 -0
  70. package/file-upload/entities/index.js +18 -0
  71. package/file-upload/enums/file-type.enum.d.ts +72 -0
  72. package/file-upload/enums/file-type.enum.js +212 -0
  73. package/file-upload/exceptions/file-upload.exception.d.ts +57 -0
  74. package/file-upload/exceptions/file-upload.exception.js +120 -0
  75. package/file-upload/exceptions/index.d.ts +1 -0
  76. package/file-upload/exceptions/index.js +17 -0
  77. package/file-upload/file-upload.module.d.ts +89 -0
  78. package/file-upload/file-upload.module.js +264 -0
  79. package/file-upload/index.d.ts +26 -0
  80. package/file-upload/index.js +59 -0
  81. package/file-upload/interceptors/file-upload.interceptor.d.ts +48 -0
  82. package/file-upload/interceptors/file-upload.interceptor.js +434 -0
  83. package/file-upload/interceptors/index.d.ts +1 -0
  84. package/file-upload/interceptors/index.js +17 -0
  85. package/file-upload/interfaces/custom-file-type.interface.d.ts +72 -0
  86. package/file-upload/interfaces/custom-file-type.interface.js +2 -0
  87. package/file-upload/interfaces/file-buffer.interface.d.ts +72 -0
  88. package/file-upload/interfaces/file-buffer.interface.js +2 -0
  89. package/file-upload/interfaces/file-entity.interface.d.ts +142 -0
  90. package/file-upload/interfaces/file-entity.interface.js +28 -0
  91. package/file-upload/interfaces/file-metadata.interface.d.ts +21 -0
  92. package/file-upload/interfaces/file-metadata.interface.js +2 -0
  93. package/file-upload/interfaces/file-upload-options.interface.d.ts +117 -0
  94. package/file-upload/interfaces/file-upload-options.interface.js +2 -0
  95. package/file-upload/interfaces/index.d.ts +7 -0
  96. package/file-upload/interfaces/index.js +24 -0
  97. package/file-upload/interfaces/storage-provider.interface.d.ts +239 -0
  98. package/file-upload/interfaces/storage-provider.interface.js +2 -0
  99. package/file-upload/interfaces/upload-options.interface.d.ts +19 -0
  100. package/file-upload/interfaces/upload-options.interface.js +2 -0
  101. package/file-upload/providers/index.d.ts +2 -0
  102. package/file-upload/providers/index.js +18 -0
  103. package/file-upload/providers/local-storage.provider.d.ts +98 -0
  104. package/file-upload/providers/local-storage.provider.js +484 -0
  105. package/file-upload/providers/s3-storage.provider.d.ts +87 -0
  106. package/file-upload/providers/s3-storage.provider.js +455 -0
  107. package/file-upload/services/file-signature-validator.service.d.ts +118 -0
  108. package/file-upload/services/file-signature-validator.service.js +376 -0
  109. package/file-upload/services/file.service.d.ts +190 -0
  110. package/file-upload/services/file.service.js +609 -0
  111. package/file-upload/services/index.d.ts +4 -0
  112. package/file-upload/services/index.js +20 -0
  113. package/file-upload/services/malicious-file-detector.service.d.ts +274 -0
  114. package/file-upload/services/malicious-file-detector.service.js +1035 -0
  115. package/file-upload/services/mime-registry.service.d.ts +47 -0
  116. package/file-upload/services/mime-registry.service.js +167 -0
  117. package/file-upload/utils/checksum.util.d.ts +28 -0
  118. package/file-upload/utils/checksum.util.js +65 -0
  119. package/file-upload/utils/dynamic-import.util.d.ts +50 -0
  120. package/file-upload/utils/dynamic-import.util.js +144 -0
  121. package/file-upload/utils/filename.util.d.ts +59 -0
  122. package/file-upload/utils/filename.util.js +184 -0
  123. package/file-upload/utils/filepath.util.d.ts +70 -0
  124. package/file-upload/utils/filepath.util.js +152 -0
  125. package/file-upload/utils/index.d.ts +4 -0
  126. package/file-upload/utils/index.js +20 -0
  127. package/index.d.ts +3 -1
  128. package/index.js +4 -1
  129. package/package.json +4 -5
  130. package/setup/bootstrap.setup.d.ts +1 -0
  131. package/setup/bootstrap.setup.js +1 -0
  132. package/shared/index.d.ts +1 -1
  133. package/shared/index.js +1 -1
  134. package/shared/{serviceRegistryModule.js → service-registry.module.js} +0 -12
  135. package/shared/services/index.d.ts +0 -1
  136. package/shared/services/index.js +0 -1
  137. package/transaction/__tests__/mocks.d.ts +9 -0
  138. package/transaction/__tests__/mocks.js +33 -0
  139. package/transaction/base-service-transaction.d.ts +99 -0
  140. package/transaction/base-service-transaction.js +286 -0
  141. package/transaction/cls-compatibility.service.d.ts +55 -0
  142. package/transaction/cls-compatibility.service.js +127 -0
  143. package/transaction/data-source-registry.d.ts +91 -0
  144. package/transaction/data-source-registry.js +349 -0
  145. package/transaction/database-adapter.d.ts +44 -0
  146. package/transaction/database-adapter.js +240 -0
  147. package/transaction/decorators/entity-datasource.decorator.d.ts +62 -0
  148. package/transaction/decorators/entity-datasource.decorator.js +105 -0
  149. package/transaction/index.d.ts +14 -0
  150. package/transaction/index.js +57 -0
  151. package/transaction/logging-transactional.interceptor.d.ts +18 -0
  152. package/transaction/logging-transactional.interceptor.js +163 -0
  153. package/transaction/transaction-context.service.d.ts +137 -0
  154. package/transaction/transaction-context.service.js +411 -0
  155. package/transaction/transaction-manager.d.ts +230 -0
  156. package/transaction/transaction-manager.js +1001 -0
  157. package/transaction/transaction-synchronization.d.ts +171 -0
  158. package/transaction/transaction-synchronization.js +380 -0
  159. package/transaction/transaction.errors.d.ts +91 -0
  160. package/transaction/transaction.errors.js +206 -0
  161. package/transaction/transaction.module.d.ts +30 -0
  162. package/transaction/transaction.module.js +98 -0
  163. package/transaction/transactional.decorator.d.ts +82 -0
  164. package/transaction/transactional.decorator.js +319 -0
  165. package/transaction/typeorm-module-wrapper.d.ts +96 -0
  166. package/transaction/typeorm-module-wrapper.js +197 -0
  167. package/validators/file-mimetype.validator.d.ts +0 -2
  168. package/validators/file-mimetype.validator.js +4 -6
  169. package/validators/is-exists.validator.d.ts +2 -5
  170. package/validators/is-exists.validator.js +4 -6
  171. package/validators/is-unique.validator.d.ts +2 -5
  172. package/validators/is-unique.validator.js +6 -11
  173. package/shared/services/validator.service.d.ts +0 -3
  174. package/shared/services/validator.service.js +0 -20
  175. /package/shared/{serviceRegistryModule.d.ts → service-registry.module.d.ts} +0 -0
@@ -24,7 +24,7 @@ __decorate([
24
24
  __metadata("design:type", String)
25
25
  ], EntityTransactionEntity.prototype, "description", void 0);
26
26
  __decorate([
27
- (0, typeorm_1.Column)({ type: 'enum', enum: enums_1.TransactionStatus, comment: '事务状态' }),
27
+ (0, typeorm_1.Column)({ type: 'enum', enum: enums_1.AuditTransactionStatus, comment: '事务状态' }),
28
28
  __metadata("design:type", String)
29
29
  ], EntityTransactionEntity.prototype, "status", void 0);
30
30
  __decorate([
@@ -71,13 +71,20 @@ __decorate([
71
71
  __metadata("design:type", Date)
72
72
  ], EntityTransactionEntity.prototype, "completedAt", void 0);
73
73
  __decorate([
74
- (0, typeorm_1.OneToMany)('ManualOperationLogEntity', (log) => log.transaction),
74
+ (0, typeorm_1.OneToMany)('ManualOperationLogEntity', 'transaction'),
75
75
  __metadata("design:type", Array)
76
76
  ], EntityTransactionEntity.prototype, "manualOperations", void 0);
77
77
  exports.EntityTransactionEntity = EntityTransactionEntity = __decorate([
78
- (0, typeorm_1.Entity)('entity_transaction'),
78
+ (0, typeorm_1.Entity)('entity_transaction')
79
+ // 单字段索引
80
+ ,
79
81
  (0, typeorm_1.Index)('idx_status', ['status']),
80
82
  (0, typeorm_1.Index)('idx_user', ['userId']),
81
83
  (0, typeorm_1.Index)('idx_created_at', ['createdAt']),
82
84
  (0, typeorm_1.Index)('idx_template_key', ['operationTemplateKey'])
85
+ // 复合索引 - 优化常见查询
86
+ ,
87
+ (0, typeorm_1.Index)('idx_user_status', ['userId', 'status']),
88
+ (0, typeorm_1.Index)('idx_user_time', ['userId', 'createdAt']),
89
+ (0, typeorm_1.Index)('idx_status_time', ['status', 'createdAt'])
83
90
  ], EntityTransactionEntity);
@@ -2,3 +2,4 @@ export * from './entity-audit-log.entity';
2
2
  export * from './entity-transaction.entity';
3
3
  export * from './operation-template.entity';
4
4
  export * from './manual-operation-log.entity';
5
+ export * from './audit-action-summary.entity';
@@ -18,3 +18,4 @@ __exportStar(require("./entity-audit-log.entity"), exports);
18
18
  __exportStar(require("./entity-transaction.entity"), exports);
19
19
  __exportStar(require("./operation-template.entity"), exports);
20
20
  __exportStar(require("./manual-operation-log.entity"), exports);
21
+ __exportStar(require("./audit-action-summary.entity"), exports);
@@ -78,9 +78,16 @@ __decorate([
78
78
  __metadata("design:type", entity_transaction_entity_1.EntityTransactionEntity)
79
79
  ], ManualOperationLogEntity.prototype, "transaction", void 0);
80
80
  exports.ManualOperationLogEntity = ManualOperationLogEntity = __decorate([
81
- (0, typeorm_1.Entity)('manual_operation_log'),
81
+ (0, typeorm_1.Entity)('manual_operation_log')
82
+ // 单字段索引
83
+ ,
82
84
  (0, typeorm_1.Index)('idx_transaction', ['transactionId']),
83
85
  (0, typeorm_1.Index)('idx_template_key', ['operationTemplateKey']),
84
86
  (0, typeorm_1.Index)('idx_user', ['userId']),
85
87
  (0, typeorm_1.Index)('idx_created_at', ['createdAt'])
88
+ // 复合索引 - 优化常见查询
89
+ ,
90
+ (0, typeorm_1.Index)('idx_transaction_time', ['transactionId', 'createdAt']),
91
+ (0, typeorm_1.Index)('idx_user_time', ['userId', 'createdAt']),
92
+ (0, typeorm_1.Index)('idx_template_user', ['operationTemplateKey', 'userId'])
86
93
  ], ManualOperationLogEntity);
@@ -1,14 +1,5 @@
1
- /**
2
- * 操作类型(设计文档标准命名)
3
- */
4
- export declare enum OperationType {
5
- CREATE = "create",
6
- UPDATE = "update",
7
- DELETE = "delete"
8
- }
9
1
  /**
10
2
  * 审计操作类型(兼容性别名,映射到 OperationType)
11
- * @deprecated 请使用 OperationType
12
3
  */
13
4
  export declare enum AuditOperation {
14
5
  CREATE = "CREATE",
@@ -19,7 +10,7 @@ export declare enum AuditOperation {
19
10
  /**
20
11
  * 事务状态(设计文档标准命名)
21
12
  */
22
- export declare enum TransactionStatus {
13
+ export declare enum AuditTransactionStatus {
23
14
  PENDING = "pending",
24
15
  COMMITTED = "committed",
25
16
  ROLLED_BACK = "rolled_back"
@@ -1,18 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChangeType = exports.RollbackActionType = exports.MaskingStrategy = exports.RecordStrategy = exports.RecoveryStrategy = exports.TransactionStatus = exports.AuditOperation = exports.OperationType = void 0;
4
- /**
5
- * 操作类型(设计文档标准命名)
6
- */
7
- var OperationType;
8
- (function (OperationType) {
9
- OperationType["CREATE"] = "create";
10
- OperationType["UPDATE"] = "update";
11
- OperationType["DELETE"] = "delete";
12
- })(OperationType || (exports.OperationType = OperationType = {}));
3
+ exports.ChangeType = exports.RollbackActionType = exports.MaskingStrategy = exports.RecordStrategy = exports.RecoveryStrategy = exports.AuditTransactionStatus = exports.AuditOperation = void 0;
13
4
  /**
14
5
  * 审计操作类型(兼容性别名,映射到 OperationType)
15
- * @deprecated 请使用 OperationType
16
6
  */
17
7
  var AuditOperation;
18
8
  (function (AuditOperation) {
@@ -24,12 +14,12 @@ var AuditOperation;
24
14
  /**
25
15
  * 事务状态(设计文档标准命名)
26
16
  */
27
- var TransactionStatus;
28
- (function (TransactionStatus) {
29
- TransactionStatus["PENDING"] = "pending";
30
- TransactionStatus["COMMITTED"] = "committed";
31
- TransactionStatus["ROLLED_BACK"] = "rolled_back";
32
- })(TransactionStatus || (exports.TransactionStatus = TransactionStatus = {}));
17
+ var AuditTransactionStatus;
18
+ (function (AuditTransactionStatus) {
19
+ AuditTransactionStatus["PENDING"] = "pending";
20
+ AuditTransactionStatus["COMMITTED"] = "committed";
21
+ AuditTransactionStatus["ROLLED_BACK"] = "rolled_back";
22
+ })(AuditTransactionStatus || (exports.AuditTransactionStatus = AuditTransactionStatus = {}));
33
23
  /**
34
24
  * 恢复策略
35
25
  */
package/audit/index.d.ts CHANGED
@@ -2,9 +2,10 @@ export * from './entities';
2
2
  export * from './enums';
3
3
  export * from './interfaces';
4
4
  export * from './dto';
5
- export { AuditEntity, AuditField, getEntityAuditConfig, getFieldAuditConfig, getFieldLabel, getFieldValueLabel, ENTITY_AUDIT_OPTIONS, FIELD_AUDIT_OPTIONS, } from './decorators/entity-audit.decorator';
5
+ export { AuditEntity, AuditField, getEntityAuditConfig, getFieldAuditConfig, getFieldLabel, getFieldValueLabel, getEntityLabel, ENTITY_AUDIT_OPTIONS, FIELD_AUDIT_OPTIONS, } from './decorators/entity-audit.decorator';
6
6
  export { AuditController, AuditMethod, CONTROLLER_AUDIT_OPTIONS, METHOD_AUDIT_OPTIONS, } from './decorators/audit-controller.decorator';
7
7
  export { AuditLog, getAuditOperationConfig, hasAuditOperation, AUDIT_OPERATION_OPTIONS, } from './decorators/audit-operation.decorator';
8
+ export { AuditAction, AuditActionOptions, AuditActionMetadata, AUDIT_ACTION_METADATA, } from './decorators/audit-action.decorator';
8
9
  export * from './services';
9
10
  export * from './subscribers';
10
11
  export * from './interceptors';
package/audit/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.AUDIT_OPERATION_OPTIONS = exports.hasAuditOperation = exports.getAuditOperationConfig = exports.AuditLog = exports.METHOD_AUDIT_OPTIONS = exports.CONTROLLER_AUDIT_OPTIONS = exports.AuditMethod = exports.AuditController = exports.FIELD_AUDIT_OPTIONS = exports.ENTITY_AUDIT_OPTIONS = exports.getFieldValueLabel = exports.getFieldLabel = exports.getFieldAuditConfig = exports.getEntityAuditConfig = exports.AuditField = exports.AuditEntity = void 0;
17
+ exports.AUDIT_ACTION_METADATA = exports.AuditAction = exports.AUDIT_OPERATION_OPTIONS = exports.hasAuditOperation = exports.getAuditOperationConfig = exports.AuditLog = exports.METHOD_AUDIT_OPTIONS = exports.CONTROLLER_AUDIT_OPTIONS = exports.AuditMethod = exports.AuditController = exports.FIELD_AUDIT_OPTIONS = exports.ENTITY_AUDIT_OPTIONS = exports.getEntityLabel = exports.getFieldValueLabel = exports.getFieldLabel = exports.getFieldAuditConfig = exports.getEntityAuditConfig = exports.AuditField = exports.AuditEntity = void 0;
18
18
  // Entities
19
19
  __exportStar(require("./entities"), exports);
20
20
  // Enums
@@ -31,6 +31,7 @@ Object.defineProperty(exports, "getEntityAuditConfig", { enumerable: true, get:
31
31
  Object.defineProperty(exports, "getFieldAuditConfig", { enumerable: true, get: function () { return entity_audit_decorator_1.getFieldAuditConfig; } });
32
32
  Object.defineProperty(exports, "getFieldLabel", { enumerable: true, get: function () { return entity_audit_decorator_1.getFieldLabel; } });
33
33
  Object.defineProperty(exports, "getFieldValueLabel", { enumerable: true, get: function () { return entity_audit_decorator_1.getFieldValueLabel; } });
34
+ Object.defineProperty(exports, "getEntityLabel", { enumerable: true, get: function () { return entity_audit_decorator_1.getEntityLabel; } });
34
35
  Object.defineProperty(exports, "ENTITY_AUDIT_OPTIONS", { enumerable: true, get: function () { return entity_audit_decorator_1.ENTITY_AUDIT_OPTIONS; } });
35
36
  Object.defineProperty(exports, "FIELD_AUDIT_OPTIONS", { enumerable: true, get: function () { return entity_audit_decorator_1.FIELD_AUDIT_OPTIONS; } });
36
37
  var audit_controller_decorator_1 = require("./decorators/audit-controller.decorator");
@@ -43,6 +44,9 @@ Object.defineProperty(exports, "AuditLog", { enumerable: true, get: function ()
43
44
  Object.defineProperty(exports, "getAuditOperationConfig", { enumerable: true, get: function () { return audit_operation_decorator_1.getAuditOperationConfig; } });
44
45
  Object.defineProperty(exports, "hasAuditOperation", { enumerable: true, get: function () { return audit_operation_decorator_1.hasAuditOperation; } });
45
46
  Object.defineProperty(exports, "AUDIT_OPERATION_OPTIONS", { enumerable: true, get: function () { return audit_operation_decorator_1.AUDIT_OPERATION_OPTIONS; } });
47
+ var audit_action_decorator_1 = require("./decorators/audit-action.decorator");
48
+ Object.defineProperty(exports, "AuditAction", { enumerable: true, get: function () { return audit_action_decorator_1.AuditAction; } });
49
+ Object.defineProperty(exports, "AUDIT_ACTION_METADATA", { enumerable: true, get: function () { return audit_action_decorator_1.AUDIT_ACTION_METADATA; } });
46
50
  // Services
47
51
  __exportStar(require("./services"), exports);
48
52
  // Subscribers
@@ -0,0 +1,38 @@
1
+ import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { Observable } from 'rxjs';
4
+ import { Repository } from 'typeorm';
5
+ import { AuditContextService } from '../services/audit-context.service';
6
+ import { OperationDescriptionService } from '../services/operation-description.service';
7
+ import { AuditActionSummaryEntity } from '../entities';
8
+ /**
9
+ * 审计动作拦截器
10
+ * 在请求开始时创建审计动作上下文,在请求结束时生成汇总记录
11
+ */
12
+ export declare class AuditActionInterceptor implements NestInterceptor {
13
+ private readonly reflector;
14
+ private readonly contextService;
15
+ private readonly summaryRepository;
16
+ private readonly descriptionService?;
17
+ constructor(reflector: Reflector, contextService: AuditContextService, summaryRepository: Repository<AuditActionSummaryEntity>, descriptionService?: OperationDescriptionService);
18
+ intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
19
+ /**
20
+ * 生成审计汇总记录
21
+ */
22
+ private generateSummary;
23
+ /**
24
+ * 生成操作描述文本
25
+ * 优先从 OperationDescriptionService 获取多语言模板
26
+ * 如果没有,则使用默认格式
27
+ */
28
+ private generateDescription;
29
+ /**
30
+ * 替换模板中的占位符
31
+ * 支持格式:{key} 或 {key:format}
32
+ */
33
+ private replaceTemplatePlaceholders;
34
+ /**
35
+ * 格式化值为字符串
36
+ */
37
+ private formatValue;
38
+ }
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
+ return new (P || (P = Promise))(function (resolve, reject) {
17
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
21
+ });
22
+ };
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.AuditActionInterceptor = void 0;
25
+ const common_1 = require("@nestjs/common");
26
+ const core_1 = require("@nestjs/core");
27
+ const rxjs_1 = require("rxjs");
28
+ const operators_1 = require("rxjs/operators");
29
+ const typeorm_1 = require("@nestjs/typeorm");
30
+ const typeorm_2 = require("typeorm");
31
+ const audit_context_service_1 = require("../services/audit-context.service");
32
+ const operation_description_service_1 = require("../services/operation-description.service");
33
+ const entities_1 = require("../entities");
34
+ const audit_action_decorator_1 = require("../decorators/audit-action.decorator");
35
+ /**
36
+ * 审计动作拦截器
37
+ * 在请求开始时创建审计动作上下文,在请求结束时生成汇总记录
38
+ */
39
+ let AuditActionInterceptor = class AuditActionInterceptor {
40
+ constructor(reflector, contextService, summaryRepository, descriptionService) {
41
+ this.reflector = reflector;
42
+ this.contextService = contextService;
43
+ this.summaryRepository = summaryRepository;
44
+ this.descriptionService = descriptionService;
45
+ }
46
+ intercept(context, next) {
47
+ // 获取装饰器元数据
48
+ const metadata = this.reflector.get(audit_action_decorator_1.AUDIT_ACTION_METADATA, context.getHandler());
49
+ // 如果没有 @AuditAction 装饰器或已禁用,直接放行
50
+ if (!metadata || metadata.options.enabled === false) {
51
+ return next.handle();
52
+ }
53
+ const request = context.switchToHttp().getRequest();
54
+ const actionId = this.contextService.generateActionId();
55
+ const startTime = new Date();
56
+ // 创建审计动作上下文
57
+ const actionContext = {
58
+ actionId,
59
+ actionName: metadata.actionName,
60
+ operationTemplateKey: metadata.options.templateKey,
61
+ paramsBuilder: metadata.options.paramsBuilder,
62
+ startTime,
63
+ entityChanges: [],
64
+ detailedChanges: [],
65
+ req: request,
66
+ };
67
+ // 设置到 CLS
68
+ this.contextService.setActionContext(actionContext);
69
+ return next.handle().pipe((0, operators_1.tap)((result) => __awaiter(this, void 0, void 0, function* () {
70
+ // 请求成功,记录结果
71
+ actionContext.result = result;
72
+ yield this.generateSummary(actionContext, metadata, false);
73
+ })), (0, operators_1.catchError)((error) => __awaiter(this, void 0, void 0, function* () {
74
+ // 请求失败,记录错误
75
+ actionContext.error = error;
76
+ yield this.generateSummary(actionContext, metadata, true, error);
77
+ return (0, rxjs_1.throwError)(() => error);
78
+ })));
79
+ }
80
+ /**
81
+ * 生成审计汇总记录
82
+ */
83
+ generateSummary(actionContext, metadata, hasError, error) {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ try {
86
+ const duration = Date.now() - actionContext.startTime.getTime();
87
+ const auditContext = yield this.contextService.getCurrentContext();
88
+ // 构建描述参数
89
+ let descriptionParams = {};
90
+ if (metadata.options.paramsBuilder) {
91
+ const paramsContext = this.contextService.buildParamsContext();
92
+ if (paramsContext) {
93
+ descriptionParams = metadata.options.paramsBuilder(paramsContext);
94
+ }
95
+ }
96
+ // 生成描述文本
97
+ const description = yield this.generateDescription(actionContext.operationTemplateKey, actionContext.actionName, descriptionParams, !hasError);
98
+ // 统计实体类型和操作
99
+ const entityTypes = Array.from(new Set(actionContext.detailedChanges.map((c) => c.entityType)));
100
+ const operationStats = {};
101
+ actionContext.detailedChanges.forEach((change) => {
102
+ const op = change.operation;
103
+ operationStats[op] = (operationStats[op] || 0) + 1;
104
+ });
105
+ // 创建汇总记录
106
+ const summary = this.summaryRepository.create({
107
+ id: actionContext.actionId,
108
+ actionName: actionContext.actionName,
109
+ operationTemplateKey: actionContext.operationTemplateKey,
110
+ descriptionParams,
111
+ description,
112
+ requestId: auditContext.requestId || '',
113
+ userId: auditContext.userId || '',
114
+ username: auditContext.username || '',
115
+ requestIp: auditContext.requestIp || '',
116
+ userAgent: auditContext.userAgent || '',
117
+ entityChangesCount: actionContext.detailedChanges.length,
118
+ entityTypes,
119
+ operationStats,
120
+ success: !hasError,
121
+ errorMessage: error === null || error === void 0 ? void 0 : error.message,
122
+ duration,
123
+ metadata: metadata.options.metadata,
124
+ });
125
+ yield this.summaryRepository.save(summary);
126
+ }
127
+ catch (err) {
128
+ console.error('Failed to generate audit action summary:', err);
129
+ }
130
+ finally {
131
+ // 清除审计动作上下文
132
+ this.contextService.clearActionContext();
133
+ }
134
+ });
135
+ }
136
+ /**
137
+ * 生成操作描述文本
138
+ * 优先从 OperationDescriptionService 获取多语言模板
139
+ * 如果没有,则使用默认格式
140
+ */
141
+ generateDescription(templateKey, actionName, params, success) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ let template;
144
+ // 1. 尝试从模板服务获取模板
145
+ if (templateKey && this.descriptionService) {
146
+ try {
147
+ template = yield this.descriptionService.getTemplate(templateKey);
148
+ }
149
+ catch (error) {
150
+ // 如果获取模板失败,继续使用默认逻辑
151
+ }
152
+ }
153
+ // 2. 如果没有模板,使用默认格式
154
+ if (!template) {
155
+ template = success
156
+ ? `执行操作: ${actionName}`
157
+ : `执行操作失败: ${actionName}`;
158
+ }
159
+ // 3. 执行模板替换
160
+ return this.replaceTemplatePlaceholders(template, params);
161
+ });
162
+ }
163
+ /**
164
+ * 替换模板中的占位符
165
+ * 支持格式:{key} 或 {key:format}
166
+ */
167
+ replaceTemplatePlaceholders(template, params) {
168
+ let result = template;
169
+ // 遍历所有参数,替换占位符
170
+ Object.entries(params).forEach(([key, value]) => {
171
+ const placeholder = `{${key}}`;
172
+ const valueStr = this.formatValue(value);
173
+ // 替换所有匹配的占位符
174
+ const regex = new RegExp(placeholder.replace(/[{}]/g, '\\$&'), 'g');
175
+ result = result.replace(regex, valueStr);
176
+ });
177
+ // 清理未替换的占位符
178
+ result = result.replace(/\{[^}]+\}/g, '');
179
+ return result;
180
+ }
181
+ /**
182
+ * 格式化值为字符串
183
+ */
184
+ formatValue(value) {
185
+ if (value === null || value === undefined) {
186
+ return '';
187
+ }
188
+ if (Array.isArray(value)) {
189
+ // 处理数组
190
+ if (value.length === 0) {
191
+ return '';
192
+ }
193
+ // 如果是对象数组,序列化
194
+ if (typeof value[0] === 'object') {
195
+ return JSON.stringify(value);
196
+ }
197
+ // 否则用顿号连接
198
+ return value.join('、');
199
+ }
200
+ if (typeof value === 'object') {
201
+ return JSON.stringify(value);
202
+ }
203
+ return String(value);
204
+ }
205
+ };
206
+ exports.AuditActionInterceptor = AuditActionInterceptor;
207
+ exports.AuditActionInterceptor = AuditActionInterceptor = __decorate([
208
+ (0, common_1.Injectable)(),
209
+ __param(2, (0, common_1.Inject)((0, typeorm_1.getRepositoryToken)(entities_1.AuditActionSummaryEntity))),
210
+ __param(3, (0, common_1.Optional)()),
211
+ __metadata("design:paramtypes", [core_1.Reflector,
212
+ audit_context_service_1.AuditContextService,
213
+ typeorm_2.Repository,
214
+ operation_description_service_1.OperationDescriptionService])
215
+ ], AuditActionInterceptor);
@@ -1 +1,2 @@
1
1
  export * from './audit.interceptor';
2
+ export * from './audit-action.interceptor';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./audit.interceptor"), exports);
18
+ __exportStar(require("./audit-action.interceptor"), exports);
@@ -146,16 +146,21 @@ export interface EntityAuditConfig {
146
146
  includeFields?: string[];
147
147
  excludeFields?: string[];
148
148
  maskFields?: string[];
149
+ /**
150
+ * 实体标签(支持多语言)
151
+ * @example
152
+ * // 单语言
153
+ * label: '订单'
154
+ *
155
+ * // 多语言
156
+ * label: { zh: '订单', en: 'Order', ja: '注文' }
157
+ */
158
+ label?: string | Record<string, string>;
149
159
  /**
150
160
  * 操作模板键前缀
151
161
  * @example 'user' -> 生成 'user.create', 'user.update', 'user.delete'
152
162
  */
153
163
  templateKey?: string;
154
- /**
155
- * @deprecated 使用 maskFields 代替
156
- * 敏感字段列表(别名,用于兼容旧版本)
157
- */
158
- sensitiveFields?: string[];
159
164
  }
160
165
  /**
161
166
  * 操作级别审计配置
@@ -0,0 +1,141 @@
1
+ import { AuditContextService } from './audit-context.service';
2
+ import { EntityAuditService } from './entity-audit.service';
3
+ import { AuditOperation } from '../enums';
4
+ /**
5
+ * 审计动作选项
6
+ */
7
+ export interface AuditActionOptions {
8
+ /**
9
+ * 动作名称,如 'createOrder', 'updateUser'
10
+ */
11
+ actionName: string;
12
+ /**
13
+ * 操作模板键(可选)
14
+ * @example 'order.create'
15
+ */
16
+ operationTemplateKey?: string;
17
+ /**
18
+ * 描述参数,用于模板填充
19
+ * @example { productCount: 3, receiver: '张三', totalAmount: 299.99 }
20
+ */
21
+ descriptionParams?: Record<string, any>;
22
+ /**
23
+ * 自定义元数据
24
+ */
25
+ metadata?: Record<string, any>;
26
+ }
27
+ /**
28
+ * 审计动作结果
29
+ */
30
+ export interface AuditActionResult {
31
+ actionId: string;
32
+ actionName: string;
33
+ entityChanges: Array<{
34
+ entityType: string;
35
+ entityId: string;
36
+ operation: string;
37
+ sequence: number;
38
+ }>;
39
+ duration: number;
40
+ success: boolean;
41
+ error?: Error;
42
+ }
43
+ /**
44
+ * 审计动作服务
45
+ * 提供声明式的审计动作管理,自动关联同一业务操作的所有实体变化
46
+ */
47
+ export declare class AuditActionService {
48
+ private readonly contextService;
49
+ private readonly auditService;
50
+ constructor(contextService: AuditContextService, auditService: EntityAuditService);
51
+ /**
52
+ * 在审计动作上下文中执行操作
53
+ * 自动关联该动作内的所有实体变化
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const result = await auditActionService.executeAction(
58
+ * {
59
+ * actionName: 'createOrder',
60
+ * operationTemplateKey: 'order.create',
61
+ * descriptionParams: {
62
+ * productCount: orderItems.length,
63
+ * receiver: order.receiverName,
64
+ * totalAmount: order.totalAmount,
65
+ * address: order.shippingAddress,
66
+ * },
67
+ * },
68
+ * async () => {
69
+ * // 1. 创建订单
70
+ * const order = await this.orderRepository.save(newOrder);
71
+ *
72
+ * // 2. 创建订单项
73
+ * await this.orderItemRepository.save(orderItems);
74
+ *
75
+ * // 3. 扣减库存
76
+ * await this.inventoryService.deduct(orderItems);
77
+ *
78
+ * // 4. 创建物流信息
79
+ * await this.shippingService.create(order.id);
80
+ *
81
+ * return order;
82
+ * }
83
+ * );
84
+ *
85
+ * // 结果中包含所有实体变化:
86
+ * // result.entityChanges = [
87
+ * // { entityType: 'Order', entityId: '1', operation: 'CREATE', sequence: 1 },
88
+ * // { entityType: 'OrderItem', entityId: '1', operation: 'CREATE', sequence: 2 },
89
+ * // { entityType: 'OrderItem', entityId: '2', operation: 'CREATE', sequence: 3 },
90
+ * // { entityType: 'Inventory', entityId: '1', operation: 'UPDATE', sequence: 4 },
91
+ * // { entityType: 'Shipping', entityId: '1', operation: 'CREATE', sequence: 5 },
92
+ * // ]
93
+ * ```
94
+ */
95
+ executeAction<T>(options: AuditActionOptions, operation: () => Promise<T>): Promise<AuditActionResult>;
96
+ /**
97
+ * 记录审计动作汇总日志
98
+ */
99
+ private recordActionSummary;
100
+ /**
101
+ * 获取审计动作的所有实体变化
102
+ */
103
+ getActionAuditLogs(actionId: string): Promise<{
104
+ actionId: string;
105
+ logs: import("..").EntityAuditLogEntity[];
106
+ summary: {
107
+ totalChanges: number;
108
+ operations: {
109
+ [k: string]: number;
110
+ };
111
+ entities: {
112
+ [k: string]: number;
113
+ };
114
+ };
115
+ }>;
116
+ /**
117
+ * 获取请求内的所有审计动作
118
+ */
119
+ getRequestActions(requestId: string): Promise<{
120
+ requestId: string;
121
+ actions: {
122
+ actionId: string;
123
+ actionName: string;
124
+ entityChanges: {
125
+ entityType: string;
126
+ entityId: string;
127
+ operation: AuditOperation;
128
+ sequence: number;
129
+ timestamp: Date;
130
+ }[];
131
+ logs: import("..").EntityAuditLogEntity[];
132
+ }[];
133
+ noActionLogs: import("..").EntityAuditLogEntity[];
134
+ summary: {
135
+ totalActions: number;
136
+ totalChanges: number;
137
+ };
138
+ }>;
139
+ private groupByOperation;
140
+ private groupByEntity;
141
+ }