online_reg^2
Laurent 11 months ago
parent 3991bb8650
commit 033901d97c
  1. 232
      PadelClub/Utils/Patcher.swift

@ -13,9 +13,6 @@ enum PatchError: Error {
} }
enum Patch: String, CaseIterable { enum Patch: String, CaseIterable {
case alexisLeDu
case importDataFromDevToProd
case fixMissingMatches
case cleanLogs case cleanLogs
var id: String { var id: String {
@ -32,7 +29,7 @@ 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 {
do { do {
Logger.log(">>> Patches \(patch.rawValue)...") Logger.log(">>> Patches \(patch.rawValue)...")
try self._applyPatch(patch) try self._applyPatch(patch)
@ -40,128 +37,125 @@ class Patcher {
} catch { } catch {
Logger.error(error) Logger.error(error)
} }
// } }
} }
fileprivate static func _applyPatch(_ patch: Patch) throws { fileprivate static func _applyPatch(_ patch: Patch) throws {
switch patch { switch patch {
case .alexisLeDu: self._patchAlexisLeDu()
case .importDataFromDevToProd: try self._importDataFromDev()
case .fixMissingMatches: self._patchMissingMatches()
case .cleanLogs: self._cleanLogs() case .cleanLogs: self._cleanLogs()
} }
} }
//
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 }
//
let clubs = DataStore.shared.clubs // let clubs = DataStore.shared.clubs
StoreCenter.main.resetApiCalls(collection: clubs) // StoreCenter.main.resetApiCalls(collection: clubs)
// clubs.resetApiCalls() //// clubs.resetApiCalls()
//
for club in clubs.filter({ $0.creator == "d5060b89-e979-4c19-bf78-e459a6ed5318"}) { // for club in clubs.filter({ $0.creator == "d5060b89-e979-4c19-bf78-e459a6ed5318"}) {
club.creator = StoreCenter.main.userId // club.creator = StoreCenter.main.userId
clubs.writeChangeAndInsertOnServer(instance: club) // clubs.writeChangeAndInsertOnServer(instance: club)
} // }
//
} // }
//
fileprivate static func _importDataFromDev() throws { // fileprivate static func _importDataFromDev() throws {
//
let devServices = Services(url: "https://xlr.alwaysdata.net/roads/") // let devServices = Services(url: "https://xlr.alwaysdata.net/roads/")
guard devServices.hasToken() else { // guard devServices.hasToken() else {
return // return
} // }
guard StoreCenter.main.synchronizationApiURL == "https://padelclub.app/roads/" else { // guard StoreCenter.main.synchronizationApiURL == "https://padelclub.app/roads/" else {
return // return
} // }
//
guard let userId = StoreCenter.main.userId else { // guard let userId = StoreCenter.main.userId else {
return // return
} // }
//
try StoreCenter.main.migrateToken(devServices) // try StoreCenter.main.migrateToken(devServices)
//
//
let myClubs: [Club] = DataStore.shared.clubs.filter { $0.creator == userId } // let myClubs: [Club] = DataStore.shared.clubs.filter { $0.creator == userId }
let clubIds: [String] = myClubs.map { $0.id } // let clubIds: [String] = myClubs.map { $0.id }
//
myClubs.forEach { club in // myClubs.forEach { club in
DataStore.shared.clubs.insertIntoCurrentService(item: club) // DataStore.shared.clubs.insertIntoCurrentService(item: club)
//
let courts = DataStore.shared.courts.filter { clubIds.contains($0.club) } // let courts = DataStore.shared.courts.filter { clubIds.contains($0.club) }
for court in courts { // for court in courts {
DataStore.shared.courts.insertIntoCurrentService(item: court) // DataStore.shared.courts.insertIntoCurrentService(item: court)
} // }
} // }
//
DataStore.shared.user.clubs = Array(clubIds) // DataStore.shared.user.clubs = Array(clubIds)
DataStore.shared.saveUser() // DataStore.shared.saveUser()
//
DataStore.shared.events.insertAllIntoCurrentService() // DataStore.shared.events.insertAllIntoCurrentService()
DataStore.shared.tournaments.insertAllIntoCurrentService() // DataStore.shared.tournaments.insertAllIntoCurrentService()
DataStore.shared.dateIntervals.insertAllIntoCurrentService() // DataStore.shared.dateIntervals.insertAllIntoCurrentService()
//
for tournament in DataStore.shared.tournaments { // for tournament in DataStore.shared.tournaments {
let store = tournament.tournamentStore // let store = tournament.tournamentStore
//
Task { // need to wait for the collections to load // Task { // need to wait for the collections to load
try await Task.sleep(until: .now + .seconds(2)) // try await Task.sleep(until: .now + .seconds(2))
//
store.teamRegistrations.insertAllIntoCurrentService() // store.teamRegistrations.insertAllIntoCurrentService()
store.rounds.insertAllIntoCurrentService() // store.rounds.insertAllIntoCurrentService()
store.groupStages.insertAllIntoCurrentService() // store.groupStages.insertAllIntoCurrentService()
store.matches.insertAllIntoCurrentService() // store.matches.insertAllIntoCurrentService()
store.playerRegistrations.insertAllIntoCurrentService() // store.playerRegistrations.insertAllIntoCurrentService()
store.teamScores.insertAllIntoCurrentService() // store.teamScores.insertAllIntoCurrentService()
//
} // }
} // }
//
} // }
//
fileprivate static func _patchMissingMatches() { // fileprivate static func _patchMissingMatches() {
//
guard let url = StoreCenter.main.synchronizationApiURL else { // guard let url = StoreCenter.main.synchronizationApiURL else {
return // return
} // }
guard url == "https://padelclub.app/roads/" else { // guard url == "https://padelclub.app/roads/" else {
return // return
} // }
let services = Services(url: url) // let services = Services(url: url)
//
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 identifier = StoreIdentifier(value: tournament.id, parameterName: "tournament")
//
Task { // Task {
//
do { // do {
// if nothing is online we upload the data // // if nothing is online we upload the data
let matches: [Match] = try await services.get(identifier: identifier) // let matches: [Match] = try await services.get(identifier: identifier)
if matches.isEmpty { // if matches.isEmpty {
store.matches.insertAllIntoCurrentService() // store.matches.insertAllIntoCurrentService()
} // }
//
let playerRegistrations: [PlayerRegistration] = try await services.get(identifier: identifier) // let playerRegistrations: [PlayerRegistration] = try await services.get(identifier: identifier)
if playerRegistrations.isEmpty { // if playerRegistrations.isEmpty {
store.playerRegistrations.insertAllIntoCurrentService() // store.playerRegistrations.insertAllIntoCurrentService()
} // }
//
let teamScores: [TeamScore] = try await services.get(identifier: identifier) // let teamScores: [TeamScore] = try await services.get(identifier: identifier)
if teamScores.isEmpty { // if teamScores.isEmpty {
store.teamScores.insertAllIntoCurrentService() // store.teamScores.insertAllIntoCurrentService()
} // }
//
} catch { // } catch {
Logger.error(error) // Logger.error(error)
} // }
//
} // }
} // }
//
} // }
fileprivate static func _cleanLogs() { fileprivate static func _cleanLogs() {
StoreCenter.main.resetLoggingCollections() StoreCenter.main.resetLoggingCollections()

Loading…
Cancel
Save