feat: add currency assertion to account creation
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import { db } from "@platform/database";
|
||||
import { BadRequestError, ConflictError } from "@platform/relay";
|
||||
import { BadRequestError } from "@platform/relay";
|
||||
|
||||
import { type Account, type AccountInsert, AccountInsertSchema, AccountSchema } from "../schemas/account.ts";
|
||||
import { getLedgerById } from "./ledger.ts";
|
||||
import { getWalletById } from "./wallet.ts";
|
||||
|
||||
/**
|
||||
* Create a new account.
|
||||
@@ -15,23 +13,6 @@ export async function createAccount(values: AccountInsert): Promise<string> {
|
||||
.begin(async () => {
|
||||
const _id = crypto.randomUUID();
|
||||
|
||||
// const wallet = await getWalletById(values.walletId);
|
||||
// if (wallet === undefined) {
|
||||
// throw new ConflictError(`Wallet '${values.walletId}' does not exist`);
|
||||
// }
|
||||
|
||||
// const ledger = await getLedgerById(wallet.ledgerId);
|
||||
// if (ledger === undefined) {
|
||||
// // TODO: RAISE ALARMS, THIS SHOULD NEVER OCCUR
|
||||
// throw new ConflictError(`Wallet ledger '${wallet.ledgerId}' does not exist`);
|
||||
// }
|
||||
|
||||
// if (ledger.currencies.includes(values.currency) === false) {
|
||||
// throw new BadRequestError(
|
||||
// `Ledger does not support '${values.currency}' currency, supported currencies '${ledger.currencies.join(", ")}'`,
|
||||
// );
|
||||
// }
|
||||
|
||||
// Assert wallet exists
|
||||
await db.sql`
|
||||
ASSERT EXISTS (
|
||||
@@ -58,11 +39,16 @@ export async function createAccount(values: AccountInsert): Promise<string> {
|
||||
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)} = ANY(l.currencies)
|
||||
AND ${db.text(values.currency)} IN (
|
||||
SELECT
|
||||
currency
|
||||
FROM
|
||||
UNNEST(l.currencies) AS x(currency)
|
||||
)
|
||||
), 'unsupported_currency';
|
||||
`;
|
||||
|
||||
await db.sql`INSERT INTO payment.wallet RECORDS ${db.transit({ _id, ...AccountInsertSchema.parse(values) })}`;
|
||||
await db.sql`INSERT INTO payment.account RECORDS ${db.transit({ _id, ...AccountInsertSchema.parse(values) })}`;
|
||||
|
||||
return _id;
|
||||
})
|
||||
@@ -72,13 +58,13 @@ export async function createAccount(values: AccountInsert): Promise<string> {
|
||||
}
|
||||
switch (error.message) {
|
||||
case "missing_wallet": {
|
||||
throw new ConflictError("Account wallet does not exist");
|
||||
throw new BadRequestError("Account wallet does not exist");
|
||||
}
|
||||
case "missing_ledger": {
|
||||
throw new ConflictError("Account ledger does not exist");
|
||||
throw new BadRequestError("Account ledger does not exist");
|
||||
}
|
||||
case "unsupported_currency": {
|
||||
throw new ConflictError("Invalid account currency");
|
||||
throw new BadRequestError("Invalid account currency");
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user