Skip to content

Commit

Permalink
added itemName in return value of points shop scanner to display to user
Browse files Browse the repository at this point in the history
  • Loading branch information
s0phialiu committed Feb 15, 2024
1 parent 61a655a commit cff00f2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions HIAPI/Models/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
10 changes: 6 additions & 4 deletions HIAPI/Models/Staff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

public static func recordUserAttendance(userToken: String, userId: String, eventId: String) -> APIRequest<UserAttendanceContainer> {
public static func recordUserAttendance(userToken: String, staffToken: String, eventId: String) -> APIRequest<DietaryRestrictions> {
var body = HTTPBody()
body["userId"] = userId
body["attendeeJWT"] = userToken
body["eventId"] = eventId
var headers = HTTPHeaders()
headers["Authorization"] = userToken
return APIRequest<UserAttendanceContainer>(service: self, endpoint: "scan-attendee/", body: body, headers: headers, method: .PUT)
return APIRequest<DietaryRestrictions>(service: self, endpoint: "scan-attendee/", body: body, headers: headers, method: .PUT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,15 @@ extension HIScanPointsShopViewController: AVCaptureMetadataOutputObjectsDelegate
}
}

func handlePointsShopAlert(status: String) {
print("hi")
func handlePointsShopAlert(status: String, itemName: String) {
print(status)
var alertTitle = ""
var alertMessage = ""
var error = true
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!"
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions HackIllinois/ViewControllers/HIScanQRCodeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyCancellable>()
var currentUserID = ""
var currentUserName = ""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cff00f2

Please sign in to comment.