From cff00f2f04d7ca3a48d4b50fd1907a30c8b55ba1 Mon Sep 17 00:00:00 2001 From: Sophia Liu Date: Thu, 15 Feb 2024 02:07:06 -0600 Subject: [PATCH] added itemName in return value of points shop scanner to display to user --- HIAPI/Models/Item.swift | 1 + HIAPI/Models/Staff.swift | 10 ++++---- HIAPI/Services/StaffService.swift | 6 ++--- .../HIScanPointsShopViewController.swift | 10 ++++---- .../HIScanQRCodeViewController.swift | 24 +++++++++---------- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/HIAPI/Models/Item.swift b/HIAPI/Models/Item.swift index aa196a53..aac82d2b 100644 --- a/HIAPI/Models/Item.swift +++ b/HIAPI/Models/Item.swift @@ -35,6 +35,7 @@ public struct Item: Codable { } public struct RedeemItem: Codable, APIReturnable { + public let itemName: String? // Return itemName upon success public let success: Bool public let error: String? } diff --git a/HIAPI/Models/Staff.swift b/HIAPI/Models/Staff.swift index dfefdc21..78320134 100644 --- a/HIAPI/Models/Staff.swift +++ b/HIAPI/Models/Staff.swift @@ -58,12 +58,14 @@ public struct Staff: Codable { } -public struct UserAttendanceContainer: Codable, APIReturnable { +/*public struct UserAttendanceContainer: Codable, APIReturnable { internal enum CodingKeys: String, CodingKey { - case sucess + case success + case dietaryRestrictions } - public let sucess: Bool -} + public let success: Bool? + public let dietaryRestrictions: [AnyObject] +}*/ public struct StaffAttendanceContainer: Codable, APIReturnable { diff --git a/HIAPI/Services/StaffService.swift b/HIAPI/Services/StaffService.swift index d3dbb2ed..88fd9495 100644 --- a/HIAPI/Services/StaffService.swift +++ b/HIAPI/Services/StaffService.swift @@ -32,12 +32,12 @@ public final class StaffService: BaseService { return APIRequest(service: self, endpoint: "attendance/", body: body, headers: headers, method: .POST) } - public static func recordUserAttendance(userToken: String, userId: String, eventId: String) -> APIRequest { + public static func recordUserAttendance(userToken: String, staffToken: String, eventId: String) -> APIRequest { var body = HTTPBody() - body["userId"] = userId + body["attendeeJWT"] = userToken body["eventId"] = eventId var headers = HTTPHeaders() headers["Authorization"] = userToken - return APIRequest(service: self, endpoint: "scan-attendee/", body: body, headers: headers, method: .PUT) + return APIRequest(service: self, endpoint: "scan-attendee/", body: body, headers: headers, method: .PUT) } } diff --git a/HackIllinois/ViewControllers/HIScanPointsShopViewController.swift b/HackIllinois/ViewControllers/HIScanPointsShopViewController.swift index 779747fc..5a8fdbab 100644 --- a/HackIllinois/ViewControllers/HIScanPointsShopViewController.swift +++ b/HackIllinois/ViewControllers/HIScanPointsShopViewController.swift @@ -198,8 +198,7 @@ extension HIScanPointsShopViewController: AVCaptureMetadataOutputObjectsDelegate } } - func handlePointsShopAlert(status: String) { - print("hi") + func handlePointsShopAlert(status: String, itemName: String) { print(status) var alertTitle = "" var alertMessage = "" @@ -207,7 +206,7 @@ extension HIScanPointsShopViewController: AVCaptureMetadataOutputObjectsDelegate switch status { case "Success": alertTitle = "\n\nPrize Obtained!" - alertMessage = "\nYou have successfully redeemed your points at the Points Shop!" + alertMessage = "\nYou have successfully redeemed \(itemName) at the Points Shop!" error = false case "invalidHTTPReponse(code: 404, description: \"forbidden\")": alertTitle = "\n\nError!" @@ -282,14 +281,15 @@ extension HIScanPointsShopViewController: AVCaptureMetadataOutputObjectsDelegate do { let (codeResult, _) = try result.get() let status = codeResult.error + let itemName = codeResult.itemName NSLog(status ?? "Success") DispatchQueue.main.async { - self.handlePointsShopAlert(status: status ?? "Success") + self.handlePointsShopAlert(status: status ?? "Success", itemName: itemName ?? "") } } catch { NSLog("Error info: \(error)") DispatchQueue.main.async { [self] in - self.handlePointsShopAlert(status: "\(error)") + self.handlePointsShopAlert(status: "\(error)", itemName: "") } } sleep(2) diff --git a/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift b/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift index 8b639afc..345c0104 100644 --- a/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift +++ b/HackIllinois/ViewControllers/HIScanQRCodeViewController.swift @@ -40,7 +40,7 @@ class HIScanQRCodeViewController: HIBaseViewController { $0.baseImage = #imageLiteral(resourceName: "CloseButton") } private let errorView = HIErrorView(style: .codePopup) - private var selectedEventID = "" + private var selectedEventId = "" private var cancellables = Set() var currentUserID = "" var currentUserName = "" @@ -68,8 +68,8 @@ extension HIScanQRCodeViewController { setupCaptureSession() if user.roles.contains(.STAFF) { let observable = HIStaffButtonViewObservable() - observable.$selectedEventId.sink { eventID in - self.selectedEventID = eventID + observable.$selectedEventId.sink { eventId in + self.selectedEventId = eventId }.store(in: &cancellables) let staffButtonController = UIHostingController(rootView: HIStaffButtonView(observable: observable)) addChild(staffButtonController) @@ -333,32 +333,32 @@ extension HIScanQRCodeViewController: AVCaptureMetadataOutputObjectsDelegate { let meta = metadataObjects.first as? AVMetadataMachineReadableCodeObject let code = meta?.stringValue ?? "" guard let user = HIApplicationStateController.shared.user else { return } + let staffToken = user.token + print("staff token is:", staffToken) if user.roles.contains(.STAFF) { - if selectedEventID != "" { + if selectedEventId != "" { + print("event id is", selectedEventId) if let range = code.range(of: "userToken=") { let userToken = code[range.upperBound...] respondingToQRCodeFound = false - HIAPI.EventService.staffCheckIn(userToken: String(userToken), eventId: selectedEventID) + HIAPI.StaffService.recordUserAttendance(userToken: String(userToken), staffToken: String(staffToken), eventId: selectedEventId) .onCompletion { result in do { let (codeResult, _) = try result.get() + print("code result", codeResult) DispatchQueue.main.async { [self] in - if let qrInfo = self.decode(code) { - if let userId = qrInfo["userId"] { - currentUserID = userId as? String ?? "" - staffCheckIn(userID: currentUserID, status: codeResult.status) - } - } + handleStaffCheckInAlert(status: "Success") } } catch { print(error, error.localizedDescription) + self.handleStaffCheckInAlert(status: error.localizedDescription) } sleep(2) } .authorize(with: HIApplicationStateController.shared.user) .launch() + } } - } } else { respondingToQRCodeFound = false HIAPI.EventService.checkIn(code: code)