diff options
author | Florian Dold <florian@dold.me> | 2023-05-25 19:26:40 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-05-25 19:26:40 +0200 |
commit | fe8749c3f81547d080ea23d580497750d52fed91 (patch) | |
tree | 2c0e8960fa33fc318b460538fd09fe33e5896500 /packages/taler-wallet-core/src/operations | |
parent | 8624d798b799d78a4b6393493a0750027094904d (diff) | |
download | wallet-core-fe8749c3f81547d080ea23d580497750d52fed91.tar.xz |
wallet-core: implement cancelAbortingTransaction for deposit groups
Diffstat (limited to 'packages/taler-wallet-core/src/operations')
-rw-r--r-- | packages/taler-wallet-core/src/operations/deposits.ts | 80 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/operations/transactions.ts | 18 |
2 files changed, 72 insertions, 26 deletions
diff --git a/packages/taler-wallet-core/src/operations/deposits.ts b/packages/taler-wallet-core/src/operations/deposits.ts index 6c41d76f6..dc743e17f 100644 --- a/packages/taler-wallet-core/src/operations/deposits.ts +++ b/packages/taler-wallet-core/src/operations/deposits.ts @@ -167,6 +167,14 @@ export function computeDepositTransactionStatus( return { major: TransactionMajorState.Aborted, } + case DepositOperationStatus.Failed: + return { + major: TransactionMajorState.Failed, + } + case DepositOperationStatus.SuspendedAborting: + return { + major: TransactionMajorState.SuspendedAborting, + } default: throw Error(`unexpected deposit group state (${dg.operationStatus})`); } @@ -184,7 +192,7 @@ export async function suspendDepositGroup( tag: PendingTaskType.Deposit, depositGroupId, }); - let res = await ws.db + const transitionInfo = await ws.db .mktx((x) => [x.depositGroups]) .runReadWrite(async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); @@ -212,14 +220,7 @@ export async function suspendDepositGroup( return undefined; }); stopLongpolling(ws, retryTag); - if (res) { - ws.notify({ - type: NotificationType.TransactionStateTransition, - transactionId, - oldTxState: res.oldTxState, - newTxState: res.newTxState, - }); - } + notifyTransition(ws, transactionId, transitionInfo); } export async function resumeDepositGroup( @@ -230,7 +231,7 @@ export async function resumeDepositGroup( tag: TransactionType.Deposit, depositGroupId, }); - let res = await ws.db + const transitionInfo = await ws.db .mktx((x) => [x.depositGroups]) .runReadWrite(async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); @@ -258,14 +259,7 @@ export async function resumeDepositGroup( return undefined; }); ws.workAvailable.trigger(); - if (res) { - ws.notify({ - type: NotificationType.TransactionStateTransition, - transactionId, - oldTxState: res.oldTxState, - newTxState: res.newTxState, - }); - } + notifyTransition(ws, transactionId, transitionInfo); } export async function abortDepositGroup( @@ -280,7 +274,7 @@ export async function abortDepositGroup( tag: PendingTaskType.Deposit, depositGroupId, }); - let res = await ws.db + const transitionInfo = await ws.db .mktx((x) => [x.depositGroups]) .runReadWrite(async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); @@ -311,14 +305,48 @@ export async function abortDepositGroup( stopLongpolling(ws, retryTag); // Need to process the operation again. ws.workAvailable.trigger(); - if (res) { - ws.notify({ - type: NotificationType.TransactionStateTransition, - transactionId, - oldTxState: res.oldTxState, - newTxState: res.newTxState, + notifyTransition(ws, transactionId, transitionInfo); +} + +export async function cancelAbortingDepositGroup( + ws: InternalWalletState, + depositGroupId: string, +): Promise<void> { + const transactionId = constructTransactionIdentifier({ + tag: TransactionType.Deposit, + depositGroupId, + }); + const retryTag = constructTaskIdentifier({ + tag: PendingTaskType.Deposit, + depositGroupId, + }); + const transitionInfo = await ws.db + .mktx((x) => [x.depositGroups]) + .runReadWrite(async (tx) => { + const dg = await tx.depositGroups.get(depositGroupId); + if (!dg) { + logger.warn( + `can't cancel aborting deposit group, depositGroupId=${depositGroupId} not found`, + ); + return undefined; + } + const oldState = computeDepositTransactionStatus(dg); + switch (dg.operationStatus) { + case DepositOperationStatus.SuspendedAborting: + case DepositOperationStatus.Aborting: { + dg.operationStatus = DepositOperationStatus.Failed; + await tx.depositGroups.put(dg); + return { + oldTxState: oldState, + newTxState: computeDepositTransactionStatus(dg), + }; + } + } + return undefined; }); - } + // FIXME: Also cancel ongoing work (via cancellation token, once implemented) + stopLongpolling(ws, retryTag); + notifyTransition(ws, transactionId, transitionInfo); } export async function deleteDepositGroup( diff --git a/packages/taler-wallet-core/src/operations/transactions.ts b/packages/taler-wallet-core/src/operations/transactions.ts index 74df9acfb..84f879f58 100644 --- a/packages/taler-wallet-core/src/operations/transactions.ts +++ b/packages/taler-wallet-core/src/operations/transactions.ts @@ -75,6 +75,7 @@ import { } from "./common.js"; import { abortDepositGroup, + cancelAbortingDepositGroup, computeDepositTransactionStatus, processDepositGroup, resumeDepositGroup, @@ -1401,6 +1402,23 @@ export async function suspendTransaction( } } +export async function cancelAbortingTransaction( + ws: InternalWalletState, + transactionId: string, +): Promise<void> { + const tx = parseTransactionIdentifier(transactionId); + if (!tx) { + throw Error("invalid transaction ID"); + } + switch (tx.tag) { + case TransactionType.Deposit: + await cancelAbortingDepositGroup(ws, tx.depositGroupId); + return; + default: + logger.warn(`unable to suspend transaction of type '${tx.tag}'`); + } +} + /** * Resume a suspended transaction. */ |