Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
54 changes: 17 additions & 37 deletions src/domains/payments/payments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
Expand All @@ -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(
Expand All @@ -141,35 +140,16 @@ 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(
payment.userId,
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}`);
Expand Down
6 changes: 5 additions & 1 deletion src/domains/withdrawals/withdrawals.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down