forked from fga-eps-mds/2023.2-PrintGo-PrinterService
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/22-geracao-periodica-relatorio' of github.com:f…
…ga-eps-mds/2024.1-PrintGo-PrinterService into feature/28-gerar-relatorio-contrato
- Loading branch information
Showing
20 changed files
with
1,828 additions
and
57 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- CreateTable | ||
CREATE TABLE "Impressora" ( | ||
"id" SERIAL NOT NULL, | ||
"numContrato" TEXT NOT NULL, | ||
"numSerie" TEXT NOT NULL, | ||
"enderecoIp" TEXT NOT NULL, | ||
"estaNaRede" BOOLEAN NOT NULL, | ||
"dataInstalacao" TIMESTAMP(3) NOT NULL, | ||
"dataRetirada" TIMESTAMP(3), | ||
"ativo" BOOLEAN NOT NULL, | ||
"contadorInstalacaoPB" INTEGER NOT NULL, | ||
"contadorInstalacaoCor" INTEGER NOT NULL, | ||
"contadorAtualPB" INTEGER NOT NULL, | ||
"contadorAtualCor" INTEGER NOT NULL, | ||
"contadorRetiradaPB" INTEGER NOT NULL, | ||
"contadorRetiradaCor" INTEGER NOT NULL, | ||
"localizacao" TEXT NOT NULL, | ||
"modeloId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Impressora_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Impressora_numSerie_key" ON "Impressora"("numSerie"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
-- CreateTable | ||
CREATE TABLE "padroes" ( | ||
"id" SERIAL NOT NULL, | ||
"marca" TEXT NOT NULL, | ||
"modelo" TEXT NOT NULL, | ||
"tipo" TEXT NOT NULL, | ||
"colorido" BOOLEAN NOT NULL, | ||
"oidModelo" TEXT, | ||
"oidNumeroSerie" TEXT, | ||
"oidFirmware" TEXT, | ||
"oidTempoAtivo" TEXT, | ||
"oidDigitalizacoes" TEXT, | ||
"oidCopiasPB" TEXT, | ||
"oidCopiasCor" TEXT, | ||
"oidTotalGeral" TEXT, | ||
"ativo" BOOLEAN NOT NULL DEFAULT true, | ||
|
||
CONSTRAINT "padroes_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Impressora" ( | ||
"id" SERIAL NOT NULL, | ||
"numContrato" TEXT NOT NULL, | ||
"numSerie" TEXT NOT NULL, | ||
"enderecoIp" TEXT NOT NULL, | ||
"estaNaRede" BOOLEAN NOT NULL, | ||
"dataInstalacao" TIMESTAMP(3) NOT NULL, | ||
"dataRetirada" TIMESTAMP(3), | ||
"ativo" BOOLEAN NOT NULL, | ||
"contadorInstalacaoPB" INTEGER NOT NULL, | ||
"contadorInstalacaoCor" INTEGER NOT NULL, | ||
"contadorAtualPB" INTEGER NOT NULL, | ||
"contadorAtualCor" INTEGER NOT NULL, | ||
"contadorRetiradaPB" INTEGER NOT NULL, | ||
"contadorRetiradaCor" INTEGER NOT NULL, | ||
"localizacao" TEXT NOT NULL, | ||
"modeloId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Impressora_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Relatorio" ( | ||
"id" SERIAL NOT NULL, | ||
"impressoraId" INTEGER NOT NULL, | ||
"contadorMes" INTEGER NOT NULL, | ||
"ultimoResultado" INTEGER NOT NULL, | ||
"ultimaAtualizacao" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "Relatorio_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "padroes_marca_modelo_key" ON "padroes"("marca", "modelo"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Impressora_numSerie_key" ON "Impressora"("numSerie"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Relatorio_impressoraId_key" ON "Relatorio"("impressoraId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Relatorio" ADD CONSTRAINT "Relatorio_impressoraId_fkey" FOREIGN KEY ("impressoraId") REFERENCES "Impressora"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
-- CreateTable | ||
CREATE TABLE "padroes" ( | ||
"id" SERIAL NOT NULL, | ||
"marca" TEXT NOT NULL, | ||
"modelo" TEXT NOT NULL, | ||
"tipo" TEXT NOT NULL, | ||
"colorido" BOOLEAN NOT NULL, | ||
"oidModelo" TEXT, | ||
"oidNumeroSerie" TEXT, | ||
"oidFirmware" TEXT, | ||
"oidTempoAtivo" TEXT, | ||
"oidDigitalizacoes" TEXT, | ||
"oidCopiasPB" TEXT, | ||
"oidCopiasCor" TEXT, | ||
"oidTotalGeral" TEXT, | ||
"ativo" BOOLEAN NOT NULL DEFAULT true, | ||
|
||
CONSTRAINT "padroes_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Impressora" ( | ||
"id" SERIAL NOT NULL, | ||
"numContrato" TEXT NOT NULL, | ||
"numSerie" TEXT NOT NULL, | ||
"enderecoIp" TEXT NOT NULL, | ||
"estaNaRede" BOOLEAN NOT NULL, | ||
"dataInstalacao" TIMESTAMP(3) NOT NULL, | ||
"dataRetirada" TIMESTAMP(3), | ||
"ativo" BOOLEAN NOT NULL, | ||
"contadorInstalacaoPB" INTEGER NOT NULL, | ||
"contadorInstalacaoCor" INTEGER NOT NULL, | ||
"contadorAtualPB" INTEGER NOT NULL, | ||
"contadorAtualCor" INTEGER NOT NULL, | ||
"contadorRetiradaPB" INTEGER NOT NULL, | ||
"contadorRetiradaCor" INTEGER NOT NULL, | ||
"localizacao" TEXT NOT NULL, | ||
"modeloId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Impressora_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Relatorio" ( | ||
"id" SERIAL NOT NULL, | ||
"impressoraId" INTEGER NOT NULL, | ||
"contadorMes" INTEGER NOT NULL, | ||
"ultimoResultado" INTEGER NOT NULL, | ||
"ultimaAtualizacao" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "Relatorio_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "padroes_marca_modelo_key" ON "padroes"("marca", "modelo"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Impressora_numSerie_key" ON "Impressora"("numSerie"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Relatorio_impressoraId_key" ON "Relatorio"("impressoraId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Relatorio" ADD CONSTRAINT "Relatorio_impressoraId_fkey" FOREIGN KEY ("impressoraId") REFERENCES "Impressora"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Request, Response } from 'express'; | ||
import { listImpressorasRelatorio, findImpressoraWithReport } from '../repository/Impressora.repository' | ||
import { generateReport, generateMonthReport, createPdf } from '../usecases/report/generate.report' | ||
import { Impressora } from '../types/Impressora.type' | ||
import fs from 'fs'; | ||
import { updateReport } from '../usecases/report/update.report'; | ||
|
||
export default { | ||
|
||
async listImpressorasReports(request: Request, response: Response) { | ||
try { | ||
let result = await listImpressorasRelatorio(); | ||
if (!result) { | ||
return response.status(500).json({ | ||
message: 'Erro: Não foi possível listar impressoras.', | ||
}); | ||
} | ||
|
||
return response.status(200).json({ | ||
message: 'Sucesso: Impressoras listadas com sucesso!', | ||
data: result | ||
}); | ||
|
||
} catch (error) { | ||
return response.status(500).json({ | ||
error: true, | ||
message: 'Erro: Ocorreu um erro ao listar as impressoras.' | ||
}); | ||
} | ||
}, | ||
|
||
async retrieveReport(request: Request, response: Response) { | ||
try { | ||
const numberID = parseInt(request.params.id as string) | ||
const result: Impressora | false = await findImpressoraWithReport(numberID); | ||
if (!result) { | ||
return response.status(404).json({ error: "Relatório não encontrado" }); | ||
} | ||
|
||
const filePath: string | false = await createPdf(generateReport(result)); | ||
if (!filePath) { | ||
return response.status(500).json({ error: "Erro ao gerar o relatório" }); | ||
} | ||
|
||
return sendFile(response, filePath, result.numSerie); | ||
} | ||
catch (error) { | ||
return response.status(500).send(); | ||
} | ||
}, | ||
|
||
async retrieveMonthReport(request: Request, response: Response): Promise<Response> { | ||
try { | ||
const numberID = parseInt(request.params.id as string) | ||
const result: Impressora | false = await findImpressoraWithReport(numberID); | ||
if (!result) { | ||
return response.status(404).json({ error: "Relatório não encontrado" }); | ||
} | ||
|
||
const filePath: string | false = await createPdf(generateMonthReport(result)); | ||
if (!filePath) { | ||
return response.status(500).json({ error: "Erro ao gerar o relatório" }); | ||
} | ||
|
||
return sendFile(response, filePath, result.numSerie); | ||
} | ||
catch (error) { | ||
console.error('Erro ao processar a requisição:', error); | ||
return response.status(500).send(); | ||
} | ||
}, | ||
|
||
|
||
|
||
}; | ||
|
||
const sendFile = (response: Response, filePath: string, numSerie: string): Promise<Response> => { | ||
return new Promise<Response>((resolve, reject) => { | ||
response.download(filePath, `relatorio_${numSerie}.pdf`, (err) => { | ||
if (err) { | ||
console.error('Erro ao enviar o arquivo:', err); | ||
reject(response.status(500).json({ error: "Erro ao enviar o relatório" })); | ||
} else { | ||
fs.unlink(filePath, (unlinkErr: any) => { | ||
if (unlinkErr) { | ||
console.error('Erro ao deletar o arquivo:', unlinkErr); | ||
} else { | ||
console.log('Arquivo deletado com sucesso:', filePath); | ||
} | ||
}); | ||
resolve(response); | ||
} | ||
}); | ||
}); | ||
} | ||
|
Oops, something went wrong.