@mavenmachines/event-handler-lib 1.4.5-10 → 1.4.5-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.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mavenmachines/event-handler-lib",
3
- "version": "1.4.5-10",
3
+ "version": "1.4.5-11",
4
4
  "description": "Helper libraries for creating an Event Handler",
5
5
  "module": "./dist/src/index.mjs",
6
6
  "types": "./dist/src/index.d.ts",
@@ -1,6 +1,18 @@
1
1
  import { EsReferenceIdsType } from "@mavenmachines/event-sourcing-types";
2
+ import { EsReferencesStoreClient, GetAggregateReferencesRequest } from "@mavenmachines/es-references-store-client";
2
3
  import { ProjectionModifierPlugin } from "./projectionModifierPlugin";
3
- import { LoggerService } from "../../exports";
4
+ import { EventSourcedProjection, LoggerService, ReferenceIdsType } from "../../exports";
4
5
  export declare abstract class SyncReferenceIdsPlugin<ProjectionT extends Record<string, any>> extends ProjectionModifierPlugin<ProjectionT, Record<string, EsReferenceIdsType>> {
5
- constructor(loggerService: LoggerService);
6
+ protected readonly client: EsReferencesStoreClient;
7
+ constructor(loggerService: LoggerService, client: EsReferencesStoreClient);
8
+ abstract createGetAggregateReferenceRequests(projection: EventSourcedProjection<ProjectionT>): GetAggregateReferencesRequest[];
9
+ protected getExistingReferenceIds(projection: EventSourcedProjection<ProjectionT>): Promise<Record<string, ReferenceIdsType>>;
10
+ abstract createReferenceIds(projection: EventSourcedProjection<ProjectionT>): Promise<Record<string, ReferenceIdsType>>;
11
+ setReferenceIds(projection: EventSourcedProjection<ProjectionT>, previous: Record<string, ReferenceIdsType>, updated: Record<string, ReferenceIdsType>): Promise<Record<string, ReferenceIdsType>>;
12
+ onCreate(projection: EventSourcedProjection<ProjectionT>): Promise<Record<string, ReferenceIdsType>>;
13
+ onUpdate(projection: EventSourcedProjection<ProjectionT>): Promise<Record<string, ReferenceIdsType>>;
14
+ onDelete(projection: EventSourcedProjection<ProjectionT>): Promise<Record<string, ReferenceIdsType>>;
15
+ rollbackOnCreate(projection: EventSourcedProjection<ProjectionT>, data: Record<string, ReferenceIdsType>): Promise<void>;
16
+ rollbackOnUpdate(projection: EventSourcedProjection<ProjectionT>, data: Record<string, ReferenceIdsType>): Promise<void>;
17
+ rollbackOnDelete(projection: EventSourcedProjection<ProjectionT>, data: Record<string, ReferenceIdsType>): Promise<void>;
6
18
  }
@@ -13,18 +13,98 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SyncReferenceIdsPlugin = void 0;
16
+ const es_references_store_client_1 = require("@mavenmachines/es-references-store-client");
16
17
  const projectionModifierPlugin_1 = require("./projectionModifierPlugin");
17
18
  const exports_1 = require("../../exports");
18
19
  const inversify_1 = require("inversify");
