From 2ed95ac6d02a630ba4bb76cbc5f26bb97b275ad7 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Tue, 22 Oct 2024 17:03:52 +0200 Subject: [PATCH] feat: Add the transfer to the database if upload finish with success --- .../SwissTransferInjection.kt | 2 +- .../managers/UploadManager.kt | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt index 13333088..9fe80ff2 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt @@ -65,7 +65,7 @@ class SwissTransferInjection { val accountManager by lazy { AccountManager(appSettingsController, uploadController, transferController, realmProvider) } /** A manager used to orchestrate Uploads operations. */ - val uploadManager by lazy { UploadManager(uploadController, uploadRepository) } + val uploadManager by lazy { UploadManager(uploadController, uploadRepository, transferManager) } /** An utils to help use shared routes */ val sharedApiUrlCreator by lazy { SharedApiUrlCreator(transferController, uploadController) } diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt index 74c0973c..54bfda2d 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt @@ -42,10 +42,12 @@ import kotlin.coroutines.cancellation.CancellationException * * @property uploadController The controller for managing upload data in the database. * @property uploadRepository The repository for interacting with the SwissTransfer API for uploads. + * @property transferManager Transfer operations */ class UploadManager( private val uploadController: UploadController, private val uploadRepository: UploadRepository, + private val transferManager: TransferManager, ) { /** @@ -184,7 +186,7 @@ class UploadManager( } /** - * Finishes an upload session. + * Finishes an upload session and add the transfer to the database . * * This method retrieves an upload session from the database using the provided `uuid`. * If the session is found and has a remote container UUID, it creates a `FinishUploadBody` object @@ -217,7 +219,12 @@ class UploadManager( language = uploadSession.language.code, recipientsEmails = uploadSession.recipientsEmails, ) - uploadRepository.finishUpload(finishUploadBody) - uploadController.removeUploadSession(containerUUID) + val finishUploadResponse = runCatching { + uploadRepository.finishUpload(finishUploadBody).first() + }.getOrElse { throw UnknownException(it) } + uploadController.removeUploadSession(uploadSession.uuid) + + transferManager.addTransferByLinkUUID(finishUploadResponse.linkUUID) + // TODO: If we can't retrieve the transfer cause of the Internet, we should put it in Realm and try again later. } }