first
This commit is contained in:
@@ -0,0 +1,683 @@
|
||||
import { Request } from "express";
|
||||
import { inject } from "inversify";
|
||||
import {
|
||||
controller,
|
||||
httpDelete,
|
||||
httpGet,
|
||||
httpPatch,
|
||||
httpPost,
|
||||
queryParam,
|
||||
request,
|
||||
requestBody,
|
||||
requestParam,
|
||||
} from "inversify-express-utils";
|
||||
|
||||
import { ProductFinalStepDTO, ProductStepOneDTO, ProductStepTwoDTO } from "./DTO/CreateProduct.dto";
|
||||
import { ProductService } from "./providers/product.service";
|
||||
import { HttpStatus } from "../../common";
|
||||
import { ProductAdDTO } from "./DTO/addProductAd.dto";
|
||||
import { AddVariantDTO } from "./DTO/addVariant.dto";
|
||||
import { AddWishlistDTO, RemoveWishlistDTO } from "./DTO/addwishlist.dto";
|
||||
import { CreateAnswerQuestionDTO, CreateCommentDTO, CreateQuestionDTO } from "./DTO/createQuestion.dto";
|
||||
import { CreateReportDTO } from "./DTO/createReport.dto";
|
||||
import { AddObserverDTO, RemoveObserverDTO } from "./DTO/observer.dto";
|
||||
import { ProductParamDto, SetFreeShipParamDto } from "./DTO/productParam.dto";
|
||||
import { RequestProductDTO } from "./DTO/RequestProduct.dto";
|
||||
import { UpdateProductFinalStepDTO, UpdateProductStepOneDTO, UpdateProductStepTwoDTO } from "./DTO/updateProduct.dto";
|
||||
import { ActivateVariantDTO, UpdateVariantDTO } from "./DTO/updateVariant.dto";
|
||||
import { ProductRequestService } from "./providers/product-request.service";
|
||||
import { BaseController } from "../../common/base/controller";
|
||||
import { ApiAuth, ApiFile, ApiModel, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from "../../common/decorator/swggerDocs";
|
||||
import { PaginationDTO } from "../../common/dto/pagination.dto";
|
||||
import { Guard } from "../../core/middlewares/guard.middleware";
|
||||
import { ValidationMiddleware } from "../../core/middlewares/validator.middleware";
|
||||
import { IOCTYPES } from "../../IOC/ioc.types";
|
||||
import { UploadService } from "../../utils/upload.service";
|
||||
import { ISeller } from "../seller/models/Abstraction/ISeller";
|
||||
import { OwnerRef } from "../shop/models/Abstraction/IShop";
|
||||
import { IUser } from "../user/models/Abstraction/IUser";
|
||||
|
||||
@controller("/product")
|
||||
@ApiTags("Product")
|
||||
class ProductController extends BaseController {
|
||||
@inject(IOCTYPES.ProductService) productService: ProductService;
|
||||
@inject(IOCTYPES.ProductRequestService) productRequestService: ProductRequestService;
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//create product
|
||||
@ApiOperation("first step to create a single product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(ProductStepOneDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/creation/detail", Guard.authSeller(), ValidationMiddleware.validateInput(ProductStepOneDTO))
|
||||
public async createProduct(@requestBody() createProductStepOneDto: ProductStepOneDTO, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.createProductS(createProductStepOneDto, seller._id.toString());
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("second step to create a single product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(ProductStepTwoDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/creation/attribute", Guard.authSeller(), ValidationMiddleware.validateInput(ProductStepTwoDTO))
|
||||
public async createProductStepTwo(@requestBody() createProductStepTwoDto: ProductStepTwoDTO, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.createProductStepTwoS(createProductStepTwoDto, seller._id.toString());
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("final step to create a single product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(ProductFinalStepDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/creation/save", Guard.authSeller(), ValidationMiddleware.validateInput(ProductFinalStepDTO))
|
||||
public async createProductFinalStep(@requestBody() createProductFinalStepDto: ProductFinalStepDTO, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.createProductFinalStepS(createProductFinalStepDto, seller._id.toString());
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
|
||||
//delete product of seller if not approved
|
||||
|
||||
@ApiOperation("mark a product as draft")
|
||||
@ApiResponse("successful", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product")
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/draft", Guard.authSeller())
|
||||
public async draftProduct(@requestParam("id") id: string) {
|
||||
const data = await this.productService.draft(+id);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("delete a product with its id ==> login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiAuth()
|
||||
@ApiParam("id", "id of product", true)
|
||||
@httpDelete("/:id/delete", Guard.authSeller(), ValidationMiddleware.validateParameter(ProductParamDto))
|
||||
public async deleteProduct(@request() req: Request, @requestParam() paramDto: ProductParamDto) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.deleteProduct(seller._id.toString(), +paramDto.id);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("clone exist products ==> login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of a product", true)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/clone", Guard.authSeller(), ValidationMiddleware.validateParameter(ProductParamDto))
|
||||
public async cloneProduct(@request() req: Request, @requestParam() paramDto: ProductParamDto) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.cloneProduct(seller._id.toString(), paramDto.id);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//update product if it is in draft status
|
||||
@ApiOperation("first step to update a single product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(UpdateProductStepOneDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/update/detail", Guard.authSeller(), ValidationMiddleware.validateInput(UpdateProductStepOneDTO))
|
||||
public async updateProduct(@requestBody() updateProductStepOneDto: UpdateProductStepOneDTO, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.updateProductStepOneS(updateProductStepOneDto, seller._id.toString());
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("second step to update a single product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(UpdateProductStepTwoDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/update/attribute", Guard.authSeller(), ValidationMiddleware.validateInput(UpdateProductStepTwoDTO))
|
||||
public async updateProductStepTwo(@requestBody() updateProductStepTwoDto: UpdateProductStepTwoDTO, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.updateProductStepTwoS(updateProductStepTwoDto, seller._id.toString());
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("final step to update a single product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(UpdateProductFinalStepDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/update/save", Guard.authSeller(), ValidationMiddleware.validateInput(UpdateProductFinalStepDTO))
|
||||
public async updateProductFinalStep(@requestBody() updateProductFinalStepDto: UpdateProductFinalStepDTO, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.updateProductFinalStepS(updateProductFinalStepDto, seller._id.toString());
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
// request to create a product by admin
|
||||
@ApiOperation("request to add product by admin ===> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(RequestProductDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/request/addBy-admin", Guard.authSeller(), ValidationMiddleware.validateInput(RequestProductDTO))
|
||||
public async requestProduct(@requestBody() requestProductDto: RequestProductDTO, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productRequestService.requestProductS(requestProductDto, seller._id.toString());
|
||||
return this.response(data, HttpStatus.Created);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
|
||||
@ApiOperation("check if product is in user wishlist")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "productId", true)
|
||||
@ApiParam("variantId", "variantId", true)
|
||||
@ApiAuth()
|
||||
@httpGet("/:id/wishlist/:variantId", Guard.authUser())
|
||||
public async checkProductInWishlist(
|
||||
@requestParam("id") productId: string,
|
||||
@requestParam("variantId") variantId: string,
|
||||
@request() req: Request,
|
||||
) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.checkProductInWishlist(user._id.toString(), +productId, variantId);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//product wishlist
|
||||
@ApiOperation("add product to the user wishlist ===> need to login as user")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiParam("id", "productId", true)
|
||||
@ApiModel(AddWishlistDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/wishlist/add", Guard.authUser(), ValidationMiddleware.validateInput(AddWishlistDTO))
|
||||
public async addToWishlist(@requestParam("id") productId: string, @request() req: Request, @requestBody() addDto: AddWishlistDTO) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.addToWishlist(user._id.toString(), +productId, addDto);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("remove product from the user wishlist ===> need to login as user")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiParam("id", "productId", true)
|
||||
@ApiModel(RemoveWishlistDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/wishlist/remove", Guard.authUser(), ValidationMiddleware.validateInput(RemoveWishlistDTO))
|
||||
public async removeWishlist(@requestParam("id") productId: string, @request() req: Request, @requestBody() removeDto: RemoveWishlistDTO) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.removeWishlist(user._id.toString(), +productId, removeDto);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//product observe
|
||||
@ApiOperation("add user to the product observer ===> need to login as user")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiParam("id", "productId", true)
|
||||
@ApiModel(AddObserverDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/observe/add", Guard.authUser(), ValidationMiddleware.validateInput(AddObserverDTO))
|
||||
public async addObserverToProduct(
|
||||
@requestParam("id") productId: string,
|
||||
@request() req: Request,
|
||||
@requestBody() addObserverDto: AddObserverDTO,
|
||||
) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.addObserver(user._id.toString(), +productId, addObserverDto);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("remove user from the product observer ===> need to login as user")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "productId", true)
|
||||
@ApiModel(RemoveObserverDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/observe/remove", Guard.authUser(), ValidationMiddleware.validateInput(RemoveObserverDTO))
|
||||
public async removeObserverFromProduct(
|
||||
@requestParam("id") productId: string,
|
||||
@request() req: Request,
|
||||
@requestBody() removeObserverDto: RemoveObserverDTO,
|
||||
) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.removeObserver(user._id.toString(), +productId, removeObserverDto);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//product search ad
|
||||
@ApiOperation("add product search ad ===> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiModel(ProductAdDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/search-ad", Guard.authSeller(), ValidationMiddleware.validateInput(ProductAdDTO))
|
||||
public async addProductAd(@requestBody() createDto: ProductAdDTO, @request() req: Request, @requestParam("id") productId: string) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.addProductAdS(seller._id.toString(), createDto, +productId);
|
||||
return this.response(data, HttpStatus.Created);
|
||||
}
|
||||
@ApiOperation("get a product search ad ===> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiParam("variantId", "id of product variant", true)
|
||||
@ApiAuth()
|
||||
@httpGet("/:id/search-ad/:variantId", Guard.authSeller())
|
||||
public async getProductAd(@request() req: Request, @requestParam("id") productId: string, @requestParam("variantId") variantId: string) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.getProductAds(seller._id.toString(), +productId, variantId);
|
||||
return this.response(data);
|
||||
}
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//product variant
|
||||
|
||||
@ApiOperation("add a variant to product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(AddVariantDTO)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/variants", Guard.authSeller(), ValidationMiddleware.validateInput(AddVariantDTO))
|
||||
public async addVariant(@requestBody() addVariantDto: AddVariantDTO, @request() req: Request, @requestParam("id") productId: string) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.addVariantS(addVariantDto, seller._id.toString(), +productId);
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("Activate/Deactivate a product variant status ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiModel(ActivateVariantDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/variants/activate", Guard.authSeller(), ValidationMiddleware.validateInput(ActivateVariantDTO))
|
||||
public async activateVariant(
|
||||
@requestBody() activateDto: ActivateVariantDTO,
|
||||
@request() req: Request,
|
||||
@requestParam("id") productId: string,
|
||||
) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.activateVariantS(seller._id.toString(), activateDto, +productId);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("set free shipping for a product variant ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiParam("variantId", "id of product variant", true)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/variants/:variantId/free-shipping", Guard.authSeller(), ValidationMiddleware.validateParameter(SetFreeShipParamDto))
|
||||
public async setFreeShipping(@queryParam() paramDto: SetFreeShipParamDto, @request() req: Request) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.setFreeShipping(seller._id.toString(), paramDto.id, paramDto.variantId);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("update a variant of product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiModel(UpdateVariantDTO)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiAuth()
|
||||
@httpPatch("/:id/variants", Guard.authSeller(), ValidationMiddleware.validateInput(UpdateVariantDTO))
|
||||
public async updateVariant(
|
||||
@requestBody() updateVariantDto: UpdateVariantDTO,
|
||||
@request() req: Request,
|
||||
@requestParam("id") productId: string,
|
||||
) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.updateVariantS(updateVariantDto, seller._id.toString(), +productId);
|
||||
return this.response({ data }, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("get all variants of a product ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiQuery("page", "the page want to get")
|
||||
@ApiQuery("limit", "the limit of return data")
|
||||
@ApiQuery("variant_status", "status of variant ==> value = 1 if want to include those with this status")
|
||||
@ApiQuery("shipment_method", "shipment method id of product variant")
|
||||
@ApiQuery("stock", "stock status of variant ==> value 1 if want to include those with this status")
|
||||
@ApiQuery("include_ad", "include_ad status ==> value = 1 if want to include those with this status ")
|
||||
@ApiQuery("special_sale", "special_sale status ==> value = 1 if want to include those with this status")
|
||||
@ApiQuery("sort", "sort variant ==> value = createdAt", false, "array")
|
||||
@ApiQuery("q", "q is the product variant id or special code of variant that seller specify")
|
||||
@ApiAuth()
|
||||
@httpGet("/:id/variants", Guard.authSeller())
|
||||
public async getAllVariant(
|
||||
@request() req: Request,
|
||||
@requestParam("id") productId: string,
|
||||
@queryParam("page") page: string,
|
||||
@queryParam("limit") limit: string,
|
||||
@queryParam("variant_status") variantStatus: string,
|
||||
@queryParam("shipment_method") shipmentMethod: string,
|
||||
@queryParam("stock") stock: string,
|
||||
@queryParam("include_ad") includeAd: string,
|
||||
@queryParam("special_sale") specialSale: string,
|
||||
@queryParam("sort") sort: string[],
|
||||
@queryParam("q") q: string,
|
||||
) {
|
||||
const queries = {
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
variantStatus: !!variantStatus,
|
||||
shipmentMethod: +shipmentMethod,
|
||||
stock: !!stock,
|
||||
includeAd: !!includeAd,
|
||||
specialSale: !!specialSale,
|
||||
sort,
|
||||
q,
|
||||
};
|
||||
const seller = req.user as ISeller;
|
||||
const { count, ...data } = await this.productService.getAllVariantS(seller._id.toString(), +productId, queries);
|
||||
const { pager } = this.paginate(count);
|
||||
return this.response({ pager, productVariants: data.productVariants });
|
||||
}
|
||||
|
||||
@ApiOperation("get all variants ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiQuery("page", "the page want to get")
|
||||
@ApiQuery("limit", "the limit of return data")
|
||||
@ApiQuery("variant_status", "status of variant ==> value = 1 if want to include those with this status")
|
||||
@ApiQuery("shipment_method", "shipment method id of product variant")
|
||||
@ApiQuery("stock", "stock status of variant ==> value 1 if want to include those with this status")
|
||||
@ApiQuery("include_ad", "include_ad status ==> value = 1 if want to include those with this status ")
|
||||
@ApiQuery("special_sale", "special_sale status ==> value = 1 if want to include those with this status")
|
||||
@ApiQuery("sort", "sort variant ==> value = createdAt", false, "array")
|
||||
@ApiQuery("q", "q is the product variant id or special code of variant that seller specify")
|
||||
@ApiAuth()
|
||||
@httpGet("/variants", Guard.authSeller())
|
||||
public async getAllVariants(
|
||||
@request() req: Request,
|
||||
@queryParam("page") page: string,
|
||||
@queryParam("limit") limit: string,
|
||||
@queryParam("variant_status") variantStatus: string,
|
||||
@queryParam("shipment_method") shipmentMethod: string,
|
||||
@queryParam("stock") stock: string,
|
||||
@queryParam("include_ad") includeAd: string,
|
||||
@queryParam("special_sale") specialSale: string,
|
||||
@queryParam("sort") sort: string[],
|
||||
@queryParam("q") q: string,
|
||||
) {
|
||||
const queries = {
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
variantStatus: !!variantStatus,
|
||||
shipmentMethod: +shipmentMethod,
|
||||
stock: !!stock,
|
||||
includeAd: !!includeAd,
|
||||
specialSale: !!specialSale,
|
||||
sort,
|
||||
q,
|
||||
};
|
||||
const seller = req.user as ISeller;
|
||||
const { count, ...data } = await this.productService.getAllProductsVariantsS(seller._id.toString(), queries);
|
||||
const { pager } = this.paginate(count);
|
||||
return this.response({ pager, productVariants: data.productsVariants });
|
||||
}
|
||||
/** */
|
||||
@ApiOperation("get a single product variant ==> need to login as seller")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiParam("variantId", "id of product variant", true)
|
||||
@ApiAuth()
|
||||
@httpGet("/:id/variants/:variantId", Guard.authSeller())
|
||||
public async getSingleVariant(
|
||||
@request() req: Request,
|
||||
@requestParam("id") productId: string,
|
||||
@requestParam("variantId") variantId: string,
|
||||
) {
|
||||
const seller = req.user as ISeller;
|
||||
const data = await this.productService.getSingleVariant(seller._id.toString(), +productId, variantId);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//compare product
|
||||
@ApiOperation("search to get all product to compare based on the product category")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiQuery("productId", "id of a product", true)
|
||||
@ApiQuery("limit", "the limit of return data")
|
||||
@ApiQuery("page", "the page want to get")
|
||||
@httpGet("/compare/search")
|
||||
public async searchForCompareProducts(
|
||||
@queryParam("productId") productId: string,
|
||||
@queryParam("limit") limit: string,
|
||||
@queryParam("page") page: string,
|
||||
) {
|
||||
const queries = {
|
||||
limit: parseInt(limit),
|
||||
page: parseInt(page),
|
||||
productId: +productId,
|
||||
};
|
||||
|
||||
const { count, ...data } = await this.productService.searchForCompareProductsS(queries);
|
||||
const { pager } = this.paginate(count);
|
||||
return this.response({ pager, products: data.products });
|
||||
}
|
||||
|
||||
@ApiOperation("get product specification to compare")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiQuery("productIds", "Array of product IDs to compare", true, "array")
|
||||
@httpGet("/compare")
|
||||
public async compareProducts(@queryParam("productIds") productIds: string[]) {
|
||||
const data = await this.productService.compareProducts(productIds);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
// get product and delete product
|
||||
|
||||
@ApiOperation("search the product with query")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiQuery("q", "search query", true)
|
||||
@httpGet("/search")
|
||||
public async productSearch(@queryParam("q") q: string) {
|
||||
const data = await this.productService.searchProducts(q);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("get product details for product page")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("productId", "id of a product", true)
|
||||
@httpGet("/:productId")
|
||||
public async getProductDetails(@requestParam("productId") productId: string) {
|
||||
const data = await this.productService.getProductDetails(+productId);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("get product price-history")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("productId", "id of a product", true)
|
||||
@ApiQuery("since", "search since date ==> 1403/01/15")
|
||||
@httpGet("/:productId/price-history")
|
||||
public async getProductPriceHistory(@requestParam("productId") productId: string, @queryParam("since") since: string) {
|
||||
const data = await this.productService.getProductPriceHistoryS(+productId, since);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//product questions and comments
|
||||
@ApiOperation("add a question to a product with its id ====> need to login as user")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiModel(CreateQuestionDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/questions", Guard.authUser(), ValidationMiddleware.validateInput(CreateQuestionDTO))
|
||||
public async addQuestion(
|
||||
@request() req: Request,
|
||||
@requestParam("id") productId: string,
|
||||
@requestBody() createQuestionDto: CreateQuestionDTO,
|
||||
) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.addQuestionS(user._id.toString(), +productId, createQuestionDto);
|
||||
return this.response(data, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("get all questions of a product with its id")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@httpGet("/:id/questions")
|
||||
public async getQuestions(@requestParam("id") productId: string) {
|
||||
const data = await this.productService.getQuestions(+productId);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("delete a question with its id ====> need to login as admin")
|
||||
@ApiResponse("successful", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of question")
|
||||
@ApiAuth()
|
||||
@httpDelete("/questions/:id/delete")
|
||||
public async deleteQuestion(@requestParam("id") id: string) {
|
||||
const data = await this.productService.deleteProductQuestion(id);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("delete a comment with its id ====> need to login as admin")
|
||||
@ApiResponse("successful", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of comment")
|
||||
@ApiAuth()
|
||||
@httpDelete("/comments/:id/delete")
|
||||
public async deleteComment(@requestParam("id") id: string) {
|
||||
const data = await this.productService.deleteProductComment(id);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("answer to a question with its id ====> need to login as seller")
|
||||
@ApiResponse("successful", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of question")
|
||||
@ApiModel(CreateAnswerQuestionDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/questions/:id/answer", Guard.authSeller(), ValidationMiddleware.validateInput(CreateAnswerQuestionDTO))
|
||||
public async answerQuestion(@requestParam("id") id: string, @requestBody() createDto: CreateAnswerQuestionDTO, @request() req: Request) {
|
||||
const user = req.user as ISeller;
|
||||
const data = await this.productService.answerQuestion(id, createDto, user._id.toString(), OwnerRef.SELLER);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("add a comment to a product with its id ====> need to login as user")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiModel(CreateCommentDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/comments", Guard.authUser(), ValidationMiddleware.validateInput(CreateCommentDTO))
|
||||
public async addComment(
|
||||
@request() req: Request,
|
||||
@requestParam("id") productId: string,
|
||||
@requestBody() createCommentDto: CreateCommentDTO,
|
||||
) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.addCommentS(user._id.toString(), +productId, createCommentDto);
|
||||
return this.response(data, HttpStatus.Created);
|
||||
}
|
||||
|
||||
@ApiOperation("get all comments of a product with its id")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@httpGet("/:id/comments")
|
||||
public async getComments(@requestParam("id") productId: string) {
|
||||
const data = await this.productService.getComments(+productId);
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//product report
|
||||
@ApiOperation("get a report question of a product")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@httpGet("/:id/report/questions")
|
||||
public async getReportQuestion() {
|
||||
const data = await this.productService.getReportQuestions();
|
||||
return this.response(data);
|
||||
}
|
||||
|
||||
@ApiOperation("create a report on a product ====> need to login as user")
|
||||
@ApiResponse("successfully", HttpStatus.Created)
|
||||
@ApiParam("id", "id of product", true)
|
||||
@ApiModel(CreateReportDTO)
|
||||
@ApiAuth()
|
||||
@httpPost("/:id/report/save", Guard.authUser(), ValidationMiddleware.validateInput(CreateReportDTO))
|
||||
public async addReport(@request() req: Request, @requestBody() createReportDto: CreateReportDTO, @requestParam("id") productId: string) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.addReportS(user._id.toString(), createReportDto, +productId);
|
||||
return this.response(data, HttpStatus.Created);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//popular & incredible product
|
||||
@ApiOperation("popular products")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiAuth()
|
||||
@httpGet("/popular/all", Guard.authOptional())
|
||||
public async getPopularProducts(@request() req: Request) {
|
||||
const user = req.user as IUser;
|
||||
const data = await this.productService.getPopularProducts(user?._id.toString());
|
||||
return this.response(data, HttpStatus.Ok);
|
||||
}
|
||||
|
||||
@ApiOperation("get all incredible offer that added for admin")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiQuery("page", "the page want to get")
|
||||
@ApiQuery("limit", "the limit of return data")
|
||||
@ApiAuth()
|
||||
@httpGet("/incredible/all", Guard.authAdmin(), ValidationMiddleware.validateQuery(PaginationDTO))
|
||||
public async getIncredibleProducts(@queryParam() paginationDto: PaginationDTO) {
|
||||
const { count, incredibleOffers } = await this.productService.getIncredibleOffers(paginationDto);
|
||||
const { pager } = this.paginate(count);
|
||||
return this.response({ pager, incredibleOffers }, HttpStatus.Ok);
|
||||
}
|
||||
|
||||
@ApiOperation("get all special sales product for admin")
|
||||
@ApiResponse("successfully", HttpStatus.Ok)
|
||||
@ApiQuery("page", "the page want to get")
|
||||
@ApiQuery("limit", "the limit of return data")
|
||||
@ApiAuth()
|
||||
@httpGet("/special-sale/all", Guard.authAdmin(), ValidationMiddleware.validateQuery(PaginationDTO))
|
||||
public async getAllProductWithSpecialSale(@queryParam() paginationDto: PaginationDTO) {
|
||||
const { count, specialSaleProducts } = await this.productService.getSpecialProductsForAdmin(paginationDto);
|
||||
const { pager } = this.paginate(count);
|
||||
return this.response({ pager, specialSaleProducts }, HttpStatus.Ok);
|
||||
}
|
||||
|
||||
//###############################################################
|
||||
//###############################################################
|
||||
//product uploader
|
||||
@ApiOperation("Upload a product image ==> need to login as seller")
|
||||
@ApiResponse("File uploaded successfully", HttpStatus.Accepted)
|
||||
@ApiFile("image")
|
||||
@ApiAuth()
|
||||
@httpPost("/image/upload/single", Guard.authSeller(), UploadService.single("image", "product-image"))
|
||||
public async uploadImage(@request() req: Request) {
|
||||
// eslint-disable-next-line no-undef
|
||||
const file = req.file as Express.MulterS3.File;
|
||||
const data = {
|
||||
message: "file uploaded!",
|
||||
url: {
|
||||
originalname: file.originalname,
|
||||
size: file.size,
|
||||
url: file.location,
|
||||
type: file.mimetype,
|
||||
},
|
||||
};
|
||||
return this.response({ data }, HttpStatus.Accepted);
|
||||
}
|
||||
|
||||
@ApiOperation("Upload a multiple product images ==> need to login as seller")
|
||||
@ApiResponse("File uploaded successfully", HttpStatus.Accepted)
|
||||
@ApiFile("image", true, true)
|
||||
@ApiAuth()
|
||||
@httpPost("/image/upload/multiple", Guard.authSeller(), UploadService.multiple("image", 10, "product-image"))
|
||||
public async uploadImages(@request() req: Request) {
|
||||
// eslint-disable-next-line no-undef
|
||||
const files = req.files as Express.MulterS3.File[];
|
||||
const data = {
|
||||
message: "files uploaded!",
|
||||
urls: files.map((file) => ({
|
||||
originalname: file.originalname,
|
||||
size: file.size,
|
||||
url: file.location,
|
||||
type: file.mimetype,
|
||||
})),
|
||||
};
|
||||
return this.response(data, HttpStatus.Accepted);
|
||||
}
|
||||
}
|
||||
|
||||
export { ProductController };
|
||||
Reference in New Issue
Block a user