commit
This commit is contained in:
19
src/payment/mindee.controller.ts
Normal file
19
src/payment/mindee.controller.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
https://docs.nestjs.com/controllers#controllers
|
||||
*/
|
||||
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { MindeeService } from './mindee.service';
|
||||
import { ApiExcludeEndpoint } from '@nestjs/swagger';
|
||||
|
||||
@Controller('api/v1/mindee')
|
||||
export class MindeeController {
|
||||
|
||||
constructor (private readonly mindeeService: MindeeService){}
|
||||
|
||||
@Get()
|
||||
@ApiExcludeEndpoint()
|
||||
loadInvoice() {
|
||||
return this.mindeeService.importDataInvoice();
|
||||
}
|
||||
}
|
||||
16
src/payment/mindee.module.ts
Normal file
16
src/payment/mindee.module.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { MindeeService } from './mindee.service';
|
||||
import { MindeeController } from './mindee.controller';
|
||||
/*
|
||||
https://docs.nestjs.com/modules
|
||||
*/
|
||||
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [
|
||||
MindeeController,],
|
||||
providers: [
|
||||
MindeeService,],
|
||||
})
|
||||
export class MindeeModule { }
|
||||
123
src/payment/mindee.service.ts
Normal file
123
src/payment/mindee.service.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
https://docs.nestjs.com/providers#services
|
||||
*/
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import * as mindee from 'mindee';
|
||||
import * as fs from "fs";
|
||||
|
||||
@Injectable()
|
||||
export class MindeeService {
|
||||
|
||||
async importDataInvoice() {
|
||||
|
||||
|
||||
// Init a new client and add your document endpoint
|
||||
const mindeeClient = new mindee.Client({ apiKey: "1ca3928852c03a843c6ac8535d2a5682" })
|
||||
.addEndpoint({
|
||||
accountName: "eduardoestevao",
|
||||
endpointName: "nota_fiscal_de_servico",
|
||||
});
|
||||
|
||||
// Load a file from disk and parse it
|
||||
const apiResponse = mindeeClient
|
||||
.docFromPath("/docs/n20.tiff")
|
||||
.parse(mindee.CustomV1, { endpointName: "nota_fiscal_de_servico" });
|
||||
|
||||
// Print a brief summary of the parsed data
|
||||
const resp = await apiResponse;
|
||||
const dataInvoice = resp.document.toString();
|
||||
|
||||
fs.writeFileSync('/docs/invoice.txt', dataInvoice);
|
||||
console.log('gravou arquivo');
|
||||
// , err => {
|
||||
// if (err) {
|
||||
// console.error(err);
|
||||
// }
|
||||
// // ficheiro escrito com sucesso
|
||||
// });
|
||||
|
||||
let fornecedor: string;
|
||||
let cnpj: string;
|
||||
let numero: string;
|
||||
let valor: number;
|
||||
let valoriss: number;
|
||||
|
||||
const data = fs.readFileSync('/docs/invoice.txt', 'utf-8');
|
||||
console.log('leu arquivo');
|
||||
const linhas = data.split(/\r?\n/);
|
||||
console.log(linhas);
|
||||
linhas.forEach((linha) => {
|
||||
if (!linha.includes('-----')) {
|
||||
const campo = linha.substring(0, linha.indexOf(":"));
|
||||
console.log('campo: ' + campo);
|
||||
if (campo == 'cliente') {
|
||||
const valor = linha.substring(linha.indexOf(':') + 2);
|
||||
fornecedor = valor;
|
||||
console.log('Fornecedor:' + valor);
|
||||
}
|
||||
}
|
||||
console.log('Linha: ' + linha);
|
||||
});
|
||||
console.log('retornando');
|
||||
return { fornecedor: fornecedor };
|
||||
|
||||
fs.readFile('/docs/invoice.txt', 'utf-8', function(err, data) {
|
||||
const linhas = data.split(/\r?\n/);
|
||||
linhas.forEach(function(linha){
|
||||
if (!linha.includes('-----')) {
|
||||
if (linha.includes('cliente:')) {
|
||||
const valor = linha.substring(linha.indexOf(':') + 2);
|
||||
fornecedor = valor;
|
||||
console.log('Fornecedor:' + valor);
|
||||
}
|
||||
}
|
||||
console.log('Linha: ' + linha); // aqui podes fazer o que precisas com cada linha
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
// return resp.document.toString();
|
||||
|
||||
/* apiResponse.then((resp) => {
|
||||
|
||||
if (resp.document === undefined) return;
|
||||
|
||||
// full object
|
||||
console.log(resp.document);
|
||||
|
||||
// string summary
|
||||
console.log(resp.document.toString());
|
||||
|
||||
return resp;
|
||||
|
||||
});*/
|
||||
}
|
||||
|
||||
async readFile() {
|
||||
let fornecedor = '';
|
||||
fs.readFile('/docs/invoice.txt', 'utf-8', async function(err, data) {
|
||||
const linhas = data.split(/\r?\n/);
|
||||
for ( const linha in linhas ) {
|
||||
if (!linha.includes('-----')) {
|
||||
if (linha.includes('cliente:')) {
|
||||
const valor = linha.substring(linha.indexOf(':') + 2);
|
||||
fornecedor = valor;
|
||||
console.log('Fornecedor:' + valor);
|
||||
}
|
||||
}
|
||||
console.log('Linha: ' + linha);
|
||||
}
|
||||
|
||||
// linhas.forEach(function(linha){
|
||||
// // aqui podes fazer o que precisas com cada linha
|
||||
// });
|
||||
});
|
||||
|
||||
return { "fornecedor": fornecedor };
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user