import { Entity , Reduces } from '@boostercloud/framework-core' ; import { UUID } from '@boostercloud/framework-types' ; import { StockMoved } from '../events/stock-moved' ; @ Entity export class Stock { public constructor ( readonly id , readonly warehouses ) { } @ Reduces ( StockMoved ) public static stockMoved ( event , currentStock ? ) { if ( currentStock == null ) { const stock = new Stock ( event . productID , { } ) ; stock . warehouses [ event . origin ] = 0 ; stock . warehouses [ event . destination ] = event . quantity ; return stock ; } else { currentStock . warehouses [ event . origin ] -= event . quantity ; currentStock . warehouses [ event . destination ] += event . quantity ; return currentStock ; } } }