2026-11-16 17:39:15 +00:00
|
|
|
//! Analysis units: a detailed natal analysis uses one `natal` unit, a
|
|
|
|
|
//! synastry analysis one `synastry` unit. Units come from purchases or an
|
|
|
|
|
//! admin; a failed analysis gives its unit back (the worker does that).
|
2026-11-15 10:25:35 +00:00
|
|
|
|
2026-11-16 17:39:15 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use serde_json::{Value, json};
|
|
|
|
|
use sqlx::PgConnection;
|
2026-11-15 10:25:35 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::error::ApiError;
|
|
|
|
|
|
2026-11-16 17:39:15 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
|
pub enum Unit {
|
|
|
|
|
Natal,
|
|
|
|
|
Synastry,
|
2026-11-15 10:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 17:39:15 +00:00
|
|
|
impl Unit {
|
|
|
|
|
pub fn as_str(self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
Unit::Natal => "natal",
|
|
|
|
|
Unit::Synastry => "synastry",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Units left per kind.
|
|
|
|
|
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
|
|
|
|
pub struct Balance {
|
|
|
|
|
pub natal: i64,
|
|
|
|
|
pub synastry: i64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Balance {
|
|
|
|
|
pub fn of(self, unit: Unit) -> i64 {
|
|
|
|
|
match unit {
|
|
|
|
|
Unit::Natal => self.natal,
|
|
|
|
|
Unit::Synastry => self.synastry,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn view(self) -> Value {
|
|
|
|
|
json!({ "natal": self.natal, "synastry": self.synastry })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn balance(conn: &mut PgConnection, user_id: Uuid) -> Result<Balance, ApiError> {
|
|
|
|
|
let (natal, synastry): (i64, i64) = sqlx::query_as(
|
|
|
|
|
"SELECT COALESCE(SUM(delta) FILTER (WHERE unit = 'natal'), 0)::bigint, \
|
|
|
|
|
COALESCE(SUM(delta) FILTER (WHERE unit = 'synastry'), 0)::bigint \
|
|
|
|
|
FROM credit_ledger WHERE user_id = $1",
|
|
|
|
|
)
|
|
|
|
|
.bind(user_id)
|
|
|
|
|
.fetch_one(conn)
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(Balance { natal, synastry })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Spends one unit inside the caller's transaction. The user row is locked
|
|
|
|
|
/// so concurrent requests cannot overspend.
|
|
|
|
|
pub async fn spend(tx: &mut PgConnection, user_id: Uuid, unit: Unit, analysis_id: Uuid) -> Result<(), ApiError> {
|
|
|
|
|
sqlx::query("SELECT id FROM users WHERE id = $1 FOR UPDATE").bind(user_id).execute(&mut *tx).await?;
|
|
|
|
|
let left = balance(tx, user_id).await?.of(unit);
|
|
|
|
|
if left < 1 {
|
|
|
|
|
return Err(ApiError::InsufficientCredits { unit: unit.as_str(), balance: left });
|
2026-11-15 10:25:35 +00:00
|
|
|
}
|
|
|
|
|
sqlx::query(
|
2026-11-16 17:39:15 +00:00
|
|
|
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, analysis_id) VALUES ($1, $2, $3, -1, 'analysis', $4)",
|
2026-11-15 10:25:35 +00:00
|
|
|
)
|
|
|
|
|
.bind(Uuid::now_v7())
|
|
|
|
|
.bind(user_id)
|
2026-11-16 17:39:15 +00:00
|
|
|
.bind(unit.as_str())
|
|
|
|
|
.bind(analysis_id)
|
|
|
|
|
.execute(tx)
|
2026-11-15 10:25:35 +00:00
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|