diff --git a/src/controllers/authController.ts b/src/controllers/authController.ts index c0e2ce2..5f04ce1 100644 --- a/src/controllers/authController.ts +++ b/src/controllers/authController.ts @@ -1,4 +1,3 @@ - import 'dotenv/config' import { PrismaClient, UserRole } from '@prisma/client' // Importação do enum UserRole @@ -163,12 +162,12 @@ const getMenuForRole = (role: any) => { }, { label: 'Pagamentos', - route: '/products/edit', + route: '/products/transactions', adminOnly: true, }, // { - // label: 'Delete Product', - // route: '/products/delete', + // label: 'Detalhes de Product', + // route: '/products/transactions', // adminOnly: true, // }, ] diff --git a/src/controllers/paymentController.ts b/src/controllers/paymentController.ts index 98739da..4faaae2 100644 --- a/src/controllers/paymentController.ts +++ b/src/controllers/paymentController.ts @@ -82,3 +82,37 @@ export const storePayment = async (paymentResponse: any, productId: number) => { throw new Error('Erro ao salvar pagamento no banco') } } +// Extensão do BigInt para TypeScript +declare global { + interface BigInt { + toJSON: () => string; + } +} +export const getPayments = async (req: Request, res: Response) => { + // Corrigindo a serialização do BigInt para JSON + BigInt.prototype.toJSON = function () { + // Convertendo BigInt para string para evitar perda de dados + return this.toString(); + }; + + try { + // Obtendo os pagamentos + const products = await paymentService.getPayments(); + console.log("🚀 ~ getPayments ~ products:", products); + + // Convertendo o campo 'payment_id' (assumindo que seja BigInt) para string dentro de cada item + const productsString = products.map((product: any) => ({ + ...product, + payment_id: product.payment_id.toString(), // Convertendo explicitamente + })); + + console.log("🚀 ~ getPayments ~ productsString:", productsString); + + // Enviando a resposta + res.status(200).json(productsString); + } catch (error) { + // Tratando erros de forma genérica + console.error("Erro ao obter os pagamentos:", error); + res.status(500).json({ message: (error as Error).message }); + } +}; diff --git a/src/routes/paymentRoutes.ts b/src/routes/paymentRoutes.ts index 291a784..6f67902 100644 --- a/src/routes/paymentRoutes.ts +++ b/src/routes/paymentRoutes.ts @@ -1,14 +1,22 @@ +import { UserRole } from '@prisma/client' import express from 'express' -import { createPayment } from '../controllers/paymentController' +import { createPayment, getPayments } from '../controllers/paymentController' import { checkPaymentStatus, webHook } from '@/controllers/hook.payment.controller' +import authenticate from '../middleware/authenticate' // Middleware de autenticação +import authorize from '../middleware/authorize' // Middleware de autorização const router = express.Router() +router.use(authenticate) + + // Route for creating a payment router.post('/', createPayment) router.post('/webhook', webHook) +router.get('/getPayments', authorize([UserRole.ADMIN]) ,getPayments) + // Endpoint para verificar o status do pagamento router.get('/check-payment-status/:paymentId', checkPaymentStatus); diff --git a/src/services/paymentService.ts b/src/services/paymentService.ts index 0437ff7..ad6f988 100644 --- a/src/services/paymentService.ts +++ b/src/services/paymentService.ts @@ -1,6 +1,9 @@ import 'dotenv/config' import type { PaymentData } from '../@types/PaymentData' import { isProduction, payment } from '../config/mercadoPagoConfig' +import { PrismaClient } from '@prisma/client' + +const prisma = new PrismaClient() // Obtain the correct webHookURL based on environment export const webHookURL: string = isProduction @@ -35,3 +38,13 @@ export const createPayment = async (paymentData: PaymentData) => { throw new Error(`Error creating payment: ${(error as Error).message}`) } } + + +export const getPayments = async () => { + return prisma.payment.findMany({ + // include: { + // // company: true, + // // category: true, + // }, + }) +} \ No newline at end of file