const fs=require('fs'); const {Client}=require('/home/prime/.openclaw/workspace/tmp_pg/node_modules/pg'); const manifest=JSON.parse(fs.readFileSync(process.argv[2],'utf8')); const dry=process.argv.includes('--dry'); const cs={host:'172.10.1.127',port:5432,database:'SOR',user:'SOR-DBA',password:'G073gxq7VtQaOfnITcxDh3bsibcipe6D',ssl:{rejectUnauthorized:false}}; (async()=>{ const client=new Client(cs); await client.connect(); const cardNames=manifest.cards.map(c=>c.name); const res=await client.query('select "Id","Name" from catalog.card_definitions where "Name" = any($1)', [cardNames.concat([manifest.archSpiritName])]); const byName=new Map(res.rows.map(r=>[r.Name,r.Id])); for (const c of manifest.cards) if (!byName.has(c.name)) throw new Error('Missing card '+c.name); if (!byName.has(manifest.archSpiritName)) throw new Error('Missing arch spirit '+manifest.archSpiritName); let deckId=null; const existing=await client.query('select "Id" from bazaar.prebuilt_decks where "Name"=$1 limit 1',[manifest.name]); if (existing.rows[0]) deckId=existing.rows[0].Id; const total=manifest.cards.reduce((a,c)=>a+c.quantity,0); if (total!==60) throw new Error(`Deck total ${total} != 60`); if (dry) { console.log(JSON.stringify({dry:true, deckId:deckId||'new', total, unique:manifest.cards.length},null,2)); await client.end(); return; } await client.query('begin'); try { if (!deckId) { const ins=await client.query(`insert into bazaar.prebuilt_decks ("Id","Name","Description","ImageCdnUrl","OriginPointPrice","UsdPrice","AvailableFrom","AvailableUntil","IsActive","IsStarterDeck","IsAvailableInShop","ArchSpiritCardDefinitionId","ArchSpiritName","CreatedAt","UpdatedAt") values (gen_random_uuid(),$1,$2,null,0,null,null,null,$3,$4,$5,$6,$7,now(),now()) returning "Id"`, [manifest.name, manifest.description, !!manifest.isActive, !!manifest.isStarterDeck, !!manifest.isAvailableInShop, byName.get(manifest.archSpiritName), manifest.archSpiritName]); deckId=ins.rows[0].Id; } else { await client.query(`update bazaar.prebuilt_decks set "Description"=$2,"IsActive"=$3,"IsStarterDeck"=$4,"IsAvailableInShop"=$5,"ArchSpiritCardDefinitionId"=$6,"ArchSpiritName"=$7,"UpdatedAt"=now() where "Id"=$1`, [deckId, manifest.description, !!manifest.isActive, !!manifest.isStarterDeck, !!manifest.isAvailableInShop, byName.get(manifest.archSpiritName), manifest.archSpiritName]); await client.query('delete from bazaar.prebuilt_deck_cards where "PrebuiltDeckId"=$1',[deckId]); } for (const c of manifest.cards) { await client.query(`insert into bazaar.prebuilt_deck_cards ("Id","PrebuiltDeckId","CardDefinitionId","CardName","Quantity") values (gen_random_uuid(),$1,$2,$3,$4)`, [deckId, byName.get(c.name), c.name, c.quantity]); } const verify=await client.query('select count(*)::int as unique_cards, sum("Quantity")::int as total_cards from bazaar.prebuilt_deck_cards where "PrebuiltDeckId"=$1',[deckId]); await client.query('commit'); console.log(JSON.stringify({dry:false, deckId, verify:verify.rows[0]},null,2)); } catch(e) { await client.query('rollback'); throw e; } await client.end(); })().catch(e=>{console.error(e);process.exit(1)});