Migrating reference settings to mongo and exposing them through code … (#31726)
* Migrating reference settings to mongo and exposing them through code mirror context * removing undefined optional for reference manager type settings * allowing partial updates to user settings objects, and remove repeated cypress intercepts * Zod schema parsing for user reference manager settings * Splitting ref provider mongoose schema into const * Persisting local storage reference settings to mongo and deleting from local * Enforcing ref provider group id to always be type string * Fixing test and format errors * Migrated flag for settings to only migrate once * fixing cypress tests adding migrated flag * persisting local storage to allow for easy rollback GitOrigin-RevId: f59522bdee6f0e56efb7d98b9d9373a743619ec8
This commit is contained in:
committed by
Copybot
parent
03c30a9d1b
commit
27e5044d30
@@ -0,0 +1,99 @@
|
||||
import customLocalStorage from '@/infrastructure/local-storage'
|
||||
import getMeta from '@/utils/meta'
|
||||
import {
|
||||
RefProviderSettings,
|
||||
UserSettings,
|
||||
} from '../../../../types/user-settings'
|
||||
|
||||
type RefProvider = 'mendeley' | 'zotero' | 'papers'
|
||||
|
||||
const providers: RefProvider[] = ['mendeley', 'zotero', 'papers']
|
||||
|
||||
const buildLegacyKey = (userId: string, provider: RefProvider, key: string) =>
|
||||
`user.${userId}.write-and-cite.${provider}.${key}`
|
||||
|
||||
const isBoolean = (value: unknown): value is boolean =>
|
||||
typeof value === 'boolean'
|
||||
|
||||
const parseGroups = (value: unknown): { id: string }[] | undefined => {
|
||||
return Array.isArray(value)
|
||||
? (value as { id: string | number }[]).map(group => ({
|
||||
id: typeof group.id === 'number' ? String(group.id) : group.id,
|
||||
}))
|
||||
: undefined
|
||||
}
|
||||
|
||||
export type LegacyWriteAndCiteMigration = {
|
||||
patch: Partial<Pick<UserSettings, 'mendeley' | 'zotero' | 'papers'>>
|
||||
keysToRemove: string[]
|
||||
}
|
||||
|
||||
export const getLegacyWriteAndCiteMigration = (
|
||||
userSettings: UserSettings
|
||||
): LegacyWriteAndCiteMigration => {
|
||||
const userId = getMeta('ol-user_id')
|
||||
if (!userId) {
|
||||
return {
|
||||
patch: {},
|
||||
keysToRemove: [],
|
||||
}
|
||||
}
|
||||
|
||||
const patch: LegacyWriteAndCiteMigration['patch'] = {}
|
||||
const keysToRemove: string[] = []
|
||||
|
||||
for (const provider of providers) {
|
||||
const currentProviderSettings = userSettings[provider]
|
||||
|
||||
if (currentProviderSettings.migrated) {
|
||||
continue
|
||||
}
|
||||
|
||||
const enabledKey = buildLegacyKey(userId, provider, 'enabled')
|
||||
const groupsKey = buildLegacyKey(userId, provider, 'groups')
|
||||
const disablePersonalLibraryKey = buildLegacyKey(
|
||||
userId,
|
||||
provider,
|
||||
'disablePersonalLibrary'
|
||||
)
|
||||
|
||||
const enabledValue = customLocalStorage.getItem(enabledKey)
|
||||
const groupsValue = customLocalStorage.getItem(groupsKey)
|
||||
const disablePersonalLibraryValue = customLocalStorage.getItem(
|
||||
disablePersonalLibraryKey
|
||||
)
|
||||
|
||||
// Storage.getItem returns null if the key does not exist
|
||||
const hasEnabledValue = enabledValue !== null
|
||||
const hasGroupsValue = groupsValue !== null
|
||||
const hasDisablePersonalLibraryValue = disablePersonalLibraryValue !== null
|
||||
|
||||
if (
|
||||
!hasEnabledValue &&
|
||||
!hasGroupsValue &&
|
||||
!hasDisablePersonalLibraryValue
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
const nextProviderSettings: RefProviderSettings = {
|
||||
enabled: isBoolean(enabledValue)
|
||||
? enabledValue
|
||||
: currentProviderSettings.enabled,
|
||||
groups: parseGroups(groupsValue) ?? currentProviderSettings.groups,
|
||||
disablePersonalLibrary: isBoolean(disablePersonalLibraryValue)
|
||||
? disablePersonalLibraryValue
|
||||
: currentProviderSettings.disablePersonalLibrary,
|
||||
migrated: true,
|
||||
}
|
||||
|
||||
patch[provider] = nextProviderSettings
|
||||
|
||||
keysToRemove.push(enabledKey, groupsKey, disablePersonalLibraryKey)
|
||||
}
|
||||
|
||||
return {
|
||||
patch,
|
||||
keysToRemove: [],
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user