custody_account.id.
Custody accounts live in the custody_accounts Firestore collection. Each document tracks a running balance_available field that reflects the total amount of funds received.
How custody balances are updated
Whentransactions_generate processes a transaction, it writes a copy of the transaction to the custody account’s sub-collection:
custody_process Firestore trigger, which increments balance_available by the transaction amount.
The
amount_type field gates the custody update. transactions_generate explicitly sets amount_type = "payment" before writing to the custody sub-collection. Only documents with amount_type == "payment" trigger a balance increment. Any other value causes the trigger to return without making changes.custody_process
A Firestore trigger that fires whenever a new document is created undercustody_accounts/{idaccounts}/transactions/{idtransaction}.
Trigger: onDocumentCreated("custody_accounts/{idaccounts}/transactions/{idtransaction}")
What it does
- Reads the newly created transaction document.
- Checks
data.amount_type.- If
"payment": incrementsbalance_availableon the parentcustody_accounts/{idaccounts}document bydata.amount, and setsdate.updateto the current server timestamp. - Any other value: exits without writing.
- If
Trigger path parameters
| Parameter | Description |
|---|---|
idaccounts | Firestore document ID of the custody account |
idtransaction | Firestore document ID of the transaction sub-document |
Fields read from the transaction document
Must equal
"payment" for the custody balance to be updated. Set automatically by transactions_generate.The payment amount that is added to
balance_available.Fields written to the custody account document
Incremented by
amount using Firestore’s FieldValue.increment() — safe for concurrent writes.Set to the server timestamp at the time of the update.
Example
A transaction of USD 75.00 is processed through custody accountcust_001:
transactions_generatewrites tocustody_accounts/cust_001/transactions/{auto_id}withamount: 75.00andamount_type: "payment".custody_processfires on the new document.custody_accounts/cust_001is updated:balance_availableincreases by75.00.
FieldValue.increment() is an atomic server-side operation. Multiple concurrent transactions writing to the same custody account will not produce race conditions or lost updates.Custody account document structure
| Field | Type | Description |
|---|---|---|
balance_available | number | Running total of all payment amounts received |
date.update | Timestamp | Last time the balance was updated |
custody_process.
Relationship to payout distribution
Eachorders_payout record references the same custody_account.id, custody_account.name, and custody_account.account_number that the original transaction used. This means finance staff can trace every payout obligation back to the specific custody account that holds the funds.