vendaweb-api/src/sales/shopping/shopping.controller.ts

184 lines
7.0 KiB
TypeScript

import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Post, Put, Req } from '@nestjs/common';
import { ShoppingItem } from 'src/domain/models/shopping-item.model';
import { ShoppingService } from './shopping.service';
import { ResultModel } from '../../domain/models/result.model';
import { OrderDiscount } from 'src/domain/models/order-discount.model';
import { OrderTaxDelivery } from 'src/domain/models/order-taxdelivery.model';
import { LogOrder } from 'src/domain/models/log-order.model';
import { ApiTags } from '@nestjs/swagger';
import { Cart } from 'src/domain/models/cart.model';
import { CartUpdate } from 'src/domain/models/cart-update.model';
@ApiTags('Shopping')
@Controller('api/v1/shopping')
export class ShoppingController {
constructor(private shoppingService: ShoppingService) { }
@Get('cart/:id')
async getCart(@Param('id') id: string) {
try {
const cart = await this.shoppingService.GetItensCart(id);
if (cart == null || cart.length == 0)
throw new HttpException("Carrinho de compras não encontrado", HttpStatus.NOT_FOUND);
return cart;
} catch (error) {
const status = error.status == 404 ? error.status : HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(error.message, status);
}
}
@Get(':id')
async getPreVenda(@Param('id') id: string) {
try {
const cart = await this.shoppingService.getShopping(id);
if (cart == null)
throw new HttpException("Carrinho de compras não encontrado", HttpStatus.NOT_FOUND);
return cart;
} catch (error) {
const status = error.status == 404 ? error.status : HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(error.message, status);
}
}
@Get('cart/:idcart/item/:idProduct/tipoentrega/:deliveryType')
async getItemCart(@Req() request, @Param('idCart') idCart: string,
@Param('idProduct') idProduct: string, @Param('deliveryType') deliveryType: string) {
let store = '99';
try {
if (request.headers['x-store'])
store = request.headers['x-store'];
const cart = await this.shoppingService.getItemCart(idCart, idProduct, store, deliveryType);
if (cart == null)
throw new HttpException("Item não foi encontrado no carrinho de compras.", HttpStatus.NOT_FOUND);
return cart;
} catch (error) {
const status = error.status == 404 ? error.status : HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(error.message, status);
}
}
@Get('cart/lot/:productId/:customerId')
async getLotProduct(@Req() request, @Param('productId') productId: number,
@Param('customerId') customerId: number) {
let store = '99';
try {
if (request.headers['x-store'])
store = request.headers['x-store'];
const lotsProduct = await this.shoppingService.getLotProduct(productId, customerId);
return lotsProduct;
} catch (error) {
const status = error.status == 404 ? error.status : HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(error.message, status);
}
}
@Post('item')
async createItemShopping(@Body() item: ShoppingItem) {
console.log('createItemShopping')
try {
return await this.shoppingService.createItemCart(item);
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('cart')
async updateCart(@Body() cart: CartUpdate) {
try {
if (cart.id == null) {
throw new HttpException('Cart sem Id informado, faça a inclusão do item no carrinho.', HttpStatus.BAD_REQUEST);
}
const updateCart = await this.shoppingService.updateShopping(cart);
return updateCart;
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Post('log')
async logOrderShopping(@Body() logOrder: LogOrder) {
try {
console.log('logOrderShopping')
return await this.shoppingService.createLogShopping(logOrder);
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('item')
async updateQuantityItem(@Body() item: ShoppingItem) {
console.log(item);
try {
if (item.id == null) {
throw new HttpException('Item sem Id informado, faça a inclusão do item no carrinho.', HttpStatus.BAD_REQUEST);
}
const itemCreate = await this.shoppingService.updateItem(item);
return itemCreate;
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('item/discount')
async updatePriceItem(@Body() item: ShoppingItem) {
console.log(item);
try {
if (item.id == null) {
throw new HttpException('Item sem Id informado, faça a inclusão do item no carrinho.', HttpStatus.BAD_REQUEST);
}
const itemCreate = await this.shoppingService.updatePrice(item);
return itemCreate;
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('order/discount')
async applyDiscountOrder(@Body() order: OrderDiscount) {
try {
const itensOrder = await this.shoppingService.applyDiscountOrder(order);
return itensOrder;
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('order/tax')
async applyTaxDeOrder(@Body() order: OrderTaxDelivery) {
try {
const orderTax = await this.shoppingService.applyTaxDelivery(order);
return orderTax;
} catch (error) {
console.log(error);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Delete('item/delete/:id')
async deleteItem(@Param('id') id: string) {
try {
await this.shoppingService.deleteItem(id);
return new ResultModel(true, 'Item excluído com sucesso!', id, null,);
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('update/payment/:id')
async updateItemsShopping(@Param('id') id: string, @Body() payment: any) {
try {
console.log('Atualizando preços (controller).')
const idPaymentPlan = payment.idPaymentPlan;
await this.shoppingService.updatePriceShopping(id, idPaymentPlan);
return new ResultModel(true, 'Preços atualizados com sucesso!', {}, null);
} catch (err) {
throw new HttpException(new ResultModel(false, err.errors.message, {}, err),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}