import {randomUUID} from 'node:crypto'; import {grantSealEntitlement,revokeSealEntitlement} from './seal-entitlement-store.mjs'; // Reconcile only custom recognition seals explicitly linked to the effective website rank. // Never infer purchase eligibility from supporter ranks. export async function syncRankRecognitionSeals(db,accountId,rankKey){ const {results:definitions=[]}=await db.prepare("SELECT seal_key FROM cp_seal_catalog WHERE category='recognition' AND rank_key=?").bind(rankKey).all(); const {results:current=[]}=await db.prepare("SELECT id,seal_key,source_id FROM seal_entitlements WHERE account_id=? AND source_kind='system' AND source_id LIKE 'rank:%' AND revoked_at IS NULL").bind(accountId).all(); const expected=new Set(definitions.map(d=>d.seal_key)); for(const old of current){if(!expected.has(old.seal_key))await revokeSealEntitlement(db,{entitlementId:old.id,actorId:accountId,reason:'Rank eligibility changed'})} for(const d of definitions){if(current.some(x=>x.seal_key===d.seal_key))continue;const sourceId='rank:'+accountId+':'+randomUUID();await grantSealEntitlement(db,{accountId,seal:d.seal_key,sourceKind:'system',sourceId,details:{rule:'verified website rank',rank:rankKey}})} }