@stamhoofd/models 2.140.0 → 2.142.0

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 (76) hide show
  1. package/dist/migrations/1785854600-settlements-table.sql +27 -0
  2. package/dist/migrations/1785854601-payment-settlements-table.sql +15 -0
  3. package/dist/migrations/1785854602-settlement-charges-table.sql +31 -0
  4. package/dist/migrations/1786443124-application-fees-table.sql +33 -0
  5. package/dist/migrations/1786443125-settlement-charges-drop-balance-item-id.sql +4 -0
  6. package/dist/migrations/1786443126-settlements-pending-fees.sql +2 -0
  7. package/dist/migrations/1786443128-settlement-charges-drop-orphaned.sql +1 -0
  8. package/dist/migrations/1786443129-settlement-charges-drop-organization-id-fk.sql +2 -0
  9. package/dist/migrations/1786443130-settlement-charges-organization-id-required.sql +1 -0
  10. package/dist/migrations/1786443131-settlement-charges-organization-id-fk.sql +1 -0
  11. package/dist/migrations/1786513670-payment-settlements-external-id-nullable.sql +1 -0
  12. package/dist/migrations/1786516131-payment-settlements-truncate.sql +3 -0
  13. package/dist/migrations/1786516132-payment-settlements-organization-id.sql +1 -0
  14. package/dist/migrations/1786516133-payment-settlements-organization-id-fk.sql +1 -0
  15. package/dist/migrations/1786534200-application-fees-drop-paying-organization-fk.sql +3 -0
  16. package/dist/migrations/1786534201-application-fees-drop-settlement-charge-fk.sql +1 -0
  17. package/dist/migrations/1786534202-application-fees-paying-organization-nullable.sql +1 -0
  18. package/dist/migrations/1786534203-application-fees-settlement-charge-nullable.sql +1 -0
  19. package/dist/migrations/1786534204-application-fees-paying-organization-fk.sql +1 -0
  20. package/dist/migrations/1786534205-application-fees-settlement-charge-fk.sql +3 -0
  21. package/dist/migrations/1786534206-settlements-uncollectible-fees.sql +1 -0
  22. package/dist/models/ApplicationFee.d.ts +67 -0
  23. package/dist/models/ApplicationFee.d.ts.map +1 -0
  24. package/dist/models/ApplicationFee.js +131 -0
  25. package/dist/models/ApplicationFee.js.map +1 -0
  26. package/dist/models/Payment.d.ts +15 -3
  27. package/dist/models/Payment.d.ts.map +1 -1
  28. package/dist/models/Payment.js +46 -4
  29. package/dist/models/Payment.js.map +1 -1
  30. package/dist/models/PaymentSettlement.d.ts +32 -0
  31. package/dist/models/PaymentSettlement.d.ts.map +1 -0
  32. package/dist/models/PaymentSettlement.js +82 -0
  33. package/dist/models/PaymentSettlement.js.map +1 -0
  34. package/dist/models/Settlement.d.ts +60 -0
  35. package/dist/models/Settlement.d.ts.map +1 -0
  36. package/dist/models/Settlement.js +136 -0
  37. package/dist/models/Settlement.js.map +1 -0
  38. package/dist/models/SettlementCharge.d.ts +51 -0
  39. package/dist/models/SettlementCharge.d.ts.map +1 -0
  40. package/dist/models/SettlementCharge.js +115 -0
  41. package/dist/models/SettlementCharge.js.map +1 -0
  42. package/dist/models/StripeAccount.d.ts +1 -1
  43. package/dist/models/StripeAccount.d.ts.map +1 -1
  44. package/dist/models/StripeAccount.js.map +1 -1
  45. package/dist/models/User.d.ts.map +1 -1
  46. package/dist/models/User.js +14 -0
  47. package/dist/models/User.js.map +1 -1
  48. package/package.json +3 -3
  49. package/src/migrations/1785854600-settlements-table.sql +27 -0
  50. package/src/migrations/1785854601-payment-settlements-table.sql +15 -0
  51. package/src/migrations/1785854602-settlement-charges-table.sql +31 -0
  52. package/src/migrations/1786443124-application-fees-table.sql +33 -0
  53. package/src/migrations/1786443125-settlement-charges-drop-balance-item-id.sql +4 -0
  54. package/src/migrations/1786443126-settlements-pending-fees.sql +2 -0
  55. package/src/migrations/1786443128-settlement-charges-drop-orphaned.sql +1 -0
  56. package/src/migrations/1786443129-settlement-charges-drop-organization-id-fk.sql +2 -0
  57. package/src/migrations/1786443130-settlement-charges-organization-id-required.sql +1 -0
  58. package/src/migrations/1786443131-settlement-charges-organization-id-fk.sql +1 -0
  59. package/src/migrations/1786513670-payment-settlements-external-id-nullable.sql +1 -0
  60. package/src/migrations/1786516131-payment-settlements-truncate.sql +3 -0
  61. package/src/migrations/1786516132-payment-settlements-organization-id.sql +1 -0
  62. package/src/migrations/1786516133-payment-settlements-organization-id-fk.sql +1 -0
  63. package/src/migrations/1786534200-application-fees-drop-paying-organization-fk.sql +3 -0
  64. package/src/migrations/1786534201-application-fees-drop-settlement-charge-fk.sql +1 -0
  65. package/src/migrations/1786534202-application-fees-paying-organization-nullable.sql +1 -0
  66. package/src/migrations/1786534203-application-fees-settlement-charge-nullable.sql +1 -0
  67. package/src/migrations/1786534204-application-fees-paying-organization-fk.sql +1 -0
  68. package/src/migrations/1786534205-application-fees-settlement-charge-fk.sql +3 -0
  69. package/src/migrations/1786534206-settlements-uncollectible-fees.sql +1 -0
  70. package/src/models/ApplicationFee.ts +117 -0
  71. package/src/models/Payment.ts +55 -5
  72. package/src/models/PaymentSettlement.ts +72 -0
  73. package/src/models/Settlement.ts +118 -0
  74. package/src/models/SettlementCharge.ts +101 -0
  75. package/src/models/StripeAccount.ts +1 -1
  76. package/src/models/User.ts +26 -0
