forked from artsy/eigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.ts
291 lines (286 loc) · 9.08 KB
/
migration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// easy-peasy ships with a fork of immer so let's use that instead of adding another copy of immer to our bundle.
import { LegacyNativeModules } from "app/NativeModules/LegacyNativeModules"
import { echoLaunchJson } from "app/utils/jsonFiles"
import { produce } from "immer-peasy"
import { Platform } from "react-native"
/**
* IMPORTANT
* Before you modify this file please read docs/adding_state_migrations.md
*/
export const Versions = {
AddSearchesAndNativeAndBottomTabs: 1,
AddConsignments: 2,
RenameConsignmentsToMyCollection: 3,
RemoveMyCollectionNavigationState: 4,
AddAuthAndConfigState: 5,
AddFeatureFlagInfra: 6,
RefactorConfigModel: 7,
FixEnvironmentMigrationBug: 8,
AddUserIsDev: 9,
AddAuthOnboardingState: 10,
RenameUserEmail: 11,
AddToastModel: 12,
PendingPushNotification: 13,
CopyIOSNativeSessionAuthToTS: 14,
AddExperimentsModel: 15,
AddPreviousSessionUserID: 16,
RemoveNativeOnboardingState: 17,
AddUserPreferences: 18,
AddVisualClueModel: 19,
AddArtworkSubmissionModel: 20,
AddArtworkViewOption: 21,
RenameModelsAndAddDarkModeSupport: 22,
AddUserPrefsMetricsUnit: 23,
AddSourceAndMyCollectionArtworkIDToSubmission: 24,
AddSourceInitialPhotosToSubmission: 25,
RequestedPriceEstimates: 26,
AddZipCodeAndCountryCodeInSubmissionArtworkDetails: 27,
AddDirtyFormValuesToSubmissionState: 28,
RemoveDeviceId: 29,
AddSubmissionIdForMyCollection: 30,
AddRecentPriceRangesModel: 31,
AddUserPreferredPriceRange: 32,
RenameAdminToLocalOverridesForFeatures: 33,
MoveEnvironmentToDevicePrefsAndRenameAdminToLocal: 34,
}
export const CURRENT_APP_VERSION = Versions.MoveEnvironmentToDevicePrefsAndRenameAdminToLocal
export type Migrations = Record<number, (oldState: any) => any>
export const artsyAppMigrations: Migrations = {
[Versions.AddSearchesAndNativeAndBottomTabs]: (_) => ({
bottomTabs: {},
native: {},
search: { recentSearches: [] },
}),
[Versions.AddConsignments]: (state) => {
state.consignments = {
artwork: {},
navigation: {},
}
},
[Versions.RenameConsignmentsToMyCollection]: (state) => {
state.myCollection = state.consignments
delete state.consignments
},
[Versions.RemoveMyCollectionNavigationState]: (state) => {
delete state.myCollection.navigation
},
[Versions.AddAuthAndConfigState]: (state) => {
state.auth = {
userID: null,
userAccessToken: null,
userAccessTokenExpiresIn: null,
xAppToken: null,
xApptokenExpiresIn: null,
}
state.config = {}
},
[Versions.AddFeatureFlagInfra]: (state) => {
state.config.adminFeatureFlagOverrides = {}
state.config.echoState = echoLaunchJson()
},
[Versions.RefactorConfigModel]: (state) => {
const newConfig = {} as any
newConfig.features = { adminOverrides: state.config.adminFeatureFlagOverrides }
newConfig.echo = { state: state.config.echoState }
newConfig.environment = { adminOverrides: {}, env: "staging" }
state.config = newConfig
},
[Versions.FixEnvironmentMigrationBug]: (state) => {
state.config.environment.env = __TEST__ ? "staging" : "production"
},
[Versions.AddUserIsDev]: (state) => {
state.auth.androidUserEmail = null
state.config.userIsDev = { flipValue: false }
},
[Versions.AddAuthOnboardingState]: (state) => {
state.auth.onboardingState = "none"
},
[Versions.RenameUserEmail]: (state) => {
if (Platform.OS === "ios") {
state.auth.userEmail = LegacyNativeModules.ARTemporaryAPIModule.getUserEmail()
}
if (Platform.OS === "android") {
state.auth.userEmail = state.auth.androidUserEmail
}
state.auth.userEmail = state.auth.userEmail ?? null
delete state.auth.androidUserEmail
},
[Versions.AddToastModel]: (state) => {
state.toast = {}
},
[Versions.PendingPushNotification]: (state) => {
state.pendingPushNotification = { notification: null }
},
[Versions.CopyIOSNativeSessionAuthToTS]: (state) => {
if (Platform.OS === "ios") {
const nativeState = LegacyNativeModules.ARNotificationsManager.nativeState
state.auth.userAccessToken = nativeState.authenticationToken
state.auth.onboardingState = (nativeState as any).onboardingState ?? "none"
state.auth.userID = nativeState.userID
}
},
[Versions.AddExperimentsModel]: (state) => {
state.config.experiments = {}
},
[Versions.AddPreviousSessionUserID]: (state) => {
state.auth.previousSessionUserID = null
},
[Versions.RemoveNativeOnboardingState]: (state) => {
delete state.native.onboardingState
},
[Versions.AddUserPreferences]: (state) => {
state.userPreferences = { currency: "USD", metric: "" }
},
[Versions.AddVisualClueModel]: (state) => {
state.visualClue = {
seenVisualClues: [],
}
},
[Versions.AddArtworkSubmissionModel]: (state) => {
state.artworkSubmission = {
submission: {
submissionId: "",
artworkDetails: {
artist: "",
artistId: "",
title: "",
year: "",
medium: "",
attributionClass: null,
editionNumber: "",
editionSizeFormatted: "",
dimensionsMetric: "in",
height: "",
width: "",
depth: "",
provenance: "",
state: "DRAFT",
utmMedium: "",
utmSource: "",
utmTerm: "",
location: {
city: "",
state: "",
country: "",
},
},
photos: {
photos: [],
},
photosForMyCollection: {
photos: [],
},
},
}
},
[Versions.AddArtworkViewOption]: (state) => {
state.userPreferences.artworkViewOption = "grid"
},
[Versions.RenameModelsAndAddDarkModeSupport]: (state) => {
state.artsyPrefs = state.config ?? {}
state.userPrefs = state.userPreferences ?? {}
state.devicePrefs = {
usingSystemColorScheme: false,
forcedColorScheme: "light",
}
delete state.config
delete state.userPreferences
},
[Versions.AddUserPrefsMetricsUnit]: (state) => {
state.userPrefs.metric = "in"
},
[Versions.AddSourceAndMyCollectionArtworkIDToSubmission]: (state) => {
state.artworkSubmission.submission.artworkDetails.source = null
state.artworkSubmission.submission.artworkDetails.myCollectionArtworkID = null
},
[Versions.AddSourceInitialPhotosToSubmission]: (state) => {
state.artworkSubmission.submission.photos.initialPhotos = []
state.artworkSubmission.submission.photosForMyCollection.initialPhotos = []
},
[Versions.RequestedPriceEstimates]: (state) => {
state.requestedPriceEstimates = { requestedPriceEstimates: {} }
},
[Versions.AddZipCodeAndCountryCodeInSubmissionArtworkDetails]: (state) => {
state.artworkSubmission.submission.artworkDetails.location.zipCode = ""
state.artworkSubmission.submission.artworkDetails.location.countryCode = ""
},
[Versions.AddDirtyFormValuesToSubmissionState]: (state) => {
state.artworkSubmission.submission.dirtyArtworkDetailsValues = {
artist: "",
artistId: "",
title: "",
year: "",
medium: "",
myCollectionArtworkID: null,
attributionClass: null,
editionNumber: "",
editionSizeFormatted: "",
dimensionsMetric: "in",
height: "",
width: "",
depth: "",
provenance: "",
source: null,
state: "DRAFT",
utmMedium: "",
utmSource: "",
utmTerm: "",
location: {
city: "",
state: "",
country: "",
countryCode: "",
zipCode: "",
},
}
},
[Versions.RemoveDeviceId]: (state) => {
delete state.native.deviceId
},
[Versions.AddSubmissionIdForMyCollection]: (state) => {
state.artworkSubmission.submission.submissionIdForMyCollection = ""
},
[Versions.AddRecentPriceRangesModel]: (state) => {
state.recentPriceRanges = {
ranges: [],
}
},
[Versions.AddUserPreferredPriceRange]: (state) => {
state.userPrefs.priceRange = "*-*"
},
[Versions.RenameAdminToLocalOverridesForFeatures]: (state) => {
state.artsyPrefs.features.localOverrides = state.artsyPrefs.features.adminOverrides
delete state.artsyPrefs.features.adminOverrides
},
[Versions.MoveEnvironmentToDevicePrefsAndRenameAdminToLocal]: (state) => {
state.devicePrefs.environment = state.artsyPrefs.environment
delete state.artsyPrefs.environment
state.devicePrefs.environment.localOverrides = state.devicePrefs.environment.adminOverrides
delete state.devicePrefs.environment.adminOverrides
},
}
export function migrate<State extends { version: number }>({
state,
migrations = artsyAppMigrations,
toVersion = CURRENT_APP_VERSION,
}: {
state: State
migrations?: Migrations
toVersion?: number
}): {
version: number
} {
if (typeof state.version !== "number") {
throw new Error("Bad state.version " + JSON.stringify(state))
}
while (state.version < toVersion) {
const nextVersion = state.version + 1
const migrator = migrations[nextVersion]
if (!migrator) {
throw new Error("No migrator found for app version " + nextVersion)
}
state = produce(state, migrator)
state.version = nextVersion
}
return state
}