@zeeshan60/event-processor 1.0.15 → 1.0.16

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 (83) hide show
  1. package/README.md +29 -22
  2. package/dist/ActivityEventHandler.d.ts +3 -3
  3. package/dist/ActivityEventHandler.d.ts.map +1 -1
  4. package/dist/ActivityEventHandler.js +3 -8
  5. package/dist/BaseEventHandler.d.ts +5 -3
  6. package/dist/BaseEventHandler.d.ts.map +1 -1
  7. package/dist/BaseEventHandler.js +11 -0
  8. package/dist/EventProcessorSDK.d.ts +4 -0
  9. package/dist/EventProcessorSDK.d.ts.map +1 -1
  10. package/dist/EventProcessorSDK.js +7 -0
  11. package/dist/FriendEventHandler.d.ts +4 -2
  12. package/dist/FriendEventHandler.d.ts.map +1 -1
  13. package/dist/FriendEventHandler.js +50 -36
  14. package/dist/GroupEventHandler.d.ts +2 -2
  15. package/dist/GroupEventHandler.d.ts.map +1 -1
  16. package/dist/GroupEventHandler.js +235 -186
  17. package/dist/GroupTransactionEventHandler.d.ts +1 -2
  18. package/dist/GroupTransactionEventHandler.d.ts.map +1 -1
  19. package/dist/GroupTransactionEventHandler.js +4 -9
  20. package/dist/TransactionEventHandler.d.ts +1 -2
  21. package/dist/TransactionEventHandler.d.ts.map +1 -1
  22. package/dist/TransactionEventHandler.js +4 -9
  23. package/dist/UserEventHandler.d.ts +1 -2
  24. package/dist/UserEventHandler.d.ts.map +1 -1
  25. package/dist/UserEventHandler.js +4 -9
  26. package/dist/__tests__/test-helpers/IMCurrencyStore.d.ts +8 -0
  27. package/dist/__tests__/test-helpers/IMCurrencyStore.d.ts.map +1 -0
  28. package/dist/__tests__/test-helpers/IMCurrencyStore.js +16 -0
  29. package/dist/__tests__/test-helpers/IMTransactionModelStore.d.ts +2 -0
  30. package/dist/__tests__/test-helpers/IMTransactionModelStore.d.ts.map +1 -1
  31. package/dist/__tests__/test-helpers/IMTransactionModelStore.js +20 -0
  32. package/dist/__tests__/test-helpers/IMUserModelStore.d.ts +3 -0
  33. package/dist/__tests__/test-helpers/IMUserModelStore.d.ts.map +1 -1
  34. package/dist/__tests__/test-helpers/IMUserModelStore.js +10 -0
  35. package/dist/client/controllers/FriendController.d.ts +4 -0
  36. package/dist/client/controllers/FriendController.d.ts.map +1 -1
  37. package/dist/client/controllers/FriendController.js +45 -15
  38. package/dist/client/controllers/TransactionController.d.ts +7 -1
  39. package/dist/client/controllers/TransactionController.d.ts.map +1 -1
  40. package/dist/client/controllers/TransactionController.js +61 -0
  41. package/dist/common/CurrencyRate.d.ts +7 -0
  42. package/dist/common/CurrencyRate.d.ts.map +1 -0
  43. package/dist/common/CurrencyRate.js +2 -0
  44. package/dist/contracts/friends/index.d.ts +1 -9
  45. package/dist/contracts/friends/index.d.ts.map +1 -1
  46. package/dist/environment.d.ts +12 -0
  47. package/dist/environment.d.ts.map +1 -0
  48. package/dist/environment.js +50 -0
  49. package/dist/events.d.ts +2 -0
  50. package/dist/events.d.ts.map +1 -1
  51. package/dist/events.js +7 -1
  52. package/dist/index.d.ts +3 -1
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +8 -1
  55. package/dist/store/CurrencyStore.d.ts +5 -0
  56. package/dist/store/CurrencyStore.d.ts.map +1 -0
  57. package/dist/store/CurrencyStore.js +6 -0
  58. package/dist/store/TransactionModelStore.d.ts +2 -0
  59. package/dist/store/TransactionModelStore.d.ts.map +1 -1
  60. package/dist/store/UserModelStore.d.ts +1 -0
  61. package/dist/store/UserModelStore.d.ts.map +1 -1
  62. package/dist/utils/BalanceUtil.d.ts +13 -0
  63. package/dist/utils/BalanceUtil.d.ts.map +1 -0
  64. package/dist/utils/BalanceUtil.js +97 -0
  65. package/package.json +1 -1
  66. package/dist/client/handlers/UIFriendEventHandler.d.ts +0 -16
  67. package/dist/client/handlers/UIFriendEventHandler.d.ts.map +0 -1
  68. package/dist/client/handlers/UIFriendEventHandler.js +0 -27
  69. package/dist/client/handlers/UIGroupEventHandler.d.ts +0 -18
  70. package/dist/client/handlers/UIGroupEventHandler.d.ts.map +0 -1
  71. package/dist/client/handlers/UIGroupEventHandler.js +0 -28
  72. package/dist/client/handlers/UIGroupTransactionEventHandler.d.ts +0 -18
  73. package/dist/client/handlers/UIGroupTransactionEventHandler.d.ts.map +0 -1
  74. package/dist/client/handlers/UIGroupTransactionEventHandler.js +0 -28
  75. package/dist/client/handlers/UITransactionEventHandler.d.ts +0 -14
  76. package/dist/client/handlers/UITransactionEventHandler.d.ts.map +0 -1
  77. package/dist/client/handlers/UITransactionEventHandler.js +0 -26
  78. package/dist/client/handlers/UIUserEventHandler.d.ts +0 -12
  79. package/dist/client/handlers/UIUserEventHandler.d.ts.map +0 -1
  80. package/dist/client/handlers/UIUserEventHandler.js +0 -29
  81. package/dist/utils/userPathUtils.d.ts +0 -6
  82. package/dist/utils/userPathUtils.d.ts.map +0 -1
  83. package/dist/utils/userPathUtils.js +0 -16
