fix: resolver erro de liberação de curso em pagamento por cartão Asaas webhook omitindo description
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 1m2s Details

This commit is contained in:
Sidney Gomes 2026-07-24 22:55:14 -03:00
parent 00f12e721d
commit f82fe92d70
1 changed files with 8 additions and 8 deletions

View File

@ -1757,7 +1757,7 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => {
if (existingPayment) { if (existingPayment) {
if (newStatus) { if (newStatus) {
await prisma.payment.update({ existingPayment = await prisma.payment.update({
where: { id: payment.id }, where: { id: payment.id },
data: { data: {
status: newStatus as any, status: newStatus as any,
@ -1771,7 +1771,7 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => {
orderBy: { createdAt: 'desc' } orderBy: { createdAt: 'desc' }
}); });
await prisma.payment.create({ existingPayment = await prisma.payment.create({
data: { data: {
id: payment.id, id: payment.id,
userId: user.id, userId: user.id,
@ -1788,20 +1788,20 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => {
} }
const description = payment.description || ''; 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 (isPaid) {
if (isCoursePayment) { if (isCoursePayment && courseIdToUnlock) {
const courseId = description.replace('unlock_course_', '');
let newUnlocked = user.unlockedCourses || []; let newUnlocked = user.unlockedCourses || [];
if (!newUnlocked.includes(courseId)) { if (!newUnlocked.includes(courseIdToUnlock)) {
newUnlocked.push(courseId); newUnlocked.push(courseIdToUnlock);
} }
await prisma.user.update({ await prisma.user.update({
where: { id: user.id }, where: { id: user.id },
data: { unlockedCourses: newUnlocked } 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 { } else {
await prisma.user.update({ await prisma.user.update({
where: { id: user.id }, where: { id: user.id },