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

Anushka/check in fix #627

Merged
merged 5 commits into from
Feb 20, 2025
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: 2 additions & 2 deletions HIAPI/Models/Staff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]?
}

Expand Down
7 changes: 4 additions & 3 deletions HIAPI/Services/StaffService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public final class StaffService: BaseService {
return APIRequest<StaffAttendanceContainer>(service: self, endpoint: "attendance/", body: body, headers: headers, method: .POST)
}

public static func recordUserAttendance(userToken: String, staffToken: String, eventId: String) -> APIRequest<UserAttendanceContainer> {
public static func recordUserAttendance(userQR: String, staffToken: String, eventId: String) -> APIRequest<UserAttendanceContainer> {
var body = HTTPBody()
body["attendeeJWT"] = userToken
body["eventId"] = eventId
body["attendeeQRCode"] = userQR

var headers = HTTPHeaders()
headers["Authorization"] = userToken
headers["Authorization"] = staffToken
return APIRequest<UserAttendanceContainer>(service: self, endpoint: "scan-attendee/", body: body, headers: headers, method: .PUT)
}
}
9 changes: 8 additions & 1 deletion HackIllinois/ViewControllers/HIProfileCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
69 changes: 37 additions & 32 deletions HackIllinois/ViewControllers/HIScanQRCodeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand Down