import 'reflect-metadata' ; import { Post , JsonController , Res , Req , Authorized , Delete , Param , Get , QueryParam } from 'routing-controllers' ; import { CustomerWishlist } from '../../models/CustomerWishlist' ; import { ProductService } from '../../services/ProductService' ; import { CustomerWishlistService } from '../../services/CustomerWishlistService' ; import { ProductImageService } from '../../services/ProductImageService' ; @ JsonController ( '/customer' ) export class CustomerController { constructor ( private customerWishlistService , private productImageService , private productService ) { } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-product-to-wishlist' ) @ Authorized ( 'customer' ) public async addProductToWishlist ( @ Req ( ) request , @ Res ( ) response ) { return new Promise ( async ( ) => { const data = await this . customerWishlistService . findOne ( { where : { productId : request . body . productId , customerId : request . user . id , } , } ) ; if ( data ) { const errorResponse = { status : 1 , message : 'Already added this product to wishlist.' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const newProduct = new CustomerWishlist ( ) ; newProduct . customerId = request . user . id ; newProduct . productId = request . body . productId ; newProduct . isActive = 1 ; const resultData = await this . customerWishlistService . create ( newProduct ) ; const Id = resultData . wishlistProductId ; const Product = await this . productService . findOne ( { where : { productId : resultData . productId } } ) ; const Image = await this . productImageService . findOne ( { where : { productId : resultData . productId , defaultImage : 1 , } , } ) ; const successResponse = { status : 1 , message : 'Thank you product added to the wishlist successfully.' , data : { wishlistProductId : Id , product : Product , productImage : Image , } , } ; return response . status ( 200 ) . send ( successResponse ) ; } ) ; } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/wishlist-product-delete/:id' ) @ Authorized ( 'customer' ) public async wishlistProductDelete ( @ Param ( 'id' ) wishlistId , @ Req ( ) request , @ Res ( ) response ) { const customerwishlistId = await this . customerWishlistService . findOne ( { where : { productId : wishlistId , customerId : request . user . id } } ) ; await this . customerWishlistService . delete ( customerwishlistId . wishlistProductId ) ; const successResponse = { status : 1 , message : 'Thank you, deleted the product from wishlist successfully.' , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/wishlist-product-list' ) @ Authorized ( 'customer' ) public async wishlistProductlist ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'count' ) count , @ Req ( ) request , @ Res ( ) response ) { const select = [ 'wishlistProductId' , 'productId' ] ; const whereConditions = [ { customerId : request . user . id , } , ] ; const wishlistData = await this . customerWishlistService . list ( limit , offset , select , whereConditions , count ) ; if ( count ) { const Response = { status : 1 , message : 'Successfully get count' , data : wishlistData , } ; return response . status ( 200 ) . send ( Response ) ; } const promises = wishlistData . map ( async ( results ) => { const productData = await this . productService . findOne ( { where : { productId : results . productId } } ) ; const Image = await this . productImageService . findOne ( { where : { productId : results . productId , defaultImage : 1 } } ) ; const temp = productData ; results . product = temp ; results . productImage = Image ; return results ; } ) ; const result = await Promise . all ( promises ) ; const successResponse = { status : 1 , message : 'Successfully show the wishlist Product List' , data : result , } ; return response . status ( 200 ) . send ( successResponse ) ; } }