import 'reflect-metadata' ; import { Get , JsonController , Authorized , QueryParam , Res , Req , Post , Body , Delete , Param } from 'routing-controllers' ; import { OrderService } from '../services/OrderService' ; import { UpdateOrderChangeStatus } from './requests/UpdateOrderChangeStatus' ; import { DeleteOrderRequest } from './requests/DeleteOrderRequest' ; import { OrderLogService } from '../services/OrderLogService' ; import { OrderProductService } from '../services/OrderProductService' ; import { OrderStatusService } from '../services/OrderStatusService' ; import * as fs from 'fs' ; @ JsonController ( '/order' ) export class OrderController { constructor ( private orderService , private orderLogService , private orderProductService , private orderStatusService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/orderlist' ) @ Authorized ( ) public async orderList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'orderId' ) orderId , @ QueryParam ( 'orderStatusId' ) orderStatusId , @ QueryParam ( 'customerName' ) customerName , @ QueryParam ( 'totalAmount' ) totalAmount , @ QueryParam ( 'dateAdded' ) dateAdded , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'orderId' , 'orderStatusId' , 'orderPrefixId' , 'shippingFirstname' , 'total' , 'createdDate' , 'customerId' , 'isActive' , 'modifiedDate' ] ; const search = [ { name : 'orderPrefixId' , op : 'like' , value : orderId , } , { name : 'orderStatusId' , op : 'like' , value : orderStatusId , } , { name : 'shippingFirstname' , op : 'like' , value : customerName , } , { name : 'total' , op : 'like' , value : totalAmount , } , { name : 'createdDate' , op : 'like' , value : dateAdded , } , ] ; const WhereConditions = [ ] ; const orderList = await this . orderService . list ( limit , offset , select , search , WhereConditions , 0 , count ) ; if ( count ) { const Response = { status : 1 , message : 'Successfully got count.' , data : orderList , } ; return response . status ( 200 ) . send ( Response ) ; } const orderStatus = orderList . map ( async ( value ) => { const status = await this . orderStatusService . findOne ( { where : { orderStatusId : value . orderStatusId } , select : [ 'orderStatusId' , 'name' , 'colorCode' ] , } ) ; const temp = value ; temp . orderStatus = status ; return temp ; } ) ; const results = await Promise . all ( orderStatus ) ; const successResponse = { status : 1 , message : 'Successfully got the complete order list.' , data : results , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/order-detail' ) @ Authorized ( ) public async orderDetail ( @ QueryParam ( 'orderId' ) orderid , @ Req ( ) request , @ Res ( ) response ) { const orderData = await this . orderService . findOrder ( { where : { orderId : orderid } , select : [ 'orderId' , 'orderStatusId' , 'customerId' , 'telephone' , 'invoiceNo' , 'invoicePrefix' , 'orderPrefixId' , 'shippingFirstname' , 'shippingLastname' , 'shippingCompany' , 'shippingAddress1' , 'shippingAddress2' , 'shippingCity' , 'email' , 'shippingZone' , 'shippingPostcode' , 'shippingCountry' , 'shippingAddressFormat' , 'paymentFirstname' , 'paymentLastname' , 'paymentCompany' , 'paymentAddress1' , 'paymentAddress2' , 'paymentCity' , 'paymentPostcode' , 'paymentCountry' , 'paymentZone' , 'paymentAddressFormat' , 'total' , 'customerId' , 'createdDate' ] } ) ; orderData . productList = await this . orderProductService . find ( { where : { orderId : orderid } , select : [ 'orderProductId' , 'orderId' , 'productId' , 'name' , 'model' , 'quantity' , 'total' , 'productPrice' ] } ) . then ( ( val ) => { console . log ( val ) ; const productVal = val . map ( async ( value ) => { const tempVal = value ; return tempVal ; } ) ; const results = Promise . all ( productVal ) ; return results ; } ) ; const orderStatusData = await this . orderStatusService . findOne ( { where : { orderStatusId : orderData . orderStatusId } , select : [ 'name' , 'colorCode' ] } ) ; if ( orderStatusData ) { orderData . orderStatusName = orderStatusData . name ; orderData . statusColorCode = orderStatusData . colorCode ; } else { orderData . orderStatusName = '' ; orderData . statusColorCode = '' ; } const successResponse = { status : 1 , message : 'Successfully shown the order Detail. ' , data : orderData , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/saleslist' ) @ Authorized ( ) public async salesList ( @ Res ( ) response ) { const orderList = await this . orderService . salesList ( ) ; console . log ( orderList ) ; const promises = orderList . map ( async ( result ) => { const monthNames = [ '' , 'January' , 'February' , 'March' , 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 'October' , 'November' , 'December' , ] ; const temp = result ; temp . monthYear = monthNames [ result . month ] + '-' + result . year ; return temp ; } ) ; const finalResult = await Promise . all ( promises ) ; const successResponse = { status : 1 , message : 'Successfully get sales count List' , data : finalResult , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/total-order-amount' ) @ Authorized ( ) public async totalOrderAmount ( @ Res ( ) response ) { let total = 0 ; const order = await this . orderService . findAll ( ) ; let n = 0 ; for ( n ; n < order . length ; n ++ ) { total += + order [ n ] . total ; } if ( order ) { const successResponse = { status : 1 , message : 'Successfully get total order Amount' , data : total , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to get total order amount' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/today-order-amount' ) @ Authorized ( ) public async todayOrderAmount ( @ Res ( ) response ) { const nowDate = new Date ( ) ; const todaydate = nowDate . getFullYear ( ) + '-' + ( nowDate . getMonth ( ) + 1 ) + '-' + nowDate . getDate ( ) ; console . log ( todaydate ) ; let total = 0 ; const order = await this . orderService . findAlltodayOrder ( todaydate ) ; let n = 0 ; for ( n ; n < order . length ; n ++ ) { total += + order [ n ] . total ; } if ( order ) { const successResponse = { status : 1 , message : 'Successfully get today order Amount' , data : total , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to get today order amount' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/today-order-count' ) @ Authorized ( ) public async orderCount ( @ Res ( ) response ) { const nowDate = new Date ( ) ; const todaydate = nowDate . getFullYear ( ) + '-' + ( nowDate . getMonth ( ) + 1 ) + '-' + nowDate . getDate ( ) ; const orderCount = await this . orderService . findAllTodayOrderCount ( todaydate ) ; const successResponse = { status : 1 , message : 'Successfully get Today order count' , data : orderCount , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/order-change-status' ) @ Authorized ( ) public async orderChangeStatus ( @ Body ( { validate : true } ) orderChangeStatus , @ Res ( ) response ) { const updateOrder = await this . orderService . findOrder ( orderChangeStatus . orderId ) ; console . log ( updateOrder ) ; if ( ! updateOrder ) { const errorResponse = { status : 0 , message : 'invalid order Id' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } await this . orderLogService . create ( updateOrder ) ; console . log ( updateOrder ) ; updateOrder . orderStatusId = orderChangeStatus . orderStatusId ; console . log ( updateOrder . orderStatusId ) ; const orderSave = await this . orderService . create ( updateOrder ) ; if ( orderSave ) { const successResponse = { status : 1 , message : 'Successfully updated Order Status' , data : orderSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to updated OrderStatus' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/order-excel-list' ) public async excelOrderView ( @ QueryParam ( 'orderId' ) orderId , @ Req ( ) request , @ Res ( ) response ) { const excel = require ( 'exceljs' ) ; const workbook = new excel . Workbook ( ) ; const worksheet = workbook . addWorksheet ( 'Order Detail Sheet' ) ; const rows = [ ] ; const orderid = orderId . split ( ',' ) ; for ( const id of orderid ) { const dataId = await this . orderService . find ( { where : { orderId : id } } ) ; if ( dataId . length === 0 ) { const errorResponse = { status : 0 , message : 'Invalid orderId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } worksheet . columns = [ { header : 'Order Id' , key : 'orderPrefixId' , size : 16 , width : 15 } , { header : 'Customer Name' , key : 'shippingFirstname' , size : 16 , width : 15 } , { header : 'Email' , key : 'email' , size : 16 , width : 15 } , { header : 'Mobile Number' , key : 'telephone' , size : 16 , width : 15 } , { header : 'Total Amount' , key : 'total' , size : 16 , width : 15 } , { header : 'Created Date' , key : 'createdDate' , size : 16 , width : 15 } , { header : 'Updated Date' , key : 'modifiedDate' , 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' } } ; for ( const id of orderid ) { const dataId = await this . orderService . findOrder ( id ) ; rows . push ( [ dataId . orderPrefixId , dataId . shippingFirstname + ' ' + dataId . shippingLastname , dataId . email , dataId . telephone , dataId . total , dataId . createdDate , dataId . modifiedDate ] ) ; } worksheet . addRows ( rows ) ; const fileName = './OrderExcel_' + 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-order/:id' ) @ Authorized ( ) public async deleteOrder ( @ Param ( 'id' ) orderid , @ Res ( ) response , @ Req ( ) request ) { const orderData = await this . orderService . find ( { where : { orderId : orderid } } ) ; if ( orderData . length === 0 ) { const errorResponse = { status : 0 , message : 'Invalid orderId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteOrder = await this . orderService . delete ( orderid ) ; if ( deleteOrder ) { const successResponse = { status : 1 , message : 'Order Deleted Successfully' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete Order' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/delete-order' ) @ Authorized ( ) public async deleteMultipleOrder ( @ Body ( { validate : true } ) orderDelete , @ Res ( ) response , @ Req ( ) request ) { const orderIdNo = orderDelete . orderId . toString ( ) ; const orderid = orderIdNo . split ( ',' ) ; for ( const id of orderid ) { const orderData = await this . orderService . find ( { where : { orderId : id } } ) ; if ( orderData . length === 0 ) { const errorResponse = { status : 0 , message : 'Please choose a order for delete' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } for ( const id of orderid ) { const deleteOrderId = parseInt ( id , 10 ) ; await this . orderService . delete ( deleteOrderId ) ; } const successResponse = { status : 1 , message : 'Order Deleted Successfully' , } ; return response . status ( 200 ) . send ( successResponse ) ; } }