From 717a203d488aa9d5f199dc26143fdbc14d6f6c08 Mon Sep 17 00:00:00 2001 From: Alireza Hassani Date: Wed, 9 Sep 2026 11:12:27 +0330 Subject: [PATCH] Fix SMS billing build and make credit conversion idempotent. Import the purchase-price constant for Nest build, and only convert IRT-scale balances so migrate re-runs do not divide credits again. Co-authored-by: Cursor --- database/migrations/086_sms_balance_credit_units.sql | 7 ++++--- src/auth/sms-billing.service.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/database/migrations/086_sms_balance_credit_units.sql b/database/migrations/086_sms_balance_credit_units.sql index 0a3a9b9..a944af5 100644 --- a/database/migrations/086_sms_balance_credit_units.sql +++ b/database/migrations/086_sms_balance_credit_units.sql @@ -1,11 +1,12 @@ --- Convert prepaid SMS balance from IRT to credit units. +-- Convert prepaid SMS balance from IRT to credit units (idempotent). -- Previous billing: 400 IRT per 64-char segment. -- New model: balance is SMS credits (1 credit = 1 segment); top-up at 500 IRT/credit. --- Preserve existing prepaid capacity: IRT / 400 → credits. +-- Only convert rows that still look like IRT balances (>= 100_000). Already-converted +-- credit balances are below that and are left unchanged on re-run. UPDATE businesses SET sms_balance = sms_balance / 400 -WHERE sms_balance > 0; +WHERE sms_balance >= 100000; COMMENT ON COLUMN businesses.sms_balance IS 'Prepaid SMS credit units. 1 unit = one 64-character segment per recipient. Top-up adds floor(IRT / 500) credits.'; diff --git a/src/auth/sms-billing.service.ts b/src/auth/sms-billing.service.ts index 3d1725f..914c99c 100644 --- a/src/auth/sms-billing.service.ts +++ b/src/auth/sms-billing.service.ts @@ -1,7 +1,7 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { TenantService } from '../tenant/tenant.service'; -import { calculateSmsCredits, irtToSmsCredits } from './sms-billing'; +import { calculateSmsCredits, irtToSmsCredits, SMS_PURCHASE_PRICE_PER_CREDIT_IRT } from './sms-billing'; @Injectable() export class SmsBillingService {