feat: Add SMS balance check before sending notifications to prevent low balance issues

This commit is contained in:
Nawaz Dhandala
2026-01-17 12:35:48 +00:00
parent 5452342f2f
commit 65b4a8217b

View File

@@ -1,3 +1,4 @@
import { IsBillingEnabled } from "../EnvironmentConfig";
import CreateBy from "../Types/Database/CreateBy";
import { OnCreate } from "../Types/Database/Hooks";
import logger from "../Utils/Logger";
@@ -33,6 +34,7 @@ export class Service extends DatabaseService<Model> {
},
select: {
enableSmsNotifications: true,
smsOrCallCurrentBalanceInUSDCents: true,
},
});
@@ -46,6 +48,15 @@ export class Service extends DatabaseService<Model> {
);
}
if (
(project.smsOrCallCurrentBalanceInUSDCents as number) <= 100 &&
IsBillingEnabled
) {
throw new BadDataException(
"Your SMS balance is low. Please recharge your SMS balance in Project Settings > Notification Settings.",
);
}
// Check if user already has a verified phone number for this project
const existingVerifiedNumber: Model | null = await this.findOneBy({
query: {
@@ -116,6 +127,7 @@ export class Service extends DatabaseService<Model> {
},
select: {
enableSmsNotifications: true,
smsOrCallCurrentBalanceInUSDCents: true,
},
});
@@ -129,6 +141,15 @@ export class Service extends DatabaseService<Model> {
);
}
if (
(project.smsOrCallCurrentBalanceInUSDCents as number) <= 100 &&
IsBillingEnabled
) {
throw new BadDataException(
"Your SMS balance is low. Please recharge your SMS balance in Project Settings > Notification Settings.",
);
}
// Generate new verification code
item.verificationCode = Text.generateRandomNumber(6);