refactor: import/export use cases (#2397)

This commit is contained in:
Mo
2023-08-10 08:08:17 -05:00
committed by GitHub
parent 5bb749b601
commit 1e965caf18
43 changed files with 1085 additions and 673 deletions

View File

@@ -0,0 +1,43 @@
import { ProtectionsClientInterface } from './../Protection/ProtectionClientInterface'
import { ContentType, Result, UseCaseInterface } from '@standardnotes/domain-core'
import {
BackupFile,
CreateDecryptedBackupFileContextPayload,
CreateEncryptedBackupFileContextPayload,
isDecryptedPayload,
isEncryptedPayload,
} from '@standardnotes/models'
import { PayloadManagerInterface } from '../Payloads/PayloadManagerInterface'
import { ProtocolVersionLatest } from '@standardnotes/common'
import { isNotUndefined } from '@standardnotes/utils'
export class CreateDecryptedBackupFile implements UseCaseInterface<BackupFile> {
constructor(
private payloads: PayloadManagerInterface,
private protections: ProtectionsClientInterface,
) {}
async execute(): Promise<Result<BackupFile>> {
if (!(await this.protections.authorizeBackupCreation())) {
return Result.fail('Failed to authorize backup creation')
}
const payloads = this.payloads.nonDeletedItems.filter((item) => item.content_type !== ContentType.TYPES.ItemsKey)
const data: BackupFile = {
version: ProtocolVersionLatest,
items: payloads
.map((payload) => {
if (isDecryptedPayload(payload)) {
return CreateDecryptedBackupFileContextPayload(payload)
} else if (isEncryptedPayload(payload)) {
return CreateEncryptedBackupFileContextPayload(payload)
}
return undefined
})
.filter(isNotUndefined),
}
return Result.ok(data)
}
}