← back to Dw Photo Capture
ops/tres-tintas-dwts-2026-09-24/fm-create-masters.js
32 lines
// TK-12162: create FileMaker WALLPAPER masters for the 583 live Tres Tintas Shopify products that have none.
// Fields mirror server.js buildFmMaster(). Guards: skip if a DWTS master already has this JS Pattern or this Mfr Pattern.
// run on Kamatera from /root/public-projects/dwphoto: node - preview|canary|all < fm-create-masters.js
// undo: delete the recordIds listed in /root/tk-tt-fm-created.jsonl (manual in FileMaker Pro; API delete returns code 110)
const fs=require('fs'); const DIR='/root/public-projects/dwphoto';
for (const l of fs.readFileSync(DIR+'/.env','utf8').split('\n')){const m=l.match(/^([A-Z0-9_]+)=(.*)$/); if(m&&!process.env[m[1]])process.env[m[1]]=m[2];}
const FM=require(DIR+'/fm-client.js'); const DB='WALLPAPER', L='*List Wallpapers - Full View';
const MODE=process.argv[2]||'preview'; const plan=JSON.parse(fs.readFileSync('/root/tk-tt-fm-plan.json'));
function fd(x){ const f={'Record Type':'Master','Series':'DWTS','JS Pattern':x.js,'Mfr Pattern':x.mfr,'mfr pattern number':x.mfr,'vid':'tres'};
if(x.name){f['Name of Pattern']=x.name; f['Internal Description']=x.name;}
if(x.color) f['Color of Pattern']=x.color;
if(x.width){const w=String(x.width); f['Width']=/"/.test(w)?w:((w.match(/[\d.]+/)||[w])[0]+'"');}
if(x.material) f['Content']=x.material;
if(x.repeat) f['Repeat']=String(x.repeat);
f['Sold Per']='Roll'; if(+x.price>0) f['Retail Price']=String(x.price);
return f; }
(async()=>{
if(MODE==='preview'){ for(const x of plan.slice(0,2)) console.log(JSON.stringify(fd(x),null,1)); console.log('planned creates:',plan.length); return; }
const list=MODE==='canary'?plan.slice(0,1):plan; const log=fs.createWriteStream('/root/tk-tt-fm-created.jsonl',{flags:'a'});
let made=0,skip=0,fail=0;
for(const x of list){ try{
const byNum=(await FM.fmFind(DB,L,[{'Series':'==DWTS','JS Pattern':'=='+x.js}],{limit:1}).catch(()=>({records:[]}))).records;
const byMfr=(await FM.fmFind(DB,L,[{'Series':'==DWTS','Mfr Pattern':'=='+x.mfr}],{limit:1}).catch(()=>({records:[]}))).records;
if(byNum.length||byMfr.length){skip++; continue;}
const r=await FM.fmCreate(DB,L,fd(x),{dryRun:false}); log.write(JSON.stringify({recordId:r.recordId,js:x.js,mfr:x.mfr,shopify_id:x.shopify_id})+'\n');
const g=(await FM.fmGet(DB,L,r.recordId)).fieldData;
if(g['seriesdashnumber']==='DWTS-'+x.js&&g['Mfr Pattern']===x.mfr){made++; if(MODE==='canary')console.log('CANARY OK',r.recordId,g['seriesdashnumber'],g['Name of Pattern'],'|',g['Mfr Pattern'],'| vendor:',g['vendors::Company']);}
else {fail++; console.log('VERIFY FAIL',r.recordId,g['seriesdashnumber'],g['Mfr Pattern']);}
}catch(e){fail++; console.log('ERR',x.js,e.message.slice(0,100));}
if((made+skip+fail)%50===0) console.log(`progress ${made+skip+fail}/${list.length} made=${made} skip=${skip} fail=${fail}`); }
console.log(`mode=${MODE} made=${made} skipped=${skip} failed=${fail} of ${list.length}`); })();