From d82e313875afafdb2ab0318abc35cd6de08d9f48 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 4 Jul 2024 18:00:19 +0200 Subject: [PATCH] Rewrite data migration from dev to PROD, which IS enabled! --- PadelClub/Data/DataStore.swift | 1 - PadelClub/Utils/Patcher.swift | 114 +++++++++--------- PadelClub/Utils/URLs.swift | 6 +- .../Views/Tournament/Subscription/Guard.swift | 4 +- 4 files changed, 59 insertions(+), 66 deletions(-) diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index a7a722a..b55110f 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -62,7 +62,6 @@ class DataStore: ObservableObject { } #else StoreCenter.main.synchronizationApiURL = serverURL - #endif StoreCenter.main.logsFailedAPICalls() diff --git a/PadelClub/Utils/Patcher.swift b/PadelClub/Utils/Patcher.swift index 4596316..53945d1 100644 --- a/PadelClub/Utils/Patcher.swift +++ b/PadelClub/Utils/Patcher.swift @@ -8,6 +8,10 @@ import Foundation import LeStorage +enum PatchError: Error { + case patchError(message: String) +} + enum Patch: String, CaseIterable { case alexisLeDu case importDataFromDev @@ -27,23 +31,26 @@ class Patcher { static func patchIfPossible(_ patch: Patch) { if UserDefaults.standard.value(forKey: patch.id) == nil { - self._applyPatch(patch) - UserDefaults.standard.setValue(true, forKey: patch.id) + do { + Logger.log(">>> Patches \(patch.rawValue)...") + try self._applyPatch(patch) + 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 { case .alexisLeDu: self._patchAlexisLeDu() - case .importDataFromDev: self._importDataFromDev() + case .importDataFromDev: try self._importDataFromDev() } } fileprivate static func _patchAlexisLeDu() { guard StoreCenter.main.userId == "94f45ed2-8938-4c32-a4b6-e4525073dd33" else { return } - Logger.log(">>> Start patch...") - let clubs = DataStore.shared.clubs StoreCenter.main.resetApiCalls(collection: clubs) // 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 } + 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/") - if services.hasToken() { - - Task { - - let clubs: [Club] = try await services.get() - let events: [Event] = try await services.get() - let tournaments: [Tournament] = try await services.get() - let courts: [Court] = try await services.get() - let dateIntervals: [DateInterval] = try await services.get() + try StoreCenter.main.migrateToken(devServices) + + + let myClubs: [Club] = DataStore.shared.clubs.filter { $0.creator == userId } + let clubIds: [String] = myClubs.map { $0.id } - let clubIds = Set(events.compactMap { $0.club }) - let myClubs: [Club] = clubIds.compactMap { clubId in - clubs.first { $0.id == clubId } - } - myClubs.forEach { $0.creator = userId } - - try DataStore.shared.clubs.addOrUpdate(contentOfs: myClubs) - for club in myClubs { - let courts = courts.filter { $0.club == club.id } - try DataStore.shared.courts.addOrUpdate(contentOfs: courts) - } - - DataStore.shared.user.clubs.append(contentsOf: clubIds) - DataStore.shared.saveUser() - - events.forEach { $0.creator = userId } + 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)) - try DataStore.shared.events.addOrUpdate(contentOfs: events) - try DataStore.shared.tournaments.addOrUpdate(contentOfs: tournaments) - try DataStore.shared.dateIntervals.addOrUpdate(contentOfs: dateIntervals) - - for tournament in DataStore.shared.tournaments { - let store = tournament.tournamentStore - let identifier = StoreIdentifier(value: tournament.id, parameterName: "tournament") - - let rounds: [Round] = try await services.get(identifier: identifier) - let groupStages: [GroupStage] = try await services.get(identifier: identifier) - 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) - try store.rounds.addOrUpdate(contentOfs: rounds) - try store.groupStages.addOrUpdate(contentOfs: groupStages) - try store.matches.addOrUpdate(contentOfs: matches) - try store.playerRegistrations.addOrUpdate(contentOfs: playerRegistrations) - try store.teamScores.addOrUpdate(contentOfs: teamScores) - } + store.teamRegistrations.insertAllIntoCurrentService() + store.rounds.insertAllIntoCurrentService() + store.groupStages.insertAllIntoCurrentService() + store.matches.insertAllIntoCurrentService() + store.playerRegistrations.insertAllIntoCurrentService() + store.teamScores.insertAllIntoCurrentService() } - - } else { - Logger.log("no token for import") } } diff --git a/PadelClub/Utils/URLs.swift b/PadelClub/Utils/URLs.swift index dd36e02..654493e 100644 --- a/PadelClub/Utils/URLs.swift +++ b/PadelClub/Utils/URLs.swift @@ -8,10 +8,10 @@ import Foundation enum URLs: String, Identifiable { - case activationHost = "xlr.alwaysdata.net" + case activationHost = "https://padelclub.app" // xlr.alwaysdata.net case subscriptions = "https://apple.co/2Th4vqI" - case main = "https://xlr.alwaysdata.net/" - case api = "https://xlr.alwaysdata.net/roads/" + case main = "https://padelclub.app/" + case api = "https://padelclub.app/roads/" case beachPadel = "https://beach-padel.app.fft.fr/beachja/index/" //case padelClub = "https://padelclub.app" case tenup = "https://tenup.fft.fr" diff --git a/PadelClub/Views/Tournament/Subscription/Guard.swift b/PadelClub/Views/Tournament/Subscription/Guard.swift index a60ef55..36c77cd 100644 --- a/PadelClub/Views/Tournament/Subscription/Guard.swift +++ b/PadelClub/Views/Tournament/Subscription/Guard.swift @@ -108,7 +108,7 @@ import LeStorage } fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws { - if self.purchases.hasLoadedFromServer { + if self.purchases.hasLoaded { if self._purchaseById(transaction.originalID) == nil { let purchase: Purchase = try transaction.purchase() try self.purchases.addOrUpdate(instance: purchase) @@ -117,7 +117,7 @@ import LeStorage } fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws { - if self.purchases.hasLoadedFromServer { + if self.purchases.hasLoaded { if let existing: Purchase = self._purchaseById(transaction.originalID) { existing.revocationDate = transaction.revocationDate try self.purchases.addOrUpdate(instance: existing)