diff --git a/server.ts b/server.ts index 365aebe..3f5cbd6 100644 --- a/server.ts +++ b/server.ts @@ -1757,7 +1757,7 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => { if (existingPayment) { if (newStatus) { - await prisma.payment.update({ + existingPayment = await prisma.payment.update({ where: { id: payment.id }, data: { status: newStatus as any, @@ -1771,7 +1771,7 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => { orderBy: { createdAt: 'desc' } }); - await prisma.payment.create({ + existingPayment = await prisma.payment.create({ data: { id: payment.id, userId: user.id, @@ -1788,20 +1788,20 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => { } const description = payment.description || ''; - const isCoursePayment = description.startsWith('unlock_course_'); + const isCoursePayment = existingPayment?.courseId ? true : description.startsWith('unlock_course_'); + const courseIdToUnlock = existingPayment?.courseId || description.replace('unlock_course_', ''); if (isPaid) { - if (isCoursePayment) { - const courseId = description.replace('unlock_course_', ''); + if (isCoursePayment && courseIdToUnlock) { let newUnlocked = user.unlockedCourses || []; - if (!newUnlocked.includes(courseId)) { - newUnlocked.push(courseId); + if (!newUnlocked.includes(courseIdToUnlock)) { + newUnlocked.push(courseIdToUnlock); } await prisma.user.update({ where: { id: user.id }, data: { unlockedCourses: newUnlocked } }); - console.log(`Webhook: Curso ${courseId} LIBERADO para usuário ${user.email}`); + console.log(`Webhook: Curso ${courseIdToUnlock} LIBERADO para usuário ${user.email}`); } else { await prisma.user.update({ where: { id: user.id },