@@ -0,0 +1,101 @@
1
+ import { column } from '@simonbackx/simple-database';
2
+ import { QueryableModel } from '@stamhoofd/sql';
3
+ import type { SettlementChargeType } from '@stamhoofd/structures/settlements/SettlementChargeType.js';
4
+ import { v4 as uuidv4 } from 'uuid';
5
+
6
+ /**
7
+ * Every charge in a settlement that is not a payment: application fee deductions on the payer's
8
+ * payout, provider fees, VAT, transfers, reserves and disputes.
9
+ */
10
+ export class SettlementCharge extends QueryableModel {
11
+ static table = 'settlement_charges';
12
+
13
+ @column({
14
+ primary: true, type: 'string', beforeSave(value) {
15
+ return value ?? uuidv4();
16
+ },
17
+ })
18
+ id!: string;
19
+
20
+ @column({ type: 'string' })
21
+ type: SettlementChargeType;
22
+
23
+ /**
24
+ * Globally unique, deterministic id so every sync can upsert. Fee rows use `<feeId>:<type>`,
25
+ * other rows the provider's transaction id (txn_…, txn_…:fee:<i>, fr_…).
26
+ */
27
+ @column({ type: 'string' })
28
+ externalId: string;
29
+
30
+ /**
31
+ * Signed effect on the `settlementId` payout.
32
+ */
33
+ @column({ type: 'integer' })
34
+ amount = 0;
35
+
36
+ /**
37
+ * The payout containing this charge. NULL until that payout is synced.
38
+ */
39
+ @column({ type: 'string', nullable: true })
40
+ settlementId: string | null = null;
41
+
42
+ /**
43
+ * Stripe fee_… id: links the deduction rows on the organization payout to the application_fees
44
+ * rows of the same application fee.
45
+ */
46
+ @column({ type: 'string', nullable: true })
47
+ applicationFeeId: string | null = null;
48
+
49
+ @column({ type: 'string', nullable: true })
50
+ paymentId: string | null = null;
51
+
52
+ /**
53
+ * The organization that was charged. Always the organization of the payout it is deducted
54
+ * from, and of the payment it relates to: a charge never crosses organizations.
55
+ */
56
+ @column({ type: 'string' })
57
+ organizationId: string;
58
+
59
+ @column({ type: 'string', nullable: true })
60
+ stripeAccountId: string | null = null;
61
+
62
+ /**
63
+ * Which invoice bills this fee to the charged party: Mollie's real invoice id (inv_…), the
64
+ * derived `stripe-YYYY-MM` for Stripe provider fees, or the Stamhoofd invoice number for
65
+ * application fee deductions.
66
+ */
67
+ @column({ type: 'string', nullable: true })
68
+ providerInvoiceId: string | null = null;
69
+
70
+ @column({ type: 'string' })
71
+ description = '';
72
+
73
+ /**
74
+ * When the provider created the charge. For application fees this drives the monthly
75
+ * invoice grouping.
76
+ */
77
+ @column({ type: 'datetime' })
78
+ occurredAt: Date;
79
+
80
+ @column({
81
+ type: 'datetime', beforeSave(old?: any) {
82
+ if (old !== undefined) {
83
+ return old;
84
+ }
85
+ const date = new Date();
86
+ date.setMilliseconds(0);
87
+ return date;
88
+ },
89
+ })
90
+ createdAt: Date;
91
+
92
+ @column({
93
+ type: 'datetime', beforeSave() {
94
+ const date = new Date();
95
+ date.setMilliseconds(0);
96
+ return date;
97
+ },
98
+ skipUpdate: true,
99
+ })
100
+ updatedAt: Date;
101
+ }
@@ -27,7 +27,7 @@ export class StripeAccount extends QueryableModel {
27
27
  meta = StripeMetaData.create({});
28
28
 
29
29
  @column({ type: 'string' })
30
- status: 'active' | 'deleted' = 'active';
30
+ status: 'active' | 'deleted' | 'inaccessible' = 'active';
31
31
 
32
32
  @column({
33
33
  type: 'datetime', beforeSave(old?: any) {
@@ -191,6 +191,32 @@ export class User extends QueryableModel {
191
191
  // Hide api accounts
192
192
  global = global.filter(a => !a.isApiUser);
193
193
 
194
+ {
195
+ // Also load global admins with explicit permissions set
196
+ const q = User.select()
197
+ .where('organizationId', null)
198
+ .where('permissions', '!=', null)
199
+ .where(
200
+ SQL.jsonOverlaps(
201
+ SQL.jsonKeys(SQL.jsonExtract(SQL.column('permissions'), '$.value.organizationPermissions')),
202
+ SQL.cast(SQL.scalar(JSON.stringify(Array.isArray(organizationId) ? organizationId : [organizationId])), 'JSON'),
203
+ ),
204
+ '=',
205
+ 1,
206
+ )
207
+ ;
208
+
209
+ if (options?.verified !== undefined) {
210
+ q.where('verified', options.verified);
211
+ }
212
+
213
+ let xtra = await q.fetch();
214
+ // Hide api accounts
215
+ xtra = xtra.filter(a => !a.isApiUser);
216
+
217
+ global.push(...xtra);
218
+ }
219
+
194
220
  return global;
195
221
  }
196
222