diff --git a/package.json b/package.json index df6a564..a538946 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "brainbox-backend", - "version": "1.7.1", + "version": "1.7.2", "description": "Backend for BrainBox", "author": "lzaycoe (Lazy Code)", "private": true, diff --git a/src/domains/payments/payments.service.ts b/src/domains/payments/payments.service.ts index 4eb62a0..0c2b31c 100644 --- a/src/domains/payments/payments.service.ts +++ b/src/domains/payments/payments.service.ts @@ -94,6 +94,13 @@ export class PaymentsService { throw new Error(`Payment with ID ${orderCode} not found`); } + // Update payment status to avoid inconsistencies in case of errors in further processing + await this.prismaService.payment.update({ + where: { id: orderCode }, + data: { status: 'paid' }, + }); + this.logger.debug(`Payment ${orderCode} marked as paid`); + const user = await this.prismaService.user.findUnique({ where: { id: payment.userId }, }); @@ -104,22 +111,14 @@ export class PaymentsService { } const isBecomeTeacher = description === 'BrainBox Become a Teacher'; - const updateRolePromise = isBecomeTeacher - ? this.clerkClient.users - .updateUser(user.clerkId, { - publicMetadata: { role: 'teacher' }, - }) - .then(() => { - this.logger.debug( - `User ${user.id} has been updated to teacher role`, - ); - }) - .catch((error) => { - this.logger.error( - `Failed to update user ${user.id} role: ${error.message}`, - ); - }) - : Promise.resolve(); + + if (isBecomeTeacher) { + await this.clerkClient.users.updateUser(user.clerkId, { + publicMetadata: { role: 'teacher' }, + }); + this.logger.debug(`User ${user.id} has been updated to teacher role`); + return 'Webhook processed successfully for teacher registration'; + } if (!payment.courseId) { this.logger.error( @@ -141,14 +140,6 @@ export class PaymentsService { .catch((error) => { this.logger.error(`Failed to update revenue: ${error.message}`); }); - - this.logger.debug( - `User ${payment.userId} is enrolled in course ${payment.courseId}`, - ); - } else { - this.logger.warn( - `Course ${payment.courseId} not found or amount is missing, skipping revenue update.`, - ); } const createProgressPromise = this.coursesService.createProgress( @@ -156,20 +147,9 @@ export class PaymentsService { payment.courseId, ); - const status = isSuccess ? 'paid' : 'canceled'; - const updatePaymentPromise = this.prismaService.payment.update({ - where: { id: orderCode }, - data: { status }, - }); - - await Promise.all([ - updateRolePromise, - updatePaymentPromise, - updateRevenuePromise, - createProgressPromise, - ]); + await Promise.all([updateRevenuePromise, createProgressPromise]); - this.logger.debug(`Payment ${orderCode} marked as ${status}`); + this.logger.debug(`Webhook processing completed successfully.`); return 'Webhook processed successfully'; } catch (error) { this.logger.error(`Error processing webhook: ${error.message}`); diff --git a/src/domains/withdrawals/withdrawals.service.ts b/src/domains/withdrawals/withdrawals.service.ts index 1831f1d..617f8c5 100644 --- a/src/domains/withdrawals/withdrawals.service.ts +++ b/src/domains/withdrawals/withdrawals.service.ts @@ -115,7 +115,11 @@ export class WithdrawalsService { withdrawal.teacherId.toString(), ); - const email = teacher.email_addresses[0]?.email_address; + this.logger.debug('Teacher found', teacher); + + const email = teacher.emailAddresses?.[0]?.emailAddress; + + this.logger.debug('Sending email to', email); const subject = dto.status === 'approved' ? '[BrainBox] Withdrawal Request Approved'