import { Entity , Reduces } from '@boostercloud/framework-core' ; import { SKU } from '../common/sku' ; import { ProductCreated } from '../events/product-created' ; import { ProductUpdated } from '../events/product-updated' ; import { ProductDeleted } from '../events/product-deleted' ; import { Money } from '../common/money' ; import { UUID } from '@boostercloud/framework-types' ; import { Picture } from '../common/picture' ; import { ProductAvailabilityChanged } from '../events/product-availability-changed' ; import { UserWithEmail } from '../roles' ; /**
 * A product is the representation of a sellable unit in our sample store
 */ @ Entity ( { authorizeReadEvents : [ UserWithEmail ] , } ) export class Product { public constructor ( public id , readonly sku , readonly displayName , readonly description , readonly price , readonly pictures , public deleted = false , public availability = 0 ) { } public getId ( ) { return this . id ; } @ Reduces ( ProductCreated ) public static create ( event ) { return new Product ( event . productId , event . sku , event . displayName , event . description , event . price , [ ] ) ; } @ Reduces ( ProductUpdated ) public static update ( event ) { return new Product ( event . id , event . sku , event . name , event . description , event . price , event . pictures , event . deleted ) ; } @ Reduces ( ProductDeleted ) public static delete ( _event , currentProduct ) { if ( currentProduct ) { currentProduct . deleted = true ; } return currentProduct ; } @ Reduces ( ProductAvailabilityChanged ) public static changeAvailability ( event , currentProduct ) { if ( currentProduct ) { currentProduct . availability += event . quantity ; } return currentProduct ; } }