Questions about an analysis or forecast, answered by the model that wrote it
- 3 questions in every package, +5 questions add-on (1 000 Ft); question units, refund on failure - The answer sees the chart data, the analysis and earlier questions; at most 250 words - Editable question prompt (Promptok → Kérdés-válasz); admins regenerate answers - Package texts and FAQ updated Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
//! Analysis units: a detailed natal analysis uses one `natal` unit, a
|
||||
//! synastry analysis one `synastry` unit, a yearly forecast one `forecast`
|
||||
//! unit. Units come from purchases or an admin; a failed analysis gives its
|
||||
//! unit back (the worker does that).
|
||||
//! unit, a question about an analysis one `question` unit. Units come from
|
||||
//! purchases or an admin; a failed analysis or answer gives its unit back
|
||||
//! (the worker does that).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
@@ -19,6 +20,8 @@ pub enum Unit {
|
||||
Forecast,
|
||||
/// A slot for adding a person (see [`crate::persons`]).
|
||||
Person,
|
||||
/// A question about a finished analysis (see `routes::questions`).
|
||||
Question,
|
||||
}
|
||||
|
||||
impl Unit {
|
||||
@@ -28,6 +31,7 @@ impl Unit {
|
||||
Unit::Synastry => "synastry",
|
||||
Unit::Forecast => "forecast",
|
||||
Unit::Person => "person",
|
||||
Unit::Question => "question",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +44,7 @@ pub struct Balance {
|
||||
pub forecast: i64,
|
||||
/// Bought person slots (not the slots left; see [`crate::persons::slots`]).
|
||||
pub person: i64,
|
||||
pub question: i64,
|
||||
}
|
||||
|
||||
impl Balance {
|
||||
@@ -49,26 +54,28 @@ impl Balance {
|
||||
Unit::Synastry => self.synastry,
|
||||
Unit::Forecast => self.forecast,
|
||||
Unit::Person => self.person,
|
||||
Unit::Question => self.question,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn view(self) -> Value {
|
||||
json!({ "natal": self.natal, "synastry": self.synastry, "forecast": self.forecast })
|
||||
json!({ "natal": self.natal, "synastry": self.synastry, "forecast": self.forecast, "question": self.question })
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn balance(conn: &mut PgConnection, user_id: Uuid) -> Result<Balance, ApiError> {
|
||||
let (natal, synastry, forecast, person): (i64, i64, i64, i64) = sqlx::query_as(
|
||||
let (natal, synastry, forecast, person, question): (i64, i64, i64, i64, i64) = sqlx::query_as(
|
||||
"SELECT COALESCE(SUM(delta) FILTER (WHERE unit = 'natal'), 0)::bigint, \
|
||||
COALESCE(SUM(delta) FILTER (WHERE unit = 'synastry'), 0)::bigint, \
|
||||
COALESCE(SUM(delta) FILTER (WHERE unit = 'forecast'), 0)::bigint, \
|
||||
COALESCE(SUM(delta) FILTER (WHERE unit = 'person' AND reason <> 'person'), 0)::bigint \
|
||||
COALESCE(SUM(delta) FILTER (WHERE unit = 'person' AND reason <> 'person'), 0)::bigint, \
|
||||
COALESCE(SUM(delta) FILTER (WHERE unit = 'question'), 0)::bigint \
|
||||
FROM credit_ledger WHERE user_id = $1",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(conn)
|
||||
.await?;
|
||||
Ok(Balance { natal, synastry, forecast, person })
|
||||
Ok(Balance { natal, synastry, forecast, person, question })
|
||||
}
|
||||
|
||||
/// Spends one unit inside the caller's transaction. The user row is locked
|
||||
@@ -90,3 +97,22 @@ pub async fn spend(tx: &mut PgConnection, user_id: Uuid, unit: Unit, analysis_id
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Spends one `question` unit for a question, inside the caller's transaction.
|
||||
pub async fn spend_question(tx: &mut PgConnection, user_id: Uuid, question_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?.question;
|
||||
if left < 1 {
|
||||
return Err(ApiError::InsufficientCredits { unit: Unit::Question.as_str(), balance: left });
|
||||
}
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, question_id) \
|
||||
VALUES ($1, $2, 'question', -1, 'question', $3)",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(user_id)
|
||||
.bind(question_id)
|
||||
.execute(tx)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user