mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-11 14:35:13 +00:00
feat: Add precision handling and overflow protection to monthly fee billing
- Add rounding to monthly fee conversion to cents to prevent precision loss - Replace direct subtraction with saturating_sub to prevent i64 underflow - Add fee_cents variable for clearer monthly fee calculation in billing cycle
This commit is contained in:
@@ -246,7 +246,9 @@ impl super::ChargingEngine {
|
|||||||
if let Ok(Some(mut account)) = redis::AsyncCommands::get::<_, Option<crate::charging::types::SubscriberAccount>>(&mut conn, &key).await {
|
if let Ok(Some(mut account)) = redis::AsyncCommands::get::<_, Option<crate::charging::types::SubscriberAccount>>(&mut conn, &key).await {
|
||||||
// Get rating plan from Postgres and apply monthly fee.
|
// Get rating plan from Postgres and apply monthly fee.
|
||||||
if let Ok(Some(plan)) = self.get_rating_plan("basic").await {
|
if let Ok(Some(plan)) = self.get_rating_plan("basic").await {
|
||||||
account.balance -= (plan.monthly_fee * 100.0) as i64; // Convert to cents
|
// Use rounding to avoid precision loss when converting to cents
|
||||||
|
let fee_cents = (plan.monthly_fee * 100.0).round() as i64;
|
||||||
|
account.balance = account.balance.saturating_sub(fee_cents);
|
||||||
account.last_updated = Utc::now();
|
account.last_updated = Utc::now();
|
||||||
|
|
||||||
let _: () = redis::AsyncCommands::set(&mut conn, &key, &account).await.unwrap_or(());
|
let _: () = redis::AsyncCommands::set(&mut conn, &key, &account).await.unwrap_or(());
|
||||||
|
|||||||
Reference in New Issue
Block a user