-
Notifications
You must be signed in to change notification settings - Fork 4
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
JNdhlovu
wants to merge
7
commits into
main
Choose a base branch
from
feature/better_error_handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,250
−396
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5d51b54
feat: ios
JNdhlovu 046089a
feat: ios submissions for biometric kyc
JNdhlovu 2cee84d
feat: ios docv
JNdhlovu d22c8c7
feat: job submissions
JNdhlovu 777701d
feat: merge main
JNdhlovu 4ed768b
feat: lint files
JNdhlovu ecc3468
Merge branch 'main' into feature/better_error_handling
jumaallan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
delegate.didSucceed( | ||
selfieImage: selfiePath, | ||
livenessImages: livenessFiles.compactMap { getRelativePath(from: $0) }, | ||
|
@@ -75,7 +76,6 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject { | |
Task { | ||
do { | ||
try await handleJobSubmission() | ||
updateStep(.processing(.success)) | ||
} catch let error as SmileIDError { | ||
handleSubmissionFailure(error) | ||
} catch { | ||
|
@@ -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 { | ||
|
@@ -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, | ||
|
@@ -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) { | ||
|
@@ -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 { | ||
|
@@ -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 { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we marking these as non internal?