@stamhoofd/backend 2.106.1 → 2.107.1

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 (30) hide show
  1. package/index.ts +6 -1
  2. package/package.json +17 -11
  3. package/src/boot.ts +28 -22
  4. package/src/endpoints/frontend/FrontendEnvironmentEndpoint.ts +89 -0
  5. package/src/endpoints/global/billing/ActivatePackagesEndpoint.ts +30 -0
  6. package/src/endpoints/global/members/PatchOrganizationMembersEndpoint.ts +3 -2
  7. package/src/endpoints/global/registration/PatchUserMembersEndpoint.ts +8 -3
  8. package/src/endpoints/global/registration/RegisterMembersEndpoint.test.ts +109 -109
  9. package/src/endpoints/global/registration/RegisterMembersEndpoint.ts +7 -0
  10. package/src/endpoints/organization/dashboard/payments/PatchBalanceItemsEndpoint.ts +9 -7
  11. package/src/endpoints/organization/dashboard/payments/PatchPaymentsEndpoint.ts +7 -0
  12. package/src/endpoints/organization/webshops/PlaceOrderEndpoint.ts +9 -2
  13. package/src/excel-loaders/payments.ts +5 -5
  14. package/src/excel-loaders/receivable-balances.ts +7 -7
  15. package/src/helpers/AdminPermissionChecker.ts +20 -0
  16. package/src/helpers/BuckarooHelper.ts +1 -1
  17. package/src/helpers/ServiceFeeHelper.ts +8 -4
  18. package/src/helpers/StripeHelper.ts +20 -35
  19. package/src/seeds/1752848561-groups-registration-periods.ts +35 -0
  20. package/src/services/BalanceItemService.ts +15 -2
  21. package/src/services/PaymentReallocationService.test.ts +298 -128
  22. package/src/services/PaymentReallocationService.ts +46 -16
  23. package/src/services/PaymentService.ts +49 -2
  24. package/src/services/uitpas/getSocialTariffForEvent.ts +2 -2
  25. package/src/services/uitpas/getSocialTariffForUitpasNumbers.ts +2 -2
  26. package/src/services/uitpas/registerTicketSales.ts +2 -2
  27. package/tests/e2e/bundle-discounts.test.ts +415 -391
  28. package/tests/e2e/documents.test.ts +21 -21
  29. package/tests/e2e/register.test.ts +93 -93
  30. package/tests/e2e/stock.test.ts +4 -4
