import { ReadModel , Projects } from '@boostercloud/framework-core' ; import { UUID , ProjectionResult } from '@boostercloud/framework-types' ; import { Product } from '../entities/product' ; interface ProductIDWithPrice { productId : UUID ; priceCents : number ; } @ ReadModel ( { authorize : 'all' , } ) export class ProductsBySKU { public constructor ( public id , readonly products , readonly firstProduct ? , readonly record ? ) { } @ Projects ( Product , 'sku' ) public static projectProduct ( entity , currentProductsBySKU ? ) { if ( ! currentProductsBySKU ) { currentProductsBySKU = new ProductsBySKU ( entity . sku , [ ] ) ; } const newAndSortedProducts = currentProductsBySKU . products . filter ( ( pp ) => pp . productId != entity . id ) . concat ( { productId : entity . id , priceCents : entity . price . cents } ) . sort ( ( a , b ) => b . priceCents - a . priceCents ) ; return new ProductsBySKU ( currentProductsBySKU . id , newAndSortedProducts , newAndSortedProducts [ 0 ] , { items : newAndSortedProducts . length , } ) ; } }