From 91e7411d3f79603e25703348f6d5264f0d5d4e43 Mon Sep 17 00:00:00 2001 From: yashjagtap23 <108370176+yashjagtap23@users.noreply.github.com> Date: Tue, 10 Dec 2024 19:05:39 -0600 Subject: [PATCH 1/4] works but without good error descriptions removed error descriptions for now to simplify. Error seems to be due to missing field in event.swift for boolean success --- HIAPI/Models/Event.swift | 4 +- HIAPI/Services/EventService.swift | 6 ++ HackIllinois/GoogleService-Info.plist | 2 +- .../HIScanAttendanceViewController.swift | 57 ++++++++++++------- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/HIAPI/Models/Event.swift b/HIAPI/Models/Event.swift index f12f48ac..f93542f5 100644 --- a/HIAPI/Models/Event.swift +++ b/HIAPI/Models/Event.swift @@ -130,5 +130,7 @@ public struct EventTracker: Codable { } public struct Attendance: Codable, APIReturnable { - public let status: String + public let success: Bool? // Present in success responses + public let error: String? // Present in error responses + public let message: String? // Present in error responses } diff --git a/HIAPI/Services/EventService.swift b/HIAPI/Services/EventService.swift index 57346616..b0398144 100644 --- a/HIAPI/Services/EventService.swift +++ b/HIAPI/Services/EventService.swift @@ -45,6 +45,12 @@ public class EventService: BaseService { body["eventId"] = eventId var headers = HTTPHeaders() headers["Authorization"] = userToken + print(APIRequest(service: self, endpoint: "staff/attendance/", body: body, headers: headers, method: .POST)) + // Debugging Logs + print("Preparing API Request") + print("Endpoint: /staff/attendance/") + print("Request Body: \(body)") + print("Authorization Header: \(headers)") return APIRequest(service: self, endpoint: "staff/attendance/", body: body, headers: headers, method: .POST) } diff --git a/HackIllinois/GoogleService-Info.plist b/HackIllinois/GoogleService-Info.plist index 7a60ff06..1c101523 100644 --- a/HackIllinois/GoogleService-Info.plist +++ b/HackIllinois/GoogleService-Info.plist @@ -3,7 +3,7 @@ API_KEY - placeholder + AIzaSyDbLOYdDFD5Ce8EexI7uziX7mmRj4dYZi4 GCM_SENDER_ID 848675954156 PLIST_VERSION diff --git a/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift b/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift index a966dc9d..ba8a969b 100644 --- a/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift +++ b/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift @@ -187,36 +187,45 @@ extension HIScanAttendanceViewController: AVCaptureMetadataOutputObjectsDelegate func handleStaffCheckInAlert(status: String) { var alertTitle = "Error!" var alertMessage = "" + self.respondingToQRCodeFound = true + switch status { - case "Success": + case "Check-in successful!": alertTitle = "Success!" alertMessage = "You have successfully checked in." self.respondingToQRCodeFound = false - case "Error info: invalidHTTPReponse(code: 400, description: \"bad request\")": - alertMessage = "QR code expired." - case "Error info: invalidHTTPReponse(code: 500, description: \"internal error\")": - alertMessage = "Internal error." - case "Error info: invalidHTTPReponse(code: 401, description: \"unauthorized\")": + case "CodeExpired": + alertMessage = "The code for this event has expired." + case "NotFound": + alertMessage = "Could not find event." + case "Unauthorized": alertMessage = "Invalid token." + case "InternalError": + alertMessage = "Internal server error." default: alertMessage = "Something isn't quite right." } + let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert) + if alertTitle == "Success!" { alert.addAction( UIAlertAction(title: "OK", style: .default, handler: { _ in self.dismiss(animated: true, completion: nil) - //Dismisses view controller + // Dismisses the view controller self.didSelectCloseButton(self.closeButton) NotificationCenter.default.post(name: .qrCodeSuccessfulScan, object: nil) - })) + }) + ) } else { alert.addAction( UIAlertAction(title: "OK", style: .default, handler: { _ in self.registerForKeyboardNotifications() - })) + }) + ) } + self.present(alert, animated: true, completion: nil) } @@ -235,17 +244,25 @@ extension HIScanAttendanceViewController: AVCaptureMetadataOutputObjectsDelegate var codeResult: Attendance? HIAPI.EventService.staffMeetingAttendanceCheckIn(userToken: String(user.token), eventId: eventId) .onCompletion { result in - do { - let (codeResult, _) = try result.get() - DispatchQueue.main.async { [self] in - self.handleStaffCheckInAlert(status: codeResult.status) - } - } catch { - DispatchQueue.main.async { [self] in - self.handleStaffCheckInAlert(status: "Error info: \(error)") - } - print("Error info: \(error)") - } + switch result { + case .success(let (attendance, _)): // `attendance` is already decoded into the `Attendance` model + print("Success Response: \(attendance)") + + DispatchQueue.main.async { + if let success = attendance.success, success { + self.handleStaffCheckInAlert(status: "Check-in successful!") + } else if let error = attendance.error { + self.handleStaffCheckInAlert(status: error) + } else { + self.handleStaffCheckInAlert(status: "Something isn't quite right") + } + } + case .failure(let error): + print("Request failed with error: \(error)") + DispatchQueue.main.async { + self.handleStaffCheckInAlert(status: "Network or API Error") + } + } sleep(2) } .authorize(with: HIApplicationStateController.shared.user) From c571f0956682622222d44a26f66245c3ec73bd82 Mon Sep 17 00:00:00 2001 From: yashjagtap23 <108370176+yashjagtap23@users.noreply.github.com> Date: Tue, 10 Dec 2024 19:56:15 -0600 Subject: [PATCH 2/4] added back specific errors --- .../HIScanAttendanceViewController.swift | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift b/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift index ba8a969b..63ff7179 100644 --- a/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift +++ b/HackIllinois/ViewControllers/HIScanAttendanceViewController.swift @@ -195,13 +195,13 @@ extension HIScanAttendanceViewController: AVCaptureMetadataOutputObjectsDelegate alertTitle = "Success!" alertMessage = "You have successfully checked in." self.respondingToQRCodeFound = false - case "CodeExpired": + case "QR code expired.": alertMessage = "The code for this event has expired." case "NotFound": alertMessage = "Could not find event." - case "Unauthorized": + case "Invalid token.": alertMessage = "Invalid token." - case "InternalError": + case "Internal server error.": alertMessage = "Internal server error." default: alertMessage = "Something isn't quite right." @@ -257,11 +257,25 @@ extension HIScanAttendanceViewController: AVCaptureMetadataOutputObjectsDelegate self.handleStaffCheckInAlert(status: "Something isn't quite right") } } - case .failure(let error): - print("Request failed with error: \(error)") - DispatchQueue.main.async { - self.handleStaffCheckInAlert(status: "Network or API Error") - } + case .failure(let error): // Handle failure responses + print("Request failed with error: \(error)") + + DispatchQueue.main.async { + // Extract error description + let rawErrorString = String(describing: error).lowercased() + print("Raw Error String: \(rawErrorString)") + if rawErrorString.contains("code: 400") { + self.handleStaffCheckInAlert(status: "QR code expired.") + } else if rawErrorString.contains("code: 401") { + self.handleStaffCheckInAlert(status: "Invalid token.") + } else if rawErrorString.contains("code: 500") { + self.handleStaffCheckInAlert(status: "Internal server error.") + } else if rawErrorString.contains("code: 402") { + self.handleStaffCheckInAlert(status: "NotFound") + } else { + self.handleStaffCheckInAlert(status: "Something isn't quite right.") + } + } } sleep(2) } From 26ebaecabb347ec3b3e05d902d9acc488c7d40fa Mon Sep 17 00:00:00 2001 From: yashjagtap23 <108370176+yashjagtap23@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:50:10 -0600 Subject: [PATCH 3/4] Removed unneccesary prints --- HIAPI/Services/EventService.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/HIAPI/Services/EventService.swift b/HIAPI/Services/EventService.swift index b0398144..41268c9b 100644 --- a/HIAPI/Services/EventService.swift +++ b/HIAPI/Services/EventService.swift @@ -46,11 +46,6 @@ public class EventService: BaseService { var headers = HTTPHeaders() headers["Authorization"] = userToken print(APIRequest(service: self, endpoint: "staff/attendance/", body: body, headers: headers, method: .POST)) - // Debugging Logs - print("Preparing API Request") - print("Endpoint: /staff/attendance/") - print("Request Body: \(body)") - print("Authorization Header: \(headers)") return APIRequest(service: self, endpoint: "staff/attendance/", body: body, headers: headers, method: .POST) } From 278979db4602222f22e3f0985b54e90b7c1c9960 Mon Sep 17 00:00:00 2001 From: anushkasankaran <55302555+anushkasankaran@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:50:37 -0600 Subject: [PATCH 4/4] Update GoogleService-Info.plist --- HackIllinois/GoogleService-Info.plist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HackIllinois/GoogleService-Info.plist b/HackIllinois/GoogleService-Info.plist index 1c101523..7a60ff06 100644 --- a/HackIllinois/GoogleService-Info.plist +++ b/HackIllinois/GoogleService-Info.plist @@ -3,7 +3,7 @@ API_KEY - AIzaSyDbLOYdDFD5Ce8EexI7uziX7mmRj4dYZi4 + placeholder GCM_SENDER_ID 848675954156 PLIST_VERSION