Persons: other people's charts with package slots, list page, switcher
CI / test (push) Failing after 43s
CI / build (push) Skipped
CI / deploy (push) Skipped

- charts.kind own/person; a new person uses a 'person' unit (not refunded
  on delete); person packages and an unlimited entitlement; admins unlimited
- a person's birth data can be corrected within an hour (admins any time)
- /kepletek list with tiles, /kepletek/uj, correction page, mock person
  packages, chart page title switches between charts keeping the tab

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-17 11:03:55 +00:00
co-authored by Claude Opus 5.5
parent 50875c0006
commit fef1605848
28 changed files with 1008 additions and 54 deletions
+102 -14
View File
@@ -1,5 +1,6 @@
//! Natal charts of a user: their own (primary) chart and charts of others
//! they entered (partner, family). Calculated by the horoscope service.
//! Natal charts of a user: their own chart (kind `own`; the current one is
//! primary) and charts of other people they added (kind `person`, see
//! [`crate::persons`]). Calculated by the horoscope service.
use axum::Json;
use axum::extract::State;
@@ -16,6 +17,7 @@ use crate::AppState;
use crate::auth::AuthUser;
use crate::birth_changes;
use crate::error::{ApiError, ApiResult};
use crate::persons;
use crate::views::{chart_hash, chart_highlights, rfc3339};
#[derive(Debug, Clone, sqlx::FromRow)]
@@ -24,6 +26,8 @@ pub struct ChartRow {
pub owner_id: Uuid,
pub label: String,
pub is_primary: bool,
/// `own` or `person`.
pub kind: String,
pub birth_data: Value,
pub result: Value,
pub chart_hash: String,
@@ -31,7 +35,8 @@ pub struct ChartRow {
pub updated_at: OffsetDateTime,
}
const CHART_COLUMNS: &str = "id, owner_id, label, is_primary, birth_data, result, chart_hash, created_at, updated_at";
const CHART_COLUMNS: &str =
"id, owner_id, label, is_primary, kind, birth_data, result, chart_hash, created_at, updated_at";
impl ChartRow {
/// Full view for the owner.
@@ -40,6 +45,8 @@ impl ChartRow {
"id": self.id,
"label": self.label,
"is_primary": self.is_primary,
"kind": self.kind,
"editable_until": self.editable_until().map(rfc3339),
"birth_data": self.birth_data,
"highlights": chart_highlights(&self.result),
"chart": self.result,
@@ -49,11 +56,23 @@ impl ChartRow {
})
}
/// Until when a person's birth data can be corrected (admins: any time).
fn editable_until(&self) -> Option<OffsetDateTime> {
(self.kind == "person").then(|| self.created_at + persons::EDIT_WINDOW)
}
pub fn summary_view(&self) -> Value {
json!({
"id": self.id,
"label": self.label,
"is_primary": self.is_primary,
"kind": self.kind,
"editable_until": self.editable_until().map(rfc3339),
"birth": {
"date": self.birth_data.get("date"),
"time": self.birth_data.get("time"),
"place": self.result.pointer("/location/place/name"),
},
"highlights": chart_highlights(&self.result),
"created_at": rfc3339(self.created_at),
})
@@ -140,12 +159,15 @@ pub async fn create(
) -> ApiResult<(StatusCode, Json<Value>)> {
let label = clean_label(&req.label)?;
let birth = clean_birth(req.birth)?;
let existing: Option<ChartRow> =
sqlx::query_as(&format!("SELECT {CHART_COLUMNS} FROM charts WHERE owner_id = $1 AND birth_data = $2 LIMIT 1"))
.bind(auth.id())
.bind(&birth)
.fetch_optional(&state.db)
.await?;
let kind = if req.is_primary { "own" } else { "person" };
let existing: Option<ChartRow> = sqlx::query_as(&format!(
"SELECT {CHART_COLUMNS} FROM charts WHERE owner_id = $1 AND kind = $2 AND birth_data = $3 LIMIT 1"
))
.bind(auth.id())
.bind(kind)
.bind(&birth)
.fetch_optional(&state.db)
.await?;
if let Some(chart) = existing {
if req.is_primary && !chart.is_primary {
let mut tx = state.db.begin().await?;
@@ -175,6 +197,10 @@ pub async fn create(
format!("at most {} charts can be saved", state.config.max_charts_per_user),
));
}
let free = state.config.free_person_slots;
if kind == "person" {
persons::check(&mut *state.db.acquire().await?, &auth, free).await?;
}
let (birth, result, hash) = calculate(&state, birth).await?;
let mut tx = state.db.begin().await?;
@@ -185,13 +211,14 @@ pub async fn create(
.await?;
}
let row: ChartRow = sqlx::query_as(&format!(
"INSERT INTO charts (id, owner_id, label, is_primary, birth_data, result, chart_hash) \
VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING {CHART_COLUMNS}"
"INSERT INTO charts (id, owner_id, label, is_primary, kind, birth_data, result, chart_hash) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING {CHART_COLUMNS}"
))
.bind(Uuid::now_v7())
.bind(auth.id())
.bind(label)
.bind(req.is_primary)
.bind(kind)
.bind(birth)
.bind(result)
.bind(hash)
@@ -199,6 +226,8 @@ pub async fn create(
.await?;
if req.is_primary {
birth_changes::record(&mut tx, &auth, row.id).await?;
} else {
persons::spend(&mut tx, &auth, free, row.id).await?;
}
tx.commit().await?;
queue_summary(&state, &auth, &row).await;
@@ -218,10 +247,14 @@ pub async fn get_one(
pub struct UpdateChart {
label: Option<String>,
is_primary: Option<bool>,
/// Corrected birth data of a person, within an hour of adding them
/// (admins: any time). The user's own data is changed by saving a new
/// own chart instead (analyses belong to the old one).
birth: Option<Value>,
}
/// Renames the chart or changes which chart is the user's own. Birth data is
/// not editable: a corrected chart is a new chart (analyses belong to the old one).
/// Renames the chart, changes which chart is the user's own (a person made
/// primary becomes `own`) or corrects a person's birth data.
pub async fn update(
State(state): St,
auth: AuthUser,
@@ -234,7 +267,22 @@ pub async fn update(
None => chart.label.clone(),
};
let is_primary = req.is_primary.unwrap_or(chart.is_primary);
let recalculated = match req.birth {
Some(birth) => Some(corrected_birth(&state, &auth, &chart, birth).await?),
None => None,
};
let mut tx = state.db.begin().await?;
if let Some((birth, result, hash)) = &recalculated {
// The old summary (and an admin's analyses) no longer match the chart.
sqlx::query("DELETE FROM analyses WHERE chart_id = $1").bind(id).execute(&mut *tx).await?;
sqlx::query("UPDATE charts SET birth_data = $1, result = $2, chart_hash = $3 WHERE id = $4")
.bind(birth)
.bind(result)
.bind(hash)
.bind(id)
.execute(&mut *tx)
.await?;
}
if is_primary && !chart.is_primary {
sqlx::query("UPDATE charts SET is_primary = false, updated_at = now() WHERE owner_id = $1 AND is_primary")
.bind(auth.id())
@@ -242,7 +290,8 @@ pub async fn update(
.await?;
}
let row: ChartRow = sqlx::query_as(&format!(
"UPDATE charts SET label = $1, is_primary = $2, updated_at = now() WHERE id = $3 RETURNING {CHART_COLUMNS}"
"UPDATE charts SET label = $1, is_primary = $2, kind = CASE WHEN $2 THEN 'own' ELSE kind END, \
updated_at = now() WHERE id = $3 RETURNING {CHART_COLUMNS}"
))
.bind(label)
.bind(is_primary)
@@ -253,9 +302,48 @@ pub async fn update(
birth_changes::record(&mut tx, &auth, id).await?;
}
tx.commit().await?;
if recalculated.is_some() {
queue_summary(&state, &auth, &row).await;
}
Ok(Json(row.owner_view()))
}
/// Checks that a person's birth data may still be corrected and calculates the new chart.
async fn corrected_birth(
state: &AppState,
auth: &AuthUser,
chart: &ChartRow,
birth: Value,
) -> Result<(Value, Value, String), ApiError> {
if chart.kind != "person" {
return Err(ApiError::BadRequest("the own birth data is changed by saving a new own chart".into()));
}
if !persons::editable(auth, chart.created_at, OffsetDateTime::now_utc()) {
return Err(ApiError::conflict("person_locked", persons::LOCKED_MESSAGE));
}
let admin = auth.user.has_role(db::Role::Admin);
let (analysed, in_synastry): (bool, bool) = sqlx::query_as(
"SELECT EXISTS (SELECT 1 FROM analyses WHERE chart_id = $1 AND kind <> 'summary' AND status <> 'failed'), \
EXISTS (SELECT 1 FROM synastries WHERE chart1_id = $1 OR chart2_id = $1)",
)
.bind(chart.id)
.fetch_one(&state.db)
.await?;
if analysed && !admin {
return Err(ApiError::conflict(
"person_analysed",
"Ehhez a személyhez már készült részletes elemzés, ezért az adatai nem módosíthatók.",
));
}
if in_synastry {
return Err(ApiError::conflict(
"person_in_synastry",
"A személy szerepel egy összevetésben, ezért az adatai nem módosíthatók.",
));
}
calculate(state, birth).await
}
/// Deletes the chart with its synastries, analyses and shares.
pub async fn remove(
State(state): St,