diff options
author | Florian Dold <florian@dold.me> | 2024-02-19 12:49:17 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-02-19 17:47:35 +0100 |
commit | 1ec521b9d214b286e747b3ccb3113730ac3a2509 (patch) | |
tree | 2f3d2b2906810dca45859b8cbfb8d18d53b27e80 /packages/taler-wallet-core/src/operations/attention.ts | |
parent | 1034ecb5f20bd8c75e37e0b4b454ea6c1f4c1da6 (diff) | |
download | wallet-core-1ec521b9d214b286e747b3ccb3113730ac3a2509.tar.xz |
wallet-core: simplify/unify DB access
Diffstat (limited to 'packages/taler-wallet-core/src/operations/attention.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/attention.ts | 110 |
1 files changed, 49 insertions, 61 deletions
diff --git a/packages/taler-wallet-core/src/operations/attention.ts b/packages/taler-wallet-core/src/operations/attention.ts index 92d69e93e..7d8b11e79 100644 --- a/packages/taler-wallet-core/src/operations/attention.ts +++ b/packages/taler-wallet-core/src/operations/attention.ts @@ -18,20 +18,18 @@ * Imports. */ import { - AbsoluteTime, AttentionInfo, Logger, - TalerProtocolTimestamp, TalerPreciseTimestamp, UserAttentionByIdRequest, UserAttentionPriority, + UserAttentionUnreadList, UserAttentionsCountResponse, UserAttentionsRequest, UserAttentionsResponse, - UserAttentionUnreadList, } from "@gnu-taler/taler-util"; -import { InternalWalletState } from "../internal-wallet-state.js"; import { timestampPreciseFromDb, timestampPreciseToDb } from "../index.js"; +import { InternalWalletState } from "../internal-wallet-state.js"; const logger = new Logger("operations/attention.ts"); @@ -39,23 +37,21 @@ export async function getUserAttentionsUnreadCount( ws: InternalWalletState, req: UserAttentionsRequest, ): Promise<UserAttentionsCountResponse> { - const total = await ws.db - .mktx((x) => [x.userAttention]) - .runReadOnly(async (tx) => { - let count = 0; - await tx.userAttention.iter().forEach((x) => { - if ( - req.priority !== undefined && - UserAttentionPriority[x.info.type] !== req.priority - ) - return; - if (x.read !== undefined) return; - count++; - }); - - return count; + const total = await ws.db.runReadOnlyTx(["userAttention"], async (tx) => { + let count = 0; + await tx.userAttention.iter().forEach((x) => { + if ( + req.priority !== undefined && + UserAttentionPriority[x.info.type] !== req.priority + ) + return; + if (x.read !== undefined) return; + count++; }); + return count; + }); + return { total }; } @@ -63,41 +59,37 @@ export async function getUserAttentions( ws: InternalWalletState, req: UserAttentionsRequest, ): Promise<UserAttentionsResponse> { - return await ws.db - .mktx((x) => [x.userAttention]) - .runReadOnly(async (tx) => { - const pending: UserAttentionUnreadList = []; - await tx.userAttention.iter().forEach((x) => { - if ( - req.priority !== undefined && - UserAttentionPriority[x.info.type] !== req.priority - ) - return; - pending.push({ - info: x.info, - when: timestampPreciseFromDb(x.created), - read: x.read !== undefined, - }); + return await ws.db.runReadOnlyTx(["userAttention"], async (tx) => { + const pending: UserAttentionUnreadList = []; + await tx.userAttention.iter().forEach((x) => { + if ( + req.priority !== undefined && + UserAttentionPriority[x.info.type] !== req.priority + ) + return; + pending.push({ + info: x.info, + when: timestampPreciseFromDb(x.created), + read: x.read !== undefined, }); - - return { pending }; }); + + return { pending }; + }); } export async function markAttentionRequestAsRead( ws: InternalWalletState, req: UserAttentionByIdRequest, ): Promise<void> { - await ws.db - .mktx((x) => [x.userAttention]) - .runReadWrite(async (tx) => { - const ua = await tx.userAttention.get([req.entityId, req.type]); - if (!ua) throw Error("attention request not found"); - tx.userAttention.put({ - ...ua, - read: timestampPreciseToDb(TalerPreciseTimestamp.now()), - }); + await ws.db.runReadWriteTx(["userAttention"], async (tx) => { + const ua = await tx.userAttention.get([req.entityId, req.type]); + if (!ua) throw Error("attention request not found"); + tx.userAttention.put({ + ...ua, + read: timestampPreciseToDb(TalerPreciseTimestamp.now()), }); + }); } /** @@ -112,16 +104,14 @@ export async function addAttentionRequest( info: AttentionInfo, entityId: string, ): Promise<void> { - await ws.db - .mktx((x) => [x.userAttention]) - .runReadWrite(async (tx) => { - await tx.userAttention.put({ - info, - entityId, - created: timestampPreciseToDb(TalerPreciseTimestamp.now()), - read: undefined, - }); + await ws.db.runReadWriteTx(["userAttention"], async (tx) => { + await tx.userAttention.put({ + info, + entityId, + created: timestampPreciseToDb(TalerPreciseTimestamp.now()), + read: undefined, }); + }); } /** @@ -135,11 +125,9 @@ export async function removeAttentionRequest( ws: InternalWalletState, req: UserAttentionByIdRequest, ): Promise<void> { - await ws.db - .mktx((x) => [x.userAttention]) - .runReadWrite(async (tx) => { - const ua = await tx.userAttention.get([req.entityId, req.type]); - if (!ua) throw Error("attention request not found"); - await tx.userAttention.delete([req.entityId, req.type]); - }); + await ws.db.runReadWriteTx(["userAttention"], async (tx) => { + const ua = await tx.userAttention.get([req.entityId, req.type]); + if (!ua) throw Error("attention request not found"); + await tx.userAttention.delete([req.entityId, req.type]); + }); } |