web/website/control-panel/public/kit-cart.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

10 lines
3.9 KiB
JavaScript

export function createKitCart({uuid,storage,key}){
const sameId=(a,b)=>typeof a==='string'&&typeof b==='string'&&a.toLowerCase()===b.toLowerCase();
const validId=v=>typeof v==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(v);
const purchasable=l=>l?.purchaseAvailable===true||(l?.purchaseAvailable===undefined&&l?.commerceKind==='personal_sale');
const validLine=l=>l&&validId(l.listingId)&&validId(l.orderLineId)&&typeof l.name==='string'&&l.name.length>0&&l.name.length<=64&&!/[\x00-\x1f\x7f]/.test(l.name)&&['listingVersion','blueprintVersion','priceCents'].every(k=>Number.isSafeInteger(l[k])&&l[k]>0);
let state={cartId:uuid(),lines:[]},busy=false;
const raw=storage.getItem(key);if(raw){let saved;try{saved=JSON.parse(raw);}catch{throw Error('Saved cart is invalid. Contact support before retrying checkout.');}if(!validId(saved.cartId)||!Array.isArray(saved.lines)||saved.lines.length>20||!saved.lines.every(validLine)||!Number.isSafeInteger(saved.lines.reduce((sum,line)=>sum+line.priceCents,0))||(new Set(saved.lines.map(l=>l.orderLineId.toLowerCase())).size!==saved.lines.length||new Set(saved.lines.map(l=>l.listingId.toLowerCase())).size!==saved.lines.length||saved.lines.some(l=>sameId(l.orderLineId,saved.cartId))))throw Error('Saved cart is invalid. Contact support before retrying checkout.');state=saved;}
const save=next=>{storage.setItem(key,JSON.stringify(next));state=next;};
return{snapshot:()=>structuredClone(state),get busy(){return busy},total:()=>state.lines.reduce((n,l)=>n+l.priceCents,0),validateCatalogue(listings){if(!Array.isArray(listings))throw Error('Kit catalogue unavailable. Retry checkout later.');for(const line of state.lines){const matches=listings.filter(item=>sameId(item?.listingId,line.listingId));if(matches.length!==1)throw Error('A kit changed or is unavailable. Review your saved cart before paying.');const current=matches[0];if(!current||!purchasable(current)||current.listingVersion!==line.listingVersion||current.blueprintVersion!==line.blueprintVersion||current.priceCents!==line.priceCents)throw Error('A kit changed or is unavailable. Review your saved cart before paying.');}return true;},add(listing){if(busy)throw Error('Wait for checkout to finish.');if(!purchasable(listing))throw Error('This kit is not available for purchase until in-game delivery is ready.');if(state.lines.length>=20)throw Error('A cart can contain up to 20 kits.');if(state.lines.some(line=>sameId(line.listingId,listing.listingId)))throw Error('This kit is already in your cart.');const line={listingId:listing.listingId,orderLineId:uuid(),listingVersion:listing.listingVersion,blueprintVersion:listing.blueprintVersion,priceCents:listing.priceCents,name:listing.name};if(!validLine(line)||state.lines.some(existing=>sameId(existing.orderLineId,line.orderLineId))||sameId(line.orderLineId,state.cartId)||!Number.isSafeInteger(this.total()+line.priceCents))throw Error('Refresh this listing before adding it.');save({...state,lines:[...state.lines,line]});},remove(id){if(busy)throw Error('Wait for checkout to finish.');save({...state,lines:state.lines.filter(l=>!sameId(l.orderLineId,id))});},async checkout(send){if(busy)throw Error('Checkout already in progress.');if(!state.lines.length)throw Error('Add kits before checking out.');busy=true;try{storage.setItem(key,JSON.stringify(state));const payload={cartId:state.cartId,lines:state.lines.map(({listingId,orderLineId,listingVersion,blueprintVersion,priceCents})=>({listingId,orderLineId,listingVersion,blueprintVersion,priceCents}))};const result=await send(payload);if(result?.ok!==true||!sameId(result.cartId,state.cartId))throw Error('Checkout outcome unconfirmed. Retry this cart or check your receipts.');const nextId=uuid();if(!validId(nextId)||sameId(nextId,state.cartId)||state.lines.some(line=>sameId(nextId,line.orderLineId)||sameId(nextId,line.listingId)))throw Error('Checkout confirmed. Check your receipts before starting another cart.');save({cartId:nextId,lines:[]});return result;}finally{busy=false;}}};
}