import { Observable , AsyncSubject , of } from 'rxjs' ; import { flatMap } from 'rxjs/operators' ; import * as _ from 'lodash' ; import { BaseProduct , BaseProductPrice , BaseProductInventory , BaseProductOption } from '../models' ; import { inject , injectable } from 'inversify' ; import IProduct from '../interfaces/iproduct' ; import SERVICE_IDENTIFIER from '../../common/constants/identifiers' ; import ILogger from '../../common/interfaces/ilogger' ; let bpId = 0 ; let bpoId = 0 ; let invId = 0 ; let basePrice = 100.0 ; let baseInventory = 1 ; let prId = 0 ; const baseProductPrice = [ { id : prId ++ , baseProductOptionId : bpoId ++ , price : basePrice ++ } , { id : prId ++ , baseProductOptionId : bpoId ++ , price : basePrice ++ } , { id : prId ++ , baseProductOptionId : bpoId ++ , price : basePrice ++ } , { id : prId ++ , baseProductOptionId : bpoId ++ , price : basePrice ++ } , { id : prId ++ , baseProductOptionId : bpoId ++ , price : basePrice ++ } , { id : prId ++ , baseProductOptionId : bpoId ++ , price : basePrice ++ } ] ; bpoId = 0 ; const baseProductInventory = [ { id : invId ++ , baseProductOptionId : bpoId ++ , inventory : baseInventory ++ } , { id : invId ++ , baseProductOptionId : bpoId ++ , inventory : baseInventory ++ } , { id : invId ++ , baseProductOptionId : bpoId ++ , inventory : baseInventory ++ } ] ; bpoId = 0 ; const baseProductOptions1 = [ { baseProductOptionId : bpoId ++ , description : 'Base product option 0' } , { baseProductOptionId : bpoId ++ , description : 'Base product option 1' } , { baseProductOptionId : bpoId ++ , description : 'Base product option 2' } ] ; const baseProductOptions2 = [ { baseProductOptionId : bpoId ++ , description : 'Base product option 3' } , { baseProductOptionId : bpoId ++ , description : 'Base product option 4' } , { baseProductOptionId : bpoId , description : 'Base product option 5' } ] ; const baseProductOptions = Array . prototype . concat ( baseProductOptions1 , baseProductOptions2 ) ; const baseProducts = [ { id : bpId ++ , name : 'product 0' , description : 'product 0' , baseProductOptions : baseProductOptions1 } , { id : bpId ++ , name : 'product 1' , description : 'product 1' , baseProductOptions : baseProductOptions2 } ] ; /**
 * Product Service Implementation
 */ @ injectable ( ) class ProductService implements IProduct { public loggerService : ILogger ; public constructor ( @ inject ( SERVICE_IDENTIFIER . LOGGER ) loggerService ) { this . loggerService = loggerService ; } public allBaseProducts ( ) { return of ( baseProducts ) ; } public baseProductbyId ( id ) { const results = _ . filter ( baseProducts , product => { return product . id == id ; } ) ; return of ( results [ 0 ] ) ; } public allBaseProductOptions ( ) { return of ( baseProductOptions ) ; } public baseProductOptionsbyId ( id ) { const results = _ . filter ( baseProductOptions , options => { return options . baseProductOptionId == id ; } ) ; return of ( results [ 0 ] ) ; } public allBaseProductPrice ( ) { return of ( baseProductPrice ) ; } public baseProductPricebyId ( id ) { const results = _ . filter ( baseProductPrice , price => { return price . baseProductOptionId == id ; } ) ; return of ( results [ 0 ] ) ; } public allBaseProductInventory ( ) { return of ( baseProductInventory ) ; } public baseProductInventorybyId ( id ) { const results = _ . filter ( baseProductInventory , inv => { return inv . baseProductOptionId == id ; } ) ; return of ( results [ 0 ] ) ; } param id public getProductOptionPricebyId ( id ) { const loadedCharacter = new AsyncSubject < BaseProductPrice [ ] > ( ) ; const prices = [ ] ; this . baseProductbyId ( id ) . pipe ( flatMap ( prod => { if ( prod !== undefined && prod . baseProductOptions !== undefined ) { for ( const product of prod . baseProductOptions ) { this . baseProductOptionsbyId ( product . baseProductOptionId ) . pipe ( flatMap ( p => { return this . baseProductPricebyId ( p . baseProductOptionId ) ; } ) ) . subscribe ( result => { prices . push ( result ) ; } ) ; } } return of ( prices ) ; } ) ) . subscribe ( result => { loadedCharacter . next ( result ) ; loadedCharacter . complete ( ) ; } ) ; return loadedCharacter ; } } export default ProductService ;