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 codePointOrder ( a :string , b :string ){ 4const ca = Array . from ( a ); const cb = Array . from ( b ); 5for ( let i = 0 ; i < Math . max ( ca . length , cb . length ); i ++ ){ 6const x = ca [ i ]; const y = cb [ i ]; 7if ( x === undefined ) return - 1 ; if ( y === undefined ) return 1 ; 8if ( x !== y ) return x < y ?- 1 :1 ; 9} 10return 0 ; 11} 12export function reserve ( stock :Stock [], requests :Request []){ 13const remaining = buildStock ( stock ); const accepted :string [] = []; const rejected :{ id :string ; reason :string }[] = []; 14for ( const r of uniqueRequests ( requests )){ 15const n = remaining . get ( r . sku ); 16if ( n === undefined ){ rejected . push ({ id :r . id , reason :'unknown-sku' }); continue ;} 17if ( n >= r . quantity ){ remaining . set ( r . sku , n - r . quantity ); accepted . push ( r . id );} 18else rejected . push ({ id :r . id , reason :'insufficient-stock' }); 19} 20const keys = [ ...remaining . keys ()]. sort ( codePointOrder ); 21return { accepted, rejected, remaining :Object . fromEntries ( keys . map ( k => [ k , remaining . get ( k ) ! ]))}; 22}