parent
ea370cec79
commit
54e872622f
@ -0,0 +1,126 @@ |
||||
// |
||||
// ConfigurationService.swift |
||||
// PadelClub |
||||
// |
||||
// Created by razmig on 14/04/2025. |
||||
// |
||||
|
||||
import Foundation |
||||
import LeStorage |
||||
|
||||
class ConfigurationService { |
||||
static func fetchTournamentConfig() async throws -> TimeToConfirmConfig { |
||||
let service = try StoreCenter.main.service() |
||||
let urlRequest = try service._baseRequest(servicePath: "config/tournament/", method: .get, requiresToken: true) |
||||
let (data, _) = try await URLSession.shared.data(for: urlRequest) |
||||
return try JSONDecoder().decode(TimeToConfirmConfig.self, from: data) |
||||
} |
||||
|
||||
static func fetchPaymentConfig() async throws -> PaymentConfig { |
||||
let service = try StoreCenter.main.service() |
||||
let urlRequest = try service._baseRequest(servicePath: "config/payment/", method: .get, requiresToken: true) |
||||
let (data, _) = try await URLSession.shared.data(for: urlRequest) |
||||
return try JSONDecoder().decode(PaymentConfig.self, from: data) |
||||
} |
||||
} |
||||
|
||||
struct TimeToConfirmConfig: Codable { |
||||
let timeProximityRules: [String: Int] |
||||
let waitingListRules: [String: Int] |
||||
let businessRules: BusinessRules |
||||
let urgencyOverride: UrgencyOverride |
||||
|
||||
private enum CodingKeys: String, CodingKey { |
||||
case timeProximityRules = "time_proximity_rules" |
||||
case waitingListRules = "waiting_list_rules" |
||||
case businessRules = "business_rules" |
||||
case urgencyOverride = "urgency_override" |
||||
} |
||||
|
||||
// Default configuration |
||||
static let defaultConfig = TimeToConfirmConfig( |
||||
timeProximityRules: [ |
||||
"24": 30, // within 24h → 30 min |
||||
"48": 60, // within 48h → 60 min |
||||
"72": 120, // within 72h → 120 min |
||||
"default": 240 |
||||
], |
||||
waitingListRules: [ |
||||
"30": 30, // 30+ teams → 30 min |
||||
"20": 60, // 20+ teams → 60 min |
||||
"10": 120, // 10+ teams → 120 min |
||||
"default": 240 |
||||
], |
||||
businessRules: BusinessRules( |
||||
hours: Hours( |
||||
start: 8, |
||||
end: 21, |
||||
defaultConfirmationHour: 8 |
||||
), |
||||
days: Days( |
||||
workingDays: [0, 1, 2, 3, 4, 5, 6], |
||||
weekend: [] |
||||
) |
||||
), |
||||
urgencyOverride: UrgencyOverride( |
||||
thresholds: [ |
||||
"24": true, |
||||
"12": true |
||||
], |
||||
minimumResponseTime: 30 |
||||
) |
||||
) |
||||
|
||||
} |
||||
|
||||
struct BusinessRules: Codable { |
||||
let hours: Hours |
||||
let days: Days |
||||
} |
||||
|
||||
struct Hours: Codable { |
||||
let start: Int |
||||
let end: Int |
||||
let defaultConfirmationHour: Int |
||||
|
||||
private enum CodingKeys: String, CodingKey { |
||||
case start |
||||
case end |
||||
case defaultConfirmationHour = "default_confirmation_hour" |
||||
} |
||||
|
||||
} |
||||
|
||||
struct Days: Codable { |
||||
let workingDays: [Int] |
||||
let weekend: [Int] |
||||
|
||||
private enum CodingKeys: String, CodingKey { |
||||
case workingDays = "working_days" |
||||
case weekend |
||||
} |
||||
|
||||
} |
||||
|
||||
struct UrgencyOverride: Codable { |
||||
let thresholds: [String: Bool] |
||||
let minimumResponseTime: Int |
||||
|
||||
private enum CodingKeys: String, CodingKey { |
||||
case thresholds |
||||
case minimumResponseTime = "minimum_response_time" |
||||
} |
||||
|
||||
} |
||||
|
||||
struct PaymentConfig: Codable { |
||||
let stripeFee: Double |
||||
|
||||
// Default configuration |
||||
static let defaultConfig = PaymentConfig(stripeFee: 0.0075) |
||||
|
||||
private enum CodingKeys: String, CodingKey { |
||||
case stripeFee = "stripe_fee" |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue