import 'reflect-metadata' ; import { Get , JsonController , Authorized , QueryParam , Res , Body , Req , Post , Param , Put , Delete } from 'routing-controllers' ; import { ProductService } from '../services/ProductService' ; import { ProductToCategoryService } from '../services/ProductToCategoryService' ; import { ProductImageService } from '../services/ProductImageService' ; import { Product } from '../models/ProductModel' ; import { classToPlain } from 'class-transformer' ; import { DeleteProductRequest } from './requests/DeleteProductRequest' ; import { AddProductRequest } from './requests/CreateProductRequest' ; import { UpdateProductRequest } from './requests/UpdateProductRequest' ; import { ProductToCategory } from '../models/ProductToCategory' ; import { ProductImage } from '../models/ProductImage' ; import { CategoryService } from '../services/CategoryService' ; import { OrderProductService } from '../services/OrderProductService' ; import { OrderService } from '../services/OrderService' ; import { UpdateTodayDealsParam } from './requests/UpdateTodayDealsParam' ; import { ProductViewLogService } from '../services/ProductViewLogService' ; import moment = require ( 'moment' ) ; import fs = require ( 'fs' ) ; @ JsonController ( '/product' ) export class ProductController { constructor ( private productService , private productToCategoryService , private productImageService , private categoryService , private orderProductService , private orderService , private productViewLogService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/productlist' ) @ Authorized ( ) public async productList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'sku' ) sku , @ QueryParam ( 'status' ) status , @ QueryParam ( 'price' ) price , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'productId' , 'sku' , 'name' , 'quantity' , 'price' , 'image' , 'imagePath' , 'todayDeals' , 'isActive' ] ; const relation = [ ] ; const WhereConditions = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'sku' , op : 'like' , value : sku , } , { name : 'isActive' , op : 'like' , value : status , } , ] ; const productLists = await this . productService . list ( limit , offset , select , relation , WhereConditions , 0 , price , count ) ; if ( count ) { const successRes = { status : 1 , message : 'Successfully got count ' , data : productLists , } ; return response . status ( 200 ) . send ( successRes ) ; } const productList = productLists . map ( async ( value ) => { const defaultValue = await this . productImageService . findOne ( { where : { productId : value . productId , defaultImage : 1 , } , } ) ; const temp = value ; temp . productImage = defaultValue ; return temp ; } ) ; const results = await Promise . all ( productList ) ; const successResponse = { status : 1 , message : 'Successfully got the complete product list. ' , data : classToPlain ( results ) , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-product' ) @ Authorized ( ) public async addProduct ( @ Body ( { validate : true } ) product , @ Res ( ) response ) { const newProduct = new Product ( ) ; newProduct . name = product . productName ; newProduct . description = product . productDescription ; newProduct . sku = product . sku ; newProduct . upc = product . upc ; newProduct . location = product . location ; newProduct . price = product . price ; newProduct . stockStatusId = product . outOfStockStatus ; newProduct . shipping = product . requiredShipping ; newProduct . dateAvailable = moment ( product . dateAvailable ) . toISOString ( ) ; newProduct . metaTagTitle = product . metaTagTitle ; newProduct . condition = product . condition ; newProduct . manufacturerId = product . model ; newProduct . isActive = product . status ; newProduct . todayDeals = 0 ; newProduct . sortOrder = product . sortOrder ; const saveProduct = await this . productService . create ( newProduct ) ; if ( product . categoryId ) { const category = product . categoryId ; for ( const categoryId of category ) { const newProductToCategory = new ProductToCategory ( ) ; newProductToCategory . productId = saveProduct . productId ; newProductToCategory . categoryId = categoryId ; newProductToCategory . isActive = 1 ; this . productToCategoryService . create ( newProductToCategory ) ; } } const productImage = product . image ; for ( const imageRow of productImage ) { const imageData = JSON . stringify ( imageRow ) ; const imageResult = JSON . parse ( imageData ) ; const newProductImage = new ProductImage ( ) ; newProductImage . productId = saveProduct . productId ; newProductImage . image = imageResult . image ; newProductImage . containerName = imageResult . containerName ; newProductImage . defaultImage = imageResult . defaultImage ; this . productImageService . create ( newProductImage ) ; } if ( saveProduct ) { const successResponse = { status : 1 , message : 'Successfully created Product' , data : saveProduct , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to create Product' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/update-product/:id' ) @ Authorized ( ) public async updateProduct ( @ Body ( { validate : true } ) product , @ Res ( ) response ) { console . log ( product ) ; const updateProduct = await this . productService . findOne ( { where : { productId : product . productId , } , } ) ; if ( ! updateProduct ) { const errorResponse = { status : 0 , message : 'Invalid productId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } updateProduct . name = product . productName ; updateProduct . description = product . productDescription ; updateProduct . sku = product . sku ; updateProduct . upc = product . upc ; updateProduct . location = product . location ; updateProduct . price = product . price ; updateProduct . stockStatusId = product . outOfStockStatus ; updateProduct . shipping = product . requiredShipping ; updateProduct . dateAvailable = moment ( product . dateAvailable ) . toISOString ( ) ; updateProduct . metaTagTitle = product . metaTagTitle ; updateProduct . manufacturerId = product . model ; updateProduct . condition = product . condition ; updateProduct . isActive = product . status ; updateProduct . sortOrder = product . sortOrder ; const saveProduct = await this . productService . create ( updateProduct ) ; this . productToCategoryService . delete ( { productId : saveProduct . productId } ) ; if ( product . categoryId ) { const category = product . categoryId ; for ( const categoryId of category ) { const newProductToCategory = new ProductToCategory ( ) ; newProductToCategory . productId = saveProduct . productId ; newProductToCategory . categoryId = categoryId ; newProductToCategory . isActive = 1 ; this . productToCategoryService . create ( newProductToCategory ) ; } } this . productImageService . delete ( { productId : saveProduct . productId } ) ; if ( product . image ) { const productImage = product . image ; for ( const imageRow of productImage ) { const imageData = JSON . stringify ( imageRow ) ; const imageResult = JSON . parse ( imageData ) ; const newProductImage = new ProductImage ( ) ; newProductImage . productId = saveProduct . productId ; newProductImage . image = imageResult . image ; newProductImage . containerName = imageResult . containerName ; newProductImage . defaultImage = imageResult . defaultImage ; this . productImageService . create ( newProductImage ) ; } } if ( saveProduct ) { const successResponse = { status : 1 , message : 'Successfully updated Product' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to updated Product' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/product-detail/:id' ) @ Authorized ( ) public async productDetail ( @ Param ( 'id' ) id , @ 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 ; } ) ; const successResponse = { status : 1 , message : 'Successfully get productDetail' , data : productDetails , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/top-selling-productlist' ) @ Authorized ( ) public async topSellingProductList ( @ Req ( ) request , @ Res ( ) response ) { const data = await this . productService . recentProductSelling ( 4 ) ; const promise = data . map ( async ( result ) => { const product = await this . productService . findOne ( { select : [ 'productId' , 'image' , 'imagePath' , 'price' , 'name' , 'description' ] , where : { productId : result . product } , } ) ; const temp = result ; const productImage = await this . productImageService . findAll ( { select : [ 'productId' , 'image' , 'containerName' ] , where : { productId : result . product , defaultImage : 1 , } , } ) ; temp . product = product ; temp . productImage = productImage ; return temp ; } ) ; const value = await Promise . all ( promise ) ; const successResponse = { status : 1 , message : 'Successfully get Top Selling Product..!' , data : value , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/recent-selling-product' ) @ Authorized ( ) public async sellingProduct ( @ Req ( ) request , @ Res ( ) response ) { const limit = 3 ; const orderList = await this . orderProductService . List ( limit ) ; const promises = orderList . map ( async ( result ) => { const order = await this . orderService . findOrder ( { select : [ 'invoiceNo' , 'invoicePrefix' , 'orderPrefixId' , 'orderId' , 'orderStatusId' ] , where : { orderId : result . orderId } , } ) ; const temp = result ; temp . order = order ; const product = await this . productImageService . findAll ( { where : { productId : result . productId , defaultImage : 1 , } , } ) ; temp . productImage = product ; return temp ; } ) ; const results = await Promise . all ( promises ) ; const successResponse = { status : 1 , message : 'successfully listed recently selling products..!' , data : results , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-todayDeals/:id' ) @ Authorized ( ) public async updateTodayDeals ( @ Param ( 'id' ) id , @ Body ( { validate : true } ) updateTodayDealsParam , @ Res ( ) response ) { const product = await this . productService . findOne ( { where : { productId : id , } , } ) ; if ( ! product ) { const errorResponse = { status : 0 , message : 'Invalid productId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } product . todayDeals = updateTodayDealsParam . todayDeals ; const productSave = await this . productService . create ( product ) ; if ( productSave ) { const successResponse = { status : 1 , message : 'product updated successfully .' , data : productSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to update product' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/viewLog-list' ) @ Authorized ( ) public async productViewLogList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'count' ) count , @ Req ( ) request , @ Res ( ) response ) { const select = [ ] ; const whereConditions = [ ] ; const search = [ ] ; const viewLogs = await this . productViewLogService . list ( limit , offset , select , search , whereConditions , 0 , count ) ; if ( count ) { const successresponse = { status : 1 , message : 'Successfully got view log count' , data : viewLogs , } ; return response . status ( 200 ) . send ( successresponse ) ; } else { const successResponse = { status : 1 , message : 'Successfully got view log List' , data : viewLogs , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/customerProductView-list/:id' ) @ Authorized ( ) public async customerProductView ( @ Param ( 'id' ) id , @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'count' ) count , @ Req ( ) request , @ Res ( ) response ) { const select = [ ] ; const whereConditions = [ { name : 'customerId' , value : id , } ] ; const search = [ ] ; const customerProductview = await this . productViewLogService . list ( limit , offset , select , search , whereConditions , 0 , count ) ; if ( count ) { const successresponse = { status : 1 , message : 'Successfully got view log count' , data : customerProductview , } ; return response . status ( 200 ) . send ( successresponse ) ; } else { const successResponse = { status : 1 , message : 'Successfully got view log List' , data : customerProductview , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/product-excel-list' ) public async excelProductView ( @ QueryParam ( 'productId' ) productId , @ Req ( ) request , @ Res ( ) response ) { const excel = require ( 'exceljs' ) ; const workbook = new excel . Workbook ( ) ; const worksheet = workbook . addWorksheet ( 'Product Detail Sheet' ) ; const rows = [ ] ; const productid = productId . split ( ',' ) ; for ( const id of productid ) { const dataId = await this . productService . findOne ( id ) ; if ( dataId === undefined ) { const errorResponse = { status : 0 , message : 'Invalid productId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } worksheet . columns = [ { header : 'Product Id' , key : 'productId' , size : 16 , width : 15 } , { header : 'Product Name' , key : 'name' , size : 16 , width : 15 } , { header : 'Description' , key : 'description' , size : 16 , width : 30 } , { header : 'Price' , key : 'price' , size : 16 , width : 15 } , { header : 'SKU' , key : 'sku' , size : 16 , width : 15 } , { header : 'UPC' , key : 'upc' , size : 16 , width : 15 } , { header : 'Quantity' , key : 'quantity' , size : 16 , width : 15 } , { header : 'Minimum Quantity' , key : 'minimumQuantity' , size : 16 , width : 19 } , { header : 'Subtract Stock' , key : 'subtractstock' , size : 16 , width : 15 } , { header : 'Manufacture Id' , key : 'manufactureId' , size : 16 , width : 15 } , { header : 'Meta Tag Title' , key : 'metaTagTitle' , size : 16 , width : 15 } , ] ; worksheet . getCell ( 'A1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'B1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'C1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'D1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'E1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'F1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'G1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'H1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'I1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'J1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; worksheet . getCell ( 'K1' ) . border = { top : { style : 'thin' } , left : { style : 'thin' } , bottom : { style : 'thin' } , right : { style : 'thin' } } ; for ( const id of productid ) { const dataId = await this . productService . findOne ( id ) ; const productDescription = dataId . description ; const dataDescription = productDescription . replace ( /(&nbsp;|(<([^>]+)>))/ig , '' ) ; rows . push ( [ dataId . productId , dataId . name , dataDescription . trim ( ) , dataId . price , dataId . sku , dataId . upc , dataId . quantity , dataId . minimumQuantity , dataId . subtractStock , dataId . manufacturerId , dataId . metaTagTitle ] ) ; } worksheet . addRows ( rows ) ; const fileName = './ProductExcel_' + Date . now ( ) + '.xlsx' ; await workbook . xlsx . writeFile ( fileName ) ; return new Promise ( ( resolve , reject ) => { response . download ( fileName , ( err , data ) => { if ( err ) { reject ( err ) ; } else { fs . unlinkSync ( fileName ) ; return response . end ( ) ; } } ) ; } ) ; } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-product/:id' ) @ Authorized ( ) public async deleteProduct ( @ Param ( 'id' ) productid , @ Res ( ) response , @ Req ( ) request ) { const product = await this . productService . findOne ( productid ) ; if ( product === undefined ) { const errorResponse = { status : 0 , message : 'Invalid productId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const orderProductId = await this . orderProductService . findOne ( { where : { productId : productid } } ) ; if ( orderProductId ) { const errorResponse = { status : 0 , message : 'That product is ordered' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteProduct = await this . productService . delete ( productid ) ; if ( deleteProduct ) { const successResponse = { status : 1 , message : 'Successfully deleted Product' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete product' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/delete-product' ) @ Authorized ( ) public async deleteMultipleProduct ( @ Body ( { validate : true } ) productDelete , @ Res ( ) response , @ Req ( ) request ) { const productIdNo = productDelete . productId . toString ( ) ; const productid = productIdNo . split ( ',' ) ; for ( const id of productid ) { const dataId = await this . productService . findOne ( id ) ; if ( dataId === undefined ) { const errorResponse = { status : 0 , message : 'Please choose a product for delete' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } for ( const id of productid ) { const orderProductId = await this . orderProductService . findOne ( { where : { productId : id } } ) ; if ( orderProductId ) { const errorResponse = { status : 0 , message : 'That product is ordered' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } for ( const id of productid ) { const deleteProductId = parseInt ( id , 10 ) ; await this . productService . delete ( deleteProductId ) ; } const successResponse = { status : 1 , message : 'Successfully deleted Product' , } ; return response . status ( 200 ) . send ( successResponse ) ; } }