diff options
author | Sebastian <sebasjm@gmail.com> | 2021-07-07 13:28:02 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-07-07 13:28:10 -0300 |
commit | ebfc79756f14b08ac3735b2746ac48fd3483b5e4 (patch) | |
tree | 2937c7377b9620088d660aebcb4e2974fb646880 | |
parent | ff09c98f3b0515dd92dc5f9bab887336b080b4f5 (diff) |
add providers filter to runBackupCycle
-rw-r--r-- | packages/taler-util/src/helpers.ts | 15 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/operations/backup/index.ts | 23 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/wallet.ts | 4 |
3 files changed, 39 insertions, 3 deletions
diff --git a/packages/taler-util/src/helpers.ts b/packages/taler-util/src/helpers.ts index 53a9a43d4..089602c9d 100644 --- a/packages/taler-util/src/helpers.ts +++ b/packages/taler-util/src/helpers.ts @@ -110,3 +110,18 @@ export function strcmp(s1: string, s2: string): number { export function j2s(x: any): string { return JSON.stringify(x, undefined, 2); } + +/** + * Use this to filter null or undefined from an array in a type-safe fashion + * + * example: + * const array: Array<T | undefined> = [undefined, null] + * const filtered: Array<T> = array.filter(notEmpty) + * + * @param value + * @returns + */ +export function notEmpty<T>(value: T | null | undefined): value is T { + return value !== null && value !== undefined; +} + diff --git a/packages/taler-wallet-core/src/operations/backup/index.ts b/packages/taler-wallet-core/src/operations/backup/index.ts index 68040695c..9593325a3 100644 --- a/packages/taler-wallet-core/src/operations/backup/index.ts +++ b/packages/taler-wallet-core/src/operations/backup/index.ts @@ -33,6 +33,7 @@ import { Codec, codecForAmountString, codecForBoolean, + codecForList, codecForNumber, codecForString, codecOptional, @@ -41,6 +42,7 @@ import { getTimestampNow, j2s, Logger, + notEmpty, NotificationType, PreparePayResultType, RecoveryLoadRequest, @@ -520,6 +522,19 @@ export async function processBackupForProvider( await guardOperationException(run, onOpErr); } + +export interface RunBackupCycleRequest { + /** + * List of providers to backup or empty for all known providers. + */ + providers?: Array<string>; +} + +export const codecForRunBackupCycle = (): Codec<RunBackupCycleRequest> => + buildCodecForObject<RunBackupCycleRequest>() + .property("providers", codecOptional(codecForList(codecForString()))) + .build("RunBackupCycleRequest"); + /** * Do one backup cycle that consists of: * 1. Exporting a backup and try to upload it. @@ -527,11 +542,15 @@ export async function processBackupForProvider( * 2. Download, verify and import backups from connected sync accounts. * 3. Upload the updated backup blob. */ -export async function runBackupCycle(ws: InternalWalletState): Promise<void> { +export async function runBackupCycle(ws: InternalWalletState, req: RunBackupCycleRequest): Promise<void> { const providers = await ws.db .mktx((x) => ({ backupProviders: x.backupProviders })) .runReadOnly(async (tx) => { - return await tx.backupProviders.iter().toArray(); + if (req.providers) { + const rs = await Promise.all(req.providers.map(id => tx.backupProviders.get(id))) + return rs.filter(notEmpty) + } + return await tx.backupProviders.iter(req.providers).toArray(); }); const backupJson = await exportBackup(ws); const backupConfig = await provideBackupState(ws); diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index ca9afc073..d46914b37 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -41,6 +41,7 @@ import { import { addBackupProvider, codecForAddBackupProviderRequest, + codecForRunBackupCycle, getBackupInfo, getBackupRecovery, loadBackupRecovery, @@ -809,7 +810,8 @@ async function dispatchRequestInternal( return {}; } case "runBackupCycle": { - await runBackupCycle(ws); + const req = codecForRunBackupCycle().decode(payload); + await runBackupCycle(ws, req); return {}; } case "exportBackupRecovery": { |