Rewrite data migration from dev to PROD, which IS enabled!

club_update
Laurent 1 year ago
parent a972dbfdd9
commit d82e313875
  1. 1
      PadelClub/Data/DataStore.swift
  2. 100
      PadelClub/Utils/Patcher.swift
  3. 6
      PadelClub/Utils/URLs.swift
  4. 4
      PadelClub/Views/Tournament/Subscription/Guard.swift

@ -62,7 +62,6 @@ class DataStore: ObservableObject {
} }
#else #else
StoreCenter.main.synchronizationApiURL = serverURL StoreCenter.main.synchronizationApiURL = serverURL
#endif
StoreCenter.main.logsFailedAPICalls() StoreCenter.main.logsFailedAPICalls()

@ -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 {
UserDefaults.standard.setValue(true, forKey: patch.id) 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 { 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/") }
if services.hasToken() {
Task { try StoreCenter.main.migrateToken(devServices)
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()
let clubIds = Set(events.compactMap { $0.club }) let myClubs: [Club] = DataStore.shared.clubs.filter { $0.creator == userId }
let myClubs: [Club] = clubIds.compactMap { clubId in let clubIds: [String] = myClubs.map { $0.id }
clubs.first { $0.id == clubId }
}
myClubs.forEach { $0.creator = userId }
try DataStore.shared.clubs.addOrUpdate(contentOfs: myClubs) myClubs.forEach { club in
for club in myClubs { DataStore.shared.clubs.insertIntoCurrentService(item: club)
let courts = courts.filter { $0.club == club.id }
try DataStore.shared.courts.addOrUpdate(contentOfs: courts)
}
DataStore.shared.user.clubs.append(contentsOf: clubIds) let courts = DataStore.shared.courts.filter { clubIds.contains($0.club) }
DataStore.shared.saveUser() for court in courts {
DataStore.shared.courts.insertIntoCurrentService(item: court)
}
}
events.forEach { $0.creator = userId } DataStore.shared.user.clubs = Array(clubIds)
DataStore.shared.saveUser()
try DataStore.shared.events.addOrUpdate(contentOfs: events) DataStore.shared.events.insertAllIntoCurrentService()
try DataStore.shared.tournaments.addOrUpdate(contentOfs: tournaments) DataStore.shared.tournaments.insertAllIntoCurrentService()
try DataStore.shared.dateIntervals.addOrUpdate(contentOfs: dateIntervals) DataStore.shared.dateIntervals.insertAllIntoCurrentService()
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")
} }
} }

@ -8,10 +8,10 @@
import Foundation import Foundation
enum URLs: String, Identifiable { enum URLs: String, Identifiable {
case activationHost = "xlr.alwaysdata.net" case activationHost = "https://padelclub.app" // xlr.alwaysdata.net
case subscriptions = "https://apple.co/2Th4vqI" case subscriptions = "https://apple.co/2Th4vqI"
case main = "https://xlr.alwaysdata.net/" case main = "https://padelclub.app/"
case api = "https://xlr.alwaysdata.net/roads/" case api = "https://padelclub.app/roads/"
case beachPadel = "https://beach-padel.app.fft.fr/beachja/index/" case beachPadel = "https://beach-padel.app.fft.fr/beachja/index/"
//case padelClub = "https://padelclub.app" //case padelClub = "https://padelclub.app"
case tenup = "https://tenup.fft.fr" case tenup = "https://tenup.fft.fr"

@ -108,7 +108,7 @@ import LeStorage
} }
fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws { fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws {
if self.purchases.hasLoadedFromServer { if self.purchases.hasLoaded {
if self._purchaseById(transaction.originalID) == nil { if self._purchaseById(transaction.originalID) == nil {
let purchase: Purchase = try transaction.purchase() let purchase: Purchase = try transaction.purchase()
try self.purchases.addOrUpdate(instance: purchase) try self.purchases.addOrUpdate(instance: purchase)
@ -117,7 +117,7 @@ import LeStorage
} }
fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws { fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws {
if self.purchases.hasLoadedFromServer { if self.purchases.hasLoaded {
if let existing: Purchase = self._purchaseById(transaction.originalID) { if let existing: Purchase = self._purchaseById(transaction.originalID) {
existing.revocationDate = transaction.revocationDate existing.revocationDate = transaction.revocationDate
try self.purchases.addOrUpdate(instance: existing) try self.purchases.addOrUpdate(instance: existing)

Loading…
Cancel
Save