import 'reflect-metadata' ; import { Get , Post , Delete , Put , Body , QueryParam , Param , JsonController , Authorized , Req , Res } from 'routing-controllers' ; import * as AWS from 'aws-sdk' ; import { classToPlain } from 'class-transformer' ; import { aws_setup } from '../../env' ; import { CustomerService } from '../services/CustomerService' ; import { Customer } from '../models/Customer' ; import { CreateCustomer } from './requests/CreateCustomerRequest' ; import { User } from '../models/User' ; import { MAILService } from '../../auth/mail.services' ; import { UpdateCustomer } from './requests/UpdateCustomerRequest' ; import { OrderService } from '../services/OrderService' ; import { ProductImageService } from '../services/ProductImageService' ; import { ProductService } from '../services/ProductService' ; import { OrderProductService } from '../services/OrderProductService' ; import { EmailTemplateService } from '../services/EmailTemplateService' ; import { DeleteCustomerRequest } from './requests/DeleteCustomerRequest' ; import * as fs from 'fs' ; @ JsonController ( '/customer' ) export class CustomerController { constructor ( private customerService , private orderProductService , private productService , private productImageService , private orderService , private emailTemplateService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-customer' ) @ Authorized ( ) public async addCustomer ( @ Body ( { validate : true } ) customerParam , @ Res ( ) response ) { const avatar = customerParam . avatar ; const newCustomer = new Customer ( ) ; const resultUser = await this . customerService . findOne ( { where : { email : customerParam . email , deleteFlag : 0 } } ) ; if ( resultUser ) { const successResponse = { status : 1 , message : 'Already registered with this emailId.' , } ; return response . status ( 400 ) . send ( successResponse ) ; } if ( avatar ) { const type = avatar . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const s3 = new AWS . S3 ( ) ; const path = 'customer/' ; const base64Data = new Buffer ( avatar . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; const params = { Bucket : aws_setup . AWS_BUCKET , Key : 'customer/' + name , Body : base64Data , ACL : 'public-read' , ContentEncoding : 'base64' , ContentType : `image/${ type }` , } ; newCustomer . avatar = name ; newCustomer . avatarPath = path ; s3 . upload ( params , ( err , data ) => { if ( data ) { console . log ( 'image upload successfully' ) ; console . log ( data ) ; } else { console . log ( 'error while uploading image' ) ; } } ) ; } if ( customerParam . password === customerParam . confirmPassword ) { const password = await User . hashPassword ( customerParam . password ) ; newCustomer . customerGroupId = customerParam . customerGroupId ; newCustomer . firstName = customerParam . username ; newCustomer . username = customerParam . email ; newCustomer . email = customerParam . email ; newCustomer . mobileNumber = customerParam . mobileNumber ; newCustomer . password = password ; newCustomer . mailStatus = customerParam . mailStatus ; newCustomer . deleteFlag = 0 ; newCustomer . isActive = customerParam . status ; const customerSave = await this . customerService . create ( newCustomer ) ; if ( customerSave ) { if ( customerParam . mailStatus === 1 ) { const emailContent = await this . emailTemplateService . findOne ( 4 ) ; const message = emailContent . content . replace ( '{name}' , customerParam . username ) . replace ( '{username}' , customerParam . email ) . replace ( '{password}' , customerParam . password ) ; MAILService . customerLoginMail ( message , customerParam . email , emailContent . subject ) ; const successResponse = { status : 1 , message : 'Successfully created new Customer with user name and password and send an email. ' , data : customerSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const successResponse = { status : 1 , message : 'Customer Created Successfully' , data : customerSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } } } else { const errorResponse = { status : 0 , message : 'Password does not match.' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/customerlist' ) @ Authorized ( ) public async customerList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'name' ) name , @ QueryParam ( 'status' ) status , @ QueryParam ( 'email' ) email , @ QueryParam ( 'customerGroup' ) customerGroup , @ QueryParam ( 'date' ) date , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const search = [ { name : 'firstName' , op : 'like' , value : name , } , { name : 'email' , op : 'like' , value : email , } , { name : 'createdDate' , op : 'like' , value : date , } , { name : 'customerGroupId' , op : 'like' , value : customerGroup , } , { name : 'isActive' , op : 'like' , value : status , } , ] ; const WhereConditions = [ { name : 'deleteFlag' , value : 0 , } , ] ; const customerList = await this . customerService . list ( limit , offset , search , WhereConditions , 0 , count ) ; if ( count ) { const successRes = { status : 1 , message : 'Successfully got count ' , data : customerList , } ; return response . status ( 200 ) . send ( successRes ) ; } const data = customerList . map ( async ( value ) => { const temp = value ; return temp ; } ) ; const Customers = await Promise . all ( data ) ; const successResponse = { status : 1 , message : 'Successfully got Customer list.' , data : Customers , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-customer/:id' ) @ Authorized ( ) public async deleteCustomer ( @ Param ( 'id' ) id , @ Res ( ) response , @ Req ( ) request ) { const customer = await this . customerService . findOne ( { where : { id , } , } ) ; if ( ! customer ) { const errorResponse = { status : 0 , message : 'Invalid customerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } customer . deleteFlag = 1 ; const deleteCustomer = await this . customerService . create ( customer ) ; if ( deleteCustomer ) { const successResponse = { status : 1 , message : 'Customer Deleted Successfully' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to change delete flag status' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-customer/:id' ) @ Authorized ( ) public async updateCustomer ( @ Param ( 'id' ) id , @ Body ( { validate : true } ) customerParam , @ Res ( ) response ) { console . log ( customerParam ) ; const customer = await this . customerService . findOne ( { where : { id , } , } ) ; if ( ! customer ) { const errorResponse = { status : 0 , message : 'invalid customer id' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } if ( customerParam . password === customerParam . confirmPassword ) { const avatar = customerParam . avatar ; if ( avatar ) { const type = avatar . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const s3 = new AWS . S3 ( ) ; const path = 'customer/' ; const base64Data = new Buffer ( avatar . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; const params = { Bucket : aws_setup . AWS_BUCKET , Key : 'customer/' + name , Body : base64Data , ACL : 'public-read' , ContentEncoding : 'base64' , ContentType : `image/${ type }` , } ; s3 . upload ( params , ( err , data ) => { if ( data ) { console . log ( 'image upload successfully' ) ; console . log ( data ) ; } else { console . log ( 'error while uploading image' ) ; } } ) ; customer . avatar = name ; customer . avatarPath = path ; } customer . customerGroupId = customerParam . customerGroupId ; customer . firstName = customerParam . username ; customer . username = customerParam . email ; customer . email = customerParam . email ; customer . mobileNumber = customerParam . mobileNumber ; if ( customerParam . password ) { const password = await User . hashPassword ( customerParam . password ) ; customer . password = password ; } customer . mailStatus = customerParam . mailStatus ; customer . isActive = customerParam . status ; const customerSave = await this . customerService . create ( customer ) ; if ( customerSave ) { const successResponse = { status : 1 , message : 'Customer Updated Successfully' , data : customerSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } } else { const errorResponse = { status : 0 , message : 'Password does not match.' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/customer-details/:id' ) @ Authorized ( ) public async customerDetails ( @ Param ( 'id' ) Id , @ Res ( ) response ) { const customer = await this . customerService . findOne ( { where : { id : Id } , } ) ; if ( ! customer ) { const errorResponse = { status : 0 , message : 'invalid CustomerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const order = await this . orderService . find ( { where : { customerId : Id } } ) ; const productLists = await order . map ( async ( result ) => { const product = await this . orderProductService . find ( { where : { orderId : result . orderId } , select : [ 'productId' , 'orderId' , 'name' , 'model' , 'total' , 'createdDate' ] , } ) ; const productPromises = await product . map ( async ( value ) => { const productsDetails = value ; const products = await this . productService . find ( { where : { productId : value . productId } } ) ; const productImages = await products . map ( async ( values ) => { const productImagesResult = values ; const Image = await this . productImageService . findOne ( { select : [ 'productId' , 'productImageId' , 'image' , 'containerName' , 'defaultImage' ] , where : { productId : values . productId , defaultImage : 1 } , } ) ; productImagesResult . productImages = Image ; return productImagesResult ; } ) ; const images = await Promise . all ( productImages ) ; productsDetails . productDetails = images ; return productsDetails ; } ) ; const productsListWithImages = await Promise . all ( productPromises ) ; const temp = await productsListWithImages ; return temp ; } ) ; const finalResult = await Promise . all ( productLists ) ; customer . productList = finalResult ; if ( finalResult ) { const successResponse = { status : 1 , message : 'successfully got Customer details. ' , data : customer , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to get customer Details' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/recent-customerlist' ) @ Authorized ( ) public async recentCustomerList ( @ Res ( ) response ) { const order = 1 ; const WhereConditions = [ { name : 'deleteFlag' , value : 0 , } , ] ; const customerList = await this . customerService . list ( 0 , 0 , 0 , WhereConditions , order , 0 ) ; const successResponse = { status : 1 , message : 'Successfully got Customer list.' , data : classToPlain ( customerList ) , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/today-customercount' ) @ Authorized ( ) public async customerCount ( @ Res ( ) response ) { const nowDate = new Date ( ) ; const todaydate = nowDate . getFullYear ( ) + '-' + ( nowDate . getMonth ( ) + 1 ) + '-' + nowDate . getDate ( ) ; const customerCount = await this . customerService . todayCustomerCount ( todaydate ) ; const successResponse = { status : 1 , message : 'Successfully get customerCount' , data : customerCount , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/delete-customer' ) @ Authorized ( ) public async deleteMultipleCustomer ( @ Body ( { validate : true } ) deleteCustomerId , @ Res ( ) response , @ Req ( ) request ) { const customers = deleteCustomerId . customerId . toString ( ) ; const customer = customers . split ( ',' ) ; console . log ( customer ) ; const data = customer . map ( async ( id ) => { const dataId = await this . customerService . findOne ( id ) ; if ( dataId === undefined ) { const errorResponse = { status : 0 , message : 'Please choose customer for delete' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } else { dataId . deleteFlag = 1 ; return await this . customerService . create ( dataId ) ; } } ) ; const deleteCustomer = await Promise . all ( data ) ; if ( deleteCustomer ) { const successResponse = { status : 1 , message : 'Successfully deleted customer' , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/customer-excel-list' ) public async excelCustomerView ( @ QueryParam ( 'customerId' ) customerId , @ Req ( ) request , @ Res ( ) response ) { const excel = require ( 'exceljs' ) ; const workbook = new excel . Workbook ( ) ; const worksheet = workbook . addWorksheet ( 'Order Detail Sheet' ) ; const rows = [ ] ; const customerid = customerId . split ( ',' ) ; for ( const id of customerid ) { const dataId = await this . customerService . findOne ( id ) ; if ( dataId === undefined ) { const errorResponse = { status : 0 , message : 'Invalid customerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } worksheet . columns = [ { header : 'Customer Id' , key : 'id' , size : 16 , width : 15 } , { header : 'Customer Name' , key : 'first_name' , size : 16 , width : 15 } , { header : 'User Name' , key : 'username' , size : 16 , width : 24 } , { header : 'Email Id' , key : 'email' , size : 16 , width : 15 } , { header : 'Mobile Number' , key : 'mobileNumber' , size : 16 , width : 15 } , { header : 'Date Of Registration' , key : 'createdDate' , 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' } } ; for ( const id of customerid ) { const dataId = await this . customerService . findOne ( id ) ; if ( dataId . lastName === null ) { dataId . lastName = '' ; } rows . push ( [ dataId . id , dataId . firstName + ' ' + dataId . lastName , dataId . username , dataId . email , dataId . mobileNumber , dataId . createdDate ] ) ; } worksheet . addRows ( rows ) ; const fileName = './CustomerExcel_' + 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 ( ) ; } } ) ; } ) ; } }