add bank account record

This commit is contained in:
Sebastian 2022-09-23 15:17:29 -03:00
parent 22e87bb18f
commit fbf0502672
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
3 changed files with 113 additions and 12 deletions

View File

@ -600,8 +600,15 @@ export interface WalletCoreVersion {
bank: string;
}
export interface KnownBankAccountsInfo {
uri: PaytoUri;
kyc_completed: boolean;
currency: string;
alias: string,
}
export interface KnownBankAccounts {
accounts: { [payto: string]: PaytoUri };
accounts: KnownBankAccountsInfo[];
}
export interface ExchangeTos {
@ -1077,6 +1084,29 @@ export const codecForListKnownBankAccounts =
.property("currency", codecOptional(codecForString()))
.build("ListKnownBankAccountsRequest");
export interface AddKnownBankAccountsRequest {
payto: string;
alias: string;
currency: string;
}
export const codecForAddKnownBankAccounts =
(): Codec<AddKnownBankAccountsRequest> =>
buildCodecForObject<AddKnownBankAccountsRequest>()
.property("payto", (codecForString()))
.property("alias", (codecForString()))
.property("currency", (codecForString()))
.build("AddKnownBankAccountsRequest");
export interface ForgetKnownBankAccountsRequest {
payto: string;
}
export const codecForForgetKnownBankAccounts =
(): Codec<ForgetKnownBankAccountsRequest> =>
buildCodecForObject<ForgetKnownBankAccountsRequest>()
.property("payto", (codecForString()))
.build("ForgetKnownBankAccountsRequest");
export interface GetExchangeWithdrawalInfo {
exchangeBaseUrl: string;
amount: AmountJson;

View File

@ -2084,8 +2084,25 @@ export const WalletStoresV1 = {
}),
{},
),
bankAccounts: describeStore(
"bankAccounts",
describeContents<BankAccountsRecord>({
keyPath: "uri",
}),
{},
),
};
/**
* User accounts
*/
export interface BankAccountsRecord {
uri: string;
currency: string;
kyc_completed: boolean;
alias: string;
}
export interface MetaConfigRecord {
key: string;
value: any;

View File

@ -93,6 +93,9 @@ import {
TalerErrorDetail,
codecForTransactionByIdRequest,
DenominationInfo,
KnownBankAccountsInfo,
codecForAddKnownBankAccounts,
codecForForgetKnownBankAccounts,
} from "@gnu-taler/taler-util";
import { TalerCryptoInterface } from "./crypto/cryptoImplementation.js";
import {
@ -670,27 +673,68 @@ async function listKnownBankAccounts(
ws: InternalWalletState,
currency?: string,
): Promise<KnownBankAccounts> {
const accounts: { [account: string]: PaytoUri } = {};
const accounts: KnownBankAccountsInfo[] = [];
await ws.db
.mktx((x) => [x.withdrawalGroups])
.mktx((x) => [x.bankAccounts])
.runReadOnly(async (tx) => {
const withdrawalGroups = await tx.withdrawalGroups.iter().toArray();
for (const r of withdrawalGroups) {
const amount = r.rawWithdrawalAmount;
if (currency && currency !== amount.currency) {
const knownAccounts = await tx.bankAccounts.iter().toArray();
for (const r of knownAccounts) {
if (currency && currency !== r.currency) {
continue;
}
if (r.senderWire) {
const payto = parsePaytoUri(r.senderWire);
if (payto) {
accounts[r.senderWire] = payto;
}
const payto = parsePaytoUri(r.uri);
if (payto) {
accounts.push({
uri: payto,
alias: r.alias,
kyc_completed: r.kyc_completed,
currency: r.currency,
});
}
}
});
return { accounts };
}
/**
*/
async function addKnownBankAccounts(
ws: InternalWalletState,
payto: string,
alias: string,
currency: string,
): Promise<void> {
await ws.db
.mktx((x) => [x.bankAccounts])
.runReadWrite(async (tx) => {
tx.bankAccounts.put({
uri: payto,
alias: alias,
currency: currency,
kyc_completed: false,
});
});
return;
}
/**
*/
async function forgetKnownBankAccounts(
ws: InternalWalletState,
payto: string,
): Promise<void> {
await ws.db
.mktx((x) => [x.bankAccounts])
.runReadWrite(async (tx) => {
const account = await tx.bankAccounts.get(payto);
if (!account) {
throw Error(`account not found: ${payto}`);
}
tx.bankAccounts.delete(account.uri);
});
return;
}
async function getExchanges(
ws: InternalWalletState,
): Promise<ExchangesListResponse> {
@ -1140,6 +1184,16 @@ async function dispatchRequestInternal(
const req = codecForListKnownBankAccounts().decode(payload);
return await listKnownBankAccounts(ws, req.currency);
}
case "addKnownBankAccounts": {
const req = codecForAddKnownBankAccounts().decode(payload);
await addKnownBankAccounts(ws, req.payto, req.alias, req.currency);
return {};
}
case "forgetKnownBankAccounts": {
const req = codecForForgetKnownBankAccounts().decode(payload);
await forgetKnownBankAccounts(ws, req.payto);
return {};
}
case "getWithdrawalDetailsForUri": {
const req = codecForGetWithdrawalDetailsForUri().decode(payload);
return await getWithdrawalDetailsForUri(ws, req.talerWithdrawUri);