Wire native settings and workspace mutations
This commit is contained in:
1 parent
0a5be5ea97
commit
a291e93608
7 files changed
+562
-74
No files matched your search
@@ -154,6 +154,85 @@ impl ProfileStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_theme(&mut self, theme: &str) -> Result<()> {
|
||||
let normalized = match theme {
|
||||
"system" | "light" | "dark" => theme,
|
||||
_ => return Err(anyhow!("unsupported theme value: {theme}")),
|
||||
};
|
||||
self.conn.execute(
|
||||
"insert into settings (key, value) values ('theme', ?1) on conflict(key) do update set value = excluded.value",
|
||||
params![normalized],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_locale(&mut self, locale: &str) -> Result<()> {
|
||||
let normalized = match locale {
|
||||
"en" | "zh" => locale,
|
||||
_ => return Err(anyhow!("unsupported locale value: {locale}")),
|
||||
};
|
||||
self.conn.execute(
|
||||
"insert into settings (key, value) values ('locale', ?1) on conflict(key) do update set value = excluded.value",
|
||||
params![normalized],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_execution_mode(&mut self, company_id: &str, execution_mode: &str) -> Result<()> {
|
||||
let normalized = normalized_execution_mode(execution_mode);
|
||||
let ts = now_iso()?;
|
||||
self.conn.execute(
|
||||
"update companies set execution_mode = ?1, auto_approve_hires = ?2, review_deliverables = ?3, updated_at = ?4 where id = ?5",
|
||||
params![
|
||||
normalized,
|
||||
if normalized == "ai_ceo" { 1 } else { 0 },
|
||||
if normalized == "ai_cofounder" { 1 } else { 0 },
|
||||
ts,
|
||||
company_id
|
||||
],
|
||||
)?;
|
||||
self.add_activity(
|
||||
company_id,
|
||||
BOARD_READER_ID,
|
||||
"updated_execution_mode",
|
||||
"companies",
|
||||
company_id,
|
||||
normalized,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_primary_workspace(&mut self, company_id: &str, workspace_id: &str) -> Result<()> {
|
||||
let ts = now_iso()?;
|
||||
let tx = self.conn.transaction()?;
|
||||
let belongs = tx.query_row(
|
||||
"select count(1) from workspaces where id = ?1 and company_id = ?2",
|
||||
params![workspace_id, company_id],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)?;
|
||||
if belongs == 0 {
|
||||
return Err(anyhow!("workspace {workspace_id} was not found"));
|
||||
}
|
||||
tx.execute(
|
||||
"update workspaces set is_primary = 0, updated_at = ?1 where company_id = ?2",
|
||||
params![ts, company_id],
|
||||
)?;
|
||||
tx.execute(
|
||||
"update workspaces set is_primary = 1, updated_at = ?1 where id = ?2 and company_id = ?3",
|
||||
params![ts, workspace_id, company_id],
|
||||
)?;
|
||||
tx.commit()?;
|
||||
self.add_activity(
|
||||
company_id,
|
||||
BOARD_READER_ID,
|
||||
"updated_primary_workspace",
|
||||
"workspaces",
|
||||
workspace_id,
|
||||
"primary workspace changed",
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn decide_approval(
|
||||
&mut self,
|
||||
company_id: &str,
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
use storage::{NewCompanyInput, ProfileStore};
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn settings_and_workspace_mutations_round_trip_through_snapshot() {
|
||||
let sandbox = tempdir().expect("temp dir");
|
||||
let mut store = ProfileStore::open(sandbox.path()).expect("open store");
|
||||
|
||||
let company_id = store
|
||||
.create_company(NewCompanyInput {
|
||||
name: "Orbit Settings".to_string(),
|
||||
description: "Native control plane".to_string(),
|
||||
workspace_path: "/tmp/orbit-settings".to_string(),
|
||||
execution_mode: "ai_ceo".to_string(),
|
||||
})
|
||||
.expect("create company");
|
||||
|
||||
let ts = support::time::now_iso().expect("timestamp");
|
||||
let secondary_workspace_id = uuid::Uuid::new_v4().to_string();
|
||||
let conn = rusqlite::Connection::open(store.db_path()).expect("open raw db");
|
||||
conn.execute(
|
||||
"insert into workspaces (id, company_id, name, local_path, repo_url, repo_ref, project_id, state, is_primary, created_at, updated_at) values (?1, ?2, 'Secondary Workspace', '/tmp/orbit-settings/secondary', '', '', null, 'blocked', 0, ?3, ?3)",
|
||||
rusqlite::params![secondary_workspace_id, company_id, ts],
|
||||
)
|
||||
.expect("insert secondary workspace");
|
||||
|
||||
store.update_theme("dark").expect("update theme");
|
||||
store.update_locale("zh").expect("update locale");
|
||||
store
|
||||
.update_execution_mode(&company_id, "ai_cofounder")
|
||||
.expect("update execution mode");
|
||||
store
|
||||
.set_primary_workspace(&company_id, &secondary_workspace_id)
|
||||
.expect("set primary workspace");
|
||||
|
||||
let snapshot = store.snapshot().expect("snapshot");
|
||||
assert_eq!(snapshot.theme, "dark");
|
||||
assert_eq!(snapshot.locale, "zh");
|
||||
let company = snapshot
|
||||
.companies
|
||||
.iter()
|
||||
.find(|item| item.id == company_id)
|
||||
.expect("company present");
|
||||
assert_eq!(company.execution_mode, "ai_cofounder");
|
||||
let primary_workspace = snapshot
|
||||
.workspaces
|
||||
.iter()
|
||||
.find(|workspace| workspace.is_primary)
|
||||
.expect("primary workspace present");
|
||||
assert_eq!(primary_workspace.id, secondary_workspace_id);
|
||||
|
||||
let reopened = ProfileStore::open(sandbox.path()).expect("reopen store");
|
||||
let reopened_snapshot = reopened.snapshot().expect("reopened snapshot");
|
||||
assert_eq!(reopened_snapshot.theme, "dark");
|
||||
assert_eq!(reopened_snapshot.locale, "zh");
|
||||
assert!(
|
||||
reopened_snapshot
|
||||
.workspaces
|
||||
.iter()
|
||||
.any(|workspace| workspace.id == secondary_workspace_id && workspace.is_primary)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user