better workaround with duplication

This commit is contained in:
Sebastian 2022-10-21 10:11:41 -03:00
parent 683c89ab71
commit e63f202e36
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
2 changed files with 32 additions and 50 deletions

View File

@ -129,6 +129,7 @@ import {
} from "./common.js";
import { getExchangeDetails } from "./exchanges.js";
import { createRefreshGroup, getTotalRefreshCost } from "./refresh.js";
import { GetReadOnlyAccess } from "../util/query.js";
/**
* Logger.
@ -257,6 +258,9 @@ function getPayRequestTimeout(purchase: PurchaseRecord): Duration {
export async function expectProposalDownload(
ws: InternalWalletState,
p: PurchaseRecord,
parentTx?: GetReadOnlyAccess<{
contractTerms: typeof WalletStoresV1.contractTerms;
}>,
): Promise<{
contractData: WalletContractData;
contractTermsRaw: any;
@ -265,9 +269,10 @@ export async function expectProposalDownload(
throw Error("expected proposal to be downloaded");
}
const download = p.download;
return await ws.db
.mktx((x) => [x.contractTerms])
.runReadOnly(async (tx) => {
async function getFromTransaction(
tx: Exclude<typeof parentTx, undefined>,
): Promise<ReturnType<typeof expectProposalDownload>> {
const contractTerms = await tx.contractTerms.get(
download.contractTermsHash,
);
@ -282,7 +287,14 @@ export async function expectProposalDownload(
),
contractTermsRaw: contractTerms.contractTermsRaw,
};
});
}
if (parentTx) {
return getFromTransaction(parentTx);
}
return await ws.db
.mktx((x) => [x.contractTerms])
.runReadOnly(getFromTransaction);
}
export function extractContractData(

View File

@ -212,28 +212,13 @@ export async function getTransactionById(
}),
);
// const download = await expectProposalDownload(ws, purchase);
const download = await expectProposalDownload(ws, purchase, tx);
// FIXME: this is what expectProposalDownload, but nested tx is not supported
if (!purchase.download) {
throw Error("expected proposal to be downloaded");
}
const contractTerms = await tx.contractTerms.get(
purchase.download.contractTermsHash,
);
if (!contractTerms) {
throw Error("contract terms not found");
}
const contractData = extractContractData(
contractTerms.contractTermsRaw,
purchase.download.contractTermsHash,
purchase.download.contractTermsMerchantSig,
);
const cleanRefunds = filteredRefunds.filter(
(x): x is WalletRefundItem => !!x,
);
// const contractData = download.contractData;
const contractData = download.contractData;
const refunds = mergeRefundByExecutionTime(
cleanRefunds,
Amounts.getZero(contractData.amount.currency),
@ -306,23 +291,8 @@ export async function getTransactionById(
),
);
if (t) throw Error("deleted");
// const download = await expectProposalDownload(ws, purchase);
// const contractData = download.contractData;
// FIXME: this is what expectProposalDownload, but nested tx is not supported
if (!purchase.download) {
throw Error("expected proposal to be downloaded");
}
const contractTerms = await tx.contractTerms.get(
purchase.download.contractTermsHash,
);
if (!contractTerms) {
throw Error("contract terms not found");
}
const contractData = extractContractData(
contractTerms.contractTermsRaw,
purchase.download.contractTermsHash,
purchase.download.contractTermsMerchantSig,
);
const download = await expectProposalDownload(ws, purchase, tx);
const contractData = download.contractData;
const refunds = mergeRefundByExecutionTime(
[theRefund],
Amounts.getZero(contractData.amount.currency),