COVIDSafe code from version 2.2 (#42)

This commit is contained in:
COVIDSafe Support 2021-02-02 11:04:43 +11:00 committed by GitHub
parent 9e6e4604ef
commit f14aa60482
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
67 changed files with 3645 additions and 464 deletions

View file

@ -11,11 +11,11 @@ import KeychainSwift
final class UploadHelper {
public static func uploadEncounterData(pin: String?, _ result: @escaping (UploadResult) -> Void) {
public static func uploadEncounterData(pin: String?, _ result: @escaping (UploadResult, String?) -> Void) {
let keychain = KeychainSwift()
guard let managedContext = EncounterDB.shared.persistentContainer?.viewContext else {
result(.Failed)
result(.Failed, "[001]")
return
}
@ -25,7 +25,7 @@ final class UploadHelper {
managedContext.perform {
guard let records = try? recordsFetchRequest.execute() else {
DLog("Error fetching records")
result(.Failed)
result(.Failed, "[002]")
return
}
@ -34,37 +34,43 @@ final class UploadHelper {
let encoder = JSONEncoder()
guard let json = try? encoder.encode(data) else {
DLog("Error serializing data")
result(.Failed)
result(.Failed, "[003]")
return
}
guard let jwt = keychain.get("JWT_TOKEN") else {
DLog("Error trying to upload when not logged in")
result(.Failed)
result(.SessionExpired, "Error retrieving token, please log in.")
return
}
InitiateUploadAPI.initiateUploadAPI(session: jwt, pin: pin) { (uploadResponse, error) in
InitiateUploadAPI.initiateUploadAPI(session: jwt, pin: pin) { (uploadResponse, error, message) in
guard error == nil else {
if (error == .ExpireSession) {
result(.SessionExpired)
result(.SessionExpired, message)
return
}
result(.InvalidCode)
if let message = message, message.contains("InvalidPin") && error == .ServerError {
result(.InvalidCode, message)
return
}
result(.Failed, message)
return
}
guard let response = uploadResponse else {
// if we fail to get a link back then the otp was potentially invalid
result(.InvalidCode)
result(.Failed, message)
return
}
DataUploadS3.uploadJSONData(data: json, presignedUrl: response.UploadLink) { (isSuccessful, error) in
DataUploadS3.uploadJSONData(data: json, presignedUrl: response.UploadLink) { (isSuccessful, error, message) in
guard isSuccessful else {
DLog("Error uploading file - \(String(describing: error))")
result(.FailedUpload)
result(.FailedUpload, message)
return
}
result(.Success)
result(.Success, nil)
}
}
}