diff --git a/HIAPI/Models/Staff.swift b/HIAPI/Models/Staff.swift index 805c101f..91240c51 100644 --- a/HIAPI/Models/Staff.swift +++ b/HIAPI/Models/Staff.swift @@ -60,11 +60,11 @@ public struct Staff: Codable { public struct UserAttendanceContainer: Codable, APIReturnable { internal enum CodingKeys: String, CodingKey { case success - case error + case userId case dietaryRestrictions } public let success: Bool - public let error: String? + public let userId: String? public let dietaryRestrictions: [String]? } diff --git a/HIAPI/Services/StaffService.swift b/HIAPI/Services/StaffService.swift index c0e2ac7d..ecf67643 100644 --- a/HIAPI/Services/StaffService.swift +++ b/HIAPI/Services/StaffService.swift @@ -32,12 +32,13 @@ public final class StaffService: BaseService { return APIRequest(service: self, endpoint: "attendance/", body: body, headers: headers, method: .POST) } - public static func recordUserAttendance(userToken: String, staffToken: String, eventId: String) -> APIRequest { + public static func recordUserAttendance(userQR: String, staffToken: String, eventId: String) -> APIRequest { var body = HTTPBody() - body["attendeeJWT"] = userToken body["eventId"] = eventId + body["attendeeQRCode"] = userQR + var headers = HTTPHeaders() - headers["Authorization"] = userToken + headers["Authorization"] = staffToken return APIRequest(service: self, endpoint: "scan-attendee/", body: body, headers: headers, method: .PUT) } } diff --git a/HackIllinois/ViewControllers/HIProfileCardView.swift b/HackIllinois/ViewControllers/HIProfileCardView.swift index a9b89c0b..f5c3bdd7 100644 --- a/HackIllinois/ViewControllers/HIProfileCardView.swift +++ b/HackIllinois/ViewControllers/HIProfileCardView.swift @@ -58,7 +58,7 @@ struct HIProfileCardView: View { let isIpad = UIDevice.current.userInterfaceIdiom == .pad let role: String @State var startFetchingQR = false - @State var qrInfo = "hackillinois://user?userToken=11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" + @State var qrInfo = "hackillinois://user?userToken=111111111111111111111111111111111111111111111111111111111111111111111111111111111" // Factors used to change frame to alter based on device let padFactor = UIScreen.main.bounds.height/1366 let phoneFactor = UIScreen.main.bounds.height/844 @@ -229,6 +229,13 @@ struct HIProfileCardView: View { } .edgesIgnoringSafeArea(.bottom) // Extend to the bottom edge .offset(y: 30 * (UIScreen.main.bounds.height/926)) + .onAppear { + startFetchingQR = true + QRFetchLoop() + } + .onDisappear { + startFetchingQR = false + } } func formatName() -> String { diff --git a/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift b/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift index e9574fe7..20602e2f 100644 --- a/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift +++ b/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift @@ -392,49 +392,45 @@ extension HIScanQRCodeViewController: AVCaptureMetadataOutputObjectsDelegate { guard respondingToQRCodeFound else { return } let meta = metadataObjects.first as? AVMetadataMachineReadableCodeObject let code = meta?.stringValue ?? "" + let query = extractQueryValue(from: code) guard let user = HIApplicationStateController.shared.user else { return } let staffToken = user.token //print("staff token is:", staffToken, "event id is:", selectedEventId) print("qr code info is", code) if user.roles.contains(.STAFF) { if selectedEventId != "" { - if let range = code.range(of: "userToken=") { - let userToken = code[range.upperBound...] - respondingToQRCodeFound = false - HIAPI.StaffService.recordUserAttendance(userToken: String(userToken), staffToken: String(staffToken), eventId: selectedEventId) - .onCompletion { result in - do { - let (codeResult, _) = try result.get() - DispatchQueue.main.async { [self] in - print(codeResult.dietaryRestrictions) - // Parse dietary string - dietaryString = "" - if let dietaryRestrictions = codeResult.dietaryRestrictions, !dietaryRestrictions.isEmpty { - for (index, diet) in dietaryRestrictions.enumerated() { - dietaryString += diet - if index < dietaryRestrictions.count - 1 { - dietaryString += ", " - } + respondingToQRCodeFound = false + HIAPI.StaffService.recordUserAttendance(userQR: query ?? "", staffToken: String(staffToken), eventId: selectedEventId) + .onCompletion { result in + do { + let (codeResult, _) = try result.get() + DispatchQueue.main.async { [self] in + print(codeResult.dietaryRestrictions) + // Parse dietary string + dietaryString = "" + if let dietaryRestrictions = codeResult.dietaryRestrictions, !dietaryRestrictions.isEmpty { + for (index, diet) in dietaryRestrictions.enumerated() { + dietaryString += diet + if index < dietaryRestrictions.count - 1 { + dietaryString += ", " } - } else { - dietaryString = "None" } - self.handleStaffCheckInAlert(status: "Success") - } - } catch { - print(error, error.localizedDescription) - DispatchQueue.main.async { [self] in - self.handleStaffCheckInAlert(status: error.localizedDescription) + } else { + dietaryString = "None" } + self.handleStaffCheckInAlert(status: "Success") + } + } catch { + print(error, error.localizedDescription) + DispatchQueue.main.async { [self] in + self.handleStaffCheckInAlert(status: error.localizedDescription) } - sleep(2) - self.respondingToQRCodeFound = true } - .authorize(with: HIApplicationStateController.shared.user) - .launch() - } else { - self.handleStaffCheckInAlert(status: "Incorrect type of QR code") - } + sleep(2) + self.respondingToQRCodeFound = true + } + .authorize(with: HIApplicationStateController.shared.user) + .launch() } } else { respondingToQRCodeFound = false @@ -459,6 +455,15 @@ extension HIScanQRCodeViewController: AVCaptureMetadataOutputObjectsDelegate { .launch() } } + + func extractQueryValue(from url: String) -> String? { + guard let components = URLComponents(string: url), + let queryItem = components.queryItems?.first(where: { $0.name == "qr" }) else { + return nil + } + return queryItem.value + } + func decode(_ token: String) -> [String: AnyObject]? { let string = token.components(separatedBy: ".") if string.count == 1 { return nil }