feat: add payment module
This commit is contained in:
97
modules/payment/repositories/account.ts
Normal file
97
modules/payment/repositories/account.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { db } from "@platform/database";
|
||||
import { BadRequestError, ConflictError } 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.
|
||||
*
|
||||
* @param values - Account values to insert.
|
||||
*/
|
||||
export async function createAccount(values: AccountInsert): Promise<string> {
|
||||
return db
|
||||
.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 (
|
||||
SELECT 1
|
||||
FROM payment.wallet w
|
||||
WHERE w._id = ${db.text(values.walletId)}
|
||||
), 'missing_wallet';
|
||||
`;
|
||||
|
||||
// Assert wallet → ledger relationship exists AND ledger exists
|
||||
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';
|
||||
`;
|
||||
|
||||
// Assert ledger supports the currency
|
||||
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)} = ANY(l.currencies)
|
||||
), 'unsupported_currency';
|
||||
`;
|
||||
|
||||
await db.sql`INSERT INTO payment.wallet RECORDS ${db.transit({ _id, ...AccountInsertSchema.parse(values) })}`;
|
||||
|
||||
return _id;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof Error === false) {
|
||||
throw error;
|
||||
}
|
||||
switch (error.message) {
|
||||
case "missing_wallet": {
|
||||
throw new ConflictError("Account wallet does not exist");
|
||||
}
|
||||
case "missing_ledger": {
|
||||
throw new ConflictError("Account ledger does not exist");
|
||||
}
|
||||
case "unsupported_currency": {
|
||||
throw new ConflictError("Invalid account currency");
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAccountsByWalletId(walletId: string): Promise<Account[]> {
|
||||
return db.schema(AccountSchema).many`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.wallet
|
||||
WHERE
|
||||
"walletId" = ${walletId}
|
||||
`;
|
||||
}
|
||||
101
modules/payment/repositories/beneficiary.ts
Normal file
101
modules/payment/repositories/beneficiary.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { db } from "@platform/database";
|
||||
import { ConflictError } from "@platform/relay";
|
||||
|
||||
import {
|
||||
type Beneficiary,
|
||||
type BeneficiaryInsert,
|
||||
BeneficiaryInsertSchema,
|
||||
BeneficiarySchema,
|
||||
type BeneficiaryUpdate,
|
||||
BeneficiaryUpdateSchema,
|
||||
} from "../schemas/beneficiary.ts";
|
||||
|
||||
/**
|
||||
* Create a new beneficiary entity.
|
||||
*
|
||||
* @param values - Beneficiary values to insert.
|
||||
*/
|
||||
export async function createBeneficiary({ tenantId, label }: BeneficiaryInsert): Promise<string> {
|
||||
return db
|
||||
.begin(async () => {
|
||||
const _id = crypto.randomUUID();
|
||||
await db.sql`ASSERT NOT EXISTS (SELECT 1 FROM payment.beneficiary WHERE "tenantId" = ${db.text(tenantId)}), 'duplicate_tenant'`;
|
||||
await db.sql`INSERT INTO payment.beneficiary RECORDS ${db.transit({ _id, ...BeneficiaryInsertSchema.parse({ tenantId, label }) })}`;
|
||||
return _id;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof Error && error.message === "duplicate_tenant") {
|
||||
throw new ConflictError(`Tenant '${tenantId}' already has a beneficiary`);
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all registered beneficiaries.
|
||||
*/
|
||||
export async function getBeneficiaries(): Promise<any[]> {
|
||||
return db.schema(BeneficiarySchema).many`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.beneficiary
|
||||
ORDER BY
|
||||
_system_from DESC
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a beneficiary entity by provided tenant id.
|
||||
*
|
||||
* @param tenantId - Tenant we want to retrieve benificiary for.
|
||||
*/
|
||||
export async function getBeneficiaryByTenantId(tenantId: string): Promise<Beneficiary | undefined> {
|
||||
return db.schema(BeneficiarySchema).one`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.beneficiary
|
||||
WHERE
|
||||
"tenantId" = ${tenantId}
|
||||
ORDER BY
|
||||
_system_from DESC
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a beneficiary entity by provided id.
|
||||
*
|
||||
* @param id - Identity of the beneficiary to retrieve.
|
||||
*/
|
||||
export async function getBeneficiaryById(id: string): Promise<Beneficiary | undefined> {
|
||||
return db.schema(BeneficiarySchema).one`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.beneficiary
|
||||
WHERE
|
||||
_id = ${id}
|
||||
ORDER BY
|
||||
_system_from DESC
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a beneficiary entity with the given data.
|
||||
*
|
||||
* @param attributes - Attributes to set on the beneficiary.
|
||||
*/
|
||||
export async function updateBeneficiary(attributes: BeneficiaryUpdate): Promise<void> {
|
||||
const { _id, label } = BeneficiaryUpdateSchema.parse(attributes);
|
||||
await db.sql`UPDATE payment.beneficiary SET label = ${label} WHERE _id = ${_id}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a beneficiary entity.
|
||||
*
|
||||
* @param id - Identity of the beneficiary to delete.
|
||||
*/
|
||||
export async function deleteBeneficiary(id: string): Promise<void> {
|
||||
await db.sql`DELETE FROM payment.beneficiary WHERE _id = ${id}`;
|
||||
}
|
||||
57
modules/payment/repositories/ledger.ts
Normal file
57
modules/payment/repositories/ledger.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { db } from "@platform/database";
|
||||
import { ConflictError } from "@platform/relay";
|
||||
|
||||
import { type Ledger, type LedgerInsert, LedgerInsertSchema, LedgerSchema } from "../schemas/ledger.ts";
|
||||
|
||||
/**
|
||||
* Create a new ledger.
|
||||
*
|
||||
* @param values - Ledger values to insert.
|
||||
*/
|
||||
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'`;
|
||||
await db.sql`INSERT INTO payment.ledger RECORDS ${db.transit({ _id, ...LedgerInsertSchema.parse(values) })}`;
|
||||
return _id;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof Error && error.message === "missing_beneficiary") {
|
||||
throw new ConflictError(`Benficiary '${values.beneficiaryId}' does not exist`);
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getLedgersByBeneficiary(beneficiaryId: string): Promise<Ledger[]> {
|
||||
console.log(
|
||||
await db.sql`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.ledger
|
||||
WHERE
|
||||
"beneficiaryId" = ${beneficiaryId}
|
||||
`,
|
||||
);
|
||||
return db.schema(LedgerSchema).many`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.ledger
|
||||
WHERE
|
||||
"beneficiaryId" = ${beneficiaryId}
|
||||
`;
|
||||
}
|
||||
|
||||
export async function getLedgerById(id: string): Promise<Ledger | undefined> {
|
||||
return db.schema(LedgerSchema).one`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.ledger
|
||||
WHERE
|
||||
_id = ${id}
|
||||
`;
|
||||
}
|
||||
3
modules/payment/repositories/transaction.ts
Normal file
3
modules/payment/repositories/transaction.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { db } from "@platform/database";
|
||||
|
||||
export async function createTransaction() {}
|
||||
47
modules/payment/repositories/wallet.ts
Normal file
47
modules/payment/repositories/wallet.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { db } from "@platform/database";
|
||||
import { ConflictError } from "@platform/relay";
|
||||
|
||||
import { type Wallet, type WalletInsert, WalletInsertSchema, WalletSchema } from "../schemas/wallet.ts";
|
||||
|
||||
/**
|
||||
* Create a new wallet.
|
||||
*
|
||||
* @param values - Wallet values to insert.
|
||||
*/
|
||||
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'`;
|
||||
await db.sql`INSERT INTO payment.wallet RECORDS ${db.transit({ _id, ...WalletInsertSchema.parse(values) })}`;
|
||||
return _id;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof Error && error.message === "missing_beneficiary") {
|
||||
throw new ConflictError(`Ledger '${values.ledgerId}' does not exist`);
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWalletsByLedgerId(ledgerId: string): Promise<Wallet[]> {
|
||||
return db.schema(WalletSchema).many`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.wallet
|
||||
WHERE
|
||||
"ledgerId" = ${ledgerId}
|
||||
`;
|
||||
}
|
||||
|
||||
export async function getWalletById(id: string): Promise<Wallet | undefined> {
|
||||
return db.schema(WalletSchema).one`
|
||||
SELECT
|
||||
*, _system_from as "createdAt"
|
||||
FROM
|
||||
payment.wallet
|
||||
WHERE
|
||||
_id = ${id}
|
||||
`;
|
||||
}
|
||||
Reference in New Issue
Block a user