import 'reflect-metadata' ; import { Get , JsonController , Res , Req , QueryParam , Body , Post } from 'routing-controllers' ; import { BannerService } from '../../services/BannerService' ; import { MAILService } from '../../../auth/mail.services' ; import { classToPlain } from 'class-transformer' ; import { CategoryService } from '../../services/CategoryService' ; import { ProductService } from '../../services/ProductService' ; import arrayToTree from 'array-to-tree' ; import { ProductImageService } from '../../services/ProductImageService' ; import { CountryService } from '../../services/CountryService' ; import { ContactService } from '../../services/ContactService' ; import { ContactRequest } from './requests/ContactRequest' ; import { Contact } from '../../models/Contact' ; import { EmailTemplateService } from '../../services/EmailTemplateService' ; import { ZoneService } from '../../services/zoneService' ; import { ProductToCategoryService } from '../../services/ProductToCategoryService' ; import { CategoryPathService } from '../../services/CategoryPathService' ; import { UserService } from '../../services/UserService' ; import { CustomerWishlistService } from '../../services/CustomerWishlistService' ; import jwt from 'jsonwebtoken' ; @ JsonController ( '/list' ) export class CommonListController { constructor ( private bannerService , private categoryService , private productService , private productImageService , private countryService , private contactService , private emailTemplateService , private zoneService , private customerWishlistService , private productToCategoryService , private categoryPathService , private userService ) { } api apiGroup apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/banner-list' ) public async bannerList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'bannerId' , 'title' , 'image' , 'imagePath' , 'content' , 'link' , 'position' , 'isActive' ] ; const search = [ { name : 'title' , op : 'like' , value : keyword , } , ] ; const WhereConditions = [ { name : 'isActive' , value : 1 , } , ] ; const bannerList = await this . bannerService . list ( limit , offset , select , search , WhereConditions , count ) ; const successResponse = { status : 1 , message : 'Successfully got banner list' , data : bannerList , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/category-list' ) public async ParentCategoryList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'sortOrder' ) sortOrder , @ QueryParam ( 'count' ) count , @ Req ( ) request , @ Res ( ) response ) { const select = [ 'categoryId' , 'name' , 'parentInt' , 'sortOrder' , 'metaTagTitle' , 'metaTagDescription' , 'metaTagKeyword' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'where' , value : 1 , } , { name : 'parentInt' , op : 'where' , value : 0 , } , ] ; const WhereConditions = [ ] ; const categoryData = await this . categoryService . list ( limit , offset , select , search , WhereConditions , sortOrder , count ) ; if ( count ) { const successResponse = { status : 1 , message : 'Successfully get All category List' , data : categoryData , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const category = categoryData . map ( async ( value ) => { const tempVal = value ; const child = await this . categoryService . find ( { where : { parentInt : value . categoryId , isActive : 1 } , select : [ 'categoryId' , 'name' , 'parentInt' , 'sortOrder' , 'metaTagTitle' , 'metaTagDescription' , 'metaTagKeyword' , 'isActive' ] } ) ; const children = child . map ( async ( val ) => { const data = val ; const subChild = await this . categoryService . find ( { where : { parentInt : val . categoryId , isActive : 1 } , select : [ 'categoryId' , 'name' , 'parentInt' , 'sortOrder' , 'metaTagTitle' , 'metaTagDescription' , 'metaTagKeyword' , 'isActive' ] } ) ; if ( subChild . length > 0 ) { data . children = subChild ; return data ; } return data ; } ) ; const childrenData = await Promise . all ( children ) ; tempVal . children = childrenData ; return tempVal ; } ) ; const result = await Promise . all ( category ) ; console . log ( result ) ; if ( result ) { const successResponse = { status : 1 , message : 'Successfully got the list of categories.' , data : result , } ; return response . status ( 200 ) . send ( successResponse ) ; } } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/productlist' ) public async productList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'manufacturerId' ) manufacturerId , @ QueryParam ( 'categoryId' ) categoryId , @ QueryParam ( 'priceFrom' ) priceFrom , @ QueryParam ( 'priceTo' ) priceTo , @ QueryParam ( 'price' ) price , @ QueryParam ( 'condition' ) condition , @ QueryParam ( 'count' ) count , @ Req ( ) request , @ Res ( ) response ) { console . log ( manufacturerId ) ; const select = [ 'product.productId' , 'product.sku' , 'product.name' , 'product.quantity' , 'product.description' , 'product.price' , 'product.isActive AS isActive' , 'product.manufacturerId AS manufacturerId' , 'product.location AS location' , 'product.minimumQuantity AS minimumQuantity' , 'product.subtractStock' , 'product.stockStatusId' , 'product.shipping' , 'product.sortOrder' , 'product.condition' , 'product.dateAvailable' , 'product.amount' , 'product.metaTagTitle' , 'product.metaTagDescription' , 'product.metaTagKeyword' , 'product.discount' ] ; const searchConditions = [ { name : 'product.isActive' , op : 'where' , value : 1 , } , { name : 'product.manufacturerId' , op : 'and' , value : manufacturerId , } , { name : 'product.name' , op : 'and' , value : keyword , } , { name : 'product.condition' , op : 'andWhere' , value : condition , } , ] ; const whereConditions = [ { name : 'product.productId' , op : 'inraw' , value : categoryId , } ] ; const productList = await this . productService . productList ( limit , offset , select , searchConditions , whereConditions , categoryId , priceFrom , priceTo , price , count ) ; if ( count ) { const Response = { status : 1 , message : 'Successfully got Products count' , data : productList , } ; return response . status ( 200 ) . send ( Response ) ; } const promises = productList . map ( async ( result ) => { const productToCategory = await this . productToCategoryService . findAll ( { select : [ 'categoryId' , 'productId' ] , where : { productId : result . productId } , } ) . then ( ( val ) => { const category = val . map ( async ( value ) => { const categoryNames = await this . categoryService . findOne ( { categoryId : value . categoryId } ) ; const tempValue = value ; tempValue . categoryName = categoryNames . name ; return tempValue ; } ) ; const results = Promise . all ( category ) ; return results ; } ) ; const productImage = await this . productImageService . findOne ( { select : [ 'productId' , 'image' , 'containerName' , 'defaultImage' ] , where : { productId : result . productId , defaultImage : 1 , } , } ) ; const temp = result ; temp . Images = productImage ; temp . Category = productToCategory ; 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 !== undefined ) { temp . wishListStatus = 1 ; } else { temp . wishListStatus = 0 ; } } else { temp . wishListStatus = 0 ; } return temp ; } ) ; const finalResult = await Promise . all ( promises ) ; const maximum = [ 'Max(product.price) As maximumProductPrice' ] ; const maximumPrice = await this . productService . productMaxPrice ( maximum ) ; const productPrice = maximumPrice . maximumProductPrice ; const successResponse = { status : 1 , message : 'Successfully got the complete list of products.' , data : { maximumProductPrice : productPrice , productList : finalResult , } , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/custom-product-list' ) public async customProductList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'manufacturerId' ) manufacturerId , @ QueryParam ( 'categoryId' ) categoryId , @ QueryParam ( 'priceFrom' ) priceFrom , @ QueryParam ( 'priceTo' ) priceTo , @ QueryParam ( 'price' ) price , @ QueryParam ( 'condition' ) condition , @ QueryParam ( 'count' ) count , @ Req ( ) request , @ Res ( ) response ) { return new Promise ( async ( ) => { const productList = await this . productService . customProductList ( limit , offset , categoryId , manufacturerId , condition , keyword , priceFrom , priceTo , price ) ; const promises = productList . map ( async ( result ) => { const productImage = await this . productImageService . findOne ( { select : [ 'productId' , 'image' , 'containerName' , 'defaultImage' ] , where : { productId : result . productId , defaultImage : 1 , } , } ) ; const temp = result ; temp . Images = 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 the complete list of products.' , data : finalResult , } ; return response . status ( 200 ) . send ( successResponse ) ; } ) ; } api apiGroup apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/country-list' ) public async countryList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'countryId' , 'name' , 'isoCode2' , 'isoCode3' , 'postcodeRequired' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'where' , value : 1 , } , ] ; const WhereConditions = [ ] ; const countryList = await this . countryService . list ( limit , offset , select , search , WhereConditions , count ) ; const successResponse = { status : 1 , message : 'Successfully got the list of countries.' , data : countryList , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/contact-us' ) public async userContact ( @ Body ( { validate : true } ) contactParam , @ Req ( ) request , @ Res ( ) response ) { const contactInformation = new Contact ( ) ; contactInformation . name = contactParam . name ; contactInformation . email = contactParam . email ; contactInformation . phoneNumber = contactParam . phoneNumber ; contactInformation . message = contactParam . message ; const informationData = await this . contactService . create ( contactInformation ) ; const emailContent = await this . emailTemplateService . findOne ( 3 ) ; const message = emailContent . content . replace ( '{name}' , informationData . name ) . replace ( '{email}' , informationData . email ) . replace ( '{phoneNumber}' , informationData . phoneNumber ) . replace ( '{message}' , informationData . message ) ; const adminId = [ ] ; const adminUser = await this . userService . findAll ( { select : [ 'username' ] , where : { userGroupId : 1 } } ) ; for ( const user of adminUser ) { const val = user . username ; adminId . push ( val ) ; } const sendMailRes = MAILService . contactMail ( message , emailContent . subject , adminId ) ; if ( sendMailRes ) { const successResponse = { status : 1 , message : 'Your request Successfully send' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Mail does not send' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/zone-list' ) public async zonelist ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'zoneId' , 'countryId' , 'code' , 'name' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'where' , value : 1 , } , ] ; const WhereConditions = [ ] ; const relation = [ 'country' ] ; const zoneList = await this . zoneService . list ( limit , offset , select , search , WhereConditions , relation , count ) ; if ( zoneList ) { const successResponse = { status : 1 , message : 'Successfully get all zone List' , data : classToPlain ( zoneList ) , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 1 , message : 'unable to get zone List' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/specific-category-list' ) public async SpecificcategoryList ( @ QueryParam ( 'categoryId' ) categoryid , @ Req ( ) request , @ Res ( ) response ) { const categoryDataId = await this . categoryService . findOne ( categoryid ) ; if ( categoryDataId === undefined ) { const errorResponse = { status : 0 , message : 'Invalid categoryId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const categoryDetailId = await this . categoryPathService . findOne ( { categoryId : categoryid , level : 0 } ) ; const select = [ 'categoryId' , 'name' , 'image' , 'imagePath' , 'parentInt' , 'sortOrder' , 'metaTagTitle' , 'metaTagDescription' , 'metaTagKeyword' ] ; const categoryData = await this . categoryService . list ( 0 , 0 , select , 0 , 0 , 0 , 0 ) ; const categoryList = arrayToTree ( categoryData , { parentProperty : 'parentInt' , customID : 'categoryId' , } ) ; const mainCategoryId = categoryDetailId . pathId ; let dataList ; const key = 'categoryId' ; for ( const data of categoryList ) { if ( data [ key ] === mainCategoryId ) { dataList = data ; } } const successResponse = { status : 1 , message : 'Successfully get the related category List' , data : dataList , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/product-count' ) public async productCount ( @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'manufacturerId' ) manufacturerId , @ QueryParam ( 'categoryId' ) categoryId , @ QueryParam ( 'priceFrom' ) priceFrom , @ QueryParam ( 'priceTo' ) priceTo , @ Res ( ) response ) { const maximum = [ 'Max(product.price) As maximumProductPrice' ] ; const maximumPrice = await this . productService . productMaxPrice ( maximum ) ; const productPrice = maximumPrice . maximumProductPrice ; const productCount = await this . productService . productCount ( keyword , manufacturerId , categoryId , priceFrom , priceTo ) ; const successResponse = { status : 1 , message : 'Successfully get Product Count' , data : { productCount : productCount . productCount , maximumProductPrice : productPrice } , } ; return response . status ( 200 ) . send ( successResponse ) ; } }