Skip to content

Commit 638b488

Browse files
authored
Disable companies we are no longer singatory in (#615)
* Add active flag, set flag on block update * Hide inactive companies from the UI
1 parent 6b1fcea commit 638b488

File tree

9 files changed

+147
-28
lines changed

9 files changed

+147
-28
lines changed

crates/bcr-ebill-api/src/service/company_service.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl CompanyServiceApi for CompanyService {
340340
proof_of_registration_file,
341341
logo_file,
342342
signatories: vec![full_identity.identity.node_id.clone()], // add caller as signatory
343+
active: true,
343344
};
344345
self.store.insert(&company).await?;
345346

@@ -935,6 +936,7 @@ pub mod tests {
935936
proof_of_registration_file: None,
936937
logo_file: None,
937938
signatories: vec![node_id_test()],
939+
active: true,
938940
},
939941
CompanyKeys {
940942
private_key: private_key_test(),

crates/bcr-ebill-core/src/blockchain/company/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ mod tests {
585585
proof_of_registration_file: None,
586586
logo_file: None,
587587
signatories: vec![node_id_test()],
588+
active: true,
588589
},
589590
CompanyKeys {
590591
private_key: private_key_test(),

crates/bcr-ebill-core/src/company.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ pub struct Company {
2020
pub proof_of_registration_file: Option<File>,
2121
pub logo_file: Option<File>,
2222
pub signatories: Vec<NodeId>,
23+
pub active: bool,
2324
}
2425

2526
impl Company {
2627
/// Creates a new company from a block data payload
27-
pub fn from_block_data(data: CompanyCreateBlockData) -> Self {
28+
pub fn from_block_data(data: CompanyCreateBlockData, our_node_id: &NodeId) -> Self {
29+
let active = data.signatories.contains(our_node_id);
2830
Self {
2931
id: data.id,
3032
name: data.name,
@@ -37,10 +39,11 @@ impl Company {
3739
proof_of_registration_file: data.proof_of_registration_file,
3840
logo_file: data.logo_file,
3941
signatories: data.signatories,
42+
active,
4043
}
4144
}
4245
/// Applies data from a block to this company.
43-
pub fn apply_block_data(&mut self, data: &CompanyBlockPayload) {
46+
pub fn apply_block_data(&mut self, data: &CompanyBlockPayload, our_node_id: &NodeId) {
4447
match data {
4548
CompanyBlockPayload::Update(payload) => {
4649
self.name = payload.name.to_owned().unwrap_or(self.name.to_owned());
@@ -90,10 +93,16 @@ impl Company {
9093
CompanyBlockPayload::AddSignatory(payload) => {
9194
if !self.signatories.contains(&payload.signatory) {
9295
self.signatories.push(payload.signatory.to_owned());
96+
if &payload.signatory == our_node_id {
97+
self.active = true;
98+
}
9399
}
94100
}
95101
CompanyBlockPayload::RemoveSignatory(payload) => {
96102
self.signatories.retain(|i| i != &payload.signatory);
103+
if &payload.signatory == our_node_id {
104+
self.active = false;
105+
}
97106
}
98107
_ => {}
99108
}

crates/bcr-ebill-persistence/src/db/company.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl CompanyStoreApi for SurrealCompanyStore {
3838
bindings.add(DB_TABLE, Self::DATA_TABLE)?;
3939
bindings.add(DB_SEARCH_TERM, search_term.to_owned())?;
4040
let results: Vec<CompanyDb> = self.db
41-
.query("SELECT * from type::table($table) WHERE string::lowercase(name) CONTAINS $search_term", bindings).await?;
41+
.query("SELECT * from type::table($table) WHERE string::lowercase(name) CONTAINS $search_term AND active != false", bindings).await?;
4242
results.into_iter().map(|c| c.try_into()).collect()
4343
}
4444

@@ -57,7 +57,15 @@ impl CompanyStoreApi for SurrealCompanyStore {
5757
}
5858

5959
async fn get_all(&self) -> Result<HashMap<NodeId, (Company, CompanyKeys)>> {
60-
let companies: Vec<CompanyDb> = self.db.select_all(Self::DATA_TABLE).await?;
60+
let mut bindings = Bindings::default();
61+
bindings.add(DB_TABLE, Self::DATA_TABLE)?;
62+
let companies: Vec<CompanyDb> = self
63+
.db
64+
.query(
65+
"SELECT * from type::table($table) WHERE active != false",
66+
bindings,
67+
)
68+
.await?;
6169
let company_keys: Vec<CompanyKeysDb> = self.db.select_all(Self::KEYS_TABLE).await?;
6270
let companies_map: HashMap<NodeId, CompanyDb> = companies
6371
.into_iter()
@@ -145,6 +153,13 @@ pub struct CompanyDb {
145153
pub proof_of_registration_file: Option<FileDb>,
146154
pub logo_file: Option<FileDb>,
147155
pub signatories: Vec<NodeId>,
156+
#[serde(default = "active_default")]
157+
pub active: bool,
158+
}
159+
160+
// needed to default existing companies to active
161+
fn active_default() -> bool {
162+
true
148163
}
149164

150165
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -170,6 +185,7 @@ impl TryFrom<CompanyDb> for Company {
170185
proof_of_registration_file: value.proof_of_registration_file.map(|f| f.into()),
171186
logo_file: value.logo_file.map(|f| f.into()),
172187
signatories: value.signatories,
188+
active: value.active,
173189
})
174190
}
175191
}
@@ -195,6 +211,7 @@ impl From<&Company> for CompanyDb {
195211
.map(|f| (&f).into()),
196212
logo_file: value.logo_file.clone().map(|f| (&f).into()),
197213
signatories: value.signatories.clone(),
214+
active: value.active,
198215
}
199216
}
200217
}
@@ -249,6 +266,7 @@ mod tests {
249266
proof_of_registration_file: None,
250267
logo_file: None,
251268
signatories: vec![node_id_test()],
269+
active: true,
252270
}
253271
}
254272

@@ -379,4 +397,50 @@ mod tests {
379397
node_id_test().pub_key()
380398
);
381399
}
400+
401+
#[tokio::test]
402+
async fn test_get_all_and_search_only_return_active_companies() {
403+
let store = get_store().await;
404+
let mut company = get_baseline_company();
405+
company.name = "first company".to_string();
406+
store.insert(&company).await.unwrap();
407+
store
408+
.save_key_pair(
409+
&node_id_test(),
410+
&CompanyKeys {
411+
private_key: private_key_test(),
412+
public_key: node_id_test().pub_key(),
413+
},
414+
)
415+
.await
416+
.unwrap();
417+
let mut company2 = get_baseline_company();
418+
company2.id = node_id_test_other();
419+
company2.name = "second company".to_string();
420+
company2.active = false;
421+
422+
store.insert(&company2).await.unwrap();
423+
store
424+
.save_key_pair(
425+
&company2.id,
426+
&CompanyKeys {
427+
private_key: private_key_test(),
428+
public_key: node_id_test().pub_key(),
429+
},
430+
)
431+
.await
432+
.unwrap();
433+
let companies = store.get_all().await.unwrap();
434+
assert_eq!(companies.len(), 1);
435+
assert_eq!(
436+
companies.get(&node_id_test()).as_ref().unwrap().0.name,
437+
"first company".to_owned()
438+
);
439+
let search_results = store.search("company").await.unwrap();
440+
assert_eq!(search_results.len(), 1);
441+
assert_eq!(
442+
search_results.first().unwrap().name,
443+
"first company".to_owned()
444+
);
445+
}
382446
}

crates/bcr-ebill-persistence/src/db/company_chain.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ mod tests {
283283
proof_of_registration_file: None,
284284
logo_file: None,
285285
signatories: vec![node_id_test()],
286+
active: true,
286287
}
287288
.into(),
288289
&BcrKeys::new(),
@@ -338,6 +339,7 @@ mod tests {
338339
proof_of_registration_file: None,
339340
logo_file: None,
340341
signatories: vec![node_id_test()],
342+
active: true,
341343
}
342344
.into(),
343345
&BcrKeys::new(),

0 commit comments

Comments
 (0)