import { Injectable } from "@angular/core" ; import { DbAbstractionLayer } from "@nodeart/dal/index" ; /**
 * Product service. Has different methods for manipulating with products and its properties.
 */ @ Injectable ( ) export class ProductService { /**
     * ElasticSearch index name. For default "firebase"
     */ public esIndex = 'firebase' ; constructor ( private dal ) { } param { string } id getOneProduct ( id ) { let queryObject = { "query" : { "term" : { "_id" : id } } } ; return this . dal . requestFullData ( this . esIndex , 'product' , queryObject ) ; } param { string } categoryId param { Array } currentPrice param { Array } attributes param { Array } tags param { integer } size param { integer } from returns { Observable } searchProducts ( categoryId , currentPrice , attributes , tags , size , from ) { let queryObject = { size : size , from : from , query : { bool : { must : [ { bool : { should : [ ] } } , { match : { "category" : categoryId } } , { "range" : { "price" : { "gte" : currentPrice [ 0 ] , "lte" : currentPrice [ 1 ] } } } ] } } } ; for ( let i = 0 ; i < attributes . length ; i ++ ) { let key = "attributes." + attributes [ i ] [ 'attrId' ] ; queryObject . query . bool . must [ 0 ] [ 'bool' ] . should . push ( { multi_match : { query : attributes [ i ] [ 'valueName' ] , fields : [ key ] } } ) ; } for ( let i = 0 ; i < tags . length ; i ++ ) { let key = "tags" ; queryObject . query . bool . must [ 0 ] [ 'bool' ] . should . push ( { multi_match : { query : tags [ i ] [ 'id' ] , fields : [ key ] } } ) ; } console . log ( queryObject ) ; return this . dal . requestFullData ( this . esIndex , 'product' , queryObject ) ; } param { string } categoryId returns { Observable } getPriceRanges ( categoryId ) { let queryObject = { aggs : { max_price : { max : { field : "price" } } , min_price : { min : { field : "price" } } } , query : { bool : { should : { match : { category : categoryId } } } } } ; console . log ( JSON . stringify ( queryObject ) ) ; return this . dal . requestFullData ( this . esIndex , 'product' , queryObject ) ; } param { Array } priceRange param { Array } attributes param { Array } tags param { integer } size param { integer } from returns { Observable } filterProducts ( priceRange , attributes , tags , size , offset ) { let queryObject = { size : size , from : offset , query : { bool : { must : [ { bool : { should : [ ] } } , { "range" : { "price" : { "gte" : priceRange [ 0 ] , "lte" : priceRange [ 1 ] } } } ] } } } ; for ( let i = 0 ; i < attributes . length ; i ++ ) { let key = "attributes." + attributes [ i ] [ 'attrId' ] ; queryObject . query . bool . must [ 0 ] [ 'bool' ] . should . push ( { multi_match : { query : attributes [ i ] [ 'valueName' ] , fields : [ key ] } } ) ; } for ( let i = 0 ; i < tags . length ; i ++ ) { let key = "tags" ; queryObject . query . bool . must [ 0 ] [ 'bool' ] . should . push ( { multi_match : { query : tags [ i ] [ 'id' ] , fields : [ key ] } } ) ; } return this . dal . requestFullData ( this . esIndex , 'product' , queryObject ) ; } param { string } generalCategoryId returns { Observable } getGeneralCategory ( generalCategoryId ) { let queryObj = { "query" : { "term" : { "_id" : generalCategoryId . toString ( ) } } } ; return this . dal . requestData ( this . esIndex , 'general-category' , queryObj ) ; } param categoryId returns { Observable } getCategory ( categoryId ) { let queryObj = { "query" : { "term" : { "_id" : categoryId . toString ( ) } } } ; return this . dal . requestFullData ( this . esIndex , 'category' , queryObj ) ; } returns { Observable } getGeneralCategories ( ) { let queryObj = { "query" : { "match_all" : { } } } ; return this . dal . requestData ( this . esIndex , 'general-category' , queryObj ) ; } param { string } categoryId returns { Observable } getCategories ( categoryId ) { let queryObj ; if ( categoryId == '' ) { queryObj = { query : { filtered : { filter : { bool : { must_not : [ { exists : { field : "parentId" } } ] } } } } } ; } else { queryObj = { query : { term : { parentId : categoryId } } } ; } console . log ( JSON . stringify ( queryObj ) ) ; return this . dal . requestData ( this . esIndex , 'category' , queryObj ) ; } param { Array } categoryIds param { integer } size param { integer } from returns { Observable } getProductsByCategoryIds ( categoryIds , size , from ) { let queryObj = { size : size , from : from , query : { constant_score : { filter : { terms : { category : categoryIds } } } } } ; return this . dal . requestData ( this . esIndex , 'product' , queryObj ) ; } param { string } query param { integer } size returns { Observable } getProducts ( query , size ? ) { query = query . split ( ' ' ) ; let queryObj = { "query" : { "bool" : { "must" : [ ] } } } ; if ( size ) { queryObj [ 'size' ] = size ; } query . map ( term => { term = term . replace ( /\(|\)/g , '' ) ; queryObj . query . bool . must . push ( { "wildcard" : { "name" : term . toLowerCase ( ) + "*" } } ) ; } ) ; return this . dal . requestFullData ( this . esIndex , 'product' , queryObj ) ; } param { string } categoryId param { Array } currentPrice param { Array } attributes param { Array } tags returns { Observable } getTotalPages ( categoryId , currentPrice , attributes , tags ) { let queryObject = { size : 0 , query : { bool : { must : [ { bool : { should : [ ] } } , { match : { "category" : categoryId } } , { "range" : { "price" : { "gte" : currentPrice [ 0 ] , "lte" : currentPrice [ 1 ] } } } ] } } } ; for ( let i = 0 ; i < attributes . length ; i ++ ) { let key = "attributes." + attributes [ i ] [ 'attrId' ] ; queryObject . query . bool . must [ 0 ] [ 'bool' ] . should . push ( { multi_match : { query : attributes [ i ] [ 'valueName' ] , fields : [ key ] } } ) ; } for ( let i = 0 ; i < tags . length ; i ++ ) { let key = "tags" ; queryObject . query . bool . must [ 0 ] [ 'bool' ] . should . push ( { multi_match : { query : tags [ i ] [ 'id' ] , fields : [ key ] } } ) ; } console . log ( JSON . stringify ( queryObject ) ) ; return this . dal . requestItemsTotal ( this . esIndex , 'product' , queryObject ) ; } param { Array } categoryIds returns { Observable } getTotalPagesByCategoryIds ( categoryIds ) { let queryObj = { size : 0 , query : { constant_score : { filter : { terms : { category : categoryIds } } } } } ; return this . dal . requestItemsTotal ( this . esIndex , 'product' , queryObj ) ; } param { string } attributeId returns { Observable } getOneAttribute ( attributeId ) { let queryObj = { "query" : { "term" : { "_id" : attributeId } } } ; return this . dal . requestData ( this . esIndex , 'attributes' , queryObj ) ; } param { string } tagId returns { Observable } getOneTag ( tagId ) { let queryObj = { "query" : { "term" : { "_id" : tagId } } } ; return this . dal . requestData ( this . esIndex , 'tags' , queryObj ) ; } }