@@ -2,10 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GroupEventHandler = void 0;
4
4
  const _1 = require(".");
5
+ const BaseEventHandler_1 = require("./BaseEventHandler");
5
6
  const uuid_1 = require("uuid");
6
7
  const EventProcessorSDK_1 = require("./EventProcessorSDK");
7
- class GroupEventHandler {
8
+ class GroupEventHandler extends BaseEventHandler_1.BaseEventHandler {
8
9
  constructor(modelStore, friendModelStore, userModelStore, groupEventStore, friendEventStore, activityEventHandler) {
10
+ super();
9
11
  this.modelStore = modelStore;
10
12
  this.friendModelStore = friendModelStore;
11
13
  this.userModelStore = userModelStore;
@@ -13,15 +15,8 @@ class GroupEventHandler {
13
15
  this.friendEventStore = friendEventStore;
14
16
  this.activityEventHandler = activityEventHandler;
15
17
  }
16
- shouldSkipSave(existingModel, updatedModel) {
17
- if (!existingModel) {
18
- return false;
19
- }
20
- return (existingModel.streamId === updatedModel.streamId &&
21
- existingModel.version === updatedModel.version);
22
- }
23
18
  async handleClientEventFromRemoteListener(event) {
24
- const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
19
+ const existingModel = await this.modelStore.getByStreamId(event.streamId);
25
20
  const updatedModel = event.apply(existingModel);
26
21
  if (!this.shouldSkipSave(existingModel, updatedModel)) {
27
22
  await this.modelStore.save(updatedModel);
@@ -32,6 +27,10 @@ class GroupEventHandler {
32
27
  }
33
28
  async handleEvent(event) {
34
29
  const eventType = event.constructor.name;
30
+ if (_1.residence.isClient) {
31
+ const user = await this.userModelStore.appUser();
32
+ this.currentUserId = user.userId;
33
+ }
35
34
  switch (eventType) {
36
35
  case "GroupCreated":
37
36
  return await this.handleGroupCreated(event);
@@ -75,9 +74,7 @@ class GroupEventHandler {
75
74
  });
76
75
  await this.friendEventStore.addEvent(friendCreatedEvent);
77
76
  const friendModel = friendCreatedEvent.apply(undefined);
78
- if (!this.shouldSkipSave(undefined, friendModel)) {
79
- await this.friendModelStore.save(friendModel);
80
- }
77
+ await this.friendModelStore.save(friendModel);
81
78
  }
82
79
  const existingFriend2to1 = await this.friendModelStore.findByUserIdAndFriendIdAndDeletedIsFalse(userId2, userId1);
83
80
  if (!existingFriend2to1) {
@@ -106,125 +103,131 @@ class GroupEventHandler {
106
103
  async handleGroupCreated(event) {
107
104
  const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
108
105
  const updatedModel = event.apply(existingModel);
109
- if (!this.shouldSkipSave(existingModel, updatedModel)) {
110
- await this.modelStore.save(updatedModel);
111
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
112
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
113
- }
106
+ if (this.shouldSkipSave(existingModel, updatedModel)) {
107
+ return existingModel;
114
108
  }
115
- const allMemberIds = event.members ?? [];
116
- const otherMembers = allMemberIds.filter((id) => id !== event.userId);
117
- for (const memberId of otherMembers) {
118
- const memberUser = await this.userModelStore.getByStreamIdWhereDeletedIsFalse(memberId);
119
- if (!memberUser) {
120
- console.log(`Member ${memberId} not found, skipping`);
121
- continue;
109
+ await this.modelStore.save(updatedModel);
110
+ if (_1.residence.isClient) {
111
+ await this.groupEventStore.addEvent(event);
112
+ EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
113
+ if (this.activityEventHandler) {
114
+ await this.activityEventHandler.createActivityLog(event, undefined, updatedModel);
122
115
  }
123
- for (const otherMemberId of allMemberIds) {
124
- if (memberId !== otherMemberId) {
125
- await this.ensureBidirectionalFriendship(memberId, otherMemberId, event.groupId);
116
+ }
117
+ else {
118
+ const allMemberIds = event.members ?? [];
119
+ const otherMembers = allMemberIds.filter((id) => id !== event.userId);
120
+ for (const memberId of otherMembers) {
121
+ const memberUser = await this.userModelStore.getByStreamIdWhereDeletedIsFalse(memberId);
122
+ if (!memberUser) {
123
+ console.log(`Member ${memberId} not found, skipping`);
124
+ continue;
126
125
  }
127
- }
128
- const perspectiveGroupStreamId = (0, uuid_1.v4)();
129
- const perspectiveGroupCreated = new _1.GroupCreated({
130
- eventId: (0, uuid_1.v4)(),
131
- userId: memberId,
132
- groupId: event.groupId,
133
- name: event.name,
134
- description: event.description,
135
- members: event.members,
136
- createdAt: new Date(),
137
- createdBy: memberId,
138
- streamId: perspectiveGroupStreamId,
139
- version: 1,
140
- systemGenerated: true,
141
- });
142
- await this.groupEventStore.addEvent(perspectiveGroupCreated);
143
- const perspectiveModel = perspectiveGroupCreated.apply(undefined);
144
- if (!this.shouldSkipSave(undefined, perspectiveModel)) {
126
+ for (const otherMemberId of allMemberIds) {
127
+ if (memberId !== otherMemberId) {
128
+ await this.ensureBidirectionalFriendship(memberId, otherMemberId, event.groupId);
129
+ }
130
+ }
131
+ const perspectiveGroupStreamId = (0, uuid_1.v4)();
132
+ const perspectiveGroupCreated = new _1.GroupCreated({
133
+ eventId: (0, uuid_1.v4)(),
134
+ userId: memberId,
135
+ groupId: event.groupId,
136
+ name: event.name,
137
+ description: event.description,
138
+ members: event.members,
139
+ createdAt: new Date(),
140
+ createdBy: memberId,
141
+ streamId: perspectiveGroupStreamId,
142
+ version: 1,
143
+ systemGenerated: true,
144
+ });
145
+ await this.groupEventStore.addEvent(perspectiveGroupCreated);
146
+ const perspectiveModel = perspectiveGroupCreated.apply(undefined);
145
147
  await this.modelStore.save(perspectiveModel);
146
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
147
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", perspectiveModel);
148
+ if (this.activityEventHandler) {
149
+ await this.activityEventHandler.createActivityLog(perspectiveGroupCreated, undefined, perspectiveModel);
148
150
  }
149
151
  }
150
152
  }
151
- if (this.activityEventHandler) {
152
- await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
153
- }
154
153
  return updatedModel;
155
154
  }
156
155
  async handleGroupNameChanged(event) {
157
156
  const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
158
157
  const updatedModel = event.apply(existingModel);
159
- if (!this.shouldSkipSave(existingModel, updatedModel)) {
160
- await this.modelStore.save(updatedModel);
161
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
162
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
158
+ if (this.shouldSkipSave(existingModel, updatedModel)) {
159
+ return existingModel;
160
+ }
161
+ await this.modelStore.save(updatedModel);
162
+ if (_1.residence.isClient) {
163
+ await this.groupEventStore.addEvent(event);
164
+ EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
165
+ if (this.activityEventHandler) {
166
+ await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
163
167
  }
164
168
  }
165
- const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
166
- for (const perspective of allPerspectives) {
167
- if (perspective.streamId !== event.streamId) {
168
- const perspectiveEvent = new _1.GroupNameChanged({
169
- eventId: (0, uuid_1.v4)(),
170
- userId: perspective.userId,
171
- name: event.name,
172
- createdAt: new Date(),
173
- createdBy: event.createdBy,
174
- streamId: perspective.streamId,
175
- version: perspective.version + 1,
176
- systemGenerated: true,
177
- });
178
- await this.groupEventStore.addEvent(perspectiveEvent);
179
- const updatedPerspective = perspectiveEvent.apply(perspective);
180
- if (!this.shouldSkipSave(perspective, updatedPerspective)) {
169
+ else {
170
+ const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
171
+ for (const perspective of allPerspectives) {
172
+ if (perspective.streamId !== event.streamId) {
173
+ const perspectiveEvent = new _1.GroupNameChanged({
174
+ eventId: (0, uuid_1.v4)(),
175
+ userId: perspective.userId,
176
+ name: event.name,
177
+ createdAt: new Date(),
178
+ createdBy: event.createdBy,
179
+ streamId: perspective.streamId,
180
+ version: perspective.version + 1,
181
+ systemGenerated: true,
182
+ });
183
+ const updatedPerspective = perspectiveEvent.apply(perspective);
184
+ await this.groupEventStore.addEvent(perspectiveEvent);
181
185
  await this.modelStore.save(updatedPerspective);
182
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
183
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedPerspective);
186
+ if (this.activityEventHandler) {
187
+ await this.activityEventHandler.createActivityLog(perspectiveEvent, perspective, updatedPerspective);
184
188
  }
185
189
  }
186
190
  }
187
191
  }
188
- if (this.activityEventHandler) {
189
- await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
190
- }
191
192
  return updatedModel;
192
193
  }
193
194
  async handleGroupDescriptionChanged(event) {
194
195
  const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
195
196
  const updatedModel = event.apply(existingModel);
196
- if (!this.shouldSkipSave(existingModel, updatedModel)) {
197
- await this.modelStore.save(updatedModel);
198
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
199
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
197
+ if (this.shouldSkipSave(existingModel, updatedModel)) {
198
+ return existingModel;
199
+ }
200
+ await this.modelStore.save(updatedModel);
201
+ if (_1.residence.isClient) {
202
+ await this.groupEventStore.addEvent(event);
203
+ EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
204
+ if (this.activityEventHandler) {
205
+ await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
200
206
  }
201
207
  }
202
- const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
203
- for (const perspective of allPerspectives) {
204
- if (perspective.streamId !== event.streamId) {
205
- const perspectiveEvent = new _1.GroupDescriptionChanged({
206
- eventId: (0, uuid_1.v4)(),
207
- userId: perspective.userId,
208
- description: event.description,
209
- createdAt: new Date(),
210
- createdBy: event.createdBy,
211
- streamId: perspective.streamId,
212
- version: perspective.version + 1,
213
- systemGenerated: true,
214
- });
215
- await this.groupEventStore.addEvent(perspectiveEvent);
216
- const updatedPerspective = perspectiveEvent.apply(perspective);
217
- if (!this.shouldSkipSave(perspective, updatedPerspective)) {
208
+ else {
209
+ const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
210
+ for (const perspective of allPerspectives) {
211
+ if (perspective.streamId !== event.streamId) {
212
+ const perspectiveEvent = new _1.GroupDescriptionChanged({
213
+ eventId: (0, uuid_1.v4)(),
214
+ userId: perspective.userId,
215
+ description: event.description,
216
+ createdAt: new Date(),
217
+ createdBy: event.createdBy,
218
+ streamId: perspective.streamId,
219
+ version: perspective.version + 1,
220
+ systemGenerated: true,
221
+ });
222
+ const updatedPerspective = perspectiveEvent.apply(perspective);
223
+ await this.groupEventStore.addEvent(perspectiveEvent);
218
224
  await this.modelStore.save(updatedPerspective);
219
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
220
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedPerspective);
225
+ if (this.activityEventHandler) {
226
+ await this.activityEventHandler.createActivityLog(perspectiveEvent, perspective, updatedPerspective);
221
227
  }
222
228
  }
223
229
  }
224
230
  }
225
- if (this.activityEventHandler) {
226
- await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
227
- }
228
231
  return updatedModel;
229
232
  }
230
233
  async handleGroupMembersAdded(event) {
@@ -233,124 +236,170 @@ class GroupEventHandler {
233
236
  throw new Error(`Group model not found for streamId ${event.streamId}`);
234
237
  }
235
238
  const updatedModel = event.apply(existingModel);
236
- if (!this.shouldSkipSave(existingModel, updatedModel)) {
237
- await this.modelStore.save(updatedModel);
238
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
239
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
240
- }
239
+ if (this.shouldSkipSave(existingModel, updatedModel)) {
240
+ return existingModel;
241
241
  }
242
- const allMemberIds = updatedModel.members ?? [];
243
- for (const memberId of event.members) {
244
- const memberUser = await this.userModelStore.getByStreamIdWhereDeletedIsFalse(memberId);
245
- if (!memberUser) {
246
- console.log(`Member ${memberId} not found, skipping`);
247
- continue;
242
+ await this.modelStore.save(updatedModel);
243
+ if (_1.residence.isClient) {
244
+ await this.groupEventStore.addEvent(event);
245
+ EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
246
+ if (this.activityEventHandler) {
247
+ await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
248
248
  }
249
- for (const otherMemberId of allMemberIds) {
250
- if (memberId !== otherMemberId) {
251
- await this.ensureBidirectionalFriendship(memberId, otherMemberId, existingModel.groupId);
249
+ }
250
+ else {
251
+ const allMemberIds = updatedModel.members ?? [];
252
+ for (const memberId of event.members) {
253
+ const memberUser = await this.userModelStore.getByStreamIdWhereDeletedIsFalse(memberId);
254
+ if (!memberUser) {
255
+ console.log(`Member ${memberId} not found, skipping`);
256
+ continue;
252
257
  }
253
- }
254
- const perspectiveGroupStreamId = (0, uuid_1.v4)();
255
- const perspectiveGroupCreated = new _1.GroupCreated({
256
- eventId: (0, uuid_1.v4)(),
257
- userId: memberId,
258
- groupId: existingModel.groupId,
259
- name: existingModel.name,
260
- description: existingModel.description,
261
- members: updatedModel.members,
262
- createdAt: new Date(),
263
- createdBy: memberId,
264
- streamId: perspectiveGroupStreamId,
265
- version: 1,
266
- systemGenerated: true,
267
- });
268
- await this.groupEventStore.addEvent(perspectiveGroupCreated);
269
- const perspectiveModel = perspectiveGroupCreated.apply(undefined);
270
- if (!this.shouldSkipSave(undefined, perspectiveModel)) {
271
- await this.modelStore.save(perspectiveModel);
272
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
273
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", perspectiveModel);
258
+ for (const otherMemberId of allMemberIds) {
259
+ if (memberId !== otherMemberId) {
260
+ await this.ensureBidirectionalFriendship(memberId, otherMemberId, existingModel.groupId);
261
+ }
274
262
  }
275
- }
276
- }
277
- const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
278
- for (const perspective of allPerspectives) {
279
- if (perspective.streamId !== event.streamId &&
280
- !event.members.includes(perspective.userId)) {
281
- const perspectiveEvent = new _1.GroupMembersAdded({
263
+ const perspectiveGroupStreamId = (0, uuid_1.v4)();
264
+ const perspectiveGroupCreated = new _1.GroupCreated({
282
265
  eventId: (0, uuid_1.v4)(),
283
- userId: perspective.userId,
284
- members: event.members,
266
+ userId: memberId,
267
+ groupId: existingModel.groupId,
268
+ name: existingModel.name,
269
+ description: existingModel.description,
270
+ members: updatedModel.members,
285
271
  createdAt: new Date(),
286
- createdBy: event.createdBy,
287
- streamId: perspective.streamId,
288
- version: perspective.version + 1,
272
+ createdBy: memberId,
273
+ streamId: perspectiveGroupStreamId,
274
+ version: 1,
289
275
  systemGenerated: true,
290
276
  });
291
- await this.groupEventStore.addEvent(perspectiveEvent);
292
- const updatedPerspective = perspectiveEvent.apply(perspective);
293
- if (!this.shouldSkipSave(perspective, updatedPerspective)) {
277
+ await this.groupEventStore.addEvent(perspectiveGroupCreated);
278
+ const perspectiveModel = perspectiveGroupCreated.apply(undefined);
279
+ await this.modelStore.save(perspectiveModel);
280
+ if (this.activityEventHandler) {
281
+ await this.activityEventHandler.createActivityLog(perspectiveGroupCreated, undefined, perspectiveModel);
282
+ }
283
+ }
284
+ const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
285
+ for (const perspective of allPerspectives) {
286
+ if (perspective.streamId !== event.streamId &&
287
+ !event.members.includes(perspective.userId)) {
288
+ const perspectiveEvent = new _1.GroupMembersAdded({
289
+ eventId: (0, uuid_1.v4)(),
290
+ userId: perspective.userId,
291
+ members: event.members,
292
+ createdAt: new Date(),
293
+ createdBy: event.createdBy,
294
+ streamId: perspective.streamId,
295
+ version: perspective.version + 1,
296
+ systemGenerated: true,
297
+ });
298
+ const updatedPerspective = perspectiveEvent.apply(perspective);
299
+ await this.groupEventStore.addEvent(perspectiveEvent);
294
300
  await this.modelStore.save(updatedPerspective);
295
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
296
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedPerspective);
301
+ if (this.activityEventHandler) {
302
+ await this.activityEventHandler.createActivityLog(perspectiveEvent, perspective, updatedPerspective);
297
303
  }
298
304
  }
299
305
  }
300
306
  }
301
- if (this.activityEventHandler) {
302
- await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
303
- }
304
307
  return updatedModel;
305
308
  }
306
309
  async handleGroupMembersRemoved(event) {
307
310
  const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
308
311
  const updatedModel = event.apply(existingModel);
309
- if (!this.shouldSkipSave(existingModel, updatedModel)) {
310
- await this.modelStore.save(updatedModel);
311
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
312
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
312
+ if (this.shouldSkipSave(existingModel, updatedModel)) {
313
+ return existingModel;
314
+ }
315
+ await this.modelStore.save(updatedModel);
316
+ if (_1.residence.isClient) {
317
+ await this.groupEventStore.addEvent(event);
318
+ EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
319
+ if (this.activityEventHandler) {
320
+ await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
313
321
  }
314
322
  }
315
- const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
316
- for (const perspective of allPerspectives) {
317
- if (perspective.streamId !== event.streamId) {
318
- const perspectiveEvent = new _1.GroupMembersRemoved({
319
- eventId: (0, uuid_1.v4)(),
320
- userId: perspective.userId,
321
- members: event.members,
322
- createdAt: new Date(),
323
- createdBy: event.createdBy,
324
- streamId: perspective.streamId,
325
- version: perspective.version + 1,
326
- systemGenerated: true,
327
- });
328
- await this.groupEventStore.addEvent(perspectiveEvent);
329
- const updatedPerspective = perspectiveEvent.apply(perspective);
330
- if (!this.shouldSkipSave(perspective, updatedPerspective)) {
323
+ else {
324
+ for (const removedMemberId of event.members) {
325
+ const removedMemberPerspective = await this.modelStore.findByUserIdAndGroupIdAndDeletedIsFalse(removedMemberId, updatedModel.groupId);
326
+ if (removedMemberPerspective) {
327
+ const deleteEvent = new _1.GroupDeleted({
328
+ eventId: (0, uuid_1.v4)(),
329
+ userId: removedMemberId,
330
+ createdAt: new Date(),
331
+ createdBy: event.createdBy,
332
+ streamId: removedMemberPerspective.streamId,
333
+ version: removedMemberPerspective.version + 1,
334
+ systemGenerated: true,
335
+ });
336
+ const deletedPerspective = deleteEvent.apply(removedMemberPerspective);
337
+ await this.groupEventStore.addEvent(deleteEvent);
338
+ await this.modelStore.save(deletedPerspective);
339
+ if (this.activityEventHandler) {
340
+ await this.activityEventHandler.createActivityLog(deleteEvent, removedMemberPerspective, deletedPerspective);
341
+ }
342
+ }
343
+ }
344
+ const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
345
+ for (const perspective of allPerspectives) {
346
+ if (perspective.streamId !== event.streamId) {
347
+ const perspectiveEvent = new _1.GroupMembersRemoved({
348
+ eventId: (0, uuid_1.v4)(),
349
+ userId: perspective.userId,
350
+ members: event.members,
351
+ createdAt: new Date(),
352
+ createdBy: event.createdBy,
353
+ streamId: perspective.streamId,
354
+ version: perspective.version + 1,
355
+ systemGenerated: true,
356
+ });
357
+ const updatedPerspective = perspectiveEvent.apply(perspective);
358
+ await this.groupEventStore.addEvent(perspectiveEvent);
331
359
  await this.modelStore.save(updatedPerspective);
332
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
333
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedPerspective);
360
+ if (this.activityEventHandler) {
361
+ await this.activityEventHandler.createActivityLog(perspectiveEvent, perspective, updatedPerspective);
334
362
  }
335
363
  }
336
364
  }
337
365
  }
338
- if (this.activityEventHandler) {
339
- await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
340
- }
341
366
  return updatedModel;
342
367
  }
343
368
  async handleGroupDeleted(event) {
344
369
  const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
345
370
  const updatedModel = event.apply(existingModel);
346
- if (!this.shouldSkipSave(existingModel, updatedModel)) {
347
- await this.modelStore.save(updatedModel);
348
- if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
349
- EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
371
+ if (this.shouldSkipSave(existingModel, updatedModel)) {
372
+ return existingModel;
373
+ }
374
+ await this.modelStore.save(updatedModel);
375
+ if (_1.residence.isClient) {
376
+ await this.groupEventStore.addEvent(event);
377
+ EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupModel", updatedModel);
378
+ if (this.activityEventHandler) {
379
+ await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
350
380
  }
351
381
  }
352
- if (this.activityEventHandler) {
353
- await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
382
+ else {
383
+ const allPerspectives = await this.modelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
384
+ for (const perspective of allPerspectives) {
385
+ if (perspective.streamId !== event.streamId) {
386
+ const perspectiveEvent = new _1.GroupDeleted({
387
+ eventId: (0, uuid_1.v4)(),
388
+ userId: perspective.userId,
389
+ createdAt: new Date(),
390
+ createdBy: event.createdBy,
391
+ streamId: perspective.streamId,
392
+ version: perspective.version + 1,
393
+ systemGenerated: true,
394
+ });
395
+ const updatedPerspective = perspectiveEvent.apply(perspective);
396
+ await this.groupEventStore.addEvent(perspectiveEvent);
397
+ await this.modelStore.save(updatedPerspective);
398
+ if (this.activityEventHandler) {
399
+ await this.activityEventHandler.createActivityLog(perspectiveEvent, perspective, updatedPerspective);
400
+ }
401
+ }
402
+ }
354
403
  }
355
404
  return updatedModel;
356
405
  }
@@ -6,7 +6,7 @@ import { TransactionEventStore } from ".";
6
6
  import { TransactionModelStore } from ".";
7
7
  import { BaseEventHandler } from "./BaseEventHandler";
8
8
  import { ActivityEventHandler } from "./ActivityEventHandler";
9
- export declare class GroupTransactionEventHandler implements BaseEventHandler<GroupTransactionEvent> {
9
+ export declare class GroupTransactionEventHandler extends BaseEventHandler<GroupTransactionEvent> {
10
10
  private modelStore;
11
11
  private eventStore;
12
12
  private groupModelStore;
@@ -14,7 +14,6 @@ export declare class GroupTransactionEventHandler implements BaseEventHandler<Gr
14
14
  private transactionModelStore;
15
15
  private activityEventHandler?;
16
16
  constructor(modelStore: GroupTransactionModelStore, eventStore: GroupTransactionEventStore, groupModelStore: GroupModelStore, transactionEventStore: TransactionEventStore, transactionModelStore: TransactionModelStore, activityEventHandler?: ActivityEventHandler | undefined);
17
- private shouldSkipSave;
18
17
  handleClientEventFromRemoteListener(event: GroupTransactionEvent): Promise<void>;
19
18
  handleEvent(event: GroupTransactionEvent): Promise<GroupTransactionModel>;
20
19
  private calculateNetBalances;
@@ -1 +1 @@
1
- {"version":3,"file":"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C,OAAO,CAAC,oBAAoB;YAad,wBAAwB;YA8FxB,wBAAwB;IAatC,OAAO,CAAC,6BAA6B;YAsDvB,gCAAgC;IAqGxC,6BAA6B,CAC/B,KAAK,EAAE,uBAAuB,GAC/B,OAAO,CAAC,qBAAqB,CAAC;IAkF3B,yCAAyC,CAC3C,KAAK,EAAE,mCAAmC,GAC3C,OAAO,CAAC,qBAAqB,CAAC;CAsGpC"}
@@ -2,11 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GroupTransactionEventHandler = void 0;
4
4
  const _1 = require(".");
5
+ const BaseEventHandler_1 = require("./BaseEventHandler");
5
6
  const splitTypeUtils_1 = require("./utils/splitTypeUtils");
6
7
  const uuid_1 = require("uuid");
7
8
  const EventProcessorSDK_1 = require("./EventProcessorSDK");
8
- class GroupTransactionEventHandler {
9
+ class GroupTransactionEventHandler extends BaseEventHandler_1.BaseEventHandler {
9
10
  constructor(modelStore, eventStore, groupModelStore, transactionEventStore, transactionModelStore, activityEventHandler) {
11
+ super();
10
12
  this.modelStore = modelStore;
11
13
  this.eventStore = eventStore;
12
14
  this.groupModelStore = groupModelStore;
@@ -14,15 +16,8 @@ class GroupTransactionEventHandler {
14
16
  this.transactionModelStore = transactionModelStore;
15
17
  this.activityEventHandler = activityEventHandler;
16
18
  }
17
- shouldSkipSave(existingModel, updatedModel) {
18
- if (!existingModel) {
19
- return false;
20
- }
21
- return (existingModel.streamId === updatedModel.streamId &&
22
- existingModel.version === updatedModel.version);
23
- }
24
19
  async handleClientEventFromRemoteListener(event) {
25
- const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
20
+ const existingModel = await this.modelStore.getByStreamId(event.streamId);
26
21
  const updatedModel = event.apply(existingModel);
27
22
  if (!this.shouldSkipSave(existingModel, updatedModel)) {
28
23
  await this.modelStore.save(updatedModel);
@@ -1,12 +1,11 @@
1
1
  import { TransactionEvent, TransactionEventStore, TransactionModel, TransactionModelStore } from ".";
2
2
  import { BaseEventHandler } from "./BaseEventHandler";
3
3
  import { ActivityEventHandler } from "./ActivityEventHandler";
4
- export declare class TransactionEventHandler implements BaseEventHandler<TransactionEvent> {
4
+ export declare class TransactionEventHandler extends BaseEventHandler<TransactionEvent> {
5
5
  private modelStore;
6
6
  private eventStore;
7
7
  private activityEventHandler?;
8
8
  constructor(modelStore: TransactionModelStore, eventStore: TransactionEventStore, activityEventHandler?: ActivityEventHandler | undefined);
9
- private shouldSkipSave;
10
9
  handleClientEventFromRemoteListener(event: TransactionEvent): Promise<void>;
11
10
  handleEvent(event: TransactionEvent): Promise<TransactionModel>;
12
11
  private createMirrorEvent;
@@ -1 +1 @@
1
- {"version":3,"file":"TransactionEventHandler.d.ts","sourceRoot":"","sources":["../src/TransactionEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAUH,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,qBAAqB,EACxB,MAAM,GAAG,CAAC;AACX,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAQ9D,qBAAa,uBACT,YAAW,gBAAgB,CAAC,gBAAgB,CAAC;IAGzC,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,oBAAoB,CAAC;gBAFrB,UAAU,EAAE,qBAAqB,EACjC,UAAU,EAAE,qBAAqB,EACjC,oBAAoB,CAAC,EAAE,oBAAoB,YAAA;IAGvD,OAAO,CAAC,cAAc;IAaT,mCAAmC,CAC5C,KAAK,EAAE,gBAAgB,GACxB,OAAO,CAAC,IAAI,CAAC;IAeV,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;YA6BvD,iBAAiB;CA6KlC"}
1
+ {"version":3,"file":"TransactionEventHandler.d.ts","sourceRoot":"","sources":["../src/TransactionEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAU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