Skip to content

Commit 55f43c1

Browse files
authored
Merge pull request #3015 from DFXswiss/develop
Release: develop -> main
2 parents ab3a700 + 75b18eb commit 55f43c1

2 files changed

Lines changed: 93 additions & 96 deletions

File tree

src/subdomains/supporting/log/__tests__/log-job.service.spec.ts

Lines changed: 74 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -111,93 +111,94 @@ describe('LogJobService', () => {
111111
it('should filter same length sender & receiver', async () => {
112112
if (new Date().getHours() > 19) return;
113113

114+
// Items 0-6 are > 21 days old (will be filtered out), items 7-9 are < 21 days (will remain)
114115
const receiverTx = [
115-
createCustomExchangeTx({ id: 63189, created: Util.hoursBefore(409), amount: 9500.0 }),
116-
createCustomExchangeTx({ id: 63190, created: Util.hoursBefore(409), amount: 9500.0 }),
117-
createCustomExchangeTx({ id: 63191, created: Util.hoursBefore(409), amount: 9500.0 }),
118-
createCustomExchangeTx({ id: 63232, created: Util.hoursBefore(407), amount: 9500.0 }),
119-
createCustomExchangeTx({ id: 63277, created: Util.hoursBefore(310), amount: 9500.0 }),
120-
createCustomExchangeTx({ id: 63278, created: Util.hoursBefore(310), amount: 9500.0 }),
121-
createCustomExchangeTx({ id: 63279, created: Util.hoursBefore(310), amount: 9500.0 }),
122-
createCustomExchangeTx({ id: 63280, created: Util.hoursBefore(310), amount: 9500.0 }),
123-
createCustomExchangeTx({ id: 63281, created: Util.hoursBefore(310), amount: 9500.0 }),
124-
createCustomExchangeTx({ id: 63282, created: Util.hoursBefore(310), amount: 9500.0 }),
116+
createCustomExchangeTx({ id: 63189, created: Util.hoursBefore(529), amount: 9500.0 }), // 22d
117+
createCustomExchangeTx({ id: 63190, created: Util.hoursBefore(529), amount: 9500.0 }), // 22d
118+
createCustomExchangeTx({ id: 63191, created: Util.hoursBefore(529), amount: 9500.0 }), // 22d
119+
createCustomExchangeTx({ id: 63232, created: Util.hoursBefore(527), amount: 9500.0 }), // 22d
120+
createCustomExchangeTx({ id: 63277, created: Util.hoursBefore(520), amount: 9500.0 }), // 21.6d
121+
createCustomExchangeTx({ id: 63278, created: Util.hoursBefore(520), amount: 9500.0 }), // 21.6d
122+
createCustomExchangeTx({ id: 63279, created: Util.hoursBefore(520), amount: 9500.0 }), // 21.6d
123+
createCustomExchangeTx({ id: 63280, created: Util.hoursBefore(310), amount: 9500.0 }), // 13d
124+
createCustomExchangeTx({ id: 63281, created: Util.hoursBefore(310), amount: 9500.0 }), // 13d
125+
createCustomExchangeTx({ id: 63282, created: Util.hoursBefore(310), amount: 9500.0 }), // 13d
125126
];
126127

127128
const senderTx = [
128129
createCustomBankTx({
129130
id: 142006,
130-
created: Util.hoursBefore(432),
131-
valueDate: Util.hoursBefore(406),
131+
created: Util.hoursBefore(552), // 23d
132+
valueDate: Util.hoursBefore(526),
132133
instructedAmount: 9500.0,
133134
amount: 9500.0,
134135
}),
135136
createCustomBankTx({
136137
id: 142007,
137-
created: Util.hoursBefore(432),
138-
valueDate: Util.hoursBefore(406),
138+
created: Util.hoursBefore(552), // 23d
139+
valueDate: Util.hoursBefore(526),
139140
instructedAmount: 9500.0,
140141
amount: 9500.0,
141142
}),
142143
createCustomBankTx({
143144
id: 142008,
144-
created: Util.hoursBefore(432),
145-
valueDate: Util.hoursBefore(406),
145+
created: Util.hoursBefore(552), // 23d
146+
valueDate: Util.hoursBefore(526),
146147
instructedAmount: 9500.0,
147148
amount: 9500.0,
148149
}),
149150
createCustomBankTx({
150151
id: 142153,
151-
created: Util.hoursBefore(408),
152-
valueDate: Util.hoursBefore(406),
152+
created: Util.hoursBefore(528), // 22d
153+
valueDate: Util.hoursBefore(526),
153154
instructedAmount: 9500.0,
154155
amount: 9500.0,
155156
}),
156157
createCustomBankTx({
157158
id: 142199,
158-
created: Util.hoursBefore(384),
159-
valueDate: Util.hoursBefore(384),
159+
created: Util.hoursBefore(520), // 21.6d
160+
valueDate: Util.hoursBefore(520),
160161
instructedAmount: 9500.0,
161162
amount: 9500.0,
162163
}),
163164
createCustomBankTx({
164165
id: 142218,
165-
created: Util.hoursBefore(384),
166-
valueDate: Util.hoursBefore(384),
166+
created: Util.hoursBefore(520), // 21.6d
167+
valueDate: Util.hoursBefore(520),
167168
instructedAmount: 9500.0,
168169
amount: 9500.0,
169170
}),
170171
createCustomBankTx({
171172
id: 142265,
172-
created: Util.hoursBefore(384),
173-
valueDate: Util.hoursBefore(384),
173+
created: Util.hoursBefore(520), // 21.6d
174+
valueDate: Util.hoursBefore(520),
174175
instructedAmount: 9500.0,
175176
amount: 9500.0,
176177
}),
177178
createCustomBankTx({
178179
id: 142396,
179-
created: Util.hoursBefore(312),
180+
created: Util.hoursBefore(312), // 13d
180181
valueDate: Util.hoursBefore(312),
181182
instructedAmount: 9500.0,
182183
amount: 9500.0,
183184
}),
184185
createCustomBankTx({
185186
id: 142407,
186-
created: Util.hoursBefore(312),
187+
created: Util.hoursBefore(312), // 13d
187188
valueDate: Util.hoursBefore(312),
188189
instructedAmount: 9500.0,
189190
amount: 9500.0,
190191
}),
191192
createCustomBankTx({
192193
id: 142408,
193-
created: Util.hoursBefore(312),
194+
created: Util.hoursBefore(312), // 13d
194195
valueDate: Util.hoursBefore(312),
195196
instructedAmount: 9500.0,
196197
amount: 9500.0,
197198
}),
198199
createCustomBankTx({
199200
id: 142409,
200-
created: Util.hoursBefore(309),
201+
created: Util.hoursBefore(309), // 13d
201202
valueDate: Util.hoursBefore(309),
202203
instructedAmount: 9500.0,
203204
amount: 9500.0,
@@ -270,25 +271,25 @@ describe('LogJobService', () => {
270271
});
271272
});
272273

273-
it('should filter tx 14d or older', async () => {
274+
it('should filter tx 21d or older', async () => {
274275
const senderTx = [
275-
createCustomExchangeTx({ id: 1, created: Util.daysBefore(17), amount: 9999.0 }),
276-
createCustomExchangeTx({ id: 2, created: Util.daysBefore(12), amount: 9999.0 }),
276+
createCustomExchangeTx({ id: 1, created: Util.daysBefore(22), amount: 9999.0 }),
277+
createCustomExchangeTx({ id: 2, created: Util.daysBefore(17), amount: 9999.0 }),
277278
createCustomExchangeTx({ id: 3, created: Util.daysBefore(7), amount: 9999.0 }),
278279
];
279280

280281
const receiverTx = [
281282
createCustomBankTx({
282283
id: 1,
283-
created: Util.daysBefore(16),
284-
valueDate: Util.daysBefore(16),
284+
created: Util.daysBefore(21),
285+
valueDate: Util.daysBefore(21),
285286
instructedAmount: 9999.0,
286287
amount: 9999.0,
287288
}),
288289
createCustomBankTx({
289290
id: 2,
290-
created: Util.daysBefore(11),
291-
valueDate: Util.daysBefore(11),
291+
created: Util.daysBefore(16),
292+
valueDate: Util.daysBefore(16),
292293
instructedAmount: 9999.0,
293294
amount: 9999.0,
294295
}),
@@ -337,118 +338,122 @@ describe('LogJobService', () => {
337338
});
338339

339340
it('should filter receiver 21d or older', async () => {
341+
// All senders created after receiver, so matching finds sender with highest id
340342
const senderTx = [
341-
createCustomExchangeTx({ id: 3, created: Util.daysBefore(19), amount: 9999.0 }),
342-
createCustomExchangeTx({ id: 2, created: Util.daysBefore(18), amount: 9999.0 }),
343-
createCustomExchangeTx({ id: 1, created: Util.daysBefore(17), amount: 9999.0 }),
343+
createCustomExchangeTx({ id: 3, created: Util.daysBefore(20), amount: 9999.0 }),
344+
createCustomExchangeTx({ id: 2, created: Util.daysBefore(19), amount: 9999.0 }),
345+
createCustomExchangeTx({ id: 1, created: Util.daysBefore(19), amount: 9999.0 }),
344346
];
345347

346348
const receiverTx = [
347349
createCustomBankTx({
348350
id: 1,
349-
created: Util.daysBefore(21),
351+
created: Util.daysBefore(21), // This will be filtered out (>= 21d)
350352
valueDate: Util.daysBefore(21),
351353
instructedAmount: 9999.0,
352354
amount: 9999.0,
353355
}),
354356
createCustomBankTx({
355357
id: 2,
356-
created: Util.daysBefore(18),
358+
created: Util.daysBefore(18), // This will remain (< 21d)
357359
valueDate: Util.daysBefore(18),
358360
instructedAmount: 9999.0,
359361
amount: 9999.0,
360362
}),
361363
];
362364

365+
// Matching finds sender id=3 (oldest sender that's still older than receiver at 18d)
366+
// Filter keeps only senders with id >= 3
363367
expect(service.filterSenderPendingList(senderTx, receiverTx)).toMatchObject({
364-
sender: senderTx,
368+
sender: [senderTx[0]], // Only id=3 remains after matching
365369
receiver: [receiverTx[1]],
366370
});
367371
});
368372

369-
it('should filter same length sender & receiver', async () => {
373+
it('should filter same length sender & receiver (reverse)', async () => {
370374
if (new Date().getHours() > 19) return;
371375

376+
// Items 0-6 are > 21 days old (will be filtered out), items 7-9 are < 21 days (will remain)
372377
const receiverTx = [
373-
createCustomExchangeTx({ id: 63189, created: Util.hoursBefore(409), amount: 9500.0 }),
374-
createCustomExchangeTx({ id: 63190, created: Util.hoursBefore(409), amount: 9500.0 }),
375-
createCustomExchangeTx({ id: 63191, created: Util.hoursBefore(409), amount: 9500.0 }),
376-
createCustomExchangeTx({ id: 63232, created: Util.hoursBefore(407), amount: 9500.0 }),
377-
createCustomExchangeTx({ id: 63277, created: Util.hoursBefore(310), amount: 9500.0 }),
378-
createCustomExchangeTx({ id: 63278, created: Util.hoursBefore(310), amount: 9500.0 }),
379-
createCustomExchangeTx({ id: 63279, created: Util.hoursBefore(310), amount: 9500.0 }),
380-
createCustomExchangeTx({ id: 63280, created: Util.hoursBefore(310), amount: 9500.0 }),
381-
createCustomExchangeTx({ id: 63281, created: Util.hoursBefore(310), amount: 9500.0 }),
382-
createCustomExchangeTx({ id: 63282, created: Util.hoursBefore(310), amount: 9500.0 }),
378+
createCustomExchangeTx({ id: 63189, created: Util.hoursBefore(529), amount: 9500.0 }), // 22d
379+
createCustomExchangeTx({ id: 63190, created: Util.hoursBefore(529), amount: 9500.0 }), // 22d
380+
createCustomExchangeTx({ id: 63191, created: Util.hoursBefore(529), amount: 9500.0 }), // 22d
381+
createCustomExchangeTx({ id: 63232, created: Util.hoursBefore(527), amount: 9500.0 }), // 22d
382+
createCustomExchangeTx({ id: 63277, created: Util.hoursBefore(520), amount: 9500.0 }), // 21.6d
383+
createCustomExchangeTx({ id: 63278, created: Util.hoursBefore(520), amount: 9500.0 }), // 21.6d
384+
createCustomExchangeTx({ id: 63279, created: Util.hoursBefore(520), amount: 9500.0 }), // 21.6d
385+
createCustomExchangeTx({ id: 63280, created: Util.hoursBefore(310), amount: 9500.0 }), // 13d
386+
createCustomExchangeTx({ id: 63281, created: Util.hoursBefore(310), amount: 9500.0 }), // 13d
387+
createCustomExchangeTx({ id: 63282, created: Util.hoursBefore(310), amount: 9500.0 }), // 13d
383388
];
384389

385390
const senderTx = [
386391
createCustomBankTx({
387392
id: 142006,
388-
created: Util.hoursBefore(432),
389-
valueDate: Util.hoursBefore(406),
393+
created: Util.hoursBefore(552), // 23d
394+
valueDate: Util.hoursBefore(526),
390395
instructedAmount: 9500.0,
391396
amount: 9500.0,
392397
}),
393398
createCustomBankTx({
394399
id: 142007,
395-
created: Util.hoursBefore(432),
396-
valueDate: Util.hoursBefore(406),
400+
created: Util.hoursBefore(552), // 23d
401+
valueDate: Util.hoursBefore(526),
397402
instructedAmount: 9500.0,
398403
amount: 9500.0,
399404
}),
400405
createCustomBankTx({
401406
id: 142008,
402-
created: Util.hoursBefore(432),
403-
valueDate: Util.hoursBefore(406),
407+
created: Util.hoursBefore(552), // 23d
408+
valueDate: Util.hoursBefore(526),
404409
instructedAmount: 9500.0,
405410
amount: 9500.0,
406411
}),
407412
createCustomBankTx({
408413
id: 142153,
409-
created: Util.hoursBefore(408),
410-
valueDate: Util.hoursBefore(406),
414+
created: Util.hoursBefore(528), // 22d
415+
valueDate: Util.hoursBefore(526),
411416
instructedAmount: 9500.0,
412417
amount: 9500.0,
413418
}),
414419
createCustomBankTx({
415420
id: 142199,
416-
created: Util.hoursBefore(384),
417-
valueDate: Util.hoursBefore(384),
421+
created: Util.hoursBefore(520), // 21.6d
422+
valueDate: Util.hoursBefore(520),
418423
instructedAmount: 9500.0,
419424
amount: 9500.0,
420425
}),
421426
createCustomBankTx({
422427
id: 142218,
423-
created: Util.hoursBefore(384),
424-
valueDate: Util.hoursBefore(384),
428+
created: Util.hoursBefore(520), // 21.6d
429+
valueDate: Util.hoursBefore(520),
425430
instructedAmount: 9500.0,
426431
amount: 9500.0,
427432
}),
428433
createCustomBankTx({
429434
id: 142265,
430-
created: Util.hoursBefore(384),
431-
valueDate: Util.hoursBefore(384),
435+
created: Util.hoursBefore(520), // 21.6d
436+
valueDate: Util.hoursBefore(520),
432437
instructedAmount: 9500.0,
433438
amount: 9500.0,
434439
}),
435440
createCustomBankTx({
436441
id: 142396,
437-
created: Util.hoursBefore(312),
442+
created: Util.hoursBefore(312), // 13d
438443
valueDate: Util.hoursBefore(312),
439444
instructedAmount: 9500.0,
440445
amount: 9500.0,
441446
}),
442447
createCustomBankTx({
443448
id: 142407,
444-
created: Util.hoursBefore(312),
449+
created: Util.hoursBefore(312), // 13d
445450
valueDate: Util.hoursBefore(312),
446451
instructedAmount: 9500.0,
447452
amount: 9500.0,
448453
}),
449454
createCustomBankTx({
450455
id: 142408,
451-
created: Util.hoursBefore(312),
456+
created: Util.hoursBefore(312), // 13d
452457
valueDate: Util.hoursBefore(312),
453458
instructedAmount: 9500.0,
454459
amount: 9500.0,

src/subdomains/supporting/log/log-job.service.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -948,34 +948,24 @@ export class LogJobService {
948948
senderTx: (BankTx | ExchangeTx)[],
949949
receiverTx: (BankTx | ExchangeTx)[] | undefined,
950950
): { receiver: (BankTx | ExchangeTx)[]; sender: (BankTx | ExchangeTx)[] } {
951-
const before14Days = Util.daysBefore(14);
952951
const before21Days = Util.daysBefore(21);
953952

954-
before14Days.setHours(0, 0, 0, 0);
955-
956953
let filtered21SenderTx = senderTx.filter((s) => s.created > before21Days);
957-
let filtered14ReceiverTx = receiverTx.filter((r) => r.created > before14Days);
954+
let filtered21ReceiverTx = receiverTx.filter((r) => r.created > before21Days);
958955

959956
if (!filtered21SenderTx.length) return { receiver: [], sender: [] };
960-
if (!filtered14ReceiverTx?.length) {
961-
const filtered21ReceiverTx = receiverTx.filter((r) => r.created > before21Days);
962-
963-
const { receiverIndex: rawReceiverIndex } = this.findSenderReceiverPair(filtered21SenderTx, filtered21ReceiverTx);
964-
957+
if (!filtered21ReceiverTx?.length) {
965958
return {
966959
sender: filtered21SenderTx,
967-
receiver:
968-
rawReceiverIndex != null
969-
? filtered21ReceiverTx.filter((r) => r.id >= filtered21ReceiverTx[rawReceiverIndex]?.id)
970-
: filtered14ReceiverTx,
960+
receiver: [],
971961
};
972962
}
973963

974-
const { senderPair, receiverIndex } = this.findSenderReceiverPair(filtered21SenderTx, filtered14ReceiverTx);
964+
const { senderPair, receiverIndex } = this.findSenderReceiverPair(filtered21SenderTx, filtered21ReceiverTx);
975965

976966
if (filtered21SenderTx[0] instanceof BankTx) {
977967
this.logger.verbose(
978-
`FinanceLog receiverTxId/date: ${filtered14ReceiverTx?.[receiverIndex]?.id}/${filtered14ReceiverTx?.[
968+
`FinanceLog receiverTxId/date: ${filtered21ReceiverTx?.[receiverIndex]?.id}/${filtered21ReceiverTx?.[
979969
receiverIndex
980970
]?.created.toDateString()}; senderTx[0] id/date: ${
981971
filtered21SenderTx[0]?.id
@@ -989,18 +979,18 @@ export class LogJobService {
989979

990980
filtered21SenderTx = senderPair ? filtered21SenderTx.filter((s) => s.id >= senderPair.id) : filtered21SenderTx;
991981

992-
if (filtered14ReceiverTx.length > filtered21SenderTx.length) {
993-
const { senderPair } = this.findSenderReceiverPair(filtered21SenderTx, filtered14ReceiverTx, true);
982+
if (filtered21ReceiverTx.length > filtered21SenderTx.length) {
983+
const { senderPair } = this.findSenderReceiverPair(filtered21SenderTx, filtered21ReceiverTx, true);
994984

995985
const senderTxLength = senderPair
996986
? filtered21SenderTx.filter((s) => s.id <= senderPair.id).length
997-
: filtered14ReceiverTx.length;
987+
: filtered21ReceiverTx.length;
998988

999-
filtered14ReceiverTx = filtered14ReceiverTx.slice(filtered14ReceiverTx.length - senderTxLength);
989+
filtered21ReceiverTx = filtered21ReceiverTx.slice(filtered21ReceiverTx.length - senderTxLength);
1000990
}
1001991

1002992
return {
1003-
receiver: filtered14ReceiverTx.filter((r) => r.id >= (filtered14ReceiverTx[receiverIndex]?.id ?? 0)),
993+
receiver: filtered21ReceiverTx.filter((r) => r.id >= (filtered21ReceiverTx[receiverIndex]?.id ?? 0)),
1004994
sender: filtered21SenderTx.sort((a, b) => a.id - b.id),
1005995
};
1006996
}
@@ -1032,13 +1022,15 @@ export class LogJobService {
10321022
? (receiverTx[receiverIndex] as BankTx).instructedAmount
10331023
: receiverTx[receiverIndex].amount;
10341024

1035-
const senderPair = senderTx.find((s) =>
1036-
s instanceof BankTx
1037-
? s.instructedAmount === receiverAmount &&
1038-
receiverTx[receiverIndex].created.toDateString() === s.valueDate.toDateString() &&
1039-
receiverTx[receiverIndex].created > s.created
1040-
: s.amount === receiverAmount && receiverTx[receiverIndex].created > s.created,
1041-
);
1025+
const senderPair = senderTx.find((s) => {
1026+
const receiverCreated = receiverTx[receiverIndex].created;
1027+
const senderDate = s instanceof BankTx ? s.valueDate : s.created;
1028+
const daysDiff = Math.abs(Util.daysDiff(senderDate, receiverCreated));
1029+
1030+
return s instanceof BankTx
1031+
? s.instructedAmount === receiverAmount && daysDiff <= 5 && receiverCreated > s.created
1032+
: s.amount === receiverAmount && receiverCreated > s.created;
1033+
});
10421034

10431035
if (!senderPair) {
10441036
receiverIndex++;

0 commit comments

Comments
 (0)