payfrit-beacon-ios/PayfritBeacon/Models/Business.swift
Schwifty a1c215eb42 fix: decode actual API response format (OK + flat keys, not Success/Data)
The API returns {OK: true, BUSINESSES: [...]} but the iOS client was
decoding {Success: true, Data: [...]} which never matched — causing
"Failed to load businesses" on every call. Also fixes Business model
(BusinessID/Name vs ID/BusinessName) and ServicePoint model
(ServicePointID vs ID). All response decoders now match the real API.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 21:26:42 +00:00

49 lines
1.9 KiB
Swift

import Foundation
struct Business: Identifiable, Codable, Hashable {
let id: String
let name: String
let imageExtension: String?
enum CodingKeys: String, CodingKey {
case businessId = "BusinessID"
case name = "Name"
// Fallbacks for alternate API shapes
case altId = "ID"
case altName = "BusinessName"
case imageExtension = "ImageExtension"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// BusinessID (from list endpoint) or ID (from other endpoints), always as String
if let bid = try? container.decode(Int.self, forKey: .businessId) {
id = String(bid)
} else if let bid = try? container.decode(String.self, forKey: .businessId) {
id = bid
} else if let aid = try? container.decode(Int.self, forKey: .altId) {
id = String(aid)
} else if let aid = try? container.decode(String.self, forKey: .altId) {
id = aid
} else {
id = ""
}
// Name (from list endpoint) or BusinessName (from other endpoints)
name = (try? container.decode(String.self, forKey: .name))
?? (try? container.decode(String.self, forKey: .altName))
?? ""
imageExtension = try? container.decode(String.self, forKey: .imageExtension)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .businessId)
try container.encode(name, forKey: .name)
try container.encodeIfPresent(imageExtension, forKey: .imageExtension)
}
var headerImageURL: URL? {
guard let ext = imageExtension, !ext.isEmpty else { return nil }
return URL(string: "\(APIConfig.imageBaseURL)/businesses/\(id)/header.\(ext)")
}
}