feat #14740: admin backend client and transcript upload text

recorder-core side of linking the app to the euvino admin backend:
- backend: BackendClient (create a key with the admin's session cookie,
  check a key, upload a transcript; redirects never followed), German
  error messages, the login-window decision (LoginStep) and the upload
  flow with login, one retry after a refused key and no loop
- call_view: the upload text (header + merged, renamed lines) and the
  upload fields on the call
- store: schema v3 with crm_transcript_id and uploaded_at
- settings: backend_url (validated https), the app-managed login fields,
  kept on edit and cleared when the backend URL changes
- secrets: Service::Backend; http: per-request follow_redirects;
  FakeTransport can script transport errors

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-06 14:52:50 +01:00
co-authored by Claude Opus 5.5
parent 54a81e5ba0
commit 58e96a2954
11 changed files with 1447 additions and 30 deletions
+123 -6
View File
@@ -114,9 +114,15 @@ CREATE TABLE speaker_merges (
);
";
/// Version 3: the last upload of the transcript to the CRM.
const SCHEMA_V3_SQL: &str = "
ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT;
ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
";
/// Schema steps by `PRAGMA user_version`: entry `i` takes a database from
/// version `i` to `i + 1`. A new database runs all of them.
const MIGRATIONS: &[&str] = &[SCHEMA_V1_SQL, SCHEMA_V2_SQL];
const MIGRATIONS: &[&str] = &[SCHEMA_V1_SQL, SCHEMA_V2_SQL, SCHEMA_V3_SQL];
/// SQLite-backed store for calls and everything derived from them.
pub struct Store {
@@ -355,6 +361,22 @@ impl Store {
Ok(())
}
/// Records that the transcript was uploaded to the CRM at `at`, which
/// gave it `crm_transcript_id`; replaces an earlier upload's record.
pub fn set_uploaded(
&self,
id: CallId,
crm_transcript_id: &str,
at: DateTime<Local>,
) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE calls SET crm_transcript_id = ?1, uploaded_at = ?2 WHERE id = ?3",
params![crm_transcript_id, dt_to_str(at), id],
)?;
Ok(())
}
pub fn mark_transcribed(&self, id: CallId, at: DateTime<Local>) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
@@ -514,9 +536,9 @@ fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result<Ve
Ok(ids)
}
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title FROM calls";
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at FROM calls";
const SELECT_CALL_COLUMNS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title FROM calls WHERE id = ?1";
const SELECT_CALL_COLUMNS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at FROM calls WHERE id = ?1";
fn row_to_call(row: &Row) -> Result<Call> {
let id: CallId = row.get(0)?;
@@ -531,6 +553,8 @@ fn row_to_call(row: &Row) -> Result<Call> {
let transcribed_at: Option<String> = row.get(9)?;
let audio_deleted: bool = row.get(10)?;
let title: Option<String> = row.get(11)?;
let crm_transcript_id: Option<String> = row.get(12)?;
let uploaded_at: Option<String> = row.get(13)?;
Ok(Call {
id,
@@ -545,6 +569,8 @@ fn row_to_call(row: &Row) -> Result<Call> {
transcribed_at: opt_dt_from_str(transcribed_at)?,
audio_deleted,
title,
crm_transcript_id,
uploaded_at: opt_dt_from_str(uploaded_at)?,
})
}
@@ -653,6 +679,19 @@ CREATE TABLE IF NOT EXISTS remote_jobs (
remote_id TEXT NOT NULL,
PRIMARY KEY (call_id, track, remote_id)
);
";
/// The schema of user_version 2, exactly as release 0.1.1 created it:
/// version 1 plus these statements.
const RELEASED_V2_STEP_SQL: &str = "
ALTER TABLE calls ADD COLUMN title TEXT;
CREATE TABLE speaker_merges (
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
from_key TEXT NOT NULL,
into_key TEXT NOT NULL,
PRIMARY KEY (call_id, from_key)
);
";
fn user_version(path: &Path) -> i64 {
@@ -676,16 +715,17 @@ CREATE TABLE IF NOT EXISTS remote_jobs (
}
#[test]
fn a_new_database_starts_at_version_2() {
fn a_new_database_starts_at_version_3() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("calls.sqlite");
let store = Store::open(&path).unwrap();
let id = new_call(&store);
store.set_title(id, Some("Angebot")).unwrap();
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
store.set_uploaded(id, "4711", Local::now()).unwrap();
drop(store);
assert_eq!(user_version(&path), 2);
assert_eq!(user_version(&path), 3);
}
#[test]
@@ -742,7 +782,7 @@ CREATE TABLE IF NOT EXISTS remote_jobs (
store.set_title(1, Some("Lieferung März")).unwrap();
store.set_speaker_merge(1, "remote:2", "remote:1").unwrap();
drop(store);
assert_eq!(user_version(&path), 2);
assert_eq!(user_version(&path), 3);
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().unwrap();
assert_eq!(call.title.as_deref(), Some("Lieferung März"));
@@ -753,6 +793,83 @@ CREATE TABLE IF NOT EXISTS remote_jobs (
assert_eq!(store.list_calls().unwrap().len(), 1);
}
#[test]
fn a_version_2_database_is_upgraded_keeping_its_data() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("calls.sqlite");
let started = "2026-11-05T10:14:00+01:00";
{
let conn = rusqlite::Connection::open(&path).unwrap();
conn.execute_batch(RELEASED_V1_SCHEMA_SQL).unwrap();
conn.execute_batch(RELEASED_V2_STEP_SQL).unwrap();
conn.execute_batch("PRAGMA user_version = 2;").unwrap();
conn.execute(
"INSERT INTO calls (app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title) \
VALUES ('Microsoft Teams', ?1, ?1, 'done', 0, NULL, '/rec/1-mic.wav', '/rec/1-out.wav', ?1, 1, NULL, 'Angebot')",
rusqlite::params![started],
)
.unwrap();
conn.execute_batch(
"INSERT INTO segments (call_id, seq, speaker, start_ts, end_ts, text) VALUES \
(1, 0, 'remote:2', 0.0, 1.5, 'Hallo.'); \
INSERT INTO speaker_merges (call_id, from_key, into_key) VALUES (1, 'remote:2', 'remote:1');",
)
.unwrap();
}
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().expect("call kept");
assert_eq!(call.title.as_deref(), Some("Angebot"));
assert!(call.audio_deleted);
assert_eq!(call.crm_transcript_id, None);
assert_eq!(call.uploaded_at, None);
assert_eq!(store.segments(1).unwrap().len(), 1);
assert_eq!(
store.speaker_merges(1).unwrap(),
merge_map(&[("remote:2", "remote:1")])
);
// The new columns work, and opening again changes nothing.
let uploaded = Local::now();
store.set_uploaded(1, "crm-1", uploaded).unwrap();
drop(store);
assert_eq!(user_version(&path), 3);
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().unwrap();
assert_eq!(call.crm_transcript_id.as_deref(), Some("crm-1"));
assert_eq!(call.uploaded_at, Some(uploaded));
}
#[test]
fn an_upload_is_recorded_and_replaced_by_the_next() {
let store = Store::open_in_memory().unwrap();
let id = new_call(&store);
let other = new_call(&store);
let first = Local::now();
let second = first + chrono::Duration::minutes(5);
store.set_uploaded(id, "4711", first).unwrap();
let call = store.get_call(id).unwrap().unwrap();
assert_eq!(
(call.crm_transcript_id.as_deref(), call.uploaded_at),
(Some("4711"), Some(first))
);
store.set_uploaded(id, "4712", second).unwrap();
let listed = store.list_calls().unwrap();
let call = listed.iter().find(|c| c.id == id).unwrap();
assert_eq!(
(call.crm_transcript_id.as_deref(), call.uploaded_at),
(Some("4712"), Some(second))
);
let untouched = store.get_call(other).unwrap().unwrap();
assert_eq!(
(untouched.crm_transcript_id, untouched.uploaded_at),
(None, None)
);
}
#[test]
fn speaker_merges_are_set_replaced_and_removed() {
let store = Store::open_in_memory().unwrap();