@stamhoofd/models 2.139.0 → 2.141.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.
- package/dist/migrations/1785854600-settlements-table.sql +27 -0
- package/dist/migrations/1785854601-payment-settlements-table.sql +15 -0
- package/dist/migrations/1785854602-settlement-charges-table.sql +31 -0
- package/dist/migrations/1786443124-application-fees-table.sql +33 -0
- package/dist/migrations/1786443125-settlement-charges-drop-balance-item-id.sql +4 -0
- package/dist/migrations/1786443126-settlements-pending-fees.sql +2 -0
- package/dist/migrations/1786443128-settlement-charges-drop-orphaned.sql +1 -0
- package/dist/migrations/1786443129-settlement-charges-drop-organization-id-fk.sql +2 -0
- package/dist/migrations/1786443130-settlement-charges-organization-id-required.sql +1 -0
- package/dist/migrations/1786443131-settlement-charges-organization-id-fk.sql +1 -0
- package/dist/migrations/1786513670-payment-settlements-external-id-nullable.sql +1 -0
- package/dist/migrations/1786516131-payment-settlements-truncate.sql +3 -0
- package/dist/migrations/1786516132-payment-settlements-organization-id.sql +1 -0
- package/dist/migrations/1786516133-payment-settlements-organization-id-fk.sql +1 -0
- package/dist/models/ApplicationFee.d.ts +64 -0
- package/dist/models/ApplicationFee.d.ts.map +1 -0
- package/dist/models/ApplicationFee.js +128 -0
- package/dist/models/ApplicationFee.js.map +1 -0
- package/dist/models/Payment.d.ts +15 -3
- package/dist/models/Payment.d.ts.map +1 -1
- package/dist/models/Payment.js +46 -4
- package/dist/models/Payment.js.map +1 -1
- package/dist/models/PaymentSettlement.d.ts +32 -0
- package/dist/models/PaymentSettlement.d.ts.map +1 -0
- package/dist/models/PaymentSettlement.js +82 -0
- package/dist/models/PaymentSettlement.js.map +1 -0
- package/dist/models/Settlement.d.ts +52 -0
- package/dist/models/Settlement.d.ts.map +1 -0
- package/dist/models/Settlement.js +125 -0
- package/dist/models/Settlement.js.map +1 -0
- package/dist/models/SettlementCharge.d.ts +51 -0
- package/dist/models/SettlementCharge.d.ts.map +1 -0
- package/dist/models/SettlementCharge.js +115 -0
- package/dist/models/SettlementCharge.js.map +1 -0
- package/dist/models/User.d.ts.map +1 -1
- package/dist/models/User.js +14 -0
- package/dist/models/User.js.map +1 -1
- package/package.json +3 -3
- package/src/migrations/1785854600-settlements-table.sql +27 -0
- package/src/migrations/1785854601-payment-settlements-table.sql +15 -0
- package/src/migrations/1785854602-settlement-charges-table.sql +31 -0
- package/src/migrations/1786443124-application-fees-table.sql +33 -0
- package/src/migrations/1786443125-settlement-charges-drop-balance-item-id.sql +4 -0
- package/src/migrations/1786443126-settlements-pending-fees.sql +2 -0
- package/src/migrations/1786443128-settlement-charges-drop-orphaned.sql +1 -0
- package/src/migrations/1786443129-settlement-charges-drop-organization-id-fk.sql +2 -0
- package/src/migrations/1786443130-settlement-charges-organization-id-required.sql +1 -0
- package/src/migrations/1786443131-settlement-charges-organization-id-fk.sql +1 -0
- package/src/migrations/1786513670-payment-settlements-external-id-nullable.sql +1 -0
- package/src/migrations/1786516131-payment-settlements-truncate.sql +3 -0
- package/src/migrations/1786516132-payment-settlements-organization-id.sql +1 -0
- package/src/migrations/1786516133-payment-settlements-organization-id-fk.sql +1 -0
- package/src/models/ApplicationFee.ts +114 -0
- package/src/models/Payment.ts +55 -5
- package/src/models/PaymentSettlement.ts +72 -0
- package/src/models/Settlement.ts +108 -0
- package/src/models/SettlementCharge.ts +101 -0
- package/src/models/User.ts +26 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
CREATE TABLE `settlement_charges` (
|
|
2
|
+
`id` varchar(36) NOT NULL,
|
|
3
|
+
`type` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
4
|
+
`externalId` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
5
|
+
`amount` bigint NOT NULL DEFAULT '0',
|
|
6
|
+
`settlementId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
7
|
+
`applicationFeeId` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
8
|
+
`paymentId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
9
|
+
`balanceItemId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
10
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
11
|
+
`stripeAccountId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
12
|
+
`providerInvoiceId` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
13
|
+
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
14
|
+
`occurredAt` datetime NOT NULL,
|
|
15
|
+
`createdAt` datetime NOT NULL,
|
|
16
|
+
`updatedAt` datetime NOT NULL,
|
|
17
|
+
PRIMARY KEY (`id`),
|
|
18
|
+
UNIQUE KEY `externalId` (`externalId`),
|
|
19
|
+
KEY `settlementId` (`settlementId`),
|
|
20
|
+
KEY `applicationFeeId` (`applicationFeeId`),
|
|
21
|
+
KEY `paymentId` (`paymentId`),
|
|
22
|
+
KEY `balanceItemId` (`balanceItemId`),
|
|
23
|
+
KEY `organizationId` (`organizationId`),
|
|
24
|
+
KEY `stripeAccountId` (`stripeAccountId`),
|
|
25
|
+
KEY `providerInvoiceId` (`providerInvoiceId`),
|
|
26
|
+
CONSTRAINT `settlement_charges_ibfk_1` FOREIGN KEY (`settlementId`) REFERENCES `settlements` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
27
|
+
CONSTRAINT `settlement_charges_ibfk_2` FOREIGN KEY (`paymentId`) REFERENCES `payments` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
28
|
+
CONSTRAINT `settlement_charges_ibfk_3` FOREIGN KEY (`balanceItemId`) REFERENCES `balance_items` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
29
|
+
CONSTRAINT `settlement_charges_ibfk_4` FOREIGN KEY (`organizationId`) REFERENCES `organizations` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
30
|
+
CONSTRAINT `settlement_charges_ibfk_5` FOREIGN KEY (`stripeAccountId`) REFERENCES `stripe_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
|
|
31
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
CREATE TABLE `application_fees` (
|
|
2
|
+
`id` varchar(36) NOT NULL,
|
|
3
|
+
`externalId` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
4
|
+
`type` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
5
|
+
`amount` bigint NOT NULL DEFAULT '0',
|
|
6
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
7
|
+
`payingOrganizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
8
|
+
`payingStripeAccountId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
9
|
+
`payingPaymentId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
10
|
+
`settlementChargeId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
11
|
+
`settlementId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
12
|
+
`balanceItemId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
|
13
|
+
`occurredAt` datetime NOT NULL,
|
|
14
|
+
`createdAt` datetime NOT NULL,
|
|
15
|
+
`updatedAt` datetime NOT NULL,
|
|
16
|
+
PRIMARY KEY (`id`),
|
|
17
|
+
UNIQUE KEY `externalIdType` (`externalId`,`type`),
|
|
18
|
+
UNIQUE KEY `settlementChargeId` (`settlementChargeId`),
|
|
19
|
+
KEY `organizationId` (`organizationId`),
|
|
20
|
+
KEY `payingOrganizationId` (`payingOrganizationId`),
|
|
21
|
+
KEY `payingStripeAccountId` (`payingStripeAccountId`),
|
|
22
|
+
KEY `payingPaymentId` (`payingPaymentId`),
|
|
23
|
+
KEY `settlementId` (`settlementId`),
|
|
24
|
+
KEY `balanceItemId` (`balanceItemId`),
|
|
25
|
+
KEY `occurredAt` (`occurredAt`),
|
|
26
|
+
CONSTRAINT `application_fees_ibfk_1` FOREIGN KEY (`organizationId`) REFERENCES `organizations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
27
|
+
CONSTRAINT `application_fees_ibfk_2` FOREIGN KEY (`payingOrganizationId`) REFERENCES `organizations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
28
|
+
CONSTRAINT `application_fees_ibfk_3` FOREIGN KEY (`payingStripeAccountId`) REFERENCES `stripe_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
29
|
+
CONSTRAINT `application_fees_ibfk_4` FOREIGN KEY (`payingPaymentId`) REFERENCES `payments` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
30
|
+
CONSTRAINT `application_fees_ibfk_5` FOREIGN KEY (`settlementChargeId`) REFERENCES `settlement_charges` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
|
|
31
|
+
CONSTRAINT `application_fees_ibfk_6` FOREIGN KEY (`settlementId`) REFERENCES `settlements` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
32
|
+
CONSTRAINT `application_fees_ibfk_7` FOREIGN KEY (`balanceItemId`) REFERENCES `balance_items` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
33
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
DELETE FROM `settlement_charges` WHERE `organizationId` IS NULL;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ALTER TABLE `settlement_charges` MODIFY COLUMN `organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ALTER TABLE `settlement_charges` ADD CONSTRAINT `settlement_charges_ibfk_4` FOREIGN KEY (`organizationId`) REFERENCES `organizations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ALTER TABLE `payment_settlements` MODIFY COLUMN `externalId` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ALTER TABLE `payment_settlements` ADD COLUMN `organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL AFTER `paymentId`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ALTER TABLE `payment_settlements` ADD CONSTRAINT `payment_settlements_ibfk_3` FOREIGN KEY (`organizationId`) REFERENCES `organizations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { column } from '@simonbackx/simple-database';
|
|
2
|
+
import { QueryableModel } from '@stamhoofd/sql';
|
|
3
|
+
import type { ApplicationFeeType } from '@stamhoofd/structures/settlements/ApplicationFeeType.js';
|
|
4
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One component (service or transfer part) of an application fee the platform charged on a payment:
|
|
8
|
+
* income for the receiving organization, a cost for the paying organization. The paying side is a
|
|
9
|
+
* settlement_charges row (settlementChargeId); the receiving side is billed via a balance item
|
|
10
|
+
* (balanceItemId) and received in a payout (settlementId).
|
|
11
|
+
*
|
|
12
|
+
* Every row belongs to the receiving organization, like the balance item it creates.
|
|
13
|
+
*/
|
|
14
|
+
export class ApplicationFee extends QueryableModel {
|
|
15
|
+
static table = 'application_fees';
|
|
16
|
+
|
|
17
|
+
@column({
|
|
18
|
+
primary: true, type: 'string', beforeSave(value) {
|
|
19
|
+
return value ?? uuidv4();
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
id!: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The provider's id of the application fee (Stripe fee_… id). One fee splits into at most one
|
|
26
|
+
* row per type, so (externalId, type) is unique.
|
|
27
|
+
*/
|
|
28
|
+
@column({ type: 'string' })
|
|
29
|
+
externalId: string;
|
|
30
|
+
|
|
31
|
+
@column({ type: 'string' })
|
|
32
|
+
type: ApplicationFeeType;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* What the receiving organization gets for this component (positive, 1/10000 units).
|
|
36
|
+
*/
|
|
37
|
+
@column({ type: 'integer' })
|
|
38
|
+
amount = 0;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The receiving organization: the platform organization that charged this fee, and the owner of
|
|
42
|
+
* the balance item billing it.
|
|
43
|
+
*/
|
|
44
|
+
@column({ type: 'string' })
|
|
45
|
+
organizationId: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The paying organization: the connected organization whose payout the fee was deducted from.
|
|
49
|
+
*/
|
|
50
|
+
@column({ type: 'string' })
|
|
51
|
+
payingOrganizationId: string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The Stripe account of the paying organization the fee was deducted from. An organization can
|
|
55
|
+
* have multiple, and fees are billed per account.
|
|
56
|
+
*/
|
|
57
|
+
@column({ type: 'string', nullable: true })
|
|
58
|
+
payingStripeAccountId: string | null = null;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The paying organization's customer payment the fee was charged on.
|
|
62
|
+
*/
|
|
63
|
+
@column({ type: 'string', nullable: true })
|
|
64
|
+
payingPaymentId: string | null = null;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The deduction charge on the paying organization's payout
|
|
68
|
+
* (ApplicationFeeService/ApplicationFeeTransfer, same amount but negative).
|
|
69
|
+
*/
|
|
70
|
+
@column({ type: 'string' })
|
|
71
|
+
settlementChargeId: string;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The payout in which the receiving organization was paid this fee. NULL until that payout is
|
|
75
|
+
* synced.
|
|
76
|
+
*/
|
|
77
|
+
@column({ type: 'string', nullable: true })
|
|
78
|
+
settlementId: string | null = null;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The balance item billing this fee to the paying organization. NULL means not yet invoiced:
|
|
82
|
+
* the invoicer will pick it up.
|
|
83
|
+
*/
|
|
84
|
+
@column({ type: 'string', nullable: true })
|
|
85
|
+
balanceItemId: string | null = null;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* When the provider created the fee. Drives the monthly invoice grouping.
|
|
89
|
+
*/
|
|
90
|
+
@column({ type: 'datetime' })
|
|
91
|
+
occurredAt: Date;
|
|
92
|
+
|
|
93
|
+
@column({
|
|
94
|
+
type: 'datetime', beforeSave(old?: any) {
|
|
95
|
+
if (old !== undefined) {
|
|
96
|
+
return old;
|
|
97
|
+
}
|
|
98
|
+
const date = new Date();
|
|
99
|
+
date.setMilliseconds(0);
|
|
100
|
+
return date;
|
|
101
|
+
},
|
|
102
|
+
})
|
|
103
|
+
createdAt: Date;
|
|
104
|
+
|
|
105
|
+
@column({
|
|
106
|
+
type: 'datetime', beforeSave() {
|
|
107
|
+
const date = new Date();
|
|
108
|
+
date.setMilliseconds(0);
|
|
109
|
+
return date;
|
|
110
|
+
},
|
|
111
|
+
skipUpdate: true,
|
|
112
|
+
})
|
|
113
|
+
updatedAt: Date;
|
|
114
|
+
}
|
package/src/models/Payment.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { column } from '@simonbackx/simple-database';
|
|
2
|
-
import { BalanceItemDetailed, BalanceItemPaymentDetailed, BaseOrganization, PaymentCustomer, PaymentGeneral, PaymentMethod, PaymentProvider, PaymentStatus, PaymentType,
|
|
2
|
+
import { BalanceItemDetailed, BalanceItemPaymentDetailed, BaseOrganization, PaymentCustomer, PaymentGeneral, PaymentMethod, PaymentProvider, PaymentStatus, PaymentType, SettlementReference, TransferSettings } from '@stamhoofd/structures';
|
|
3
|
+
import { PaymentSettlementDetailed } from '@stamhoofd/structures/settlements/PaymentSettlement.js';
|
|
4
|
+
import { Settlement as SettlementStruct } from '@stamhoofd/structures/settlements/Settlement.js';
|
|
5
|
+
import type { PaymentSettlement as PaymentSettlementModel } from './PaymentSettlement.js';
|
|
6
|
+
import type { Settlement as SettlementModel } from './Settlement.js';
|
|
3
7
|
import { Formatter } from '@stamhoofd/utility';
|
|
4
8
|
import { v4 as uuidv4 } from 'uuid';
|
|
5
9
|
|
|
@@ -225,8 +229,8 @@ export class Payment extends QueryableModel {
|
|
|
225
229
|
paidAt: Date | null = null;
|
|
226
230
|
|
|
227
231
|
/// Settlement meta data
|
|
228
|
-
@column({ type: 'json', decoder:
|
|
229
|
-
settlement:
|
|
232
|
+
@column({ type: 'json', decoder: SettlementReference, nullable: true })
|
|
233
|
+
settlement: SettlementReference | null = null;
|
|
230
234
|
|
|
231
235
|
@column({ type: 'string', nullable: true })
|
|
232
236
|
iban: string | null = null;
|
|
@@ -271,20 +275,25 @@ export class Payment extends QueryableModel {
|
|
|
271
275
|
|
|
272
276
|
const { balanceItemPayments, balanceItems } = await Payment.loadBalanceItems(payments);
|
|
273
277
|
const { payingOrganizations } = await Payment.loadPayingOrganizations(payments);
|
|
278
|
+
const { paymentSettlements, settlements } = includeSettlements ? await Payment.loadSettlements(payments) : { paymentSettlements: [], settlements: [] };
|
|
274
279
|
|
|
275
280
|
return this.getGeneralStructureFromRelations({
|
|
276
281
|
payments,
|
|
277
282
|
balanceItemPayments,
|
|
278
283
|
balanceItems,
|
|
279
284
|
payingOrganizations,
|
|
285
|
+
paymentSettlements,
|
|
286
|
+
settlements,
|
|
280
287
|
}, includeSettlements);
|
|
281
288
|
}
|
|
282
289
|
|
|
283
|
-
static getGeneralStructureFromRelations({ payments, balanceItemPayments, balanceItems, payingOrganizations }: {
|
|
290
|
+
static getGeneralStructureFromRelations({ payments, balanceItemPayments, balanceItems, payingOrganizations, paymentSettlements, settlements }: {
|
|
284
291
|
payments: Payment[];
|
|
285
292
|
balanceItemPayments: BalanceItemPayment[];
|
|
286
293
|
balanceItems: BalanceItem[];
|
|
287
294
|
payingOrganizations: Organization[];
|
|
295
|
+
paymentSettlements?: PaymentSettlementModel[];
|
|
296
|
+
settlements?: SettlementModel[];
|
|
288
297
|
}, includeSettlements = false): PaymentGeneral[] {
|
|
289
298
|
if (payments.length === 0) {
|
|
290
299
|
return [];
|
|
@@ -310,7 +319,24 @@ export class Payment extends QueryableModel {
|
|
|
310
319
|
});
|
|
311
320
|
}),
|
|
312
321
|
...(payment.provider !== PaymentProvider.Stripe ? { stripeAccountId: null } : {}),
|
|
313
|
-
...(
|
|
322
|
+
...(includeSettlements
|
|
323
|
+
? {
|
|
324
|
+
settlements: (paymentSettlements ?? []).filter((line) => {
|
|
325
|
+
if (line.paymentId !== payment.id) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
// A payment is only settled by the payouts of its own organization
|
|
329
|
+
const settlement = (settlements ?? []).find(s => s.id === line.settlementId);
|
|
330
|
+
return settlement !== undefined && settlement.organizationId === payment.organizationId;
|
|
331
|
+
}).map((line) => {
|
|
332
|
+
const settlement = (settlements ?? []).find(s => s.id === line.settlementId);
|
|
333
|
+
return PaymentSettlementDetailed.create({
|
|
334
|
+
...line,
|
|
335
|
+
settlement: SettlementStruct.create({ ...settlement }),
|
|
336
|
+
});
|
|
337
|
+
}),
|
|
338
|
+
}
|
|
339
|
+
: { settlement: null, settlements: [], transferFee: 0, stripeAccountId: null, serviceFeeManual: 0, serviceFeeManualCharged: 0, serviceFeePayout: 0 }),
|
|
314
340
|
});
|
|
315
341
|
});
|
|
316
342
|
}
|
|
@@ -344,6 +370,30 @@ export class Payment extends QueryableModel {
|
|
|
344
370
|
return { balanceItemPayments, balanceItems };
|
|
345
371
|
}
|
|
346
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Mirrors loadBalanceItems for the settlements a payment was part of. Only payouts of the
|
|
375
|
+
* payments' own organizations: a payment is never settled by another organization's payout.
|
|
376
|
+
*/
|
|
377
|
+
static async loadSettlements(payments: Payment[]) {
|
|
378
|
+
if (payments.length === 0) {
|
|
379
|
+
return { paymentSettlements: [], settlements: [] };
|
|
380
|
+
}
|
|
381
|
+
const { PaymentSettlement } = await import('./PaymentSettlement.js');
|
|
382
|
+
const { Settlement } = await import('./Settlement.js');
|
|
383
|
+
|
|
384
|
+
const paymentSettlements = await PaymentSettlement.select()
|
|
385
|
+
.where('paymentId', payments.map(p => p.id))
|
|
386
|
+
.fetch();
|
|
387
|
+
|
|
388
|
+
const ids = Formatter.uniqueArray(paymentSettlements.map(line => line.settlementId));
|
|
389
|
+
const organizationIds = Formatter.uniqueArray(payments.map(p => p.organizationId));
|
|
390
|
+
const settlements = ids.length > 0
|
|
391
|
+
? await Settlement.select().where('id', ids).where('organizationId', organizationIds).fetch()
|
|
392
|
+
: [];
|
|
393
|
+
|
|
394
|
+
return { paymentSettlements, settlements };
|
|
395
|
+
}
|
|
396
|
+
|
|
347
397
|
static async loadPayingOrganizations(payments: Payment[]) {
|
|
348
398
|
const ids = Formatter.uniqueArray(payments.map(p => p.payingOrganizationId).filter(p => p !== null));
|
|
349
399
|
if (ids.length === 0) {
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { column } from '@simonbackx/simple-database';
|
|
2
|
+
import { QueryableModel } from '@stamhoofd/sql';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Links a payment to a settlement it was part of. One payment can appear in multiple settlements
|
|
7
|
+
* (e.g. the payment in one payout, its refund in a later payout).
|
|
8
|
+
*/
|
|
9
|
+
export class PaymentSettlement extends QueryableModel {
|
|
10
|
+
static table = 'payment_settlements';
|
|
11
|
+
|
|
12
|
+
@column({
|
|
13
|
+
primary: true, type: 'string', beforeSave(value) {
|
|
14
|
+
return value ?? uuidv4();
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
id!: string;
|
|
18
|
+
|
|
19
|
+
@column({ type: 'string' })
|
|
20
|
+
settlementId: string;
|
|
21
|
+
|
|
22
|
+
@column({ type: 'string' })
|
|
23
|
+
paymentId: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Owner of the payout, which is also the organization of the payment: a payment is only ever
|
|
27
|
+
* settled by the payouts of its own organization.
|
|
28
|
+
*/
|
|
29
|
+
@column({ type: 'string' })
|
|
30
|
+
organizationId: string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Signed effect of this payment on the payout amount.
|
|
34
|
+
*/
|
|
35
|
+
@column({ type: 'integer' })
|
|
36
|
+
amount = 0;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The provider's id for this entry (Stripe txn_… / Mollie entry id), unique per settlement.
|
|
40
|
+
* NULL for the lines derived from application fees: those don't exist at the provider.
|
|
41
|
+
*/
|
|
42
|
+
@column({ type: 'string', nullable: true })
|
|
43
|
+
externalId: string | null = null;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* When the provider moved the money (not the same as payments.paidAt).
|
|
47
|
+
*/
|
|
48
|
+
@column({ type: 'datetime' })
|
|
49
|
+
occurredAt: Date;
|
|
50
|
+
|
|
51
|
+
@column({
|
|
52
|
+
type: 'datetime', beforeSave(old?: any) {
|
|
53
|
+
if (old !== undefined) {
|
|
54
|
+
return old;
|
|
55
|
+
}
|
|
56
|
+
const date = new Date();
|
|
57
|
+
date.setMilliseconds(0);
|
|
58
|
+
return date;
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
createdAt: Date;
|
|
62
|
+
|
|
63
|
+
@column({
|
|
64
|
+
type: 'datetime', beforeSave() {
|
|
65
|
+
const date = new Date();
|
|
66
|
+
date.setMilliseconds(0);
|
|
67
|
+
return date;
|
|
68
|
+
},
|
|
69
|
+
skipUpdate: true,
|
|
70
|
+
})
|
|
71
|
+
updatedAt: Date;
|
|
72
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { column } from '@simonbackx/simple-database';
|
|
2
|
+
import { QueryableModel } from '@stamhoofd/sql';
|
|
3
|
+
import type { PaymentProvider } from '@stamhoofd/structures';
|
|
4
|
+
import { SettlementStatus } from '@stamhoofd/structures/settlements/SettlementStatus.js';
|
|
5
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* One payout of a payment provider, always owned by the organization whose provider account paid
|
|
9
|
+
* out. The platform's own accounts (the platform Stripe account, the platform Mollie token) belong
|
|
10
|
+
* to the platform membership organization; those payouts have `stripeAccountId === null` because
|
|
11
|
+
* the platform account has no stripe_accounts row. `syncedAt === null` means the settlement still
|
|
12
|
+
* needs a (re)sync.
|
|
13
|
+
*/
|
|
14
|
+
export class Settlement extends QueryableModel {
|
|
15
|
+
static table = 'settlements';
|
|
16
|
+
|
|
17
|
+
@column({
|
|
18
|
+
primary: true, type: 'string', beforeSave(value) {
|
|
19
|
+
return value ?? uuidv4();
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
id!: string;
|
|
23
|
+
|
|
24
|
+
@column({ type: 'string' })
|
|
25
|
+
provider: PaymentProvider;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The provider's id for this payout (Stripe payout id / Mollie settlement id).
|
|
29
|
+
* Unique per provider.
|
|
30
|
+
*/
|
|
31
|
+
@column({ type: 'string' })
|
|
32
|
+
externalId: string;
|
|
33
|
+
|
|
34
|
+
@column({ type: 'string', nullable: true })
|
|
35
|
+
stripeAccountId: string | null = null;
|
|
36
|
+
|
|
37
|
+
@column({ type: 'string' })
|
|
38
|
+
organizationId: string;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Statement descriptor / Mollie reference, to match against a bank statement.
|
|
42
|
+
*/
|
|
43
|
+
@column({ type: 'string' })
|
|
44
|
+
reference = '';
|
|
45
|
+
|
|
46
|
+
@column({ type: 'integer' })
|
|
47
|
+
amount = 0;
|
|
48
|
+
|
|
49
|
+
@column({ type: 'string' })
|
|
50
|
+
currency = 'EUR';
|
|
51
|
+
|
|
52
|
+
@column({ type: 'string' })
|
|
53
|
+
status: SettlementStatus = SettlementStatus.Paid;
|
|
54
|
+
|
|
55
|
+
@column({ type: 'datetime' })
|
|
56
|
+
settledAt: Date;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Set only after a complete, error-free sync.
|
|
60
|
+
*/
|
|
61
|
+
@column({ type: 'datetime', nullable: true })
|
|
62
|
+
syncedAt: Date | null = null;
|
|
63
|
+
|
|
64
|
+
@column({ type: 'integer' })
|
|
65
|
+
syncFailureCount = 0;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* amount minus the sum of all stored payment lines, charges and pending fees, cached after
|
|
69
|
+
* every sync. Expected to be 0.
|
|
70
|
+
*/
|
|
71
|
+
@column({ type: 'integer' })
|
|
72
|
+
unexplainedAmount = 0;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Application fees received in this platform payout that are not invoiced yet, so no payment
|
|
76
|
+
* line explains them yet. Expected to reach 0 within a month, when the invoicer runs.
|
|
77
|
+
*/
|
|
78
|
+
@column({ type: 'integer' })
|
|
79
|
+
pendingFees = 0;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Balance transactions processed during the last sync (diagnostic).
|
|
83
|
+
*/
|
|
84
|
+
@column({ type: 'integer' })
|
|
85
|
+
transactionCount = 0;
|
|
86
|
+
|
|
87
|
+
@column({
|
|
88
|
+
type: 'datetime', beforeSave(old?: any) {
|
|
89
|
+
if (old !== undefined) {
|
|
90
|
+
return old;
|
|
91
|
+
}
|
|
92
|
+
const date = new Date();
|
|
93
|
+
date.setMilliseconds(0);
|
|
94
|
+
return date;
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
createdAt: Date;
|
|
98
|
+
|
|
99
|
+
@column({
|
|
100
|
+
type: 'datetime', beforeSave() {
|
|
101
|
+
const date = new Date();
|
|
102
|
+
date.setMilliseconds(0);
|
|
103
|
+
return date;
|
|
104
|
+
},
|
|
105
|
+
skipUpdate: true,
|
|
106
|
+
})
|
|
107
|
+
updatedAt: Date;
|
|
108
|
+
}
|
|
@@ -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
|
+
}
|
package/src/models/User.ts
CHANGED
|
@@ -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
|
|