@@ -153,18 +153,18 @@ describe('PaymentReallocationService', () => {
153
153
  describe('swapPayments', () => {
154
154
  it('Equal balance item payments', async () => {
155
155
  const b1 = await createItem({
156
- unitPrice: 30 * 100,
156
+ unitPrice: 30_00,
157
157
  amount: 1,
158
158
  status: BalanceItemStatus.Canceled,
159
- paid: [30 * 100],
160
- priceOpen: -30 * 100, // This adds internal assert
159
+ paid: [30_00],
160
+ priceOpen: -30_00, // This adds internal assert
161
161
  });
162
162
 
163
163
  const b2 = await createItem({
164
- unitPrice: 30 * 100,
164
+ unitPrice: 30_00,
165
165
  amount: 1,
166
166
  paid: [],
167
- priceOpen: 30 * 100, // This adds internal assert
167
+ priceOpen: 30_00, // This adds internal assert
168
168
  });
169
169
 
170
170
  // Now do a swap
@@ -180,24 +180,24 @@ describe('PaymentReallocationService', () => {
180
180
 
181
181
  await expectItem(b2, {
182
182
  priceOpen: 0,
183
- paid: [30 * 100],
183
+ paid: [30_00],
184
184
  });
185
185
  });
186
186
 
187
187
  it('Equal pending balance items', async () => {
188
188
  const b1 = await createItem({
189
- unitPrice: 30 * 100,
189
+ unitPrice: 30_00,
190
190
  amount: 1,
191
191
  status: BalanceItemStatus.Canceled,
192
- pending: [30 * 100],
193
- priceOpen: -30 * 100, // This adds internal assert
192
+ pending: [30_00],
193
+ priceOpen: -30_00, // This adds internal assert
194
194
  });
195
195
 
196
196
  const b2 = await createItem({
197
- unitPrice: 30 * 100,
197
+ unitPrice: 30_00,
198
198
  amount: 1,
199
199
  pending: [],
200
- priceOpen: 30 * 100, // This adds internal assert
200
+ priceOpen: 30_00, // This adds internal assert
201
201
  });
202
202
 
203
203
  // Now do a swap
@@ -213,22 +213,22 @@ describe('PaymentReallocationService', () => {
213
213
 
214
214
  await expectItem(b2, {
215
215
  priceOpen: 0,
216
- pending: [30 * 100],
216
+ pending: [30_00],
217
217
  });
218
218
  });
219
219
 
220
220
  it('Failed payments are not moved', async () => {
221
221
  const b1 = await createItem({
222
- unitPrice: -30 * 100,
222
+ unitPrice: -30_00,
223
223
  amount: 1,
224
- failed: [30 * 100],
225
- priceOpen: -30 * 100, // This adds internal assert
224
+ failed: [30_00],
225
+ priceOpen: -30_00, // This adds internal assert
226
226
  });
227
227
 
228
228
  const b2 = await createItem({
229
- unitPrice: 30 * 100,
229
+ unitPrice: 30_00,
230
230
  amount: 1,
231
- priceOpen: 30 * 100, // This adds internal assert
231
+ priceOpen: 30_00, // This adds internal assert
232
232
  });
233
233
 
234
234
  // Now do a swap
@@ -238,30 +238,30 @@ describe('PaymentReallocationService', () => {
238
238
  );
239
239
 
240
240
  await expectItem(b1, {
241
- priceOpen: -30 * 100,
242
- failed: [30 * 100],
241
+ priceOpen: -30_00,
242
+ failed: [30_00],
243
243
  });
244
244
 
245
245
  await expectItem(b2, {
246
- priceOpen: 30 * 100,
246
+ priceOpen: 30_00,
247
247
  pending: [],
248
248
  });
249
249
  });
250
250
 
251
251
  it('Larger negative balance', async () => {
252
252
  const b1 = await createItem({
253
- unitPrice: 30 * 100,
253
+ unitPrice: 30_00,
254
254
  amount: 1,
255
255
  status: BalanceItemStatus.Canceled,
256
- paid: [30 * 100],
257
- priceOpen: -30 * 100, // This adds internal assert
256
+ paid: [30_00],
257
+ priceOpen: -30_00, // This adds internal assert
258
258
  });
259
259
 
260
260
  const b2 = await createItem({
261
- unitPrice: 15 * 100,
261
+ unitPrice: 15_00,
262
262
  amount: 1,
263
263
  paid: [],
264
- priceOpen: 15 * 100, // This adds internal assert
264
+ priceOpen: 15_00, // This adds internal assert
265
265
  });
266
266
 
267
267
  // Now do a swap
@@ -271,30 +271,30 @@ describe('PaymentReallocationService', () => {
271
271
  );
272
272
 
273
273
  await expectItem(b1, {
274
- priceOpen: -15 * 100,
275
- paid: [15 * 100],
274
+ priceOpen: -15_00,
275
+ paid: [15_00],
276
276
  });
277
277
 
278
278
  await expectItem(b2, {
279
279
  priceOpen: 0,
280
- paid: [15 * 100],
280
+ paid: [15_00],
281
281
  });
282
282
  });
283
283
 
284
284
  it('Larger positive balance', async () => {
285
285
  const b1 = await createItem({
286
- unitPrice: 15 * 100,
286
+ unitPrice: 15_00,
287
287
  amount: 1,
288
288
  status: BalanceItemStatus.Canceled,
289
- paid: [15 * 100],
290
- priceOpen: -15 * 100, // This adds internal assert
289
+ paid: [15_00],
290
+ priceOpen: -15_00, // This adds internal assert
291
291
  });
292
292
 
293
293
  const b2 = await createItem({
294
- unitPrice: 30 * 100,
294
+ unitPrice: 30_00,
295
295
  amount: 1,
296
296
  paid: [],
297
- priceOpen: 30 * 100, // This adds internal assert
297
+ priceOpen: 30_00, // This adds internal assert
298
298
  });
299
299
 
300
300
  // Now do a swap
@@ -309,25 +309,25 @@ describe('PaymentReallocationService', () => {
309
309
  });
310
310
 
311
311
  await expectItem(b2, {
312
- priceOpen: 15 * 100,
313
- paid: [15 * 100],
312
+ priceOpen: 15_00,
313
+ paid: [15_00],
314
314
  });
315
315
  });
316
316
 
317
317
  it('Spits up payments', async () => {
318
318
  const b1 = await createItem({
319
- unitPrice: 15 * 100,
319
+ unitPrice: 15_00,
320
320
  amount: 1,
321
321
  status: BalanceItemStatus.Canceled,
322
- paid: [50 * 100],
323
- priceOpen: -50 * 100, // This adds internal assert
322
+ paid: [50_00],
323
+ priceOpen: -50_00, // This adds internal assert
324
324
  });
325
325
 
326
326
  const b2 = await createItem({
327
- unitPrice: 30 * 100,
327
+ unitPrice: 30_00,
328
328
  amount: 1,
329
329
  paid: [],
330
- priceOpen: 30 * 100, // This adds internal assert
330
+ priceOpen: 30_00, // This adds internal assert
331
331
  });
332
332
 
333
333
  // Now do a swap
@@ -337,30 +337,30 @@ describe('PaymentReallocationService', () => {
337
337
  );
338
338
 
339
339
  await expectItem(b1, {
340
- paid: [20 * 100],
341
- priceOpen: -20 * 100, // This adds internal assert
340
+ paid: [20_00],
341
+ priceOpen: -20_00, // This adds internal assert
342
342
  });
343
343
 
344
344
  await expectItem(b2, {
345
345
  priceOpen: 0,
346
- paid: [30 * 100],
346
+ paid: [30_00],
347
347
  });
348
348
  });
349
349
 
350
350
  it('Moves multiple payments', async () => {
351
351
  const b1 = await createItem({
352
- unitPrice: 30 * 100,
352
+ unitPrice: 30_00,
353
353
  amount: 1,
354
354
  status: BalanceItemStatus.Canceled,
355
- paid: [20 * 100, 5 * 100, 10 * 100],
356
- priceOpen: -35 * 100, // This adds internal assert
355
+ paid: [20_00, 5_00, 10_00],
356
+ priceOpen: -35_00, // This adds internal assert
357
357
  });
358
358
 
359
359
  const b2 = await createItem({
360
- unitPrice: 15 * 100,
360
+ unitPrice: 15_00,
361
361
  amount: 1,
362
362
  paid: [],
363
- priceOpen: 15 * 100, // This adds internal assert
363
+ priceOpen: 15_00, // This adds internal assert
364
364
  });
365
365
 
366
366
  // Now do a swap
@@ -370,29 +370,29 @@ describe('PaymentReallocationService', () => {
370
370
  );
371
371
 
372
372
  await expectItem(b1, {
373
- priceOpen: -20 * 100,
374
- paid: [20 * 100],
373
+ priceOpen: -20_00,
374
+ paid: [20_00],
375
375
  });
376
376
 
377
377
  await expectItem(b2, {
378
378
  priceOpen: 0,
379
- paid: [5 * 100, 10 * 100],
379
+ paid: [5_00, 10_00],
380
380
  });
381
381
  });
382
382
 
383
383
  it('Moves multiple payments in both directions', async () => {
384
384
  const b1 = await createItem({
385
- unitPrice: -30 * 100,
385
+ unitPrice: -30_00,
386
386
  amount: 1,
387
387
  paid: [],
388
- priceOpen: -30 * 100, // This adds internal assert
388
+ priceOpen: -30_00, // This adds internal assert
389
389
  });
390
390
 
391
391
  const b2 = await createItem({
392
- unitPrice: 15 * 100,
392
+ unitPrice: 15_00,
393
393
  amount: 1,
394
- paid: [-20 * 100, -10 * 100],
395
- priceOpen: 45 * 100, // This adds internal assert
394
+ paid: [-20_00, -10_00],
395
+ priceOpen: 45_00, // This adds internal assert
396
396
  });
397
397
 
398
398
  // Now do a swap
@@ -403,11 +403,11 @@ describe('PaymentReallocationService', () => {
403
403
 
404
404
  await expectItem(b1, {
405
405
  priceOpen: 0,
406
- paid: [-20 * 100, -10 * 100],
406
+ paid: [-20_00, -10_00],
407
407
  });
408
408
 
409
409
  await expectItem(b2, {
410
- priceOpen: 15 * 100,
410
+ priceOpen: 15_00,
411
411
  paid: [],
412
412
  });
413
413
  });
@@ -417,11 +417,11 @@ describe('PaymentReallocationService', () => {
417
417
  it('Balances with same relations should move existing payments first', async () => {
418
418
  const memberId = (await getMember()).id;
419
419
  const b1 = await createItem({
420
- unitPrice: 30 * 100,
420
+ unitPrice: 30_00,
421
421
  amount: 1,
422
422
  status: BalanceItemStatus.Canceled,
423
- paid: [30 * 100],
424
- priceOpen: -30 * 100, // This adds internal assert
423
+ paid: [30_00],
424
+ priceOpen: -30_00, // This adds internal assert
425
425
  objectId: memberId,
426
426
  relations: {
427
427
  [BalanceItemRelationType.Group]: 'group1',
@@ -431,10 +431,10 @@ describe('PaymentReallocationService', () => {
431
431
  });
432
432
 
433
433
  const b2 = await createItem({
434
- unitPrice: 30 * 100,
434
+ unitPrice: 30_00,
435
435
  amount: 1,
436
436
  paid: [],
437
- priceOpen: 30 * 100, // This adds internal assert
437
+ priceOpen: 30_00, // This adds internal assert
438
438
  objectId: memberId,
439
439
  relations: {
440
440
  [BalanceItemRelationType.Group]: 'group1',
@@ -457,18 +457,18 @@ describe('PaymentReallocationService', () => {
457
457
 
458
458
  await expectItem(b2, {
459
459
  priceOpen: 0,
460
- paid: [30 * 100],
460
+ paid: [30_00],
461
461
  });
462
462
  });
463
463
 
464
- it.skip('Balances with different relations should create a reallocation payment', async () => {
464
+ it('Balances with different relations should create a reallocation payment', async () => {
465
465
  const memberId = (await getMember()).id;
466
466
  const b1 = await createItem({
467
- unitPrice: 30 * 100,
467
+ unitPrice: 30_00,
468
468
  amount: 1,
469
469
  status: BalanceItemStatus.Canceled,
470
- paid: [30 * 100],
471
- priceOpen: -30 * 100, // This adds internal assert
470
+ paid: [30_00],
471
+ priceOpen: -30_00, // This adds internal assert
472
472
  objectId: memberId,
473
473
  relations: {
474
474
  [BalanceItemRelationType.Group]: 'group1',
@@ -478,10 +478,10 @@ describe('PaymentReallocationService', () => {
478
478
  });
479
479
 
480
480
  const b2 = await createItem({
481
- unitPrice: 30 * 100,
481
+ unitPrice: 30_00,
482
482
  amount: 1,
483
483
  paid: [],
484
- priceOpen: 30 * 100, // This adds internal assert
484
+ priceOpen: 30_00, // This adds internal assert
485
485
  objectId: memberId,
486
486
  relations: {
487
487
  [BalanceItemRelationType.Group]: 'group1',
@@ -499,23 +499,23 @@ describe('PaymentReallocationService', () => {
499
499
  // Check if the balance items are now equal
500
500
  await expectItem(b1, {
501
501
  priceOpen: 0,
502
- paid: [30 * 100, -30 * 100],
502
+ paid: [30_00, -30_00],
503
503
  });
504
504
 
505
505
  await expectItem(b2, {
506
506
  priceOpen: 0,
507
- paid: [30 * 100],
507
+ paid: [30_00],
508
508
  });
509
509
  });
510
510
 
511
- it.skip('Balances with different relations should create a reallocation payment with 3 items', async () => {
511
+ it.skip('Balances with different relations should create a reallocation payment with 3 items and remaining open should prefer most similar item', async () => {
512
512
  const memberId = (await getMember()).id;
513
513
  const b1 = await createItem({
514
- unitPrice: 45 * 100,
514
+ unitPrice: 40_00,
515
515
  amount: 1,
516
516
  status: BalanceItemStatus.Canceled,
517
- paid: [45 * 100],
518
- priceOpen: -45 * 100, // This adds internal assert
517
+ paid: [40_00],
518
+ priceOpen: -40_00, // This adds internal assert
519
519
  objectId: memberId,
520
520
  relations: {
521
521
  [BalanceItemRelationType.Group]: 'group1',
@@ -525,10 +525,18 @@ describe('PaymentReallocationService', () => {
525
525
  });
526
526
 
527
527
  const b2 = await createItem({
528
- unitPrice: 30 * 100,
528
+ unitPrice: 30_00,
529
+ amount: 1,
530
+ paid: [],
531
+ priceOpen: 30_00, // This adds internal assert
532
+ objectId: memberId,
533
+ });
534
+
535
+ const b3 = await createItem({
536
+ unitPrice: 15_00,
529
537
  amount: 1,
530
538
  paid: [],
531
- priceOpen: 30 * 100, // This adds internal assert
539
+ priceOpen: 15_00, // This adds internal assert
532
540
  objectId: memberId,
533
541
  relations: {
534
542
  [BalanceItemRelationType.Group]: 'group1',
@@ -537,11 +545,63 @@ describe('PaymentReallocationService', () => {
537
545
  },
538
546
  });
539
547
 
548
+ await PaymentReallocationService.reallocate(
549
+ (await getOrganization()).id,
550
+ memberId,
551
+ ReceivableBalanceType.member,
552
+ );
553
+
554
+ // Check if the balance items are now equal
555
+ await expectItem(b1, {
556
+ priceOpen: 0,
557
+ paid: [40_00, -40_00],
558
+ });
559
+
560
+ await expectItem(b2, {
561
+ priceOpen: 5_00,
562
+ paid: [25_00],
563
+ });
564
+
565
+ // b3 was more similar to b1 and takes all first
566
+ await expectItem(b3, {
567
+ priceOpen: 0,
568
+ paid: [15_00],
569
+ });
570
+ });
571
+
572
+ it('Balances with same amount should create a reallocation payment over different amounts with more than 2 different relations', async () => {
573
+ const memberId = (await getMember()).id;
574
+ const b1 = await createItem({
575
+ unitPrice: 15_00,
576
+ amount: 1,
577
+ paid: [],
578
+ priceOpen: 15_00, // This adds internal assert
579
+ objectId: memberId,
580
+ relations: {
581
+ [BalanceItemRelationType.Group]: 'group1',
582
+ [BalanceItemRelationType.GroupPrice]: 'defaultprice',
583
+ [BalanceItemRelationType.Member]: 'member1',
584
+ },
585
+ });
586
+
587
+ const b2 = await createItem({
588
+ unitPrice: -30_00,
589
+ amount: 1,
590
+ paid: [],
591
+ priceOpen: -30_00, // This adds internal assert
592
+ objectId: memberId,
593
+ relations: {
594
+ [BalanceItemRelationType.Group]: 'group1',
595
+ [BalanceItemRelationType.GroupPrice]: 'price2', // This one is different
596
+ [BalanceItemRelationType.Member]: 'member2', // This one is different
597
+ },
598
+ });
599
+
540
600
  const b3 = await createItem({
541
- unitPrice: 15 * 100,
601
+ unitPrice: 30_00,
542
602
  amount: 1,
543
603
  paid: [],
544
- priceOpen: 15 * 100, // This adds internal assert
604
+ priceOpen: 30_00, // This adds internal assert
545
605
  objectId: memberId,
546
606
  });
547
607
 
@@ -553,29 +613,28 @@ describe('PaymentReallocationService', () => {
553
613
 
554
614
  // Check if the balance items are now equal
555
615
  await expectItem(b1, {
556
- priceOpen: 0,
557
- paid: [45 * 100, -45 * 100],
616
+ priceOpen: 15_00,
617
+ paid: [],
558
618
  });
559
619
 
560
620
  await expectItem(b2, {
561
621
  priceOpen: 0,
562
- paid: [30 * 100],
622
+ paid: [-30_00],
563
623
  });
564
624
 
565
625
  await expectItem(b3, {
566
626
  priceOpen: 0,
567
- paid: [15 * 100],
627
+ paid: [30_00],
568
628
  });
569
629
  });
570
630
 
571
- it.skip('Balances with different relations should create a reallocation payment with 3 items and remaining open should prefer most similar item', async () => {
631
+ it('Balances with same relations are prioritized over same amount', async () => {
572
632
  const memberId = (await getMember()).id;
573
633
  const b1 = await createItem({
574
- unitPrice: 40 * 100,
634
+ unitPrice: 30_00,
575
635
  amount: 1,
576
- status: BalanceItemStatus.Canceled,
577
- paid: [40 * 100],
578
- priceOpen: -40 * 100, // This adds internal assert
636
+ paid: [],
637
+ priceOpen: 30_00, // This adds internal assert
579
638
  objectId: memberId,
580
639
  relations: {
581
640
  [BalanceItemRelationType.Group]: 'group1',
@@ -585,26 +644,85 @@ describe('PaymentReallocationService', () => {
585
644
  });
586
645
 
587
646
  const b2 = await createItem({
588
- unitPrice: 30 * 100,
647
+ unitPrice: -30_00,
648
+ amount: 1,
649
+ paid: [],
650
+ priceOpen: -30_00, // This adds internal assert
651
+ objectId: memberId,
652
+ relations: {
653
+ [BalanceItemRelationType.Group]: 'group1',
654
+ [BalanceItemRelationType.GroupPrice]: 'defaultprice',
655
+ [BalanceItemRelationType.Member]: 'member1',
656
+ },
657
+ });
658
+
659
+ const b3 = await createItem({
660
+ unitPrice: 30_00,
589
661
  amount: 1,
590
662
  paid: [],
591
- priceOpen: 30 * 100, // This adds internal assert
663
+ priceOpen: 30_00, // This adds internal assert
592
664
  objectId: memberId,
593
665
  });
594
666
 
667
+ await PaymentReallocationService.reallocate(
668
+ (await getOrganization()).id,
669
+ memberId,
670
+ ReceivableBalanceType.member,
671
+ );
672
+
673
+ // Check if the balance items are now equal
674
+ await expectItem(b1, {
675
+ priceOpen: 0,
676
+ paid: [30_00],
677
+ });
678
+
679
+ await expectItem(b2, {
680
+ priceOpen: 0,
681
+ paid: [-30_00],
682
+ });
683
+
684
+ await expectItem(b3, {
685
+ priceOpen: 30_00,
686
+ paid: [],
687
+ });
688
+ });
689
+
690
+ it('Balances with one different relation are prioritized over same amount', async () => {
691
+ const memberId = (await getMember()).id;
692
+
595
693
  const b3 = await createItem({
596
- unitPrice: 15 * 100,
694
+ unitPrice: 30_00,
597
695
  amount: 1,
598
696
  paid: [],
599
- priceOpen: 15 * 100, // This adds internal assert
697
+ priceOpen: 30_00, // This adds internal assert
698
+ objectId: memberId,
699
+ });
700
+
701
+ const b1 = await createItem({
702
+ unitPrice: 30_00,
703
+ amount: 1,
704
+ paid: [],
705
+ priceOpen: 30_00, // This adds internal assert
600
706
  objectId: memberId,
601
707
  relations: {
602
708
  [BalanceItemRelationType.Group]: 'group1',
603
- [BalanceItemRelationType.GroupPrice]: 'price2', // This one is different
709
+ [BalanceItemRelationType.GroupPrice]: 'defaultprice',
604
710
  [BalanceItemRelationType.Member]: 'member1',
605
711
  },
606
712
  });
607
713
 
714
+ const b2 = await createItem({
715
+ unitPrice: -30_00,
716
+ amount: 1,
717
+ paid: [],
718
+ priceOpen: -30_00, // This adds internal assert
719
+ objectId: memberId,
720
+ relations: {
721
+ [BalanceItemRelationType.Group]: 'group1',
722
+ [BalanceItemRelationType.GroupPrice]: 'price2', // This one is different
723
+ [BalanceItemRelationType.Member]: 'member1',
724
+ },
725
+ });
608
726
  await PaymentReallocationService.reallocate(
609
727
  (await getOrganization()).id,
610
728
  memberId,
@@ -614,32 +732,84 @@ describe('PaymentReallocationService', () => {
614
732
  // Check if the balance items are now equal
615
733
  await expectItem(b1, {
616
734
  priceOpen: 0,
617
- paid: [40 * 100, -40 * 100],
735
+ paid: [30_00],
618
736
  });
619
737
 
620
738
  await expectItem(b2, {
621
- priceOpen: 5 * 100,
622
- paid: [25 * 100],
739
+ priceOpen: 0,
740
+ paid: [-30_00],
741
+ });
742
+
743
+ await expectItem(b3, {
744
+ priceOpen: 30_00,
745
+ paid: [],
746
+ });
747
+ });
748
+
749
+
750
+ it('Balances with different amount should not create a reallocation payment', async () => {
751
+ const memberId = (await getMember()).id;
752
+ const b1 = await createItem({
753
+ unitPrice: 40_00,
754
+ amount: 1,
755
+ status: BalanceItemStatus.Canceled,
756
+ paid: [40_00],
757
+ priceOpen: -40_00, // This adds internal assert
758
+ objectId: memberId,
759
+ relations: {
760
+ [BalanceItemRelationType.Group]: 'group1',
761
+ [BalanceItemRelationType.GroupPrice]: 'defaultprice',
762
+ [BalanceItemRelationType.Member]: 'member1',
763
+ },
764
+ });
765
+
766
+ const b2 = await createItem({
767
+ unitPrice: 30_00,
768
+ amount: 1,
769
+ paid: [],
770
+ priceOpen: 30_00, // This adds internal assert
771
+ objectId: memberId,
772
+ });
773
+
774
+ const b3 = await createItem({
775
+ unitPrice: 15_00,
776
+ amount: 1,
777
+ paid: [],
778
+ priceOpen: 15_00, // This adds internal assert
779
+ objectId: memberId,
780
+ });
781
+
782
+ await PaymentReallocationService.reallocate(
783
+ (await getOrganization()).id,
784
+ memberId,
785
+ ReceivableBalanceType.member,
786
+ );
787
+
788
+ // Check if the balance items are now equal
789
+ await expectItem(b1, {
790
+ priceOpen: -40_00,
791
+ paid: [40_00],
792
+ });
793
+
794
+ await expectItem(b2, {
795
+ priceOpen: 30_00,
796
+ paid: [],
623
797
  });
624
798
 
625
- // b3 was more similar to b1 and takes all first
626
799
  await expectItem(b3, {
627
- priceOpen: 0 * 100,
628
- paid: [15 * 100],
800
+ priceOpen: 15_00,
801
+ paid: [],
629
802
  });
630
803
  });
631
804
 
632
- /**
633
- * Note: if this one fails randomly, it might because it isn't working stable enough and doesn't fulfil the requirements
634
- */
635
- it.skip('Balances with different relations should create a reallocation payment with 3 items and remaining open should prefer largest amount', async () => {
805
+ it('Balances with different amount should only create a reallocation payment if total sum is zero', async () => {
636
806
  const memberId = (await getMember()).id;
637
807
  const b1 = await createItem({
638
- unitPrice: 40 * 100,
808
+ unitPrice: 40_00,
639
809
  amount: 1,
640
810
  status: BalanceItemStatus.Canceled,
641
- paid: [40 * 100],
642
- priceOpen: -40 * 100, // This adds internal assert
811
+ paid: [40_00],
812
+ priceOpen: -40_00, // This adds internal assert
643
813
  objectId: memberId,
644
814
  relations: {
645
815
  [BalanceItemRelationType.Group]: 'group1',
@@ -649,18 +819,18 @@ describe('PaymentReallocationService', () => {
649
819
  });
650
820
 
651
821
  const b2 = await createItem({
652
- unitPrice: 30 * 100,
822
+ unitPrice: 30_00,
653
823
  amount: 1,
654
824
  paid: [],
655
- priceOpen: 30 * 100, // This adds internal assert
825
+ priceOpen: 30_00, // This adds internal assert
656
826
  objectId: memberId,
657
827
  });
658
828
 
659
829
  const b3 = await createItem({
660
- unitPrice: 15 * 100,
830
+ unitPrice: 10_00,
661
831
  amount: 1,
662
832
  paid: [],
663
- priceOpen: 15 * 100, // This adds internal assert
833
+ priceOpen: 10_00, // This adds internal assert
664
834
  objectId: memberId,
665
835
  });
666
836
 
@@ -673,28 +843,28 @@ describe('PaymentReallocationService', () => {
673
843
  // Check if the balance items are now equal
674
844
  await expectItem(b1, {
675
845
  priceOpen: 0,
676
- paid: [40 * 100, -40 * 100],
846
+ paid: [40_00, -40_00],
677
847
  });
678
848
 
679
849
  await expectItem(b2, {
680
- priceOpen: 0 * 100,
681
- paid: [30 * 100],
850
+ priceOpen: 0,
851
+ paid: [30_00],
682
852
  });
683
853
 
684
854
  await expectItem(b3, {
685
- priceOpen: 5 * 100,
686
- paid: [10 * 100],
855
+ priceOpen: 0,
856
+ paid: [10_00],
687
857
  });
688
858
  });
689
859
 
690
860
  it('Balances due in the future should not be reallocated', async () => {
691
861
  const memberId = (await getMember()).id;
692
862
  const b1 = await createItem({
693
- unitPrice: 40 * 100,
863
+ unitPrice: 40_00,
694
864
  amount: 1,
695
865
  status: BalanceItemStatus.Canceled,
696
- paid: [40 * 100],
697
- priceOpen: -40 * 100, // This adds internal assert
866
+ paid: [40_00],
867
+ priceOpen: -40_00, // This adds internal assert
698
868
  objectId: memberId,
699
869
  relations: {
700
870
  [BalanceItemRelationType.Group]: 'group1',
@@ -704,10 +874,10 @@ describe('PaymentReallocationService', () => {
704
874
  });
705
875
 
706
876
  const b2 = await createItem({
707
- unitPrice: 30 * 100,
877
+ unitPrice: 30_00,
708
878
  amount: 1,
709
879
  paid: [],
710
- priceOpen: 30 * 100, // This adds internal assert
880
+ priceOpen: 30_00, // This adds internal assert
711
881
  objectId: memberId,
712
882
  // This is due later, so it should be the last one to be paid
713
883
  dueAt: new Date('2050-01-01'),
@@ -719,10 +889,10 @@ describe('PaymentReallocationService', () => {
719
889
  });
720
890
 
721
891
  const b3 = await createItem({
722
- unitPrice: 15 * 100,
892
+ unitPrice: 15_00,
723
893
  amount: 1,
724
894
  paid: [],
725
- priceOpen: 15 * 100, // This adds internal assert
895
+ priceOpen: 15_00, // This adds internal assert
726
896
  objectId: memberId,
727
897
  relations: {
728
898
  [BalanceItemRelationType.Group]: 'group1',
@@ -739,18 +909,18 @@ describe('PaymentReallocationService', () => {
739
909
 
740
910
  // Check if the balance items are now equal
741
911
  await expectItem(b1, {
742
- priceOpen: -25_00, // Still paid 25 too much
743
- paid: [25_00],
912
+ priceOpen: 0,
913
+ paid: [],
744
914
  });
745
915
 
746
916
  await expectItem(b2, {
747
- priceOpen: 30 * 100,
917
+ priceOpen: 30_00,
748
918
  paid: [],
749
919
  });
750
920
 
751
921
  await expectItem(b3, {
752
- priceOpen: 0 * 100, // Paid with canceled balance item that was already paid and should be refunded
753
- paid: [15 * 100],
922
+ priceOpen: -25_00, // Paid too much
923
+ paid: [40_00],
754
924
  });
755
925
  });
756
926
  });