From 3a16c76257e6d56cc0c463ff14e23bd33fd001da Mon Sep 17 00:00:00 2001 From: hauke Date: Mon, 9 Nov 2026 10:52:16 +0100 Subject: [PATCH] feat #14740: track uploads per backend server Schema v4 adds calls.crm_base_url; existing uploads are tagged with the test backend. A call counts as uploaded (list/detail fields, the "Hochgeladen am" line, the upload-once guard) only if it was uploaded to this build's backend. Uploading to the other server replaces the single upload record; nothing is deleted. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/core/src/call_view.rs | 100 +++++++++++++++++++++--- crates/core/src/model.rs | 5 ++ crates/core/src/retention.rs | 1 + crates/core/src/store.rs | 143 +++++++++++++++++++++++++++++------ src-tauri/src/state.rs | 58 +++++++++++--- 5 files changed, 265 insertions(+), 42 deletions(-) diff --git a/crates/core/src/call_view.rs b/crates/core/src/call_view.rs index 99790b9..0304ee9 100644 --- a/crates/core/src/call_view.rs +++ b/crates/core/src/call_view.rs @@ -28,7 +28,13 @@ pub struct CallListItem { } impl CallListItem { - pub fn of(call: &Call) -> Self { + /// `backend_url` is this build's backend (`crate::backend::BACKEND_URL` + /// in the app): an upload recorded for a different one is shown as not + /// uploaded at all ([`uploaded_here`]) — e.g. a transcript sent to the + /// test backend must not look done to a release build reading the same + /// row. Nothing is deleted; the fields are just not shown as set. + pub fn of(call: &Call, backend_url: &str) -> Self { + let uploaded = uploaded_here(call, backend_url); Self { id: call.id, app: call.app.clone(), @@ -37,8 +43,12 @@ impl CallListItem { status: call.status, interrupted: call.interrupted, title: call.title.clone(), - crm_transcript_id: call.crm_transcript_id.clone(), - uploaded_at: call.uploaded_at, + crm_transcript_id: if uploaded { + call.crm_transcript_id.clone() + } else { + None + }, + uploaded_at: if uploaded { call.uploaded_at } else { None }, } } } @@ -50,6 +60,16 @@ pub fn duration_secs(call: &Call) -> i64 { .map_or(0, |end| (end - call.started_at).num_seconds().max(0)) } +/// Whether `call`'s upload belongs to `backend_url`: its +/// `crm_transcript_id` is set *and* `crm_base_url` is exactly this +/// backend. An upload recorded for a different one (e.g. the test backend, +/// read by a release build) counts as not uploaded: the CRM this +/// `backend_url` talks to has never seen the transcript, so the UI offers +/// the normal upload control again instead of claiming it is done. +pub fn uploaded_here(call: &Call, backend_url: &str) -> bool { + call.crm_transcript_id.is_some() && call.crm_base_url.as_deref() == Some(backend_url) +} + /// The selected call's header data: its list row plus the error of a /// failed run and when it was transcribed (only a transcribed call can be /// uploaded). @@ -104,12 +124,14 @@ pub struct CallDetail { impl CallDetail { /// `segments` are the stored ones; `merges` the stored speaker merges - /// (source key → target key). + /// (source key → target key); `backend_url` is this build's backend + /// (see [`CallListItem::of`]). pub fn of( call: &Call, segments: &[Segment], speaker_names: BTreeMap, merges: &BTreeMap, + backend_url: &str, ) -> Self { let segments = merge::apply_merges(segments, merges) .iter() @@ -123,7 +145,7 @@ impl CallDetail { .collect(); Self { call: CallHeader { - item: CallListItem::of(call), + item: CallListItem::of(call, backend_url), error: call.error.clone(), transcribed_at: call.transcribed_at, }, @@ -234,6 +256,11 @@ mod tests { use super::*; use crate::model::Speaker; + /// This build's backend, for `CallListItem::of`/`CallDetail::of`. + const BACKEND: &str = "https://backend.example"; + /// A different backend, for the "uploaded elsewhere" cases. + const OTHER_BACKEND: &str = "https://other.example"; + fn at(h: u32, m: u32, s: u32) -> DateTime { Local.with_ymd_and_hms(2026, 11, 5, h, m, s).unwrap() } @@ -253,6 +280,7 @@ mod tests { audio_deleted: false, title: None, crm_transcript_id: None, + crm_base_url: None, uploaded_at: None, } } @@ -283,7 +311,7 @@ mod tests { c.status = CallStatus::AwaitingDecision; c.interrupted = true; assert_eq!( - CallListItem::of(&c), + CallListItem::of(&c, BACKEND), CallListItem { id: 7, app: "Microsoft Teams".into(), @@ -299,13 +327,51 @@ mod tests { c.title = Some("Angebot Rioja".into()); c.crm_transcript_id = Some("4711".into()); + c.crm_base_url = Some(BACKEND.into()); c.uploaded_at = Some(at(11, 0, 0)); - let item = CallListItem::of(&c); + let item = CallListItem::of(&c, BACKEND); assert_eq!(item.title.as_deref(), Some("Angebot Rioja")); assert_eq!(item.crm_transcript_id.as_deref(), Some("4711")); assert_eq!(item.uploaded_at, Some(at(11, 0, 0))); } + #[test] + fn uploaded_here_needs_a_transcript_id_and_a_matching_backend() { + let mut c = call(Some(at(10, 15, 0))); + assert!(!uploaded_here(&c, BACKEND), "never uploaded"); + + c.crm_transcript_id = Some("4711".into()); + c.crm_base_url = Some(OTHER_BACKEND.into()); + assert!( + !uploaded_here(&c, BACKEND), + "uploaded, but to a different backend" + ); + + c.crm_base_url = Some(BACKEND.into()); + assert!(uploaded_here(&c, BACKEND)); + } + + #[test] + fn an_upload_to_another_backend_is_hidden_from_the_list_item() { + // Uploaded to OTHER_BACKEND; this build's backend is BACKEND: the + // row still has crm_transcript_id/uploaded_at, but the view must + // not show them, so the UI offers the normal upload control again. + let mut c = call(Some(at(10, 15, 0))); + c.crm_transcript_id = Some("4711".into()); + c.crm_base_url = Some(OTHER_BACKEND.into()); + c.uploaded_at = Some(at(11, 0, 0)); + + let item = CallListItem::of(&c, BACKEND); + assert_eq!(item.crm_transcript_id, None); + assert_eq!(item.uploaded_at, None); + + // The same row, read by a build whose backend actually matches: + // shown normally. + let item = CallListItem::of(&c, OTHER_BACKEND); + assert_eq!(item.crm_transcript_id.as_deref(), Some("4711")); + assert_eq!(item.uploaded_at, Some(at(11, 0, 0))); + } + #[test] fn detail_labels_segments_with_renames_and_defaults() { let segments = [ @@ -322,6 +388,7 @@ mod tests { &segments, names.clone(), &BTreeMap::new(), + BACKEND, ); let lines: Vec<_> = detail @@ -366,7 +433,13 @@ mod tests { ("remote:10", "remote:4"), ]); - let detail = CallDetail::of(&call(Some(at(10, 15, 0))), &segments, names, &merges); + let detail = CallDetail::of( + &call(Some(at(10, 15, 0))), + &segments, + names, + &merges, + BACKEND, + ); let lines: Vec<_> = detail .segments @@ -418,7 +491,8 @@ mod tests { let merges = keys(&[("remote:2", "remote:1")]); let value = - serde_json::to_value(CallDetail::of(&c, &[], BTreeMap::new(), &merges)).unwrap(); + serde_json::to_value(CallDetail::of(&c, &[], BTreeMap::new(), &merges, BACKEND)) + .unwrap(); assert_eq!( value, @@ -450,7 +524,13 @@ mod tests { } fn detail_with(c: &Call, segments: &[Segment], names: &[(&str, &str)]) -> CallDetail { - CallDetail::of(c, segments, keys(names), &keys(&[("remote:2", "remote:1")])) + CallDetail::of( + c, + segments, + keys(names), + &keys(&[("remote:2", "remote:1")]), + BACKEND, + ) } #[test] diff --git a/crates/core/src/model.rs b/crates/core/src/model.rs index 204e743..e81f702 100644 --- a/crates/core/src/model.rs +++ b/crates/core/src/model.rs @@ -41,6 +41,11 @@ pub struct Call { pub title: Option, /// The id the CRM gave the transcript at its last upload. pub crm_transcript_id: Option, + /// The backend ([`crate::backend`]'s base URL) `crm_transcript_id` was + /// uploaded to; `None` if never uploaded. A build whose own backend + /// differs (e.g. release reading a row a test build uploaded) does not + /// count this as uploaded — see `call_view::uploaded_here`. + pub crm_base_url: Option, /// When the transcript was last uploaded. pub uploaded_at: Option>, } diff --git a/crates/core/src/retention.rs b/crates/core/src/retention.rs index f8cb46c..18d6e6e 100644 --- a/crates/core/src/retention.rs +++ b/crates/core/src/retention.rs @@ -90,6 +90,7 @@ mod tests { audio_deleted: false, title: None, crm_transcript_id: None, + crm_base_url: None, uploaded_at: None, } } diff --git a/crates/core/src/store.rs b/crates/core/src/store.rs index 930955d..6fac2ba 100644 --- a/crates/core/src/store.rs +++ b/crates/core/src/store.rs @@ -120,9 +120,19 @@ ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT; ALTER TABLE calls ADD COLUMN uploaded_at TEXT; "; +/// Version 4: which backend an upload belongs to, so a build whose own +/// backend differs (see `crate::backend`) does not show someone else's +/// upload as done (`call_view::uploaded_here`). Every upload before this +/// version only ever went to the test backend (production never had the +/// API yet): backfilled here, so existing rows keep meaning what they did. +const SCHEMA_V4_SQL: &str = " +ALTER TABLE calls ADD COLUMN crm_base_url TEXT; +UPDATE calls SET crm_base_url = 'https://test.euvinopro.eu' WHERE crm_transcript_id IS NOT NULL; +"; + /// 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, SCHEMA_V3_SQL]; +const MIGRATIONS: &[&str] = &[SCHEMA_V1_SQL, SCHEMA_V2_SQL, SCHEMA_V3_SQL, SCHEMA_V4_SQL]; /// SQLite-backed store for calls and everything derived from them. pub struct Store { @@ -361,18 +371,21 @@ 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. + /// Records that the transcript was uploaded to `crm_base_url` at `at`, + /// which gave it `crm_transcript_id`; replaces an earlier upload's + /// record (even one of another backend — one upload slot per call, see + /// `call_view::uploaded_here`). pub fn set_uploaded( &self, id: CallId, crm_transcript_id: &str, + crm_base_url: &str, at: DateTime, ) -> 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], + "UPDATE calls SET crm_transcript_id = ?1, crm_base_url = ?2, uploaded_at = ?3 WHERE id = ?4", + params![crm_transcript_id, crm_base_url, dt_to_str(at), id], )?; Ok(()) } @@ -536,9 +549,9 @@ fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result Result { let id: CallId = row.get(0)?; @@ -555,6 +568,7 @@ fn row_to_call(row: &Row) -> Result { let title: Option = row.get(11)?; let crm_transcript_id: Option = row.get(12)?; let uploaded_at: Option = row.get(13)?; + let crm_base_url: Option = row.get(14)?; Ok(Call { id, @@ -570,6 +584,7 @@ fn row_to_call(row: &Row) -> Result { audio_deleted, title, crm_transcript_id, + crm_base_url, uploaded_at: opt_dt_from_str(uploaded_at)?, }) } @@ -692,6 +707,14 @@ CREATE TABLE speaker_merges ( into_key TEXT NOT NULL, PRIMARY KEY (call_id, from_key) ); +"; + + /// The schema of user_version 3, exactly as release 0.2.0 created it + /// (copied from `SCHEMA_V3_SQL` at `HEAD`): version 2 plus these + /// statements. + const RELEASED_V3_STEP_SQL: &str = " +ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT; +ALTER TABLE calls ADD COLUMN uploaded_at TEXT; "; fn user_version(path: &Path) -> i64 { @@ -715,17 +738,19 @@ CREATE TABLE speaker_merges ( } #[test] - fn a_new_database_starts_at_version_3() { + fn a_new_database_starts_at_version_4() { 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(); + store + .set_uploaded(id, "4711", "https://backend.example", Local::now()) + .unwrap(); drop(store); - assert_eq!(user_version(&path), 3); + assert_eq!(user_version(&path), 4); } #[test] @@ -782,7 +807,7 @@ CREATE TABLE speaker_merges ( 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), 3); + assert_eq!(user_version(&path), 4); let store = Store::open(&path).unwrap(); let call = store.get_call(1).unwrap().unwrap(); assert_eq!(call.title.as_deref(), Some("Lieferung März")); @@ -832,41 +857,115 @@ CREATE TABLE speaker_merges ( // The new columns work, and opening again changes nothing. let uploaded = Local::now(); - store.set_uploaded(1, "crm-1", uploaded).unwrap(); + store + .set_uploaded(1, "crm-1", "https://backend.example", uploaded) + .unwrap(); drop(store); - assert_eq!(user_version(&path), 3); + assert_eq!(user_version(&path), 4); 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.crm_base_url.as_deref(), + Some("https://backend.example") + ); assert_eq!(call.uploaded_at, Some(uploaded)); } #[test] - fn an_upload_is_recorded_and_replaced_by_the_next() { + fn a_version_3_database_is_upgraded_backfilling_the_test_backend_for_uploads() { + 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(RELEASED_V3_STEP_SQL).unwrap(); + conn.execute_batch("PRAGMA user_version = 3;").unwrap(); + // An already-uploaded call (every pre-v4 upload went to the + // test backend; production never had the API). + conn.execute( + "INSERT INTO calls \ + (app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title, crm_transcript_id, uploaded_at) \ + VALUES ('Microsoft Teams', ?1, ?1, 'done', 0, NULL, '/rec/1-mic.wav', '/rec/1-out.wav', ?1, 0, NULL, NULL, 'crm-1', ?1)", + rusqlite::params![started], + ) + .unwrap(); + // A call that was never uploaded. + conn.execute( + "INSERT INTO calls \ + (app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title, crm_transcript_id, uploaded_at) \ + VALUES ('MicroSIP', ?1, NULL, 'awaiting_decision', 0, NULL, '/rec/2-mic.wav', '/rec/2-out.wav', NULL, 0, NULL, NULL, NULL, NULL)", + rusqlite::params![started], + ) + .unwrap(); + } + + let store = Store::open(&path).unwrap(); + + let uploaded = store.get_call(1).unwrap().expect("call kept"); + assert_eq!(uploaded.crm_transcript_id.as_deref(), Some("crm-1")); + assert_eq!( + uploaded.crm_base_url.as_deref(), + Some("https://test.euvinopro.eu") + ); + + let not_uploaded = store.get_call(2).unwrap().expect("call kept"); + assert_eq!(not_uploaded.crm_transcript_id, None); + assert_eq!(not_uploaded.crm_base_url, None); + + assert_eq!(user_version(&path), 4); + } + + #[test] + fn an_upload_is_recorded_and_replaced_by_the_next_even_for_another_backend() { 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(); + store + .set_uploaded(id, "4711", "https://test.euvinopro.eu", 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)) + ( + call.crm_transcript_id.as_deref(), + call.crm_base_url.as_deref(), + call.uploaded_at + ), + (Some("4711"), Some("https://test.euvinopro.eu"), Some(first)) ); - store.set_uploaded(id, "4712", second).unwrap(); + // One upload slot per call: a later upload, even to a different + // backend, replaces it rather than keeping both. + store + .set_uploaded(id, "4712", "https://backend.euvinopro.eu", 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)) + ( + call.crm_transcript_id.as_deref(), + call.crm_base_url.as_deref(), + call.uploaded_at + ), + ( + Some("4712"), + Some("https://backend.euvinopro.eu"), + Some(second) + ) ); let untouched = store.get_call(other).unwrap().unwrap(); assert_eq!( - (untouched.crm_transcript_id, untouched.uploaded_at), - (None, None) + ( + untouched.crm_transcript_id, + untouched.crm_base_url, + untouched.uploaded_at + ), + (None, None, None) ); } diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs index a019ae1..c2f73c3 100644 --- a/src-tauri/src/state.rs +++ b/src-tauri/src/state.rs @@ -350,12 +350,15 @@ impl AppState { /// Reserves call `id` for one upload; released when the claim is /// dropped. A second upload of the same call is refused meanwhile, and - /// so is a call that already carries a CRM transcript id — or one the - /// CRM confirmed but whose id could not be saved locally, see - /// [`AppState::note_upload_confirmed_but_unsaved`]: the CRM must never - /// get a duplicate. All three checks happen while `uploading` is - /// locked, so a second upload can never slip in between this check and - /// the one upload it allows. + /// so is a call already uploaded to *this build's* backend + /// ([`call_view::uploaded_here`]) — or one the CRM confirmed but whose + /// id could not be saved locally, see + /// [`AppState::note_upload_confirmed_but_unsaved`]: the CRM this build + /// talks to must never get a duplicate. An upload recorded for another + /// backend (e.g. the test backend, read by a release build) is not + /// "uploaded" here and is claimed like any other call. All three checks + /// happen while `uploading` is locked, so a second upload can never + /// slip in between this check and the one upload it allows. pub fn claim_upload(&self, id: CallId) -> Result, String> { let mut uploading = lock(&self.uploading); if uploading.contains(&id) { @@ -364,7 +367,10 @@ impl AppState { if lock(&self.upload_confirmed_unsaved).contains(&id) { return Err("Bereits hochgeladen".to_string()); } - if self.find_call(id)?.crm_transcript_id.is_some() { + // An upload recorded for another backend (e.g. the test backend, + // read by a release build) does not count: the CRM this build + // talks to has never seen this transcript. + if call_view::uploaded_here(&self.find_call(id)?, &self.backend_url) { return Err("Bereits hochgeladen".to_string()); } uploading.insert(id); @@ -627,7 +633,10 @@ impl AppState { /// All calls for the main window's list, newest first. pub fn call_list(&self) -> Result, String> { let calls = self.store.list_calls().map_err(|e| e.to_string())?; - Ok(calls.iter().map(CallListItem::of).collect()) + Ok(calls + .iter() + .map(|call| CallListItem::of(call, &self.backend_url)) + .collect()) } /// The selected call with its transcript (speaker merges applied), @@ -637,7 +646,7 @@ impl AppState { let segments = self.store.segments(id).map_err(|e| e.to_string())?; let names = self.store.speaker_names(id).map_err(|e| e.to_string())?; let merges = self.store.speaker_merges(id).map_err(|e| e.to_string())?; - let mut detail = CallDetail::of(&call, &segments, names, &merges); + let mut detail = CallDetail::of(&call, &segments, names, &merges, &self.backend_url); detail.uploading = self.is_uploading(id); Ok(detail) } @@ -692,7 +701,7 @@ impl AppState { pub fn mark_uploaded(&self, id: CallId, crm_transcript_id: &str) -> Result<(), String> { self.find_call(id)?; self.store - .set_uploaded(id, crm_transcript_id, Local::now()) + .set_uploaded(id, crm_transcript_id, &self.backend_url, Local::now()) .map_err(|e| format!("Upload konnte nicht vermerkt werden: {e}")) } @@ -1706,6 +1715,34 @@ mod tests { assert!(!f.state.is_uploading(id)); } + /// The real case the review asked to lock in: a call uploaded to the + /// test backend is refused for upload by a build whose own backend is + /// the test backend (the normal case, above), but uploadable again by a + /// build whose backend is the production one — the CRM at that backend + /// has never seen this transcript. + #[test] + fn an_upload_to_another_backend_is_not_counted_as_uploaded_here() { + let mut f = fixture(); + let (id, _, _) = finished_call(&f, "a"); + // A build on the test backend (what `cargo tauri dev` is), whatever + // profile this test itself runs in: mark_uploaded records it. + f.state.backend_url = crate::backend::DEV_BACKEND_URL.to_string(); + f.state.mark_uploaded(id, "4711").unwrap(); + assert_eq!( + f.state.find_call(id).unwrap().crm_base_url.as_deref(), + Some(crate::backend::DEV_BACKEND_URL) + ); + assert_eq!( + f.state.claim_upload(id).err(), + Some("Bereits hochgeladen".to_string()) + ); + + // The same row, now read as a release build would be: a different + // backend, nothing else changed. + f.state.backend_url = crate::backend::RELEASE_BACKEND_URL.to_string(); + assert!(f.state.claim_upload(id).is_ok()); + } + #[test] fn an_upload_confirmed_but_unsaved_is_never_retried() { // The CRM has the transcript, but mark_uploaded failed: the row @@ -1738,6 +1775,7 @@ mod tests { let call = f.state.store.get_call(id).unwrap().unwrap(); assert_eq!(call.crm_transcript_id.as_deref(), Some("4711")); + assert_eq!(call.crm_base_url.as_deref(), Some(BASE)); assert!(call.uploaded_at.is_some()); assert_eq!( f.state.mark_uploaded(id + 1, "1"),