feat: update client data

This commit is contained in:
2026-01-06 11:08:55 +01:00
parent 37164b560f
commit 704d0d1821
50 changed files with 1215 additions and 471 deletions

View File

@@ -9,6 +9,6 @@
},
"dependencies": {
"@platform/relay": "workspace:*",
"zod": "4.1.13"
"zod": "4.3.5"
}
}

View File

@@ -12,6 +12,6 @@
"@platform/database": "workspace:*",
"@platform/parse": "workspace:*",
"@platform/relay": "workspace:*",
"zod": "4.1.13"
"zod": "4.3.5"
}
}

View File

@@ -7,47 +7,72 @@ import { type Account, type AccountInsert, AccountInsertSchema, AccountSchema }
* Create a new account.
*
* @param values - Account values to insert.
*
* @returns Account _id
*/
export async function createAccount(values: AccountInsert): Promise<string> {
return db
.begin(async () => {
const _id = crypto.randomUUID();
// Assert wallet exists
// ### Assert Wallet
// Ensure that the wallet we are creating an account for exists.
await db.sql`
ASSERT EXISTS (
SELECT 1
FROM payment.wallet w
WHERE w._id = ${db.text(values.walletId)}
), 'missing_wallet';
SELECT
1
FROM
payment.wallet wallet
WHERE
wallet._id = ${db.text(values.walletId)}
),
'missing_wallet';
`;
// Assert wallet → ledger relationship exists AND ledger exists
// ### Assert Wallet → Ledger
// Ensure that the wallet is related to a existing ledger.
await db.sql`
ASSERT EXISTS (
SELECT 1
FROM payment.wallet w
JOIN payment.ledger l ON l._id = w."ledgerId"
WHERE w._id = ${db.text(values.walletId)}
), 'missing_ledger';
SELECT
1
FROM
payment.wallet wallet
JOIN
payment.ledger ledger ON ledger._id = wallet."ledgerId"
WHERE
wallet._id = ${db.text(values.walletId)}
),
'missing_ledger';
`;
// Assert ledger supports the currency
// ### Assert Currency
// Ensure that the account currency is supported by the ledger.
await db.sql`
ASSERT EXISTS (
SELECT 1
FROM payment.wallet w
JOIN payment.ledger l ON l._id = w."ledgerId"
WHERE w._id = ${db.text(values.walletId)}
AND ${db.text(values.currency)} IN (
SELECT
1
FROM
payment.wallet wallet
JOIN
payment.ledger ledger ON ledger._id = wallet."ledgerId"
WHERE
wallet._id = ${db.text(values.walletId)}
AND
${db.text(values.currency)} IN (
SELECT
currency
FROM
UNNEST(l.currencies) AS x(currency)
UNNEST(ledger.currencies) AS x(currency)
)
), 'unsupported_currency';
),
'unsupported_currency';
`;
// ### Create Account
await db.sql`INSERT INTO payment.account RECORDS ${db.transit({ _id, ...AccountInsertSchema.parse(values) })}`;
return _id;
@@ -71,6 +96,13 @@ export async function createAccount(values: AccountInsert): Promise<string> {
});
}
/**
* Get all accounts registered for a wallet.
*
* @param walletId - Wallet to fetch accounts from.
*
* @returns List of wallet accounts
*/
export async function getAccountsByWalletId(walletId: string): Promise<Account[]> {
return db.schema(AccountSchema).many`
SELECT

View File

@@ -56,6 +56,8 @@ export async function getBeneficiaryByTenantId(tenantId: string): Promise<Benefi
* Get a beneficiary entity by provided id.
*
* @param id - Identity of the beneficiary to retrieve.
*
* @returns Beneficiary or undefined
*/
export async function getBeneficiaryById(id: string): Promise<Beneficiary | undefined> {
return db.schema(BeneficiarySchema).one`

View File

@@ -7,12 +7,31 @@ import { type Ledger, type LedgerInsert, LedgerSchema } from "../schemas/ledger.
* Create a new ledger.
*
* @param values - Ledger values to insert.
*
* @returns Ledger _id
*/
export async function createLedger(values: LedgerInsert): Promise<string> {
return db
.begin(async () => {
const _id = crypto.randomUUID();
await db.sql`ASSERT EXISTS (SELECT 1 FROM payment.beneficiary WHERE _id = ${db.text(values.beneficiaryId)}), 'missing_beneficiary'`;
// ### Assert Beneficiary
// Ensure the beneficiary for the ledger exists.
await db.sql`
ASSERT EXISTS (
SELECT
1
FROM
payment.beneficiary
WHERE
_id = ${db.text(values.beneficiaryId)}
),
'missing_beneficiary'
`;
// ### Create Ledger
await db.sql`
INSERT INTO payment.ledger (
_id,
@@ -26,6 +45,7 @@ export async function createLedger(values: LedgerInsert): Promise<string> {
${values.label ? db.text(values.label) : null}
)
`;
return _id;
})
.catch((error) => {

View File

@@ -7,13 +7,33 @@ import { type Wallet, type WalletInsert, WalletInsertSchema, WalletSchema } from
* Create a new wallet.
*
* @param values - Wallet values to insert.
*
* @returns Wallet _id
*/
export async function createWallet(values: WalletInsert): Promise<string> {
return db
.begin(async () => {
const _id = crypto.randomUUID();
await db.sql`ASSERT EXISTS (SELECT 1 FROM payment.ledger WHERE _id = ${db.text(values.ledgerId)}), 'missing_ledger'`;
// ### Assert Ledger
// Ensure the ledger for the wallet exists.
await db.sql`
ASSERT EXISTS (
SELECT
1
FROM
payment.ledger
WHERE
_id = ${db.text(values.ledgerId)}
),
'missing_ledger'
`;
// ### Create Wallet
await db.sql`INSERT INTO payment.wallet RECORDS ${db.transit({ _id, ...WalletInsertSchema.parse(values) })}`;
return _id;
})
.catch((error) => {

View File

@@ -11,6 +11,6 @@
"@platform/database": "workspace:*",
"@platform/parse": "workspace:*",
"@platform/relay": "workspace:*",
"zod": "4.1.13"
"zod": "4.3.5"
}
}