Skip to content

Commit

Permalink
feat: add router transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Brasileiro authored and Fabio Brasileiro committed Nov 20, 2024
1 parent 6f9b8df commit 1d95c03
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/controllers/authController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'dotenv/config'

import { PrismaClient, UserRole } from '@prisma/client' // Importação do enum UserRole
Expand Down Expand Up @@ -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,
// },
]
Expand Down
34 changes: 34 additions & 0 deletions src/controllers/paymentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
};
10 changes: 9 additions & 1 deletion src/routes/paymentRoutes.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
13 changes: 13 additions & 0 deletions src/services/paymentService.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
// },
})
}

0 comments on commit 1d95c03

Please sign in to comment.