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

Fixed Staff Attendance Error #600

Merged
merged 4 commits into from
Dec 11, 2024
Merged
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
4 changes: 3 additions & 1 deletion HIAPI/Models/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions HIAPI/Services/EventService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class EventService: BaseService {
body["eventId"] = eventId
var headers = HTTPHeaders()
headers["Authorization"] = userToken
print(APIRequest<Attendance>(service: self, endpoint: "staff/attendance/", body: body, headers: headers, method: .POST))
return APIRequest<Attendance>(service: self, endpoint: "staff/attendance/", body: body, headers: headers, method: .POST)
}

Expand Down
71 changes: 51 additions & 20 deletions HackIllinois/ViewControllers/HIScanAttendanceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 "QR code expired.":
alertMessage = "The code for this event has expired."
case "NotFound":
alertMessage = "Could not find event."
case "Invalid token.":
alertMessage = "Invalid token."
case "Internal server error.":
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)
}

Expand All @@ -235,17 +244,39 @@ 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): // 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)
}
.authorize(with: HIApplicationStateController.shared.user)
Expand Down
Loading