Server overhaul: one place for stored-row reads and the shared inserts
api::rows now also loads settlements, audit entries and the epoch (sync::rows is gone; audit enums parse through rows::token) and owns the expense, split, settlement and allocation inserts that sync push, recurring generation and the importer each had their own copy of (rows::Origin carries the server-owned columns). accounts::insert_account and replace_shares are shared with the importer. Recurring generation moves the per-period expense into create_period_expense. No SQL, wire or log change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TQd3pEP9UeN2uifbHknQ7B
This commit is contained in:
@@ -51,7 +51,8 @@ async fn check_name_free(
|
||||
}
|
||||
}
|
||||
|
||||
async fn replace_shares(
|
||||
/// Replaces the account's `account_default_shares` rows.
|
||||
pub async fn replace_shares(
|
||||
conn: &mut SqliteConnection,
|
||||
account_id: Uuid,
|
||||
shares: &[(Uuid, i64)],
|
||||
@@ -73,6 +74,32 @@ async fn replace_shares(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Inserts a live, unarchived account row (without its default shares, see [`replace_shares`]); `name` is the
|
||||
/// trimmed name.
|
||||
pub async fn insert_account(
|
||||
conn: &mut SqliteConnection,
|
||||
id: Uuid,
|
||||
name: &str,
|
||||
created_by: Uuid,
|
||||
now_ts: &str,
|
||||
server_version: i64,
|
||||
) -> Result<(), AppError> {
|
||||
sqlx::query(
|
||||
"INSERT INTO accounts (id, name, name_key, created_by_user_id, created_at, updated_at, archived_at, \
|
||||
deleted_at, server_version) VALUES (?, ?, ?, ?, ?, ?, NULL, NULL, ?)",
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.bind(name)
|
||||
.bind(name_key(name))
|
||||
.bind(created_by.to_string())
|
||||
.bind(now_ts)
|
||||
.bind(now_ts)
|
||||
.bind(server_version)
|
||||
.execute(&mut *conn)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reload(conn: &mut SqliteConnection, id: Uuid) -> Result<Account, AppError> {
|
||||
rows::account(conn, id)
|
||||
.await?
|
||||
@@ -107,19 +134,7 @@ pub async fn create_account(
|
||||
let id = Uuid::now_v7();
|
||||
let now = state.clock.now_ts();
|
||||
let version = db::next_server_version(&mut tx).await?;
|
||||
sqlx::query(
|
||||
"INSERT INTO accounts (id, name, name_key, created_by_user_id, created_at, updated_at, archived_at, \
|
||||
deleted_at, server_version) VALUES (?, ?, ?, ?, ?, ?, NULL, NULL, ?)",
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.bind(&name)
|
||||
.bind(&key)
|
||||
.bind(device.user_id.to_string())
|
||||
.bind(&now)
|
||||
.bind(&now)
|
||||
.bind(version)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
insert_account(&mut tx, id, &name, device.user_id, &now, version).await?;
|
||||
replace_shares(&mut tx, id, &shares).await?;
|
||||
let account = reload(&mut tx, id).await?;
|
||||
tx.commit().await?;
|
||||
|
||||
Reference in New Issue
Block a user