Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/better error handling #261

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Combine
import Foundation

internal enum BiometricKycStep {
enum BiometricKycStep {
case selfie
case processing(ProcessingState)
}

internal class OrchestratedBiometricKycViewModel: ObservableObject {
class OrchestratedBiometricKycViewModel: BaseSubmissionViewModel<BiometricKycResult> {
// MARK: - Input Properties

private let userId: String
Expand All @@ -18,8 +18,8 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {

// MARK: - Other Properties

internal var selfieFile: URL?
internal var livenessFiles: [URL]?
var selfieFile: URL?
var livenessFiles: [URL]?
private var error: Error?
private var didSubmitBiometricJob: Bool = false

Expand Down Expand Up @@ -56,8 +56,9 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {

func onFinished(delegate: BiometricKycResultDelegate) {
if let selfieFile = selfieFile,
let livenessFiles = livenessFiles,
let selfiePath = getRelativePath(from: selfieFile) {
let livenessFiles = livenessFiles,
let selfiePath = getRelativePath(from: selfieFile)
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Opening braces should be preceded by a single space and on the same line as the declaration. (opening_brace)

delegate.didSucceed(
selfieImage: selfiePath,
livenessImages: livenessFiles.compactMap { getRelativePath(from: $0) },
Expand All @@ -75,7 +76,6 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
Task {
do {
try await handleJobSubmission()
updateStep(.processing(.success))
} catch let error as SmileIDError {
handleSubmissionFailure(error)
} catch {
Expand All @@ -89,17 +89,13 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {

private func handleJobSubmission() async throws {
try fetchRequiredFiles()

let zipData = try createZipData()

let authResponse = try await authenticate()

let preUploadResponse = try await prepareForUpload(authResponse: authResponse)

try await uploadFiles(zipData: zipData, uploadUrl: preUploadResponse.uploadUrl)
didSubmitBiometricJob = true

try moveJobToSubmittedDirectory()
let infoJson = try LocalStorage.createInfoJsonFile(
jobId: jobId,
idInfo: idInfo.copy(entered: true),
selfie: selfieFile,
livenessImages: livenessFiles
)
submitJob(jobId: jobId, skipApiSubmission: false, offlineMode: SmileID.allowOfflineMode)
}

private func fetchRequiredFiles() throws {
Expand All @@ -121,40 +117,6 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
}
}

private func createZipData() throws -> Data {
var allFiles = [URL]()
let infoJson = try LocalStorage.createInfoJsonFile(
jobId: jobId,
idInfo: idInfo.copy(entered: true),
selfie: selfieFile,
livenessImages: livenessFiles
)
if let selfieFile {
allFiles.append(contentsOf: [selfieFile, infoJson])
}
if let livenessFiles {
allFiles.append(contentsOf: livenessFiles)
}
return try LocalStorage.zipFiles(at: allFiles)
}

private func authenticate() async throws -> AuthenticationResponse {
let authRequest = AuthenticationRequest(
jobType: .biometricKyc,
enrollment: false,
jobId: jobId,
userId: userId,
country: idInfo.country,
idType: idInfo.idType
)

if SmileID.allowOfflineMode {
try saveOfflineJobIfAllowed()
}

return try await SmileID.api.authenticate(request: authRequest)
}

private func saveOfflineJobIfAllowed() throws {
try LocalStorage.saveOfflineJob(
jobId: jobId,
Expand All @@ -167,38 +129,8 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
)
}

private func prepareForUpload(authResponse: AuthenticationResponse) async throws -> PrepUploadResponse {
let prepUploadRequest = PrepUploadRequest(
partnerParams: authResponse.partnerParams.copy(extras: extraPartnerParams),
allowNewEnroll: String(allowNewEnroll), // TODO: - Fix when Michael changes this to boolean
metadata: localMetadata.metadata.items,
timestamp: authResponse.timestamp,
signature: authResponse.signature
)
do {
return try await SmileID.api.prepUpload(
request: prepUploadRequest
)
} catch let error as SmileIDError {
guard case let .api(errorCode, _) = error,
errorCode == "2215"
else {
throw error
}
return try await SmileID.api.prepUpload(
request: prepUploadRequest.copy(retry: "true"))
}
}

private func uploadFiles(zipData: Data, uploadUrl: String) async throws {
try await SmileID.api.upload(
zip: zipData,
to: uploadUrl
)
}

private func moveJobToSubmittedDirectory() throws {
try LocalStorage.moveToSubmittedJobs(jobId: self.jobId)
try LocalStorage.moveToSubmittedJobs(jobId: jobId)
}

private func updateStep(_ newStep: BiometricKycStep) {
Expand All @@ -220,7 +152,7 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
private func handleSubmissionFailure(_ smileIDError: SmileIDError) {
do {
_ = try LocalStorage.handleOfflineJobFailure(
jobId: self.jobId,
jobId: jobId,
error: smileIDError
)
} catch {
Expand All @@ -237,11 +169,72 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
didSubmitBiometricJob = false
print("Error submitting job: \(smileIDError)")
let (errorMessageRes, errorMessage) = toErrorMessage(error: smileIDError)
self.error = smileIDError
error = smileIDError
updateErrorMessages(errorMessage: errorMessage, errorMessageRes: errorMessageRes)
updateStep(.processing(.error))
}
}

override public func createSubmission() throws -> BaseJobSubmission<BiometricKycResult> {
guard let selfieImage = selfieFile,
livenessFiles != nil
else {
throw SmileIDError.unknown("Selfie images missing")
}
return BiometricKYCSubmission(
userId: userId,
jobId: jobId,
allowNewEnroll: allowNewEnroll,
livenessFiles: livenessFiles,
selfieFile: selfieImage,
idInfo: idInfo,
extraPartnerParams: extraPartnerParams,
metadata: localMetadata.metadata
)
}

override public func triggerProcessingState() {
DispatchQueue.main.async { self.step = .processing(ProcessingState.inProgress) }
}

override public func handleSuccess(data _: BiometricKycResult) {
DispatchQueue.main.async { self.step = .processing(ProcessingState.success) }
}

override public func handleError(error: Error) {
if let smileError = error as? SmileIDError {
print("Error submitting job: \(error)")
let (errorMessageRes, errorMessage) = toErrorMessage(error: smileError)
self.error = error
self.errorMessageRes = errorMessageRes
self.errorMessage = errorMessage
DispatchQueue.main.async { self.step = .processing(ProcessingState.error) }
} else {
print("Error submitting job: \(error)")
self.error = error
DispatchQueue.main.async { self.step = .processing(ProcessingState.error) }
}
}

override public func handleSubmissionFiles(jobId: String) throws {
selfieFile = try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie,
submitted: true
)
livenessFiles = try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness,
submitted: true
) ?? []
}

override public func handleOfflineSuccess() {
DispatchQueue.main.async {
self.errorMessageRes = "Offline.Message"
self.step = .processing(ProcessingState.success)
}
}
}

extension OrchestratedBiometricKycViewModel: SmartSelfieResultDelegate {
Expand Down
Loading
Loading