better workaround with duplication
This commit is contained in:
parent
683c89ab71
commit
e63f202e36
@ -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,24 +269,32 @@ export async function expectProposalDownload(
|
||||
throw Error("expected proposal to be downloaded");
|
||||
}
|
||||
const download = p.download;
|
||||
|
||||
async function getFromTransaction(
|
||||
tx: Exclude<typeof parentTx, undefined>,
|
||||
): Promise<ReturnType<typeof expectProposalDownload>> {
|
||||
const contractTerms = await tx.contractTerms.get(
|
||||
download.contractTermsHash,
|
||||
);
|
||||
if (!contractTerms) {
|
||||
throw Error("contract terms not found");
|
||||
}
|
||||
return {
|
||||
contractData: extractContractData(
|
||||
contractTerms.contractTermsRaw,
|
||||
download.contractTermsHash,
|
||||
download.contractTermsMerchantSig,
|
||||
),
|
||||
contractTermsRaw: contractTerms.contractTermsRaw,
|
||||
};
|
||||
}
|
||||
|
||||
if (parentTx) {
|
||||
return getFromTransaction(parentTx);
|
||||
}
|
||||
return await ws.db
|
||||
.mktx((x) => [x.contractTerms])
|
||||
.runReadOnly(async (tx) => {
|
||||
const contractTerms = await tx.contractTerms.get(
|
||||
download.contractTermsHash,
|
||||
);
|
||||
if (!contractTerms) {
|
||||
throw Error("contract terms not found");
|
||||
}
|
||||
return {
|
||||
contractData: extractContractData(
|
||||
contractTerms.contractTermsRaw,
|
||||
download.contractTermsHash,
|
||||
download.contractTermsMerchantSig,
|
||||
),
|
||||
contractTermsRaw: contractTerms.contractTermsRaw,
|
||||
};
|
||||
});
|
||||
.runReadOnly(getFromTransaction);
|
||||
}
|
||||
|
||||
export function extractContractData(
|
||||
|
@ -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),
|
||||
|
Loading…
Reference in New Issue
Block a user