From 1fe13cc8a3f06cf006b826a1a4f5f387b978ee7f Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 15 Nov 2023 12:07:03 +0100 Subject: wallet-core: store changed KYC URL on subsequent KYC queries --- .../taler-wallet-core/src/operations/withdraw.ts | 59 +++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'packages/taler-wallet-core/src') diff --git a/packages/taler-wallet-core/src/operations/withdraw.ts b/packages/taler-wallet-core/src/operations/withdraw.ts index c83993816..9819ae6a9 100644 --- a/packages/taler-wallet-core/src/operations/withdraw.ts +++ b/packages/taler-wallet-core/src/operations/withdraw.ts @@ -727,6 +727,58 @@ enum AmlStatus { fronzen = 2, } +/** + * Transition a withdrawal transaction with a (new) KYC URL. + * + * Emit a notification for the (self-)transition. + */ +async function transitionKycUrlUpdate( + ws: InternalWalletState, + withdrawalGroupId: string, + kycUrl: string, +): Promise { + let notificationKycUrl: string | undefined = undefined; + const transactionId = constructTransactionIdentifier({ + tag: TransactionType.Withdrawal, + withdrawalGroupId, + }); + + const transitionInfo = await ws.db + .mktx((x) => [x.planchets, x.withdrawalGroups]) + .runReadWrite(async (tx) => { + const wg2 = await tx.withdrawalGroups.get(withdrawalGroupId); + if (!wg2) { + return; + } + const oldTxState = computeWithdrawalTransactionStatus(wg2); + switch (wg2.status) { + case WithdrawalGroupStatus.PendingReady: { + wg2.kycUrl = kycUrl; + notificationKycUrl = kycUrl; + await tx.withdrawalGroups.put(wg2); + const newTxState = computeWithdrawalTransactionStatus(wg2); + return { + oldTxState, + newTxState, + }; + } + default: + return undefined; + } + }); + if (transitionInfo) { + // Always notify, even on self-transition, as the KYC URL might have changed. + ws.notify({ + type: NotificationType.TransactionStateTransition, + oldTxState: transitionInfo.oldTxState, + newTxState: transitionInfo.newTxState, + transactionId, + experimentalUserData: notificationKycUrl, + }); + } + ws.workAvailable.trigger(); +} + async function handleKycRequired( ws: InternalWalletState, withdrawalGroup: WithdrawalGroupRecord, @@ -1400,6 +1452,8 @@ async function processWithdrawalGroupPendingKyc( ); url.searchParams.set("timeout_ms", "30000"); + const withdrawalGroupId = withdrawalGroup.withdrawalGroupId; + const retryTag = TaskIdentifiers.forWithdrawal(withdrawalGroup); runLongpollAsync(ws, retryTag, async (cancellationToken) => { logger.info(`long-polling for withdrawal KYC status via ${url.href}`); @@ -1421,7 +1475,10 @@ async function processWithdrawalGroupPendingKyc( } else if (kycStatusRes.status === HttpStatusCode.Accepted) { const kycStatus = await kycStatusRes.json(); logger.info(`kyc status: ${j2s(kycStatus)}`); - // FIXME: do we need to update the KYC url, or does it always stay constant? + const kycUrl = kycStatus.kyc_url; + if (typeof kycUrl === "string") { + await transitionKycUrlUpdate(ws, withdrawalGroupId, kycUrl); + } return { ready: false }; } else if ( kycStatusRes.status === HttpStatusCode.UnavailableForLegalReasons -- cgit v1.2.3