19
20
  let SyncReferenceIdsPlugin = class SyncReferenceIdsPlugin extends projectionModifierPlugin_1.ProjectionModifierPlugin {
20
- constructor(loggerService) {
21
+ constructor(loggerService, client) {
21
22
  super(loggerService);
23
+ this.client = client;
24
+ }
25
+ async getExistingReferenceIds(projection) {
26
+ const requests = this.createGetAggregateReferenceRequests(projection);
27
+ const results = [];
28
+ for (const req of requests) {
29
+ const references = await this.client.getAggregateReferences(req);
30
+ const referenceIds = Object.fromEntries(references.map((ref) => [ref.referenceKey, ref.referenceValue]));
31
+ results.push([
32
+ `${projection.companyId}_${projection.projectionType}_${projection.entityId}`,
33
+ referenceIds,
34
+ ]);
35
+ }
36
+ return Object.fromEntries(results);
37
+ }
38
+ async setReferenceIds(projection, previous, updated) {
39
+ for (const [aggregateId, previousRefs] of Object.entries(previous)) {
40
+ const updatedRefs = updated[aggregateId] ?? {};
41
+ const refsToDelete = {};
42
+ for (const [refKey, refValue] of Object.entries(previousRefs)) {
43
+ if (!(refKey in updatedRefs)) {
44
+ refsToDelete[refKey] = refValue;
45
+ }
46
+ }
47
+ if (Object.keys(refsToDelete).length > 0) {
48
+ const deleteRequest = {
49
+ companyId: projection.companyId,
50
+ aggregateType: projection.projectionType,
51
+ referenceIdsList: [refsToDelete],
52
+ };
53
+ await this.client.deleteAggregateReferences(deleteRequest);
54
+ }
55
+ }
56
+ for (const [aggregateId, updatedRefs] of Object.entries(updated)) {
57
+ const previousRefs = previous[aggregateId] ?? {};
58
+ const refsToAdd = {};
59
+ for (const [refKey, refValue] of Object.entries(updatedRefs)) {
60
+ if (previousRefs[refKey] !== refValue) {
61
+ refsToAdd[refKey] = refValue;
62
+ }
63
+ }
64
+ if (Object.keys(refsToAdd).length > 0) {
65
+ const addRequest = {
66
+ companyId: projection.companyId,
67
+ aggregateType: projection.projectionType,
68
+ aggregateId: aggregateId,
69
+ referenceIds: refsToAdd,
70
+ };
71
+ await this.client.addAggregateReferences(addRequest);
72
+ }
73
+ }
74
+ return updated;
75
+ }
76
+ async onCreate(projection) {
77
+ const existingReferenceIds = await this.getExistingReferenceIds(projection);
78
+ return this.setReferenceIds(projection, {}, existingReferenceIds);
79
+ }
80
+ async onUpdate(projection) {
81
+ const existingReferenceIds = await this.getExistingReferenceIds(projection);
82
+ const newReferenceIds = await this.createReferenceIds(projection);
83
+ return this.setReferenceIds(projection, existingReferenceIds, newReferenceIds);
84
+ }
85
+ async onDelete(projection) {
86
+ const existingReferenceIds = await this.getExistingReferenceIds(projection);
87
+ return this.setReferenceIds(projection, existingReferenceIds, {});
88
+ }
89
+ async rollbackOnCreate(projection, data) {
90
+ const existingReferenceIds = await this.getExistingReferenceIds(projection);
91
+ this.setReferenceIds(projection, existingReferenceIds, data);
92
+ }
93
+ async rollbackOnUpdate(projection, data) {
94
+ const existingReferenceIds = await this.getExistingReferenceIds(projection);
95
+ this.setReferenceIds(projection, existingReferenceIds, data);
96
+ }
97
+ async rollbackOnDelete(projection, data) {
98
+ const existingReferenceIds = await this.getExistingReferenceIds(projection);
99
+ this.setReferenceIds(projection, existingReferenceIds, data);
22
100
  }
23
101
  };
24
102
  exports.SyncReferenceIdsPlugin = SyncReferenceIdsPlugin;
25
103
  exports.SyncReferenceIdsPlugin = SyncReferenceIdsPlugin = __decorate([
26
104
  (0, inversify_1.injectable)(),
27
105
  __param(0, (0, inversify_1.inject)(exports_1.LoggerService)),
28
- __metadata("design:paramtypes", [exports_1.LoggerService])
106
+ __param(1, (0, inversify_1.inject)(es_references_store_client_1.EsReferencesStoreClient)),
107
+ __metadata("design:paramtypes", [exports_1.LoggerService,
108
+ es_references_store_client_1.EsReferencesStoreClient])
29
109
  ], SyncReferenceIdsPlugin);
30
110
  //# sourceMappingURL=syncReferencIdsPlugin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"syncReferencIdsPlugin.js","sourceRoot":"","sources":["../../../../src/helpers/plugins/syncReferencIdsPlugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAWA,yEAAsE;AACtE,2CAIuB;AAEvB,yCAA+C;AAIxC,IAAe,sBAAsB,GAArC,MAAe,sBAEpB,SAAQ,mDAGT;IACC,YACyB,aAA4B;QAInD,KAAK,CAAC,aAAa,CAAC,CAAC;IACvB,CAAC;CA0IF,CAAA;AAtJqB,wDAAsB;iCAAtB,sBAAsB;IAD3C,IAAA,sBAAU,GAAE;IAQR,WAAA,IAAA,kBAAM,EAAC,uBAAa,CAAC,CAAA;qCAAgB,uBAAa;GAPjC,sBAAsB,CAsJ3C"}
1
+ {"version":3,"file":"syncReferencIdsPlugin.js","sourceRoot":"","sources":["../../../../src/helpers/plugins/syncReferencIdsPlugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,0FAMmD;AACnD,yEAAsE;AACtE,2CAIuB;AACvB,yCAA+C;AAIxC,IAAe,sBAAsB,GAArC,MAAe,sBAEpB,SAAQ,mDAGT;IACC,YACyB,aAA4B,EAEhC,MAA+B;QAElD,KAAK,CAAC,aAAa,CAAC,CAAC;QAFF,WAAM,GAAN,MAAM,CAAyB;IAGpD,CAAC;IAMS,KAAK,CAAC,uBAAuB,CACrC,UAA+C;QAE/C,MAAM,Q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DAAsB;iCAAtB,sBAAsB;IAD3C,IAAA,sBAAU,GAAE;IAQR,WAAA,IAAA,kBAAM,EAAC,uBAAa,CAAC,CAAA;IACrB,WAAA,IAAA,kBAAM,EAAC,oDAAuB,CAAC,CAAA;qCADM,uBAAa;QAExB,oDAAuB;GAThC,sBAAsB,CAqJ3C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mavenmachines/event-handler-lib",
3
- "version": "1.4.5-10",
3
+ "version": "1.4.5-11",
4
4
  "description": "Helper libraries for creating an Event Handler",
5
5
  "module": "./dist/src/index.mjs",
6
6
  "types": "./dist/src/index.d.ts",