Repository Public
1//2// Food-Ka iOS v23// File: FoodKa/Core/Models/Stores.swift4// Description: enseignes (catalogue, niveau de prix relatif, fraîcheur du relevé) et circulaires en cours.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import Foundation1112struct Freshness: Decodable, Hashable, Sendable {13 var lastSuccess: Double?14 var ageHours: Double?15 var status: String?1617 init(from decoder: Decoder) throws {18 let c = try decoder.container(keyedBy: AnyKey.self)19 lastSuccess = c.dbl(AnyKey("last_success"))20 ageHours = c.dbl(AnyKey("age_hours"))21 status = c.text(AnyKey("status"))22 }2324 /// Libellé factuel de l'état du relevé.25 var label: String {26 switch status {27 case "a_jour": "Relevé à jour"28 case "en_retard": "Relevé en retard"29 case "en_panne": "Relevé en panne"30 case "jamais": "Jamais relevé"31 default: "État inconnu"32 }33 }3435 var isCurrent: Bool { status == "a_jour" }36}3738/// Niveau de prix relatif : 100 = médiane des enseignes sur les produits communs.39struct PriceLevel: Decodable, Hashable, Sendable {40 var index: Double?41 var nCommon: Int42 var rank: Int?43 var of: Int?4445 init(from decoder: Decoder) throws {46 let c = try decoder.container(keyedBy: AnyKey.self)47 index = c.dbl(AnyKey("index"))48 nCommon = c.int(AnyKey("n_common")) ?? 049 rank = c.int(AnyKey("rank"))50 of = c.int(AnyKey("of"))51 }52}5354struct Store: Decodable, Hashable, Sendable, Identifiable {55 struct Named: Decodable, Hashable, Sendable, Identifiable {56 var name: String57 var n: Int58 var id: String { name }59 }6061 struct SourceDetail: Decodable, Hashable, Sendable, Identifiable {62 var id: String63 var name: String?64 var status: String?65 var notes: String?66 var lastSuccess: Double?67 var lastFound: Int?68 var lastOk: Bool?6970 init(from decoder: Decoder) throws {71 let c = try decoder.container(keyedBy: AnyKey.self)72 id = c.text(AnyKey("id")) ?? UUID().uuidString73 name = c.text(AnyKey("name"))74 status = c.text(AnyKey("status"))75 notes = c.text(AnyKey("notes"))76 lastSuccess = c.dbl(AnyKey("last_success"))77 lastFound = c.int(AnyKey("last_found"))78 lastOk = c.bool(AnyKey("last_ok"))79 }80 }8182 var id: String83 var name: String84 var type: String?85 var typeLabel: String?86 var url: String?87 var region: String?88 var connected: Bool89 /// faux : prix relevés hors Québec, enseigne sortie de toute comparaison90 var comparable: Bool91 var n: Int92 var nCatalogue: Int93 var nFlyer: Int94 var nDeals: Int95 var freshness: Freshness?96 var priceLevel: PriceLevel?97 var priceScope: PriceScope?98 /// couverture du relevé : `complet`, `echantillon`, `circulaire`99 var coverage: String?100 /// état du relevé : `a_jour`, `en_retard`, `en_panne`, `non_connectable`101 var state: String?102 var priceLevelByCategory: [CategoryLevel]103104 struct CategoryLevel: Decodable, Hashable, Sendable, Identifiable {105 var rayon: String106 var slug: String107 var index: Double?108 var n_common: Int?109 var id: String { slug }110 }111 // détail d'une enseigne112 var categories: [Category]113 var brands: [Named]114 var topDeals: [Product]115 var sample: [Product]116 var sourceDetails: [SourceDetail]117118 init(from decoder: Decoder) throws {119 let c = try decoder.container(keyedBy: AnyKey.self)120 guard let id = c.text(AnyKey("id")) else {121 throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "enseigne sans identifiant"))122 }123 self.id = id124 name = c.text(AnyKey("name")) ?? id125 type = c.text(AnyKey("type"))126 typeLabel = c.text(AnyKey("type_label"))127 url = c.text(AnyKey("url"))128 region = c.text(AnyKey("region"))129 connected = c.bool(AnyKey("connected")) ?? true130 comparable = c.bool(AnyKey("comparable")) ?? true131 n = c.int(AnyKey("n")) ?? 0132 nCatalogue = c.int(AnyKey("n_catalogue")) ?? 0133 nFlyer = c.int(AnyKey("n_flyer")) ?? 0134 nDeals = c.int(AnyKey("n_deals")) ?? 0135 freshness = c.opt(AnyKey("freshness"))136 priceLevel = c.opt(AnyKey("price_level"))137 priceScope = c.opt(AnyKey("price_scope"))138 coverage = c.text(AnyKey("coverage"))139 state = c.text(AnyKey("state"))140 priceLevelByCategory = c.list(AnyKey("price_level_by_category"), CategoryLevel.self)141 categories = c.list(AnyKey("categories"), Category.self)142 brands = c.list(AnyKey("brands"), Named.self)143 topDeals = c.list(AnyKey("top_deals"), Product.self)144 sample = c.list(AnyKey("sample"), Product.self)145 sourceDetails = c.list(AnyKey("source_details"), SourceDetail.self)146 }147}148149struct StoresResponse: Decodable, Sendable {150 var total: Int151 var connected: Int152 var nProducts: Int153 var stores: [Store]154 var typeLabels: [String: String]155 var priceLevelMethod: String?156 var coverageLabels: [String: String]157158 init(from decoder: Decoder) throws {159 let c = try decoder.container(keyedBy: AnyKey.self)160 total = c.int(AnyKey("total")) ?? 0161 connected = c.int(AnyKey("connected")) ?? 0162 nProducts = c.int(AnyKey("n_products")) ?? 0163 stores = c.list(AnyKey("stores"), Store.self)164 typeLabels = c.opt(AnyKey("type_labels")) ?? [:]165 priceLevelMethod = c.text(AnyKey("price_level_method"))166 coverageLabels = c.opt(AnyKey("coverage_labels")) ?? [:]167 }168169 /// Enseignes dont le niveau de prix est mesuré, de la moins chère à la plus chère.170 var ranked: [Store] {171 stores.filter { $0.priceLevel?.index != nil }.sorted { ($0.priceLevel?.index ?? 0) < ($1.priceLevel?.index ?? 0) }172 }173}174175/// Circulaire en cours d'une enseigne.176struct Flyer: Decodable, Hashable, Sendable, Identifiable {177 var store: String178 var storeName: String179 var type: String?180 var validFrom: Double?181 var validTo: Double?182 var n: Int183 var nVerifiedDeals: Int184 var nUpcoming: Int185 var highlights: [Product]186 var products: [Product]187 var nExpired: Int188 /// fin du rabais le plus long (certains durent plus que le cahier principal)189 var validToMax: Double?190 var longValidityItems: Int191 /// rabais d'un cahier publié mais pas encore en vigueur192 var upcomingProducts: [Product]193 var id: String { store }194195 init(from decoder: Decoder) throws {196 let c = try decoder.container(keyedBy: AnyKey.self)197 guard let s = c.text(AnyKey("store")) else {198 throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "circulaire sans enseigne"))199 }200 store = s201 storeName = c.text(AnyKey("store_name")) ?? s202 type = c.text(AnyKey("type"))203 validFrom = c.dbl(AnyKey("valid_from"))204 validTo = c.dbl(AnyKey("valid_to"))205 n = c.int(AnyKey("n")) ?? 0206 nVerifiedDeals = c.int(AnyKey("n_verified_deals")) ?? 0207 nUpcoming = c.int(AnyKey("n_upcoming")) ?? 0208 highlights = c.list(AnyKey("highlights"), Product.self)209 products = c.list(AnyKey("products"), Product.self)210 nExpired = c.int(AnyKey("n_expired")) ?? 0211 validToMax = c.dbl(AnyKey("valid_to_max"))212 longValidityItems = c.int(AnyKey("long_validity_items")) ?? 0213 upcomingProducts = c.list(AnyKey("upcoming_products"), Product.self)214 }215}216217struct FlyersResponse: Decodable, Sendable {218 var total: Int219 var nProducts: Int220 var flyers: [Flyer]221 var note: String?222 var nUpcoming: Int223 var nExpired: Int224225 init(from decoder: Decoder) throws {226 let c = try decoder.container(keyedBy: AnyKey.self)227 nUpcoming = c.int(AnyKey("n_upcoming")) ?? 0228 nExpired = c.int(AnyKey("n_expired")) ?? 0229 total = c.int(AnyKey("total")) ?? 0230 nProducts = c.int(AnyKey("n_products")) ?? 0231 flyers = c.list(AnyKey("flyers"), Flyer.self)232 note = c.text(AnyKey("note"))233 }234}235