payfrit-beacon-ios/PayfritBeacon/Models/ServicePoint.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

43 lines
1.6 KiB
Swift

import Foundation
struct ServicePoint: Identifiable, Codable, Hashable {
let id: String
let name: String
let businessId: String
enum CodingKeys: String, CodingKey {
case servicePointId = "ServicePointID"
case altId = "ID"
case name = "Name"
case businessId = "BusinessID"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// ServicePointID (from list/save endpoints) or ID (fallback)
if let sid = try? container.decode(Int.self, forKey: .servicePointId) {
id = String(sid)
} else if let sid = try? container.decode(String.self, forKey: .servicePointId) {
id = sid
} 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 = (try? container.decode(String.self, forKey: .name)) ?? ""
if let bid = try? container.decode(Int.self, forKey: .businessId) {
businessId = String(bid)
} else {
businessId = (try? container.decode(String.self, forKey: .businessId)) ?? ""
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .servicePointId)
try container.encode(name, forKey: .name)
try container.encode(businessId, forKey: .businessId)
}
}