forked from VitorCarvalho67/Boot
-
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.
Send boletim and set empresa as partner routes
- Loading branch information
1 parent
3f4043e
commit 0e38353
Showing
9 changed files
with
167 additions
and
123 deletions.
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
server/prisma/migrations/20240803010917_create_boletins_table/migration.sql
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,13 @@ | ||
-- CreateTable | ||
CREATE TABLE `boletins` ( | ||
`id` VARCHAR(191) NOT NULL, | ||
`alunoId` VARCHAR(191) NOT NULL, | ||
`link` VARCHAR(191) NOT NULL, | ||
`caminho` VARCHAR(191) NOT NULL, | ||
`status` ENUM('EM_ANALISE', 'RECUSADO', 'APROVADO') NOT NULL DEFAULT 'EM_ANALISE', | ||
|
||
PRIMARY KEY (`id`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `boletins` ADD CONSTRAINT `boletins_alunoId_fkey` FOREIGN KEY (`alunoId`) REFERENCES `alunos`(`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
163 changes: 56 additions & 107 deletions
163
server/src/modules/services/aluno/SendBoletimUseCase.ts
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 |
---|---|---|
@@ -1,119 +1,68 @@ | ||
// import puppeteerNormal from 'puppeteer'; | ||
// import puppeteer from 'puppeteer-extra'; | ||
// import RecaptchaPlugin from 'puppeteer-extra-plugin-recaptcha'; | ||
// import fs from 'fs'; | ||
// import path from 'path'; | ||
// import pdf from 'pdf-parse'; | ||
import axios from 'axios'; | ||
// import { getExecutablePath } from 'puppeteerNormal'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import pdf from 'pdf-parse'; | ||
import { prisma } from "../../../prisma/client"; | ||
import { SendNotasDTO } from "../../interfaces/alunoDTOs"; | ||
import { AppError } from "../../../errors/error"; | ||
import { clearUploads } from '../shared/helpers/helpers'; | ||
|
||
// Configure o plugin Recaptcha | ||
// puppeteer.use( | ||
// RecaptchaPlugin({ | ||
// provider: { | ||
// id: '2captcha', | ||
// token: process.env.CAPTCHA2_ID || '', // Certifique-se de que o token está definido | ||
// }, | ||
// }) | ||
// ); | ||
import { uploadToMinio } from "../../../minioService"; | ||
|
||
export class SendBoletimUseCase { | ||
async execute({ alunoId, boletim }: SendNotasDTO) { | ||
if (!alunoId) { | ||
throw new AppError('ID do aluno não fornecido.'); | ||
async execute({ alunoId, boletim }: SendNotasDTO) { | ||
if (!alunoId) { | ||
throw new AppError('ID do aluno não fornecido.'); | ||
} | ||
|
||
if (!boletim || !boletim.path) { | ||
throw new AppError('Arquivo do boletim não fornecido.'); | ||
} | ||
|
||
const aluno = await prisma.aluno.findUnique({ | ||
where: { | ||
id: alunoId, | ||
}, | ||
}); | ||
|
||
if (!aluno) { | ||
throw new AppError('Aluno não encontrado.'); | ||
} | ||
|
||
const boletimPath = path.resolve(boletim.path); | ||
const boletimBuffer = fs.readFileSync(boletimPath); | ||
|
||
const link = await this.extractAuthUrlFromPdf(boletimBuffer); | ||
|
||
const bucketName = 'boot'; | ||
const objectName = `aluno/${aluno.rm}/boletins/${path.basename(boletim.path)}`; | ||
|
||
try { | ||
await uploadToMinio(bucketName, objectName, boletimPath); | ||
|
||
await prisma.boletim.create({ | ||
data: { | ||
alunoId: aluno.id, | ||
link: link, | ||
caminho: objectName, | ||
} | ||
}); | ||
|
||
await clearUploads(); | ||
|
||
return { message: 'Boletim enviado e salvo com sucesso!' }; | ||
} catch (error) { | ||
throw new AppError(`Erro ao salvar o boletim: ${error}`); | ||
} | ||
} | ||
|
||
if (!boletim || !boletim.path) { | ||
throw new AppError('Arquivo do boletim não fornecido.'); | ||
} | ||
async extractAuthUrlFromPdf(buffer: Buffer): Promise<string> { | ||
const data = await pdf(buffer); | ||
const text = data.text; | ||
|
||
const aluno = await prisma.aluno.findUnique({ | ||
where: { | ||
id: alunoId, | ||
}, | ||
}); | ||
const urlMatch = text.match(/https:\/\/nsa\.cps\.sp\.gov\.br\?a=[a-z0-9-]+/i); | ||
if (!urlMatch) { | ||
throw new AppError('URL de autenticação não encontrado no boletim.'); | ||
} | ||
|
||
if (!aluno) { | ||
throw new AppError('Aluno não encontrado.'); | ||
return urlMatch[0]; | ||
} | ||
|
||
// const boletimPath = path.resolve(boletim.path); | ||
|
||
// const boletimBuffer = fs.readFileSync(boletimPath); | ||
|
||
// const authUrl = await this.extractAuthUrlFromPdf(boletimBuffer); | ||
|
||
// const browser = await puppeteer.launch({ | ||
// headless: false, | ||
// }); | ||
|
||
|
||
// const page = await browser.newPage(); | ||
|
||
// await page.goto(authUrl); | ||
|
||
// setTimeout(async () => { | ||
// const { solved, error } = await page.solveRecaptchas(); | ||
// console.log("Here2"); | ||
|
||
// if (!solved) { | ||
// throw new AppError('Falha ao resolver CAPTCHA: ' + error); | ||
// } else { | ||
// console.log('Captcha resolvido com sucesso, url: ' + authUrl); | ||
// } | ||
|
||
// await page.waitForSelector('#btnValidar'); | ||
// await page.click('#btnValidar'); | ||
// }, 10000); | ||
|
||
// const downloadUrl = await page.evaluate(() => { | ||
// const linkElement = document.querySelector('a#downloadLink') as HTMLAnchorElement | null; | ||
// return linkElement ? linkElement.href : ''; | ||
// }); | ||
|
||
|
||
// const response = await axios.get(downloadUrl, { responseType: 'arraybuffer' }); | ||
// const downloadedFilePath = path.join(__dirname, 'downloads', 'downloaded_boletim.pdf'); | ||
// fs.writeFileSync(downloadedFilePath, response.data); | ||
|
||
// // Comparar os dois arquivos PDF | ||
// const downloadedBoletimBuffer = fs.readFileSync(downloadedFilePath); | ||
|
||
// if (!boletimBuffer.equals(downloadedBoletimBuffer)) { | ||
// throw new AppError("Os arquivos enviados não coincidem."); | ||
// } | ||
|
||
// Extrair notas do boletim | ||
// const notas = this.extractNotasFromBoletim(boletimBuffer); | ||
|
||
// Fechar o navegador | ||
// await browser.close(); | ||
|
||
await clearUploads(); | ||
|
||
} | ||
|
||
// async extractAuthUrlFromPdf(buffer: Buffer): Promise<string> { | ||
// const data = await pdf(buffer); | ||
// const text = data.text; | ||
|
||
// const urlMatch = text.match(/https:\/\/nsa\.cps\.sp\.gov\.br\?a=[a-z0-9-]+/i); | ||
// if (!urlMatch) { | ||
// throw new AppError('URL de autenticação não encontrado no boletim.'); | ||
// } | ||
|
||
// return urlMatch[0]; | ||
// } | ||
|
||
extractNotasFromBoletim(buffer: Buffer) { | ||
return [ | ||
// { materia: 'Matemática', nota: 'MB' }, | ||
// { materia: 'Português', nota: 'MB' }, | ||
]; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
server/src/modules/services/funcionario/SetAsParceiraUseCase.ts
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,42 @@ | ||
import { prisma } from "../../../prisma/client"; | ||
import { SetEmpresaParceiraDTO } from "../../interfaces/funcionarioDTOs"; | ||
import { AppError } from "../../../errors/error"; | ||
|
||
export class SetEmpresaParceiraUseCase { | ||
async execute({ funcionarioId, emailEmpresa }: SetEmpresaParceiraDTO) { | ||
if (!funcionarioId || !emailEmpresa) { | ||
throw new AppError("Parâmetros insuficientes ou inválidos."); | ||
} | ||
|
||
const funcionario = await prisma.funcionario.findUnique({ | ||
where: { | ||
id: funcionarioId | ||
} | ||
}); | ||
|
||
if (!funcionario) { | ||
throw new AppError("Funcionário não encontrado."); | ||
} | ||
|
||
const empresa = await prisma.empresa.findUnique({ | ||
where: { | ||
email: emailEmpresa | ||
} | ||
}); | ||
|
||
if (!empresa) { | ||
throw new AppError("Empresa não encontrada."); | ||
} | ||
|
||
const empresaParceira = await prisma.empresa.update({ | ||
where: { | ||
id: emailEmpresa | ||
}, | ||
data: { | ||
patrocinador: true | ||
} | ||
}); | ||
|
||
return empresaParceira; | ||
} | ||
} |
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