feat: Add new sales and shopping cart management module.
All checks were successful
Build (develop) / Promote (main) / build-and-push-deploy (push) Successful in 1m45s

This commit is contained in:
Luis Eduardo Estevao
2026-02-11 10:49:17 -03:00
parent f2b2572a35
commit f0ef3dd21c
3 changed files with 87 additions and 43 deletions

View File

@@ -1613,7 +1613,7 @@ export class SalesService {
// const sql = `SELECT ESF_CALCULAR_PRAZO_ENTREGA(TO_DATE('${saleDate}', 'DD-MM-YYYY')) AS "days" FROM DUAL`;
const timeDays = await queryRunner.query(sql);
const sqlRetiraPosterior = `SELECT ( PROXIMO_DIA_UTIL(TO_DATE('${saleDate}', 'DD-MM-YYYY'), '4') - TRUNC(SYSDATE) ) AS "days" FROM DUAL`;
const sqlRetiraPosterior = `SELECT ( PROXIMO_DIA_UTIL(TO_DATE('${saleDate}', 'DD-MM-YYYY'), '1') - TRUNC(SYSDATE) ) AS "days" FROM DUAL`;
const timeDaysRetiraPosterior = await queryRunner.query(sqlRetiraPosterior);
return { deliveryDays: timeDays[0].days, retiraPosteriorDays: timeDaysRetiraPosterior[0].days };

View File

@@ -6,117 +6,127 @@ 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';
@ApiTags('Shopping')
@Controller('api/v1/shopping')
export class ShoppingController {
constructor(private shoppingService: ShoppingService){}
constructor(private shoppingService: ShoppingService) { }
@Get('cart/:id')
async getCart(@Param('id') id: string){
try {
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);
throw new HttpException(error.message, status);
}
}
@Get(':id')
async getPreVenda(@Param('id') id: string ){
try {
async getPreVenda(@Param('id') id: string) {
try {
const cart = await this.shoppingService.getShopping(id);
if (cart == null )
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);
throw new HttpException(error.message, status);
}
}
@Put('cart')
async updateCart(@Body() cart: Cart) {
try {
return await this.shoppingService.updateShopping(cart);
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Get('cart/:idcart/item/:idProduct/tipoentrega/:deliveryType')
async getItemCart(@Req() request, @Param('idCart') idCart: string,
@Param('idProduct') idProduct: string, @Param('deliveryType') deliveryType: string){
async getItemCart(@Req() request, @Param('idCart') idCart: string,
@Param('idProduct') idProduct: string, @Param('deliveryType') deliveryType: string) {
let store = '99';
try {
try {
if (request.headers['x-store'])
store = request.headers['x-store'];
const cart = await this.shoppingService.getItemCart(idCart, idProduct, store, deliveryType);
if (cart == null )
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);
throw new HttpException(error.message, status);
}
}
@Get('cart/lot/:productId/:customerId')
async getLotProduct( @Req() request, @Param('productId') productId: number,
@Param('customerId') customerId: number ) {
async getLotProduct(@Req() request, @Param('productId') productId: number,
@Param('customerId') customerId: number) {
let store = '99';
try {
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);
throw new HttpException(error.message, status);
}
}
@Post('item')
async createItemShopping(@Body() item: ShoppingItem){
async createItemShopping(@Body() item: ShoppingItem) {
console.log('createItemShopping')
try {
try {
return await this.shoppingService.createItemCart(item);
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Post('log')
async logOrderShopping(@Body() logOrder: LogOrder){
try {
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);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('item')
async updateQuantityItem(@Body() item: ShoppingItem){
console.log(item);
try {
if (item.id == null){
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);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Put('item/discount')
async updatePriceItem(@Body() item: ShoppingItem){
console.log(item);
try {
if (item.id == null){
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);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@@ -126,7 +136,7 @@ export class ShoppingController {
const itensOrder = await this.shoppingService.applyDiscountOrder(order);
return itensOrder;
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@@ -137,17 +147,17 @@ export class ShoppingController {
return orderTax;
} catch (error) {
console.log(error);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Delete('item/delete/:id')
async deleteItem(@Param('id') id: string){
try {
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);
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@@ -165,4 +175,4 @@ export class ShoppingController {
}
}
}

View File

@@ -8,6 +8,7 @@ import { Shopping } from 'src/domain/entity/tables/estprevendac.entity';
import { OrderTaxDelivery } from 'src/domain/models/order-taxdelivery.model';
import { connectionOptions } from 'src/configs/typeorm.config';
import { LogOrder } from 'src/domain/models/log-order.model';
import { Cart } from 'src/domain/models/cart.model';
@Injectable()
export class ShoppingService {
@@ -165,11 +166,11 @@ export class ShoppingService {
const dataStockItem = await queryRunner.query(`SELECT E.estoque_disp_loja as "quantityStock"
FROM ESVLISTAPRODUTOS E WHERE E.CODPROD = ${itemShopping.idProduct}
AND E.CODFILIAL = '${itemShopping.stockStore}'`);
let quantityStock = 0;
if ( dataStockItem.length > 0 ) {
if (dataStockItem.length > 0) {
quantityStock = dataStockItem[0].quantityStock;
}
}
const sqlInsertitem = 'INSERT INTO ESTPREVENDAI ( ID, IDCART, NUMSEQ, CODPROD, QT, PVENDA, DTINCLUSAO, NOMEECOMMERCE, URLIMAGEM, TIPOPRODUTO, CODFILIALRETIRA, TIPOENTREGA, ' +
@@ -605,6 +606,39 @@ export class ShoppingService {
}
}
async updateShopping(shopping: any) {
const connection = new Connection(connectionOptions);
await connection.connect();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
try {
console.log(JSON.stringify(shopping));
const sql = `UPDATE ESTPREVENDAC
SET ESTPREVENDAC.CODCLI = :codcli
,ESTPREVENDAC.codendentcli = :codendentcli
,ESTPREVENDAC.codplpag = :codplpag
,ESTPREVENDAC.codcob = :codcob
,ESTPREVENDAC.codfilial = :codfilial
WHERE ID = :id `;
await queryRunner.query(sql, [
shopping.codcli,
shopping.codendentcli,
shopping.codplpag,
shopping.codcob,
shopping.saleStore,
shopping.id
]);
return shopping;
} catch (error) {
throw error;
} finally {
await queryRunner.release();
await connection.close();
}
}
async updatePriceShopping(idCart: string, idPaymentPlan: number) {
const connection = new Connection(connectionOptions);
await connection.connect();