diff --git a/src/config/legacy.migrations.part-1.ts b/src/config/legacy.migrations.part-1.ts index 036a0c5a22..3c4d187610 100644 --- a/src/config/legacy.migrations.part-1.ts +++ b/src/config/legacy.migrations.part-1.ts @@ -6,80 +6,85 @@ import { mergeMissing, } from "./legacy.shared.js"; +function migrateBindings( + raw: Record, + changes: string[], + changeNote: string, + mutator: (match: Record) => boolean, +) { + const bindings = Array.isArray(raw.bindings) ? raw.bindings : null; + if (!bindings) { + return; + } + + let touched = false; + for (const entry of bindings) { + if (!isRecord(entry)) { + continue; + } + const match = getRecord(entry.match); + if (!match) { + continue; + } + if (!mutator(match)) { + continue; + } + entry.match = match; + touched = true; + } + + if (touched) { + raw.bindings = bindings; + changes.push(changeNote); + } +} + export const LEGACY_CONFIG_MIGRATIONS_PART_1: LegacyConfigMigration[] = [ { id: "bindings.match.provider->bindings.match.channel", describe: "Move bindings[].match.provider to bindings[].match.channel", apply: (raw, changes) => { - const bindings = Array.isArray(raw.bindings) ? raw.bindings : null; - if (!bindings) { - return; - } - - let touched = false; - for (const entry of bindings) { - if (!isRecord(entry)) { - continue; - } - const match = getRecord(entry.match); - if (!match) { - continue; - } - if (typeof match.channel === "string" && match.channel.trim()) { - continue; - } - const provider = typeof match.provider === "string" ? match.provider.trim() : ""; - if (!provider) { - continue; - } - match.channel = provider; - delete match.provider; - entry.match = match; - touched = true; - } - - if (touched) { - raw.bindings = bindings; - changes.push("Moved bindings[].match.provider → bindings[].match.channel."); - } + migrateBindings( + raw, + changes, + "Moved bindings[].match.provider → bindings[].match.channel.", + (match) => { + if (typeof match.channel === "string" && match.channel.trim()) { + return false; + } + const provider = typeof match.provider === "string" ? match.provider.trim() : ""; + if (!provider) { + return false; + } + match.channel = provider; + delete match.provider; + return true; + }, + ); }, }, { id: "bindings.match.accountID->bindings.match.accountId", describe: "Move bindings[].match.accountID to bindings[].match.accountId", apply: (raw, changes) => { - const bindings = Array.isArray(raw.bindings) ? raw.bindings : null; - if (!bindings) { - return; - } - - let touched = false; - for (const entry of bindings) { - if (!isRecord(entry)) { - continue; - } - const match = getRecord(entry.match); - if (!match) { - continue; - } - if (match.accountId !== undefined) { - continue; - } - const accountID = - typeof match.accountID === "string" ? match.accountID.trim() : match.accountID; - if (!accountID) { - continue; - } - match.accountId = accountID; - delete match.accountID; - entry.match = match; - touched = true; - } - - if (touched) { - raw.bindings = bindings; - changes.push("Moved bindings[].match.accountID → bindings[].match.accountId."); - } + migrateBindings( + raw, + changes, + "Moved bindings[].match.accountID → bindings[].match.accountId.", + (match) => { + if (match.accountId !== undefined) { + return false; + } + const accountID = + typeof match.accountID === "string" ? match.accountID.trim() : match.accountID; + if (!accountID) { + return false; + } + match.accountId = accountID; + delete match.accountID; + return true; + }, + ); }, }, {