swagger configurado na rota chavenfe
This commit is contained in:
21
src/orders/application/invoice.service.ts
Normal file
21
src/orders/application/invoice.service.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InvoiceRepository } from '../repositories/invoice.repository';
|
||||
import { InvoiceDto } from '../dto/invoice.dto';
|
||||
|
||||
@Injectable()
|
||||
export class InvoiceService {
|
||||
constructor(private readonly invoiceRepo: InvoiceRepository) {}
|
||||
|
||||
async getInvoiceByChave(chavenfe: string): Promise<InvoiceDto> {
|
||||
const invoice = await this.invoiceRepo.findInvoiceByChave(chavenfe);
|
||||
if (!invoice) {
|
||||
throw new NotFoundException('Nota fiscal não localizada.');
|
||||
}
|
||||
|
||||
const itens = await this.invoiceRepo.findInvoiceItems(invoice.transactionId);
|
||||
return {
|
||||
...invoice,
|
||||
itens,
|
||||
};
|
||||
}
|
||||
}
|
||||
6
src/orders/application/orders.service.ts
Normal file
6
src/orders/application/orders.service.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
///← Orquestra operações de pedido
|
||||
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
|
||||
25
src/orders/dto/invoice.dto.ts
Normal file
25
src/orders/dto/invoice.dto.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export class InvoiceItemDto {
|
||||
productId: number;
|
||||
productName: string;
|
||||
package: string;
|
||||
qt: number;
|
||||
ean: string;
|
||||
multiple: number;
|
||||
productType: string;
|
||||
image: string | null;
|
||||
}
|
||||
|
||||
export class InvoiceDto {
|
||||
storeId: number;
|
||||
invoiceDate: Date;
|
||||
orderId: number;
|
||||
invoiceId: number;
|
||||
transactionId: number;
|
||||
customerId: number;
|
||||
customer: string;
|
||||
sellerId: number;
|
||||
sellerName: string;
|
||||
itensQt: number;
|
||||
itens: InvoiceItemDto[];
|
||||
}
|
||||
|
||||
@@ -7,20 +7,35 @@ https://docs.nestjs.com/controllers#controllers
|
||||
|
||||
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
|
||||
import { OrdersService } from './orders.service';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import { ApiOperation } from '@nestjs/swagger';
|
||||
import { InvoiceService } from './application/invoice.service';
|
||||
import { InvoiceDto } from './dto/invoice.dto';
|
||||
|
||||
@Controller('api/v1/orders')
|
||||
|
||||
|
||||
@ApiTags('Invoice')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Controller('api/v1/invoice')
|
||||
export class OrdersController {
|
||||
|
||||
constructor( private readonly ordersService: OrdersService) {}
|
||||
constructor(
|
||||
private readonly ordersService: OrdersService,
|
||||
private readonly invoiceService: InvoiceService,
|
||||
) {}
|
||||
|
||||
|
||||
@Get('find')
|
||||
findOrders(@Query() query) {
|
||||
return this.ordersService.findOrders(query);
|
||||
}
|
||||
|
||||
@Get('invoice/:chavenfe')
|
||||
findInvoice(@Param('chavenfe') chavenfe: string) {
|
||||
return this.ordersService.findInvoice(chavenfe);
|
||||
@Get(':chavenfe')
|
||||
async getByChave(@Param('chavenfe') chavenfe: string): Promise<InvoiceDto> {
|
||||
return this.invoiceService.getInvoiceByChave(chavenfe);
|
||||
}
|
||||
|
||||
@Get('itens/:id')
|
||||
|
||||
@@ -1,19 +1,27 @@
|
||||
/* eslint-disable prettier/prettier */
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
|
||||
import { OrdersService } from './orders.service';
|
||||
import { OrdersController } from './orders.controller';
|
||||
/*
|
||||
https://docs.nestjs.com/modules
|
||||
*/
|
||||
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { OrdersService } from './orders.service';
|
||||
import { InvoiceRepository } from './repositories/invoice.repository';
|
||||
import { InvoiceService } from './application/invoice.service';
|
||||
import { OrdersController } from './orders.controller';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [
|
||||
OrdersController,],
|
||||
providers: [
|
||||
OrdersService,],
|
||||
imports: [
|
||||
ConfigModule,
|
||||
TypeOrmModule,
|
||||
],
|
||||
controllers: [
|
||||
OrdersController,
|
||||
],
|
||||
providers: [
|
||||
OrdersService,
|
||||
InvoiceService,
|
||||
InvoiceRepository,
|
||||
],
|
||||
})
|
||||
export class OrdersModule { }
|
||||
export class OrdersModule {}
|
||||
|
||||
78
src/orders/repositories/invoice.repository.ts
Normal file
78
src/orders/repositories/invoice.repository.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class InvoiceRepository {
|
||||
constructor(
|
||||
@InjectDataSource() private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async findInvoiceByChave(chavenfe: string) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
try {
|
||||
const sql = `
|
||||
SELECT
|
||||
pcnfsaid.codfilial AS "storeId",
|
||||
pcnfsaid.dtsaida AS "invoiceDate",
|
||||
pcnfsaid.numped AS "orderId",
|
||||
pcnfsaid.numnota AS "invoiceId",
|
||||
pcnfsaid.numtransvenda AS "transactionId",
|
||||
pcnfsaid.codcli AS "customerId",
|
||||
pcclient.cliente AS "customer",
|
||||
pcnfsaid.codusur AS "sellerId",
|
||||
pcusuari.nome AS "sellerName",
|
||||
(
|
||||
SELECT SUM(pcmov.qt)
|
||||
FROM pcmov
|
||||
WHERE pcmov.numtransvenda = pcnfsaid.numtransvenda
|
||||
) AS "itensQt",
|
||||
NULL AS "itens"
|
||||
FROM pcnfsaid, pcclient, pcusuari
|
||||
WHERE
|
||||
pcnfsaid.codcli = pcclient.codcli
|
||||
AND pcnfsaid.codusur = pcusuari.codusur
|
||||
AND pcnfsaid.chavenfe = :chavenfe
|
||||
`;
|
||||
return await queryRunner.manager.query(sql, [chavenfe]);
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
async findInvoiceItems(transactionId: number) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
try {
|
||||
const sql = `
|
||||
SELECT
|
||||
pcmov.codprod AS "productId",
|
||||
pcprodut.descricao AS "productName",
|
||||
pcprodut.embalagem AS "package",
|
||||
pcmov.qt AS "qt",
|
||||
pcprodut.codauxiliar AS "ean",
|
||||
pcprodut.multiplo AS "multiple",
|
||||
pcprodut.tipoproduto AS "productType",
|
||||
REPLACE(
|
||||
CASE
|
||||
WHEN INSTR(PCPRODUT.URLIMAGEM, ';') > 0 THEN
|
||||
SUBSTR(PCPRODUT.URLIMAGEM, 1, INSTR(PCPRODUT.URLIMAGEM, ';') - 1)
|
||||
WHEN PCPRODUT.URLIMAGEM IS NOT NULL THEN
|
||||
PCPRODUT.URLIMAGEM
|
||||
ELSE NULL
|
||||
END,
|
||||
'167.249.211.178:8001',
|
||||
'10.1.1.191'
|
||||
) AS "image"
|
||||
FROM pcmov, pcprodut
|
||||
WHERE
|
||||
pcmov.codprod = pcprodut.codprod
|
||||
AND pcmov.numtransvenda = :transactionId
|
||||
`;
|
||||
return await queryRunner.manager.query(sql, [transactionId]);
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
0
src/orders/repositories/orders.repository.ts
Normal file
0
src/orders/repositories/orders.repository.ts
Normal file
Reference in New Issue
Block a user