// Set SITE_ORIGIN and ARRIVAL_KEY in Script Properties. Use the site's public buyer URL when enabled. function arrivalCall(action,values){ const props=PropertiesService.getScriptProperties(); const origin=(props.getProperty('SITE_ORIGIN')||'').trim().replace(/\/$/,''); const key=props.getProperty('ARRIVAL_KEY')||''; if(!/^https:\/\/[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?(?::[0-9]{1,5})?$/i.test(origin)||key.length!==72)throw Error('Set SITE_ORIGIN and ARRIVAL_KEY.'); const mailbox=Session.getEffectiveUser().getEmail().toLowerCase(); if(!['majorvehicles@gmail.com','greatbusesforsale@gmail.com'].includes(mailbox))throw Error('Sign in with a dealership sales mailbox.'); const headers={Authorization:'Bearer '+key}; const r=UrlFetchApp.fetch(origin+'/api/arrivals/bridge',{method:'post',headers,contentType:'application/json',payload:JSON.stringify({action,mailbox,...values}),followRedirects:false,muteHttpExceptions:true}); if(r.getResponseCode()!==200)throw Error('Site returned '+r.getResponseCode()+'. Check the key and site access.'); return JSON.parse(r.getContentText()); } function testAndConnect(){ const me=Session.getEffectiveUser().getEmail().toLowerCase(); if(!['majorvehicles@gmail.com','greatbusesforsale@gmail.com'].includes(me))throw Error('Use a dealership sales mailbox.'); const p=PropertiesService.getScriptProperties(), origin=(p.getProperty('SITE_ORIGIN')||'').trim().replace(/\/$/,''); const bus={title:'Staff delivery test',stock:'A5431',language:'en',seats:'Sample seating configuration',miles:'',image:'',email:me,unsubscribe:''}; sendArrival(bus,origin,true); arrivalCall('tested',{}); if(!ScriptApp.getProjectTriggers().some(t=>t.getHandlerFunction()==='deliverArrivals'))ScriptApp.newTrigger('deliverArrivals').timeBased().everyMinutes(5).create(); } function escapeMail(value){return String(value||'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));} function sendArrival(b,origin,test){ const es=b.language==='es',subject=(test?'[Staff test] ':'')+(es?'Nueva llegada · ':'New arrival · ')+b.title; const url=origin+(es?'/es':'')+'/inventory/'+encodeURIComponent(b.stock.toLowerCase())+'/'; const stop=origin+'/?unsubscribe='+encodeURIComponent(b.unsubscribe); const specs=[b.stock,b.miles&&b.miles+(es?' millas':' miles'),b.seats].filter(Boolean).join(' · '); const label=es?'Ver este autobús':'View this bus',unsubscribe=es?'Cancelar suscripción':'Unsubscribe'; const image=b.image?'

'+escapeMail(b.title)+'

':''; const address='Major Vehicle Exchange · 5000 Brush Hollow Rd, Westbury, NY 11590'; const html='

'+escapeMail(b.title)+'

'+image+'

'+escapeMail(specs)+'

'+label+'

'+address+'

'+(test?'':''+unsubscribe+''); MailApp.sendEmail({to:b.email,subject,body:[b.title,specs,url,address,test?'':unsubscribe+' '+stop].filter(Boolean).join('\n'),htmlBody:html,name:'Major Vehicle Exchange'}); } function deliverArrivals(){ const lock=LockService.getScriptLock();if(!lock.tryLock(1000))return; try{ const start=Date.now(),origin=PropertiesService.getScriptProperties().getProperty('SITE_ORIGIN').trim().replace(/\/$/,''); while(Date.now()-start<150000 && MailApp.getRemainingDailyQuota()>0){ const {item}=arrivalCall('next',{});if(!item)break; try{sendArrival(item,origin,false);} catch(e){arrivalCall('result',{claim:item.claim,state:'review'});throw e;} arrivalCall('result',{claim:item.claim,state:'sent'}); } }finally{lock.releaseLock();} }