@zeeshan60/event-processor 1.0.16 → 1.0.17
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/ActivityEventHandler.d.ts +4 -1
- package/dist/ActivityEventHandler.d.ts.map +1 -1
- package/dist/ActivityEventHandler.js +23 -7
- package/dist/ActivityLogEvents.d.ts.map +1 -1
- package/dist/ActivityLogEvents.js +1 -0
- package/dist/BaseEventHandler.d.ts +7 -0
- package/dist/BaseEventHandler.d.ts.map +1 -1
- package/dist/BaseEventHandler.js +4 -0
- package/dist/EventProcessorSDK.d.ts +4 -0
- package/dist/EventProcessorSDK.d.ts.map +1 -1
- package/dist/EventProcessorSDK.js +13 -6
- package/dist/FriendEventHandler.d.ts +4 -1
- package/dist/FriendEventHandler.d.ts.map +1 -1
- package/dist/FriendEventHandler.js +25 -13
- package/dist/GroupEventHandler.d.ts +4 -1
- package/dist/GroupEventHandler.d.ts.map +1 -1
- package/dist/GroupEventHandler.js +34 -18
- package/dist/GroupTransactionEventHandler.d.ts +4 -6
- package/dist/GroupTransactionEventHandler.d.ts.map +1 -1
- package/dist/GroupTransactionEventHandler.js +24 -291
- package/dist/TransactionEventHandler.d.ts +4 -1
- package/dist/TransactionEventHandler.d.ts.map +1 -1
- package/dist/TransactionEventHandler.js +24 -6
- package/dist/TransactionEvents.d.ts +10 -9
- package/dist/TransactionEvents.d.ts.map +1 -1
- package/dist/TransactionEvents.js +10 -12
- package/dist/UserEventHandler.d.ts +4 -1
- package/dist/UserEventHandler.d.ts.map +1 -1
- package/dist/UserEventHandler.js +25 -7
- package/dist/__tests__/test-helpers/IMActivityLogEventStore.d.ts +1 -0
- package/dist/__tests__/test-helpers/IMActivityLogEventStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMActivityLogEventStore.js +3 -0
- package/dist/__tests__/test-helpers/IMActivityLogModelStore.d.ts +2 -0
- package/dist/__tests__/test-helpers/IMActivityLogModelStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMActivityLogModelStore.js +10 -0
- package/dist/__tests__/test-helpers/IMCurrencyStore.d.ts +1 -0
- package/dist/__tests__/test-helpers/IMCurrencyStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMCurrencyStore.js +3 -0
- package/dist/__tests__/test-helpers/IMFriendModelStore.d.ts +1 -0
- package/dist/__tests__/test-helpers/IMFriendModelStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMFriendModelStore.js +7 -0
- package/dist/__tests__/test-helpers/IMGroupModelStore.d.ts +1 -0
- package/dist/__tests__/test-helpers/IMGroupModelStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMGroupModelStore.js +7 -0
- package/dist/__tests__/test-helpers/IMGroupTransactionEventStore.d.ts +2 -0
- package/dist/__tests__/test-helpers/IMGroupTransactionEventStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMGroupTransactionEventStore.js +18 -0
- package/dist/__tests__/test-helpers/IMGroupTransactionModelStore.d.ts +1 -0
- package/dist/__tests__/test-helpers/IMGroupTransactionModelStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMGroupTransactionModelStore.js +7 -0
- package/dist/__tests__/test-helpers/IMTransactionEventStore.d.ts +2 -0
- package/dist/__tests__/test-helpers/IMTransactionEventStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMTransactionEventStore.js +6 -0
- package/dist/__tests__/test-helpers/IMTransactionModelStore.d.ts +1 -0
- package/dist/__tests__/test-helpers/IMTransactionModelStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMTransactionModelStore.js +7 -0
- package/dist/__tests__/test-helpers/IMUserModelStore.d.ts +1 -0
- package/dist/__tests__/test-helpers/IMUserModelStore.d.ts.map +1 -1
- package/dist/__tests__/test-helpers/IMUserModelStore.js +7 -0
- package/dist/client/controllers/FriendController.d.ts.map +1 -1
- package/dist/client/controllers/FriendController.js +40 -4
- package/dist/client/controllers/TransactionController.d.ts +7 -2
- package/dist/client/controllers/TransactionController.d.ts.map +1 -1
- package/dist/client/controllers/TransactionController.js +90 -17
- package/dist/client/handlers/UIEventHandler.d.ts +3 -0
- package/dist/client/handlers/UIEventHandler.d.ts.map +1 -1
- package/dist/client/handlers/UIEventHandler.js +36 -0
- package/dist/common/Model.d.ts +1 -0
- package/dist/common/Model.d.ts.map +1 -1
- package/dist/contracts/friends/index.d.ts +1 -0
- package/dist/contracts/friends/index.d.ts.map +1 -1
- package/dist/contracts/groups/index.d.ts +1 -1
- package/dist/contracts/groups/index.d.ts.map +1 -1
- package/dist/contracts/transactions/index.d.ts +33 -23
- package/dist/contracts/transactions/index.d.ts.map +1 -1
- package/dist/contracts/transactions/index.js +14 -11
- package/dist/converters/ActivityLogConverters.d.ts.map +1 -1
- package/dist/converters/ActivityLogConverters.js +3 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -5
- package/dist/store/CurrencyStore.d.ts +1 -0
- package/dist/store/CurrencyStore.d.ts.map +1 -1
- package/dist/store/ModelStore.d.ts +1 -0
- package/dist/store/ModelStore.d.ts.map +1 -1
- package/dist/utils/BalanceUtil.d.ts +5 -1
- package/dist/utils/BalanceUtil.d.ts.map +1 -1
- package/dist/utils/BalanceUtil.js +9 -1
- package/dist/utils/splitTypeUtils.d.ts.map +1 -1
- package/dist/utils/splitTypeUtils.js +21 -21
- package/package.json +1 -1
- package/dist/environment.d.ts +0 -12
- package/dist/environment.d.ts.map +0 -1
- package/dist/environment.js +0 -50
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GroupTransactionEventHandler = void 0;
|
|
4
|
-
const _1 = require(".");
|
|
5
4
|
const BaseEventHandler_1 = require("./BaseEventHandler");
|
|
6
|
-
const splitTypeUtils_1 = require("./utils/splitTypeUtils");
|
|
7
|
-
const uuid_1 = require("uuid");
|
|
8
|
-
const EventProcessorSDK_1 = require("./EventProcessorSDK");
|
|
9
5
|
class GroupTransactionEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
10
|
-
constructor(modelStore, eventStore, groupModelStore, transactionEventStore, transactionModelStore, activityEventHandler) {
|
|
11
|
-
super();
|
|
6
|
+
constructor(modelStore, eventStore, groupModelStore, transactionEventStore, transactionModelStore, isClient, modelChangeEmitter, activityEventHandler) {
|
|
7
|
+
super(isClient, modelChangeEmitter);
|
|
12
8
|
this.modelStore = modelStore;
|
|
13
9
|
this.eventStore = eventStore;
|
|
14
10
|
this.groupModelStore = groupModelStore;
|
|
@@ -16,18 +12,35 @@ class GroupTransactionEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
|
16
12
|
this.transactionModelStore = transactionModelStore;
|
|
17
13
|
this.activityEventHandler = activityEventHandler;
|
|
18
14
|
}
|
|
15
|
+
getModelStore() {
|
|
16
|
+
return this.modelStore;
|
|
17
|
+
}
|
|
19
18
|
async handleClientEventFromRemoteListener(event) {
|
|
20
19
|
const existingModel = await this.modelStore.getByStreamId(event.streamId);
|
|
21
20
|
const updatedModel = event.apply(existingModel);
|
|
22
21
|
if (!this.shouldSkipSave(existingModel, updatedModel)) {
|
|
23
22
|
await this.modelStore.save(updatedModel);
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
this.modelChangeEmitter?.emit("GroupTransactionModel", updatedModel);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async handleClientEventsFromRemoteListenerBulk(events) {
|
|
27
|
+
const updatedModels = [];
|
|
28
|
+
for (const event of events) {
|
|
29
|
+
const existingModel = await this.modelStore.getByStreamId(event.streamId);
|
|
30
|
+
const updatedModel = event.apply(existingModel);
|
|
31
|
+
if (!this.shouldSkipSave(existingModel, updatedModel)) {
|
|
32
|
+
await this.modelStore.save(updatedModel);
|
|
33
|
+
updatedModels.push(updatedModel);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (this.modelChangeEmitter && updatedModels.length > 0) {
|
|
37
|
+
for (const model of updatedModels) {
|
|
38
|
+
this.modelChangeEmitter.emit("GroupTransactionModel", model);
|
|
26
39
|
}
|
|
27
40
|
}
|
|
28
41
|
}
|
|
29
42
|
async handleEvent(event) {
|
|
30
|
-
if (
|
|
43
|
+
if (this.isClient) {
|
|
31
44
|
await this.eventStore.addEvent(event);
|
|
32
45
|
}
|
|
33
46
|
const eventType = event.constructor.name;
|
|
@@ -40,258 +53,16 @@ class GroupTransactionEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
|
40
53
|
throw new Error(`Unknown group transaction event type: ${eventType}`);
|
|
41
54
|
}
|
|
42
55
|
}
|
|
43
|
-
calculateNetBalances(members) {
|
|
44
|
-
const netBalances = new Map();
|
|
45
|
-
for (const member of members) {
|
|
46
|
-
const net = member.paid - member.owed;
|
|
47
|
-
netBalances.set(member.memberId, net);
|
|
48
|
-
}
|
|
49
|
-
return netBalances;
|
|
50
|
-
}
|
|
51
|
-
async createFriendTransactions(groupTransactionEvent) {
|
|
52
|
-
const netBalances = this.calculateNetBalances(groupTransactionEvent.originalTransaction.members);
|
|
53
|
-
const creditors = [];
|
|
54
|
-
const debtors = [];
|
|
55
|
-
for (const [memberId, net] of netBalances.entries()) {
|
|
56
|
-
if (net > 0) {
|
|
57
|
-
creditors.push({ memberId, amount: net });
|
|
58
|
-
}
|
|
59
|
-
else if (net < 0) {
|
|
60
|
-
debtors.push({ memberId, amount: Math.abs(net) });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
for (const creditor of creditors) {
|
|
64
|
-
for (const debtor of debtors) {
|
|
65
|
-
if (creditor.amount > 0 && debtor.amount > 0) {
|
|
66
|
-
const transactionAmount = Math.min(creditor.amount, debtor.amount);
|
|
67
|
-
const creditorStreamId = (0, uuid_1.v4)();
|
|
68
|
-
const creditorTransactionEvent = new _1.TransactionCreated({
|
|
69
|
-
eventId: (0, uuid_1.v4)(),
|
|
70
|
-
userId: creditor.memberId,
|
|
71
|
-
recipientUserId: debtor.memberId,
|
|
72
|
-
logicalTransactionId: groupTransactionEvent.transactionId,
|
|
73
|
-
description: groupTransactionEvent.description ?? "",
|
|
74
|
-
currency: groupTransactionEvent.currency,
|
|
75
|
-
splitType: _1.SplitType.SpecificAmounts,
|
|
76
|
-
totalAmount: transactionAmount,
|
|
77
|
-
amount: transactionAmount,
|
|
78
|
-
isOwed: false,
|
|
79
|
-
transactionDate: groupTransactionEvent.transactionDate,
|
|
80
|
-
groupId: groupTransactionEvent.groupId,
|
|
81
|
-
groupTransactionId: groupTransactionEvent.transactionId,
|
|
82
|
-
createdAt: new Date(),
|
|
83
|
-
createdBy: groupTransactionEvent.createdBy,
|
|
84
|
-
streamId: creditorStreamId,
|
|
85
|
-
version: 1,
|
|
86
|
-
systemGenerated: true,
|
|
87
|
-
});
|
|
88
|
-
await this.transactionEventStore.addEvent(creditorTransactionEvent);
|
|
89
|
-
const creditorModel = creditorTransactionEvent.apply(undefined);
|
|
90
|
-
if (!this.shouldSkipSave(undefined, creditorModel)) {
|
|
91
|
-
await this.transactionModelStore.save(creditorModel);
|
|
92
|
-
}
|
|
93
|
-
const debtorStreamId = (0, uuid_1.v4)();
|
|
94
|
-
const debtorTransactionEvent = new _1.TransactionCreated({
|
|
95
|
-
eventId: (0, uuid_1.v4)(),
|
|
96
|
-
userId: debtor.memberId,
|
|
97
|
-
recipientUserId: creditor.memberId,
|
|
98
|
-
logicalTransactionId: groupTransactionEvent.transactionId,
|
|
99
|
-
description: groupTransactionEvent.description ?? "",
|
|
100
|
-
currency: groupTransactionEvent.currency,
|
|
101
|
-
splitType: _1.SplitType.SpecificAmounts,
|
|
102
|
-
totalAmount: transactionAmount,
|
|
103
|
-
amount: transactionAmount,
|
|
104
|
-
isOwed: true,
|
|
105
|
-
transactionDate: groupTransactionEvent.transactionDate,
|
|
106
|
-
groupId: groupTransactionEvent.groupId,
|
|
107
|
-
groupTransactionId: groupTransactionEvent.transactionId,
|
|
108
|
-
createdAt: new Date(),
|
|
109
|
-
createdBy: groupTransactionEvent.createdBy,
|
|
110
|
-
streamId: debtorStreamId,
|
|
111
|
-
version: 1,
|
|
112
|
-
systemGenerated: true,
|
|
113
|
-
});
|
|
114
|
-
await this.transactionEventStore.addEvent(debtorTransactionEvent);
|
|
115
|
-
const debtorModel = debtorTransactionEvent.apply(undefined);
|
|
116
|
-
if (!this.shouldSkipSave(undefined, debtorModel)) {
|
|
117
|
-
await this.transactionModelStore.save(debtorModel);
|
|
118
|
-
}
|
|
119
|
-
creditor.amount -= transactionAmount;
|
|
120
|
-
debtor.amount -= transactionAmount;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
async updateFriendTransactions(event, existingModel, _oldMembers, newMembers) {
|
|
126
|
-
await this.updateExistingFriendTransactions(event, existingModel, newMembers);
|
|
127
|
-
}
|
|
128
|
-
calculateRequiredTransactions(newMembers) {
|
|
129
|
-
const newNetBalances = this.calculateNetBalances(newMembers);
|
|
130
|
-
const requiredTransactions = new Map();
|
|
131
|
-
const creditors = [];
|
|
132
|
-
const debtors = [];
|
|
133
|
-
for (const [memberId, net] of newNetBalances.entries()) {
|
|
134
|
-
if (net > 0) {
|
|
135
|
-
creditors.push({ memberId, amount: net });
|
|
136
|
-
}
|
|
137
|
-
else if (net < 0) {
|
|
138
|
-
debtors.push({ memberId, amount: Math.abs(net) });
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
for (const creditor of creditors) {
|
|
142
|
-
for (const debtor of debtors) {
|
|
143
|
-
if (creditor.amount > 0 && debtor.amount > 0) {
|
|
144
|
-
const transactionAmount = Math.min(creditor.amount, debtor.amount);
|
|
145
|
-
const creditorKey = `${creditor.memberId}-${debtor.memberId}`;
|
|
146
|
-
requiredTransactions.set(creditorKey, {
|
|
147
|
-
recipientUserId: debtor.memberId,
|
|
148
|
-
amount: transactionAmount,
|
|
149
|
-
isOwed: false,
|
|
150
|
-
});
|
|
151
|
-
const debtorKey = `${debtor.memberId}-${creditor.memberId}`;
|
|
152
|
-
requiredTransactions.set(debtorKey, {
|
|
153
|
-
recipientUserId: creditor.memberId,
|
|
154
|
-
amount: transactionAmount,
|
|
155
|
-
isOwed: true,
|
|
156
|
-
});
|
|
157
|
-
creditor.amount -= transactionAmount;
|
|
158
|
-
debtor.amount -= transactionAmount;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return requiredTransactions;
|
|
163
|
-
}
|
|
164
|
-
async updateExistingFriendTransactions(event, existingModel, newMembers) {
|
|
165
|
-
const requiredTransactions = this.calculateRequiredTransactions(newMembers);
|
|
166
|
-
const oldMemberIds = new Set(existingModel.originalTransaction.members.map((m) => m.memberId));
|
|
167
|
-
const newMemberIds = new Set(newMembers.map((m) => m.memberId));
|
|
168
|
-
const allMemberIds = new Set([...oldMemberIds, ...newMemberIds]);
|
|
169
|
-
for (const memberId of allMemberIds) {
|
|
170
|
-
const existingTransactions = await this.transactionModelStore.findByGroupTransactionId(memberId, existingModel.transactionId);
|
|
171
|
-
for (const existingTx of existingTransactions) {
|
|
172
|
-
const key = `${existingTx.userId}-${existingTx.recipientUserId}`;
|
|
173
|
-
const requiredTx = requiredTransactions.get(key);
|
|
174
|
-
if (requiredTx) {
|
|
175
|
-
if (existingTx.amount !== requiredTx.amount ||
|
|
176
|
-
existingTx.totalAmount !== requiredTx.amount ||
|
|
177
|
-
existingTx.isOwed !== requiredTx.isOwed) {
|
|
178
|
-
const updateEvent = new _1.TransactionDetailsChanged({
|
|
179
|
-
eventId: (0, uuid_1.v4)(),
|
|
180
|
-
userId: existingTx.userId,
|
|
181
|
-
totalAmount: requiredTx.amount,
|
|
182
|
-
amount: requiredTx.amount,
|
|
183
|
-
isOwed: requiredTx.isOwed,
|
|
184
|
-
streamId: existingTx.streamId,
|
|
185
|
-
version: existingTx.version + 1,
|
|
186
|
-
createdAt: new Date(),
|
|
187
|
-
createdBy: event.createdBy,
|
|
188
|
-
systemGenerated: true,
|
|
189
|
-
});
|
|
190
|
-
await this.transactionEventStore.addEvent(updateEvent);
|
|
191
|
-
const updatedModel = updateEvent.apply(existingTx);
|
|
192
|
-
if (!this.shouldSkipSave(existingTx, updatedModel)) {
|
|
193
|
-
await this.transactionModelStore.save(updatedModel);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
requiredTransactions.delete(key);
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
const deleteEvent = new _1.TransactionDeleted({
|
|
200
|
-
eventId: (0, uuid_1.v4)(),
|
|
201
|
-
userId: existingTx.userId,
|
|
202
|
-
createdAt: new Date(),
|
|
203
|
-
createdBy: event.createdBy,
|
|
204
|
-
streamId: existingTx.streamId,
|
|
205
|
-
version: existingTx.version + 1,
|
|
206
|
-
systemGenerated: true,
|
|
207
|
-
});
|
|
208
|
-
await this.transactionEventStore.addEvent(deleteEvent);
|
|
209
|
-
const deletedModel = deleteEvent.apply(existingTx);
|
|
210
|
-
if (!this.shouldSkipSave(existingTx, deletedModel)) {
|
|
211
|
-
await this.transactionModelStore.save(deletedModel);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
for (const [key, txDetails] of requiredTransactions.entries()) {
|
|
217
|
-
const [userId, recipientUserId] = key.split("-");
|
|
218
|
-
const streamId = (0, uuid_1.v4)();
|
|
219
|
-
const createEvent = new _1.TransactionCreated({
|
|
220
|
-
eventId: (0, uuid_1.v4)(),
|
|
221
|
-
userId: userId,
|
|
222
|
-
recipientUserId: recipientUserId,
|
|
223
|
-
logicalTransactionId: existingModel.transactionId,
|
|
224
|
-
description: existingModel.description ?? "",
|
|
225
|
-
currency: existingModel.currency,
|
|
226
|
-
splitType: _1.SplitType.SpecificAmounts,
|
|
227
|
-
totalAmount: txDetails.amount,
|
|
228
|
-
amount: txDetails.amount,
|
|
229
|
-
isOwed: txDetails.isOwed,
|
|
230
|
-
transactionDate: existingModel.transactionDate,
|
|
231
|
-
groupId: existingModel.groupId,
|
|
232
|
-
groupTransactionId: existingModel.transactionId,
|
|
233
|
-
createdAt: new Date(),
|
|
234
|
-
createdBy: event.createdBy,
|
|
235
|
-
streamId: streamId,
|
|
236
|
-
version: 1,
|
|
237
|
-
systemGenerated: true,
|
|
238
|
-
});
|
|
239
|
-
await this.transactionEventStore.addEvent(createEvent);
|
|
240
|
-
const model = createEvent.apply(undefined);
|
|
241
|
-
if (!this.shouldSkipSave(undefined, model)) {
|
|
242
|
-
await this.transactionModelStore.save(model);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
56
|
async handleGroupTransactionCreated(event) {
|
|
247
57
|
const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
|
|
248
58
|
const updatedModel = event.apply(existingModel);
|
|
249
59
|
if (!this.shouldSkipSave(existingModel, updatedModel)) {
|
|
250
60
|
await this.modelStore.save(updatedModel);
|
|
251
|
-
|
|
252
|
-
EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupTransactionModel", updatedModel);
|
|
253
|
-
}
|
|
61
|
+
this.modelChangeEmitter?.emit("GroupTransactionModel", updatedModel);
|
|
254
62
|
}
|
|
255
63
|
if (this.activityEventHandler) {
|
|
256
64
|
await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
|
|
257
65
|
}
|
|
258
|
-
const allGroupPerspectives = await this.groupModelStore.findAllByGroupIdAndDeletedIsFalse(event.groupId);
|
|
259
|
-
for (const groupPerspective of allGroupPerspectives) {
|
|
260
|
-
if (groupPerspective.userId !== event.userId) {
|
|
261
|
-
const perspectiveStreamId = (0, uuid_1.v4)();
|
|
262
|
-
const perspectiveSplitType = (0, splitTypeUtils_1.reverseSplitType)(event.splitType);
|
|
263
|
-
const perspectiveEvent = new _1.GroupTransactionCreated({
|
|
264
|
-
eventId: (0, uuid_1.v4)(),
|
|
265
|
-
userId: groupPerspective.userId,
|
|
266
|
-
groupId: event.groupId,
|
|
267
|
-
transactionId: event.transactionId,
|
|
268
|
-
description: event.description,
|
|
269
|
-
notes: event.notes,
|
|
270
|
-
totalAmount: event.totalAmount,
|
|
271
|
-
currency: event.currency,
|
|
272
|
-
splitType: perspectiveSplitType,
|
|
273
|
-
transactionDate: event.transactionDate,
|
|
274
|
-
originalTransaction: event.originalTransaction,
|
|
275
|
-
createdAt: new Date(),
|
|
276
|
-
createdBy: event.createdBy,
|
|
277
|
-
streamId: perspectiveStreamId,
|
|
278
|
-
version: 1,
|
|
279
|
-
systemGenerated: true,
|
|
280
|
-
});
|
|
281
|
-
await this.eventStore.addEvent(perspectiveEvent);
|
|
282
|
-
const perspectiveModel = perspectiveEvent.apply(undefined);
|
|
283
|
-
if (!this.shouldSkipSave(undefined, perspectiveModel)) {
|
|
284
|
-
await this.modelStore.save(perspectiveModel);
|
|
285
|
-
if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
|
|
286
|
-
EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupTransactionModel", perspectiveModel);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
if (this.activityEventHandler) {
|
|
290
|
-
await this.activityEventHandler.createActivityLog(perspectiveEvent, undefined, perspectiveModel);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
await this.createFriendTransactions(event);
|
|
295
66
|
return updatedModel;
|
|
296
67
|
}
|
|
297
68
|
async handleGroupTransactionSplitDetailsChanged(event) {
|
|
@@ -299,52 +70,14 @@ class GroupTransactionEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
|
299
70
|
if (!existingModel) {
|
|
300
71
|
throw new Error("GroupTransaction must exist to change split details");
|
|
301
72
|
}
|
|
302
|
-
const oldMembers = existingModel.originalTransaction.members;
|
|
303
|
-
const newMembers = event.originalTransaction.members;
|
|
304
73
|
const updatedModel = event.apply(existingModel);
|
|
305
74
|
if (!this.shouldSkipSave(existingModel, updatedModel)) {
|
|
306
75
|
await this.modelStore.save(updatedModel);
|
|
307
|
-
|
|
308
|
-
EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupTransactionModel", updatedModel);
|
|
309
|
-
}
|
|
76
|
+
this.modelChangeEmitter?.emit("GroupTransactionModel", updatedModel);
|
|
310
77
|
}
|
|
311
78
|
if (this.activityEventHandler) {
|
|
312
79
|
await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
|
|
313
80
|
}
|
|
314
|
-
const allGroupPerspectives = await this.groupModelStore.findAllByGroupIdAndDeletedIsFalse(updatedModel.groupId);
|
|
315
|
-
for (const groupPerspective of allGroupPerspectives) {
|
|
316
|
-
if (groupPerspective.userId !== event.userId) {
|
|
317
|
-
const perspectiveStreamId = updatedModel.streamId;
|
|
318
|
-
const perspectiveSplitType = (0, splitTypeUtils_1.reverseSplitType)(event.splitType);
|
|
319
|
-
const perspectiveEvent = new _1.GroupTransactionSplitDetailsChanged({
|
|
320
|
-
eventId: (0, uuid_1.v4)(),
|
|
321
|
-
userId: groupPerspective.userId,
|
|
322
|
-
totalAmount: event.totalAmount,
|
|
323
|
-
splitType: perspectiveSplitType,
|
|
324
|
-
originalTransaction: event.originalTransaction,
|
|
325
|
-
createdAt: new Date(),
|
|
326
|
-
createdBy: event.createdBy,
|
|
327
|
-
streamId: perspectiveStreamId,
|
|
328
|
-
version: updatedModel.version,
|
|
329
|
-
systemGenerated: true,
|
|
330
|
-
});
|
|
331
|
-
await this.eventStore.addEvent(perspectiveEvent);
|
|
332
|
-
const existingPerspectiveModel = await this.modelStore.findByUserIdAndTransactionId(groupPerspective.userId, updatedModel.transactionId);
|
|
333
|
-
if (existingPerspectiveModel) {
|
|
334
|
-
const perspectiveModel = perspectiveEvent.apply(existingPerspectiveModel);
|
|
335
|
-
if (!this.shouldSkipSave(existingPerspectiveModel, perspectiveModel)) {
|
|
336
|
-
await this.modelStore.save(perspectiveModel);
|
|
337
|
-
if ((0, EventProcessorSDK_1.isSDKInitialized)()) {
|
|
338
|
-
EventProcessorSDK_1.EventProcessorSDK.getInstance().emitModelChange("GroupTransactionModel", perspectiveModel);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
if (this.activityEventHandler) {
|
|
342
|
-
await this.activityEventHandler.createActivityLog(perspectiveEvent, existingPerspectiveModel, perspectiveModel);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
await this.updateFriendTransactions(event, existingModel, oldMembers, newMembers);
|
|
348
81
|
return updatedModel;
|
|
349
82
|
}
|
|
350
83
|
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { TransactionEvent, TransactionEventStore, TransactionModel, TransactionModelStore } from ".";
|
|
2
2
|
import { BaseEventHandler } from "./BaseEventHandler";
|
|
3
3
|
import { ActivityEventHandler } from "./ActivityEventHandler";
|
|
4
|
+
import { ModelChangeEmitter } from "./ModelChangeEmitter";
|
|
4
5
|
export declare class TransactionEventHandler extends BaseEventHandler<TransactionEvent> {
|
|
5
6
|
private modelStore;
|
|
6
7
|
private eventStore;
|
|
7
8
|
private activityEventHandler?;
|
|
8
|
-
constructor(modelStore: TransactionModelStore, eventStore: TransactionEventStore, activityEventHandler?: ActivityEventHandler | undefined);
|
|
9
|
+
constructor(modelStore: TransactionModelStore, eventStore: TransactionEventStore, isClient: boolean, modelChangeEmitter?: ModelChangeEmitter, activityEventHandler?: ActivityEventHandler | undefined);
|
|
10
|
+
getModelStore(): TransactionModelStore;
|
|
9
11
|
handleClientEventFromRemoteListener(event: TransactionEvent): Promise<void>;
|
|
12
|
+
handleClientEventsFromRemoteListenerBulk(events: TransactionEvent[]): Promise<void>;
|
|
10
13
|
handleEvent(event: TransactionEvent): Promise<TransactionModel>;
|
|
11
14
|
private createMirrorEvent;
|
|
12
15
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionEventHandler.d.ts","sourceRoot":"","sources":["../src/TransactionEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"TransactionEventHandler.d.ts","sourceRoot":"","sources":["../src/TransactionEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EASH,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;AAM9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,qBAAa,uBAAwB,SAAQ,gBAAgB,CAAC,gBAAgB,CAAC;IAEvE,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,UAAU;IAGlB,OAAO,CAAC,oBAAoB,CAAC;gBAJrB,UAAU,EAAE,qBAAqB,EACjC,UAAU,EAAE,qBAAqB,EACzC,QAAQ,EAAE,OAAO,EACjB,kBAAkB,CAAC,EAAE,kBAAkB,EAC/B,oBAAoB,CAAC,EAAE,oBAAoB,YAAA;IAKhD,aAAa,IAAI,qBAAqB;IAIhC,mCAAmC,CAC5C,KAAK,EAAE,gBAAgB,GACxB,OAAO,CAAC,IAAI,CAAC;IAWH,wCAAwC,CACjD,MAAM,EAAE,gBAAgB,EAAE,GAC3B,OAAO,CAAC,IAAI,CAAC;IAqBV,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;YA0BvD,iBAAiB;CA6KlC"}
|
|
@@ -5,20 +5,38 @@ const _1 = require(".");
|
|
|
5
5
|
const BaseEventHandler_1 = require("./BaseEventHandler");
|
|
6
6
|
const splitTypeUtils_1 = require("./utils/splitTypeUtils");
|
|
7
7
|
const uuid_1 = require("uuid");
|
|
8
|
-
const EventProcessorSDK_1 = require("./EventProcessorSDK");
|
|
9
8
|
class TransactionEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
10
|
-
constructor(modelStore, eventStore, activityEventHandler) {
|
|
11
|
-
super();
|
|
9
|
+
constructor(modelStore, eventStore, isClient, modelChangeEmitter, activityEventHandler) {
|
|
10
|
+
super(isClient, modelChangeEmitter);
|
|
12
11
|
this.modelStore = modelStore;
|
|
13
12
|
this.eventStore = eventStore;
|
|
14
13
|
this.activityEventHandler = activityEventHandler;
|
|
15
14
|
}
|
|
15
|
+
getModelStore() {
|
|
16
|
+
return this.modelStore;
|
|
17
|
+
}
|
|
16
18
|
async handleClientEventFromRemoteListener(event) {
|
|
17
19
|
const existingModel = await this.modelStore.getByStreamId(event.streamId);
|
|
18
20
|
const model = event.apply(existingModel);
|
|
19
21
|
if (!this.shouldSkipSave(existingModel, model)) {
|
|
20
22
|
await this.modelStore.save(model);
|
|
21
|
-
|
|
23
|
+
this.modelChangeEmitter?.emit("TransactionModel", model);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async handleClientEventsFromRemoteListenerBulk(events) {
|
|
27
|
+
const updatedModels = [];
|
|
28
|
+
for (const event of events) {
|
|
29
|
+
const existingModel = await this.modelStore.getByStreamId(event.streamId);
|
|
30
|
+
const model = event.apply(existingModel);
|
|
31
|
+
if (!this.shouldSkipSave(existingModel, model)) {
|
|
32
|
+
await this.modelStore.save(model);
|
|
33
|
+
updatedModels.push(model);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (this.modelChangeEmitter && updatedModels.length > 0) {
|
|
37
|
+
for (const model of updatedModels) {
|
|
38
|
+
this.modelChangeEmitter.emit("TransactionModel", model);
|
|
39
|
+
}
|
|
22
40
|
}
|
|
23
41
|
}
|
|
24
42
|
async handleEvent(event) {
|
|
@@ -26,9 +44,9 @@ class TransactionEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
|
26
44
|
const model = event.apply(existingModel);
|
|
27
45
|
if (!this.shouldSkipSave(existingModel, model)) {
|
|
28
46
|
await this.modelStore.save(model);
|
|
29
|
-
if (
|
|
47
|
+
if (this.isClient) {
|
|
30
48
|
await this.eventStore.addEvent(event);
|
|
31
|
-
|
|
49
|
+
this.modelChangeEmitter?.emit("TransactionModel", model);
|
|
32
50
|
if (this.activityEventHandler) {
|
|
33
51
|
await this.activityEventHandler.createActivityLog(event, existingModel, model);
|
|
34
52
|
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { TransactionEvent } from "./common/DomainEvents";
|
|
2
2
|
import { Model } from "./common/Model";
|
|
3
3
|
import { ActivityLogCreatedProps } from "./ActivityLogEvents";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
export type SplitType = (typeof SplitTypeEnum)[keyof typeof SplitTypeEnum];
|
|
5
|
+
export declare const SplitTypeEnum: {
|
|
6
|
+
readonly YouPaidSplitEqually: "YouPaidSplitEqually";
|
|
7
|
+
readonly TheyPaidSplitEqually: "TheyPaidSplitEqually";
|
|
8
|
+
readonly TheyOweYouAll: "TheyOweYouAll";
|
|
9
|
+
readonly YouOweThemAll: "YouOweThemAll";
|
|
10
|
+
readonly TheyPaidToSettle: "TheyPaidToSettle";
|
|
11
|
+
readonly YouPaidToSettle: "YouPaidToSettle";
|
|
12
|
+
readonly SpecificAmounts: "SpecificAmounts";
|
|
13
|
+
};
|
|
13
14
|
export interface AmountDto {
|
|
14
15
|
amount: number;
|
|
15
16
|
currency: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionEvents.d.ts","sourceRoot":"","sources":["../src/TransactionEvents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,uBAAuB,EAAc,MAAM,qBAAqB,CAAC;AAI1E,
|
|
1
|
+
{"version":3,"file":"TransactionEvents.d.ts","sourceRoot":"","sources":["../src/TransactionEvents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,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AClB,eAAe,EAAE,OAAO,CAAC;gBAEpB,KAAK,EAAE,8BAA8B;IAajD,KAAK,CAAC,QAAQ,CAAC,EAAE,KAAK,GAAG,gBAAgB;IA4CzC,WAAW,CACP,QAAQ,CAAC,EAAE,KAAK,EAChB,SAAS,CAAC,EAAE,MAAM,GACnB,uBAAuB,GAAG,SAAS;CAwBzC"}
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TransactionDetailsChanged = exports.TransactionDateChanged = exports.CurrencyChanged = exports.SplitTypeChanged = exports.TotalAmountChanged = exports.DescriptionChanged = exports.TransactionDeleted = exports.TransactionCreated = exports.ChangeHistory = exports.
|
|
3
|
+
exports.TransactionDetailsChanged = exports.TransactionDateChanged = exports.CurrencyChanged = exports.SplitTypeChanged = exports.TotalAmountChanged = exports.DescriptionChanged = exports.TransactionDeleted = exports.TransactionCreated = exports.ChangeHistory = exports.SplitTypeEnum = void 0;
|
|
4
4
|
const ActivityLogEvents_1 = require("./ActivityLogEvents");
|
|
5
5
|
const uuid_1 = require("uuid");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
SplitType["SpecificAmounts"] = "SpecificAmounts";
|
|
16
|
-
})(SplitType || (exports.SplitType = SplitType = {}));
|
|
6
|
+
exports.SplitTypeEnum = {
|
|
7
|
+
YouPaidSplitEqually: "YouPaidSplitEqually",
|
|
8
|
+
TheyPaidSplitEqually: "TheyPaidSplitEqually",
|
|
9
|
+
TheyOweYouAll: "TheyOweYouAll",
|
|
10
|
+
YouOweThemAll: "YouOweThemAll",
|
|
11
|
+
TheyPaidToSettle: "TheyPaidToSettle",
|
|
12
|
+
YouPaidToSettle: "YouPaidToSettle",
|
|
13
|
+
SpecificAmounts: "SpecificAmounts",
|
|
14
|
+
};
|
|
17
15
|
var ChangeHistory;
|
|
18
16
|
(function (ChangeHistory) {
|
|
19
17
|
function empty() {
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { UserCreated, UserEvent, UserEventStore, UserModel, UserModelStore } from ".";
|
|
2
2
|
import { BaseEventHandler } from "./BaseEventHandler";
|
|
3
3
|
import { ActivityEventHandler } from "./ActivityEventHandler";
|
|
4
|
+
import { ModelChangeEmitter } from "./ModelChangeEmitter";
|
|
4
5
|
export declare class UserEventHandler extends BaseEventHandler<UserEvent> {
|
|
5
6
|
private modelStore;
|
|
6
7
|
private eventStore;
|
|
7
8
|
private activityEventHandler?;
|
|
8
|
-
constructor(modelStore: UserModelStore, eventStore: UserEventStore, activityEventHandler?: ActivityEventHandler | undefined);
|
|
9
|
+
constructor(modelStore: UserModelStore, eventStore: UserEventStore, isClient: boolean, modelChangeEmitter?: ModelChangeEmitter, activityEventHandler?: ActivityEventHandler | undefined);
|
|
10
|
+
getModelStore(): UserModelStore;
|
|
9
11
|
handleClientEventFromRemoteListener(event: UserEvent): Promise<void>;
|
|
12
|
+
handleClientEventsFromRemoteListenerBulk(events: UserEvent[]): Promise<void>;
|
|
10
13
|
handleEvent(event: UserEvent): Promise<UserModel>;
|
|
11
14
|
handleUserCreatedBackend(event: UserCreated): Promise<UserModel>;
|
|
12
15
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UserEventHandler.d.ts","sourceRoot":"","sources":["../src/UserEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"UserEventHandler.d.ts","sourceRoot":"","sources":["../src/UserEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,WAAW,EACX,SAAS,EACT,cAAc,EACd,SAAS,EACT,cAAc,EACjB,MAAM,GAAG,CAAC;AACX,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,qBAAa,gBAAiB,SAAQ,gBAAgB,CAAC,SAAS,CAAC;IAEzD,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,UAAU;IAGlB,OAAO,CAAC,oBAAoB,CAAC;gBAJrB,UAAU,EAAE,cAAc,EAC1B,UAAU,EAAE,cAAc,EAClC,QAAQ,EAAE,OAAO,EACjB,kBAAkB,CAAC,EAAE,kBAAkB,EAC/B,oBAAoB,CAAC,EAAE,oBAAoB,YAAA;IAKhD,aAAa,IAAI,cAAc;IAIzB,mCAAmC,CAC5C,KAAK,EAAE,SAAS,GACjB,OAAO,CAAC,IAAI,CAAC;IAWH,wCAAwC,CACjD,MAAM,EAAE,SAAS,EAAE,GACpB,OAAO,CAAC,IAAI,CAAC;IAqBV,WAAW,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;IA6BjD,wBAAwB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC;CAuDzE"}
|
package/dist/UserEventHandler.js
CHANGED
|
@@ -4,20 +4,38 @@ exports.UserEventHandler = void 0;
|
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
const BaseEventHandler_1 = require("./BaseEventHandler");
|
|
6
6
|
const uuid_1 = require("uuid");
|
|
7
|
-
const EventProcessorSDK_1 = require("./EventProcessorSDK");
|
|
8
7
|
class UserEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
9
|
-
constructor(modelStore, eventStore, activityEventHandler) {
|
|
10
|
-
super();
|
|
8
|
+
constructor(modelStore, eventStore, isClient, modelChangeEmitter, activityEventHandler) {
|
|
9
|
+
super(isClient, modelChangeEmitter);
|
|
11
10
|
this.modelStore = modelStore;
|
|
12
11
|
this.eventStore = eventStore;
|
|
13
12
|
this.activityEventHandler = activityEventHandler;
|
|
14
13
|
}
|
|
14
|
+
getModelStore() {
|
|
15
|
+
return this.modelStore;
|
|
16
|
+
}
|
|
15
17
|
async handleClientEventFromRemoteListener(event) {
|
|
16
18
|
const existingModel = await this.modelStore.getByStreamId(event.streamId);
|
|
17
19
|
const updatedModel = event.apply(existingModel);
|
|
18
20
|
if (!this.shouldSkipSave(existingModel, updatedModel)) {
|
|
19
21
|
await this.modelStore.save(updatedModel);
|
|
20
|
-
|
|
22
|
+
this.modelChangeEmitter?.emit("UserModel", updatedModel);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async handleClientEventsFromRemoteListenerBulk(events) {
|
|
26
|
+
const updatedModels = [];
|
|
27
|
+
for (const event of events) {
|
|
28
|
+
const existingModel = await this.modelStore.getByStreamId(event.streamId);
|
|
29
|
+
const updatedModel = event.apply(existingModel);
|
|
30
|
+
if (!this.shouldSkipSave(existingModel, updatedModel)) {
|
|
31
|
+
await this.modelStore.save(updatedModel);
|
|
32
|
+
updatedModels.push(updatedModel);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (this.modelChangeEmitter && updatedModels.length > 0) {
|
|
36
|
+
for (const model of updatedModels) {
|
|
37
|
+
this.modelChangeEmitter.emit("UserModel", model);
|
|
38
|
+
}
|
|
21
39
|
}
|
|
22
40
|
}
|
|
23
41
|
async handleEvent(event) {
|
|
@@ -30,9 +48,9 @@ class UserEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
|
30
48
|
const updatedModel = event.apply(existingModel);
|
|
31
49
|
if (!this.shouldSkipSave(existingModel, updatedModel)) {
|
|
32
50
|
await this.modelStore.save(updatedModel);
|
|
33
|
-
if (
|
|
51
|
+
if (this.isClient) {
|
|
34
52
|
await this.eventStore.addEvent(event);
|
|
35
|
-
|
|
53
|
+
this.modelChangeEmitter?.emit("UserModel", updatedModel);
|
|
36
54
|
if (this.activityEventHandler) {
|
|
37
55
|
await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
|
|
38
56
|
}
|
|
@@ -42,7 +60,7 @@ class UserEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
|
42
60
|
}
|
|
43
61
|
}
|
|
44
62
|
async handleUserCreatedBackend(event) {
|
|
45
|
-
if (
|
|
63
|
+
if (this.isClient) {
|
|
46
64
|
throw new Error("handleUserCreated should not be called on client side");
|
|
47
65
|
}
|
|
48
66
|
let placeholderUser;
|
|
@@ -3,6 +3,7 @@ export declare class IMActivityLogEventStore implements ActivityLogEventStore {
|
|
|
3
3
|
private events;
|
|
4
4
|
addEvent(event: ActivityLogEvent): Promise<void>;
|
|
5
5
|
getAllEvents(): ActivityLogEvent[];
|
|
6
|
+
getAll(): ActivityLogEvent[];
|
|
6
7
|
clear(): void;
|
|
7
8
|
}
|
|
8
9
|
//# sourceMappingURL=IMActivityLogEventStore.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IMActivityLogEventStore.d.ts","sourceRoot":"","sources":["../../../src/__tests__/test-helpers/IMActivityLogEventStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,OAAO,CAAC;AAEhE,qBAAa,uBAAwB,YAAW,qBAAqB;IACjE,OAAO,CAAC,MAAM,CAA0B;IAElC,QAAQ,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;IAKtD,YAAY,IAAI,gBAAgB,EAAE;IAIlC,KAAK,IAAI,IAAI;CAGhB"}
|
|
1
|
+
{"version":3,"file":"IMActivityLogEventStore.d.ts","sourceRoot":"","sources":["../../../src/__tests__/test-helpers/IMActivityLogEventStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,OAAO,CAAC;AAEhE,qBAAa,uBAAwB,YAAW,qBAAqB;IACjE,OAAO,CAAC,MAAM,CAA0B;IAElC,QAAQ,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;IAKtD,YAAY,IAAI,gBAAgB,EAAE;IAIlC,MAAM,IAAI,gBAAgB,EAAE;IAI5B,KAAK,IAAI,IAAI;CAGhB"}
|