|
|
|
@ -8,6 +8,10 @@ |
|
|
|
import Foundation |
|
|
|
import Foundation |
|
|
|
import LeStorage |
|
|
|
import LeStorage |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum PatchError: Error { |
|
|
|
|
|
|
|
case patchError(message: String) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum Patch: String, CaseIterable { |
|
|
|
enum Patch: String, CaseIterable { |
|
|
|
case alexisLeDu |
|
|
|
case alexisLeDu |
|
|
|
case importDataFromDev |
|
|
|
case importDataFromDev |
|
|
|
@ -27,23 +31,26 @@ class Patcher { |
|
|
|
|
|
|
|
|
|
|
|
static func patchIfPossible(_ patch: Patch) { |
|
|
|
static func patchIfPossible(_ patch: Patch) { |
|
|
|
if UserDefaults.standard.value(forKey: patch.id) == nil { |
|
|
|
if UserDefaults.standard.value(forKey: patch.id) == nil { |
|
|
|
self._applyPatch(patch) |
|
|
|
do { |
|
|
|
|
|
|
|
Logger.log(">>> Patches \(patch.rawValue)...") |
|
|
|
|
|
|
|
try self._applyPatch(patch) |
|
|
|
UserDefaults.standard.setValue(true, forKey: patch.id) |
|
|
|
UserDefaults.standard.setValue(true, forKey: patch.id) |
|
|
|
|
|
|
|
} catch { |
|
|
|
|
|
|
|
Logger.error(error) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileprivate static func _applyPatch(_ patch: Patch) { |
|
|
|
fileprivate static func _applyPatch(_ patch: Patch) throws { |
|
|
|
switch patch { |
|
|
|
switch patch { |
|
|
|
case .alexisLeDu: self._patchAlexisLeDu() |
|
|
|
case .alexisLeDu: self._patchAlexisLeDu() |
|
|
|
case .importDataFromDev: self._importDataFromDev() |
|
|
|
case .importDataFromDev: try self._importDataFromDev() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileprivate static func _patchAlexisLeDu() { |
|
|
|
fileprivate static func _patchAlexisLeDu() { |
|
|
|
guard StoreCenter.main.userId == "94f45ed2-8938-4c32-a4b6-e4525073dd33" else { return } |
|
|
|
guard StoreCenter.main.userId == "94f45ed2-8938-4c32-a4b6-e4525073dd33" else { return } |
|
|
|
|
|
|
|
|
|
|
|
Logger.log(">>> Start patch...") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let clubs = DataStore.shared.clubs |
|
|
|
let clubs = DataStore.shared.clubs |
|
|
|
StoreCenter.main.resetApiCalls(collection: clubs) |
|
|
|
StoreCenter.main.resetApiCalls(collection: clubs) |
|
|
|
// clubs.resetApiCalls() |
|
|
|
// clubs.resetApiCalls() |
|
|
|
@ -55,69 +62,56 @@ class Patcher { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileprivate static func _importDataFromDev() { |
|
|
|
fileprivate static func _importDataFromDev() throws { |
|
|
|
|
|
|
|
|
|
|
|
guard let userId = StoreCenter.main.userId else { |
|
|
|
let devServices = Services(url: "https://xlr.alwaysdata.net/roads/") |
|
|
|
|
|
|
|
guard devServices.hasToken() else { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
guard URLs.api.rawValue == "https://padelclub.app/roads/" else { |
|
|
|
|
|
|
|
throw PatchError.patchError(message: "not on prod server") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// return // TODO review |
|
|
|
guard let userId = StoreCenter.main.userId else { |
|
|
|
|
|
|
|
throw PatchError.patchError(message: "no user ID") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let services = Services(url: "https://xlr.alwaysdata.net/roads/") |
|
|
|
try StoreCenter.main.migrateToken(devServices) |
|
|
|
if services.hasToken() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let clubs: [Club] = try await services.get() |
|
|
|
let myClubs: [Club] = DataStore.shared.clubs.filter { $0.creator == userId } |
|
|
|
let events: [Event] = try await services.get() |
|
|
|
let clubIds: [String] = myClubs.map { $0.id } |
|
|
|
let tournaments: [Tournament] = try await services.get() |
|
|
|
|
|
|
|
let courts: [Court] = try await services.get() |
|
|
|
|
|
|
|
let dateIntervals: [DateInterval] = try await services.get() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let clubIds = Set(events.compactMap { $0.club }) |
|
|
|
myClubs.forEach { club in |
|
|
|
let myClubs: [Club] = clubIds.compactMap { clubId in |
|
|
|
DataStore.shared.clubs.insertIntoCurrentService(item: club) |
|
|
|
clubs.first { $0.id == clubId } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
myClubs.forEach { $0.creator = userId } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try DataStore.shared.clubs.addOrUpdate(contentOfs: myClubs) |
|
|
|
let courts = DataStore.shared.courts.filter { clubIds.contains($0.club) } |
|
|
|
for club in myClubs { |
|
|
|
for court in courts { |
|
|
|
let courts = courts.filter { $0.club == club.id } |
|
|
|
DataStore.shared.courts.insertIntoCurrentService(item: court) |
|
|
|
try DataStore.shared.courts.addOrUpdate(contentOfs: courts) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
DataStore.shared.user.clubs.append(contentsOf: clubIds) |
|
|
|
DataStore.shared.user.clubs = Array(clubIds) |
|
|
|
DataStore.shared.saveUser() |
|
|
|
DataStore.shared.saveUser() |
|
|
|
|
|
|
|
|
|
|
|
events.forEach { $0.creator = userId } |
|
|
|
DataStore.shared.events.insertAllIntoCurrentService() |
|
|
|
|
|
|
|
DataStore.shared.tournaments.insertAllIntoCurrentService() |
|
|
|
try DataStore.shared.events.addOrUpdate(contentOfs: events) |
|
|
|
DataStore.shared.dateIntervals.insertAllIntoCurrentService() |
|
|
|
try DataStore.shared.tournaments.addOrUpdate(contentOfs: tournaments) |
|
|
|
|
|
|
|
try DataStore.shared.dateIntervals.addOrUpdate(contentOfs: dateIntervals) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for tournament in DataStore.shared.tournaments { |
|
|
|
for tournament in DataStore.shared.tournaments { |
|
|
|
let store = tournament.tournamentStore |
|
|
|
let store = tournament.tournamentStore |
|
|
|
let identifier = StoreIdentifier(value: tournament.id, parameterName: "tournament") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let rounds: [Round] = try await services.get(identifier: identifier) |
|
|
|
Task { // need to wait for the collections to load |
|
|
|
let groupStages: [GroupStage] = try await services.get(identifier: identifier) |
|
|
|
try await Task.sleep(until: .now + .seconds(2)) |
|
|
|
let matches: [Match] = try await services.get(identifier: identifier) |
|
|
|
|
|
|
|
let teamRegistrations: [TeamRegistration] = try await services.get(identifier: identifier) |
|
|
|
|
|
|
|
let teamScores: [TeamScore] = try await services.get(identifier: identifier) |
|
|
|
|
|
|
|
let playerRegistrations: [PlayerRegistration] = try await services.get(identifier: identifier) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try store.teamRegistrations.addOrUpdate(contentOfs: teamRegistrations) |
|
|
|
store.teamRegistrations.insertAllIntoCurrentService() |
|
|
|
try store.rounds.addOrUpdate(contentOfs: rounds) |
|
|
|
store.rounds.insertAllIntoCurrentService() |
|
|
|
try store.groupStages.addOrUpdate(contentOfs: groupStages) |
|
|
|
store.groupStages.insertAllIntoCurrentService() |
|
|
|
try store.matches.addOrUpdate(contentOfs: matches) |
|
|
|
store.matches.insertAllIntoCurrentService() |
|
|
|
try store.playerRegistrations.addOrUpdate(contentOfs: playerRegistrations) |
|
|
|
store.playerRegistrations.insertAllIntoCurrentService() |
|
|
|
try store.teamScores.addOrUpdate(contentOfs: teamScores) |
|
|
|
store.teamScores.insertAllIntoCurrentService() |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Logger.log("no token for import") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|