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 )){ const n = remaining . get ( r . sku ) ?? 0 ; remaining . set ( r . sku , Math . max ( 0 , n - r . quantity )); if ( n >= r . quantity ) accepted . push ( r . id ); else rejected . push ({ id :r . id , reason :'insufficient-stock' });} 6return { accepted, rejected, remaining :Object . fromEntries ( remaining )}; 7}