Adds patch to add storeId to needing entities

sync2
Laurent 11 months ago
parent ce4f0d11d7
commit 8851315cf4
  1. 2
      PadelClub/Data/DataStore.swift
  2. 171
      PadelClub/Utils/Patcher.swift

@ -127,7 +127,7 @@ class DataStore: ObservableObject {
self._fixMissingEventCreatorIfNecessary(eventsCollection) self._fixMissingEventCreatorIfNecessary(eventsCollection)
} }
if Store.main.collectionsAllLoaded() { if Store.main.fileCollectionsAllLoaded() {
Patcher.applyAllWhenApplicable() Patcher.applyAllWhenApplicable()
} }

@ -13,9 +13,8 @@ enum PatchError: Error {
} }
enum Patch: String, CaseIterable { enum Patch: String, CaseIterable {
case alexisLeDu case cleanLogs
case importDataFromDevToProd case syncUpgrade
case fixMissingMatches
var id: String { var id: String {
return "padelclub.app.patch.\(self.rawValue)" return "padelclub.app.patch.\(self.rawValue)"
@ -44,32 +43,162 @@ class Patcher {
fileprivate static func _applyPatch(_ patch: Patch) throws { fileprivate static func _applyPatch(_ patch: Patch) throws {
switch patch { switch patch {
case .alexisLeDu: self._patchAlexisLeDu() case .cleanLogs: self._cleanLogs()
case .importDataFromDevToProd: try self._importDataFromDev() case .syncUpgrade: self._syncUpgrade()
case .fixMissingMatches: self._patchMissingMatches()
} }
} }
//
// fileprivate static func _patchAlexisLeDu() {
// guard StoreCenter.main.userId == "94f45ed2-8938-4c32-a4b6-e4525073dd33" else { return }
//
// let clubs = DataStore.shared.clubs
// StoreCenter.main.resetApiCalls(collection: clubs)
//// clubs.resetApiCalls()
//
// for club in clubs.filter({ $0.creator == "d5060b89-e979-4c19-bf78-e459a6ed5318"}) {
// club.creator = StoreCenter.main.userId
// clubs.writeChangeAndInsertOnServer(instance: club)
// }
//
// }
//
// fileprivate static func _importDataFromDev() throws {
//
// let devServices = Services(url: "https://xlr.alwaysdata.net/roads/")
// guard devServices.hasToken() else {
// return
// }
// guard StoreCenter.main.synchronizationApiURL == "https://padelclub.app/roads/" else {
// return
// }
//
// guard let userId = StoreCenter.main.userId else {
// return
// }
//
// try StoreCenter.main.migrateToken(devServices)
//
//
// let myClubs: [Club] = DataStore.shared.clubs.filter { $0.creator == userId }
// let clubIds: [String] = myClubs.map { $0.id }
//
// myClubs.forEach { club in
// DataStore.shared.clubs.insertIntoCurrentService(item: club)
//
// let courts = DataStore.shared.courts.filter { clubIds.contains($0.club) }
// for court in courts {
// DataStore.shared.courts.insertIntoCurrentService(item: court)
// }
// }
//
// DataStore.shared.user.clubs = Array(clubIds)
// DataStore.shared.saveUser()
//
// DataStore.shared.events.insertAllIntoCurrentService()
// DataStore.shared.tournaments.insertAllIntoCurrentService()
// DataStore.shared.dateIntervals.insertAllIntoCurrentService()
//
// for tournament in DataStore.shared.tournaments {
// let store = tournament.tournamentStore
//
// Task { // need to wait for the collections to load
// try await Task.sleep(until: .now + .seconds(2))
//
// store.teamRegistrations.insertAllIntoCurrentService()
// store.rounds.insertAllIntoCurrentService()
// store.groupStages.insertAllIntoCurrentService()
// store.matches.insertAllIntoCurrentService()
// store.playerRegistrations.insertAllIntoCurrentService()
// store.teamScores.insertAllIntoCurrentService()
//
// }
// }
//
// }
//
// fileprivate static func _patchMissingMatches() {
//
// guard let url = StoreCenter.main.synchronizationApiURL else {
// return
// }
// guard url == "https://padelclub.app/roads/" else {
// return
// }
// let services = Services(url: url)
//
// for tournament in DataStore.shared.tournaments {
//
// let store = tournament.tournamentStore
// let identifier = StoreIdentifier(value: tournament.id, parameterName: "tournament")
//
// Task {
//
// do {
// // if nothing is online we upload the data
// let matches: [Match] = try await services.get(identifier: identifier)
// if matches.isEmpty {
// store.matches.insertAllIntoCurrentService()
// }
//
// let playerRegistrations: [PlayerRegistration] = try await services.get(identifier: identifier)
// if playerRegistrations.isEmpty {
// store.playerRegistrations.insertAllIntoCurrentService()
// }
//
// let teamScores: [TeamScore] = try await services.get(identifier: identifier)
// if teamScores.isEmpty {
// store.teamScores.insertAllIntoCurrentService()
// }
//
// } catch {
// Logger.error(error)
// }
//
// }
// }
//
// }
fileprivate static func _patchAlexisLeDu() { fileprivate static func _cleanLogs() {
guard StoreCenter.main.userId == "94f45ed2-8938-4c32-a4b6-e4525073dd33" else { return } // StoreCenter.main.resetLoggingCollections()
let clubs = DataStore.shared.clubs
StoreCenter.main.resetApiCalls(collection: clubs)
// clubs.resetApiCalls()
for club in clubs.filter({ $0.creator == "d5060b89-e979-4c19-bf78-e459a6ed5318"}) {
club.creator = StoreCenter.main.userId
clubs.writeChangeAndInsertOnServer(instance: club)
}
} }
fileprivate static func _importDataFromDev() throws { fileprivate static func _syncUpgrade() {
for tournament in DataStore.shared.tournaments {
let id = tournament.id
} let store = TournamentLibrary.shared.store(tournamentId: tournament.id)
fileprivate static func _patchMissingMatches() { for round in store.rounds {
round.storeId = id
}
store.rounds.addOrUpdate(contentOfs: store.rounds)
for groupStage in store.groupStages {
groupStage.storeId = id
}
store.groupStages.addOrUpdate(contentOfs: store.groupStages)
for teamRegistration in store.teamRegistrations {
teamRegistration.storeId = id
}
store.teamRegistrations.addOrUpdate(contentOfs: store.teamRegistrations)
for pr in store.playerRegistrations {
pr.storeId = id
}
store.playerRegistrations.addOrUpdate(contentOfs: store.playerRegistrations)
for match in store.matches {
match.storeId = id
}
store.matches.addOrUpdate(contentOfs: store.matches)
for ts in store.teamScores {
ts.storeId = id
}
store.teamScores.addOrUpdate(contentOfs: store.teamScores)
for ms in store.matchSchedulers {
ms.storeId = id
}
store.matchSchedulers.addOrUpdate(contentOfs: store.matchSchedulers)
}
} }
} }

Loading…
Cancel
Save