@@ -38,7 +38,7 @@ impl CompanyStoreApi for SurrealCompanyStore {
38
38
bindings. add ( DB_TABLE , Self :: DATA_TABLE ) ?;
39
39
bindings. add ( DB_SEARCH_TERM , search_term. to_owned ( ) ) ?;
40
40
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 ?;
42
42
results. into_iter ( ) . map ( |c| c. try_into ( ) ) . collect ( )
43
43
}
44
44
@@ -57,7 +57,15 @@ impl CompanyStoreApi for SurrealCompanyStore {
57
57
}
58
58
59
59
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 ?;
61
69
let company_keys: Vec < CompanyKeysDb > = self . db . select_all ( Self :: KEYS_TABLE ) . await ?;
62
70
let companies_map: HashMap < NodeId , CompanyDb > = companies
63
71
. into_iter ( )
@@ -145,6 +153,13 @@ pub struct CompanyDb {
145
153
pub proof_of_registration_file : Option < FileDb > ,
146
154
pub logo_file : Option < FileDb > ,
147
155
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
148
163
}
149
164
150
165
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
@@ -170,6 +185,7 @@ impl TryFrom<CompanyDb> for Company {
170
185
proof_of_registration_file : value. proof_of_registration_file . map ( |f| f. into ( ) ) ,
171
186
logo_file : value. logo_file . map ( |f| f. into ( ) ) ,
172
187
signatories : value. signatories ,
188
+ active : value. active ,
173
189
} )
174
190
}
175
191
}
@@ -195,6 +211,7 @@ impl From<&Company> for CompanyDb {
195
211
. map ( |f| ( & f) . into ( ) ) ,
196
212
logo_file : value. logo_file . clone ( ) . map ( |f| ( & f) . into ( ) ) ,
197
213
signatories : value. signatories . clone ( ) ,
214
+ active : value. active ,
198
215
}
199
216
}
200
217
}
@@ -249,6 +266,7 @@ mod tests {
249
266
proof_of_registration_file : None ,
250
267
logo_file : None ,
251
268
signatories : vec ! [ node_id_test( ) ] ,
269
+ active : true ,
252
270
}
253
271
}
254
272
@@ -379,4 +397,50 @@ mod tests {
379
397
node_id_test( ) . pub_key( )
380
398
) ;
381
399
}
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
+ }
382
446
}
0 commit comments