import { groupBy } from '../utils/groupBy' ; import { Order , OrderItem } from './types' ; const round2 = ( value ) : number => Math . round ( value * 100 ) / 100 ; export const createOrderItem = ( id = null , orderId = null , sku = null , unitPrice = 0 , quantity = 0 ) : OrderItem => ( { id , orderId , sku , unitPrice , quantity } ) ; const orderItemPrice = ( { unitPrice , quantity } ) : number => round2 ( unitPrice * quantity ) ; export const createOrder = ( id = null , items = [ ] , total = 0 ) : Order => ( { id , items , total } ) ; export const ordersFromItems = groupBy ( ( { orderId } ) => orderId , ( order = createOrder ( ) , orderItem ) => { order . id = orderItem . orderId ; order . items . push ( orderItem ) ; order . total = round2 ( order . total + orderItemPrice ( orderItem ) ) ; return order ; } ) ;