import 'reflect-metadata' ; import { Get , QueryParam , JsonController , Res , Req , Param } from 'routing-controllers' ; import { classToPlain } from 'class-transformer' ; import { ProductToCategoryService } from '../../services/ProductToCategoryService' ; import { ProductService } from '../../services/ProductService' ; import { CategoryService } from '../../services/CategoryService' ; import { ProductImageService } from '../../services/ProductImageService' ; import { ProductViewLog } from '../../models/productViewLog' ; import { ProductViewLogService } from '../../services/ProductViewLogService' ; import jwt from 'jsonwebtoken' ; import { CustomerService } from '../../services/CustomerService' ; import { CategoryPathService } from '../../services/CategoryPathService' ; import { CustomerWishlistService } from '../../services/CustomerWishlistService' ; @ JsonController ( '/product-store' ) export class ProductController { constructor ( private productService , private productToCategoryService , private categoryService , private productImageService , private customerService , private productViewLogService , private customerWishlistService , private categoryPathService ) { } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/productdetail/:id' ) public async productDetail ( @ Param ( 'id' ) id , @ Req ( ) request , @ Res ( ) response ) { const productDetail = await this . productService . findOne ( { productId : id , } ) ; const productDetails = classToPlain ( productDetail ) ; productDetails . productImage = await this . productImageService . findAll ( { select : [ 'productId' , 'image' , 'containerName' , 'defaultImage' ] , where : { productId : id , } , } ) ; productDetails . Category = await this . productToCategoryService . findAll ( { select : [ 'categoryId' , 'productId' ] , where : { productId : id } , } ) . then ( ( val ) => { const category = val . map ( async ( value ) => { const categoryNames = await this . categoryService . findOne ( { categoryId : value . categoryId } ) ; const temp = value ; if ( categoryNames !== undefined ) { temp . categoryName = categoryNames . name ; } else { temp . categoryName = '' ; } return temp ; } ) ; const results = Promise . all ( category ) ; return results ; } ) ; if ( request . header ( 'authorization' ) ) { let customerId ; jwt . verify ( request . header ( 'authorization' ) . split ( ' ' ) [ 1 ] , '123##$$)(***&' , ( err , decoded ) => { if ( err ) { console . log ( err ) ; } console . log ( 'lll' , decoded . id ) ; customerId = decoded . id ; } ) ; const wishStatus = await this . customerWishlistService . findOne ( { where : { productId : id , customerId , } , } ) ; if ( wishStatus ) { productDetails . wishListStatus = 1 ; } else { productDetails . wishListStatus = 0 ; } const customerDetail = await this . customerService . findOne ( { where : { id : customerId } } ) ; const viewLog = new ProductViewLog ( ) ; viewLog . productId = id ; viewLog . customerId = customerDetail . id ; viewLog . firstName = customerDetail . firstName ; viewLog . lastName = customerDetail . lastName ; viewLog . username = customerDetail . username ; viewLog . email = customerDetail . email ; viewLog . mobileNumber = customerDetail . mobileNumber ; viewLog . address = customerDetail . address ; await this . productViewLogService . create ( viewLog ) ; } else { productDetails . wishListStatus = 0 ; } const successResponse = { status : 1 , message : 'Successfully get productDetail' , data : productDetails , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/todayDeals-list' ) public async todayDealsList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'sku' ) sku , @ QueryParam ( 'count' ) count , @ Req ( ) request , @ Res ( ) response ) { const select = [ 'productId' , 'sku' , 'upc' , 'name' , 'description' , 'location' , 'minimumQuantity' , 'quantity' , 'subtractStock' , 'metaTagTitle' , 'manufacturerId' , 'stockStatusId' , 'todayDeals' , 'shipping' , 'dateAvailable' , 'sortOrder' , 'price' , 'isActive' ] ; const whereConditions = [ { name : 'deleteFlag' , op : 'where' , value : 0 , } , { name : 'todayDeals' , op : 'where' , value : 1 , } , { name : 'isActive' , op : 'where' , value : 1 , } , ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'sku' , op : 'like' , value : sku , } , ] ; const todayDeals = await this . productService . list ( limit , offset , select , 0 , whereConditions , search , 0 , count ) ; if ( count ) { const successresponse = { status : 1 , message : 'Successfully got today deals count' , data : todayDeals , } ; return response . status ( 200 ) . send ( successresponse ) ; } const promises = todayDeals . map ( async ( result ) => { const productImage = await this . productImageService . findOne ( { select : [ 'productId' , 'image' , 'containerName' , 'defaultImage' ] , where : { productId : result . productId , defaultImage : 1 , } , } ) ; const temp = result ; temp . productImage = productImage ; if ( request . header ( 'authorization' ) ) { const userId = jwt . verify ( request . header ( 'authorization' ) . split ( ' ' ) [ 1 ] , '123##$$)(***&' ) ; const userUniqueId = Object . keys ( userId ) . map ( ( key ) => { return [ ( key ) , userId [ key ] ] ; } ) ; console . log ( userUniqueId [ 0 ] [ 1 ] ) ; const wishStatus = await this . customerWishlistService . findOne ( { where : { productId : result . productId , customerId : userUniqueId [ 0 ] [ 1 ] , } , } ) ; if ( wishStatus ) { temp . wishListStatus = 1 ; } else { temp . wishListStatus = 0 ; } } else { temp . wishListStatus = 0 ; } return temp ; } ) ; const finalResult = await Promise . all ( promises ) ; const successResponse = { status : 1 , message : 'Successfully got today deals List' , data : finalResult , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/Get-Category' ) public async getCategory ( @ QueryParam ( 'CategoryId' ) CategoryId , @ Res ( ) response ) { const select = [ 'categoryId' , 'name' , 'parentInt' , 'sortOrder' ] ; const search = [ ] ; const WhereConditions = [ { name : 'categoryId' , value : CategoryId , } ] ; const category = await this . categoryService . list ( 0 , 0 , select , search , WhereConditions , 0 , 0 ) ; const promise = category . map ( async ( result ) => { const temp = result ; const categoryLevel = await this . categoryPathService . find ( { select : [ 'level' , 'pathId' ] , where : { categoryId : result . categoryId } , order : { level : 'ASC' } , } ) . then ( ( values ) => { const categories = values . map ( async ( val ) => { const categoryNames = await this . categoryService . findOne ( { categoryId : val . pathId } ) ; const tempVal = val ; tempVal . categoryName = categoryNames . name ; return tempVal ; } ) ; const results = Promise . all ( categories ) ; return results ; } ) ; temp . levels = categoryLevel ; return temp ; } ) ; const value = await Promise . all ( promise ) ; if ( category ) { const successResponse = { status : 1 , message : 'successfully got the category. ' , data : value , } ; return response . status ( 200 ) . send ( successResponse ) ; } } }