web/website/node/minimal-account-migration.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

36 lines
4.2 KiB
JavaScript

// Task 01: deliberately minimal account migration; no live database mutation.
// Keep internal account IDs to preserve rank, badge and game-link relationships.
export const ACCOUNT_MIGRATION_ALLOWLIST=Object.freeze({
site_accounts:Object.freeze(['id','username','display_name','password_hash','password_salt','role','created_at']),
site_contact_emails:Object.freeze(['user_id','address','verified_at']),
site_game_links:Object.freeze(['user_id','minecraft_uuid']),
cp_account_ranks:Object.freeze(['user_id','rank_key']),
website_badges:Object.freeze(['user_id','badge_key','earned_at']),
seal_entitlements:Object.freeze(['id','account_id','seal_key','source_kind','source_id','granted_by','granted_at','revoked_at','revoked_by','revoke_reason'])
});
export const EXCLUDED_BY_DEFAULT=Object.freeze(['site_sessions','site_link_challenges','account_audit','forum_topics','forum_posts','forum_reports','support_tickets','support_messages','banking','transactions','messages','gameplay']);
export function validateMinimalAccountSnapshot(snapshot){
if(!snapshot||typeof snapshot!=='object'||Array.isArray(snapshot))throw new TypeError('Snapshot required');
for(const table of Object.keys(snapshot))if(!Object.hasOwn(ACCOUNT_MIGRATION_ALLOWLIST,table))throw new TypeError('Unexpected snapshot table '+table);
for(const [table,columns] of Object.entries(ACCOUNT_MIGRATION_ALLOWLIST)){
const rows=snapshot[table];if(!Array.isArray(rows))throw new TypeError('Missing table '+table);
for(const row of rows){if(!row||typeof row!=='object'||Array.isArray(row)||Object.keys(row).some(k=>!columns.includes(k)))throw new TypeError('Unexpected column in '+table);}
}
const ids=new Set(snapshot.site_accounts.map(a=>a.id));
if(ids.size!==snapshot.site_accounts.length)throw new Error('Duplicate account ID');
const usernames=new Set();for(const account of snapshot.site_accounts){if(typeof account.username!=='string')throw new Error('Invalid username');if(account.username!==account.username.trim())throw new Error('Invalid username whitespace');const normalized=account.username.toLowerCase();if(usernames.has(normalized))throw new Error('Duplicate username');usernames.add(normalized);}
for(const account of snapshot.site_accounts){for(const key of ['id','username','display_name','password_hash','password_salt','role'])if(typeof account[key]!=='string'||account[key].trim()==='')throw new Error('Invalid account field '+key);}
for(const a of snapshot.site_accounts){if(!a.id||!a.username||!a.display_name||!a.password_hash||!a.password_salt||!(typeof a.created_at==='number'||typeof a.created_at==='string'&&a.created_at.trim()!=='')||!Number.isFinite(Number(a.created_at)))throw new Error('Missing account identity or password verifier');}
for(const table of ['site_contact_emails','site_game_links','cp_account_ranks','website_badges'])for(const row of snapshot[table])if(!ids.has(row.user_id))throw new Error('Orphaned '+table+' record');
for(const row of snapshot.seal_entitlements)if(!ids.has(row.account_id))throw new Error('Orphaned seal entitlement');
const uniqueBy=(table,fields)=>{const seen=new Set();for(const row of snapshot[table]){const values=fields.map(field=>row[field]);if(values.some(value=>typeof value!=='string'||value.trim()===''||value!==value.trim()))throw new Error('Invalid '+table+' identity');const key=JSON.stringify(values.map(value=>value.toLowerCase()));if(seen.has(key))throw new Error('Duplicate '+table+' record');seen.add(key);}};
uniqueBy('site_game_links',['user_id']);
uniqueBy('site_game_links',['minecraft_uuid']);
uniqueBy('site_contact_emails',['address']);
uniqueBy('site_contact_emails',['user_id','address']);
uniqueBy('cp_account_ranks',['user_id','rank_key']);
uniqueBy('website_badges',['user_id','badge_key']);
uniqueBy('seal_entitlements',['id']);
for(const row of snapshot.seal_entitlements){for(const field of ['seal_key','source_kind'])if(typeof row[field]!=='string'||row[field].trim()==='')throw new Error('Invalid seal entitlement '+field);}
return Object.freeze({accounts:ids.size,emails:snapshot.site_contact_emails.length,gameLinks:snapshot.site_game_links.length,extraRanks:snapshot.cp_account_ranks.length,badges:snapshot.website_badges.length,seals:snapshot.seal_entitlements.length});
}