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' ; 3function codePointCompare ( a :string , b :string ){ const ca = Array . from ( a ); const cb = Array . from ( b ); const n = Math . min ( ca . length , cb . length ); for ( let i = 0 ; i < n ; i ++ ){ if ( ca [ i ] !== cb [ i ]) return ca [ i ] < cb [ i ] ?- 1 :1 ;} return ca . length - cb . length ;} 4export function reserve ( stock :Stock [], requests :Request []){ 5const remaining = buildStock ( stock ); const accepted :string [] = []; const rejected :{ id :string ; reason :string }[] = []; 6for ( const r of uniqueRequests ( requests )){ 7if ( ! remaining . has ( r . sku )){ rejected . push ({ id :r . id , reason :'unknown-sku' }); continue ;} 8const n = remaining . get ( r . sku ) ! ; 9if ( n >= r . quantity ){ remaining . set ( r . sku , n - r . quantity ); accepted . push ( r . id );} else rejected . push ({ id :r . id , reason :'insufficient-stock' }); 10} 11const out :Record < string , number > = {}; 12for ( const key of [ ...remaining . keys ()]. sort ( codePointCompare )) out [ key ] = remaining . get ( key ) ! ; 13return { accepted, rejected, remaining :out }; 14}