okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
407ea8192
main
1import { buildStock } from './stock.ts' ; import type { Stock } from './stock.ts' ; 2import { uniqueRequests } from './requests.ts' ; import type { Request } from './requests.ts' ; 3export function reserve ( stock :Stock [], requests :Request []){ 4const remaining = buildStock ( stock ); const accepted :string [] = []; const rejected :{ id :string ; reason :string }[] = []; 5for ( const r of uniqueRequests ( requests )){ 6if ( ! remaining . has ( r . sku )){ rejected . push ({ id :r . id , reason :'unknown-sku' }); continue ;} 7const n = remaining . get ( r . sku ) ! ; 8if ( n >= r . quantity ){ remaining . set ( r . sku , n - r . quantity ); accepted . push ( r . id );} 9else rejected . push ({ id :r . id , reason :'insufficient-stock' }); 10} 11const sorted = [ ...remaining ]. sort (( a , b ) => a [ 0 ] < b [ 0 ] ?- 1 :a [ 0 ] > b [ 0 ] ?1 :0 ); 12return { accepted, rejected, remaining :Object . fromEntries ( sorted )}; 13}