Skip to content

Commit 2cfca49

Browse files
authored
Add log_level to config, Fix file upload for bills (#443)
* add log_level to config, fix file upload for bills * review changes
1 parent 7d834c4 commit 2cfca49

File tree

27 files changed

+158
-177
lines changed

27 files changed

+158
-177
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 0.3.1
22

3+
* Added `log_level` to Config, which defaults to `info`
4+
* Changed the API for uploading files to bill to use `file` instead of `files`.
5+
So files can only be uploaded individually, but for `issue()`, `file_upload_ids`
6+
can be passed - a list of file upload ids, to upload multiple files for one bill.
37
* Restructured `BitcreditBillWeb` to a more structured approach, separating `status`,
48
`data` and `participants` and adding the concept of `current_waiting_state`, to
59
have all data available, if the bill is in a waiting state.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ sha2 = { version = "0.10", default-features = false }
99
borsh = "1.5"
1010
borsh-derive = "1.5"
1111
env_logger = { version = "0.11", default-features = false }
12-
log = "0.4"
12+
log = { version = "0.4", features = ["serde"] }
1313
chrono = { version = "0.4", default-features = false, features = ["serde", "clock"] }
1414
tokio = { version = "1.43", default-features = false, features = ["rt"] }
1515
async-trait = "0.1"

crates/bcr-ebill-api/src/service/bill_service/issue.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ impl BillService {
2929
country_of_payment: String,
3030
city_of_payment: String,
3131
language: String,
32-
file_upload_id: Option<String>,
32+
file_upload_ids: Vec<String>,
3333
drawer_public_data: IdentityPublicData,
3434
drawer_keys: BcrKeys,
3535
timestamp: u64,
3636
) -> Result<BitcreditBill> {
3737
let (sum, bill_type) = self.validate_bill_issue(
3838
&sum,
39-
&file_upload_id,
39+
&file_upload_ids,
4040
&issue_date,
4141
&maturity_date,
4242
&drawee,
@@ -112,23 +112,16 @@ impl BillService {
112112
self.store.save_keys(&bill_id, &bill_keys).await?;
113113

114114
let mut bill_files: Vec<File> = vec![];
115-
if let Some(ref upload_id) = file_upload_id {
116-
let files = self
115+
for file_upload_id in file_upload_ids.iter() {
116+
let (file_name, file_bytes) = &self
117117
.file_upload_store
118-
.read_temp_upload_files(upload_id)
118+
.read_temp_upload_file(file_upload_id)
119119
.await
120120
.map_err(|_| Error::NoFileForFileUploadId)?;
121-
for (file_name, file_bytes) in files {
122-
bill_files.push(
123-
self.encrypt_and_save_uploaded_file(
124-
&file_name,
125-
&file_bytes,
126-
&bill_id,
127-
&public_key,
128-
)
121+
bill_files.push(
122+
self.encrypt_and_save_uploaded_file(file_name, file_bytes, &bill_id, &public_key)
129123
.await?,
130-
);
131-
}
124+
);
132125
}
133126

134127
let bill = BitcreditBill {
@@ -172,13 +165,16 @@ impl BillService {
172165
.await?;
173166

174167
// clean up temporary file uploads, if there are any, logging any errors
175-
if let Some(ref upload_id) = file_upload_id {
168+
for file_upload_id in file_upload_ids.iter() {
176169
if let Err(e) = self
177170
.file_upload_store
178-
.remove_temp_upload_folder(upload_id)
171+
.remove_temp_upload_folder(file_upload_id)
179172
.await
180173
{
181-
error!("Error while cleaning up temporary file uploads for {upload_id}: {e}");
174+
error!(
175+
"Error while cleaning up temporary file uploads for {}: {e}",
176+
&file_upload_id
177+
);
182178
}
183179
}
184180

crates/bcr-ebill-api/src/service/bill_service/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub trait BillServiceApi: ServiceTraitBounds {
135135
country_of_payment: String,
136136
city_of_payment: String,
137137
language: String,
138-
file_upload_id: Option<String>,
138+
file_upload_ids: Vec<String>,
139139
drawer_public_data: IdentityPublicData,
140140
drawer_keys: BcrKeys,
141141
timestamp: u64,
@@ -417,8 +417,8 @@ pub mod tests {
417417
let file_bytes = String::from("hello world").as_bytes().to_vec();
418418

419419
ctx.file_upload_store
420-
.expect_read_temp_upload_files()
421-
.returning(move |_| Ok(vec![(expected_file_name.to_string(), file_bytes.clone())]));
420+
.expect_read_temp_upload_file()
421+
.returning(move |_| Ok((expected_file_name.to_string(), file_bytes.clone())));
422422
ctx.file_upload_store
423423
.expect_remove_temp_upload_folder()
424424
.returning(|_| Ok(()));
@@ -453,7 +453,7 @@ pub mod tests {
453453
String::from("AT"),
454454
String::from("Vienna"),
455455
String::from("en-UK"),
456-
Some("1234".to_string()),
456+
vec!["1234".to_string()],
457457
IdentityPublicData::new(drawer.identity).unwrap(),
458458
drawer.key_pair,
459459
1731593928,
@@ -471,8 +471,8 @@ pub mod tests {
471471
let file_bytes = String::from("hello world").as_bytes().to_vec();
472472

473473
ctx.file_upload_store
474-
.expect_read_temp_upload_files()
475-
.returning(move |_| Ok(vec![(expected_file_name.to_string(), file_bytes.clone())]));
474+
.expect_read_temp_upload_file()
475+
.returning(move |_| Ok((expected_file_name.to_string(), file_bytes.clone())));
476476
ctx.file_upload_store
477477
.expect_remove_temp_upload_folder()
478478
.returning(|_| Ok(()));
@@ -507,7 +507,7 @@ pub mod tests {
507507
String::from("AT"),
508508
String::from("Vienna"),
509509
String::from("en-UK"),
510-
Some("1234".to_string()),
510+
vec!["1234".to_string()],
511511
IdentityPublicData::from(drawer.1.0), // public company data
512512
BcrKeys::from_private_key(&drawer.1.1.private_key).unwrap(), // company keys
513513
1731593928,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ impl BillServiceApi for BillService {
468468
country_of_payment: String,
469469
city_of_payment: String,
470470
language: String,
471-
file_upload_id: Option<String>,
471+
file_upload_ids: Vec<String>,
472472
drawer_public_data: IdentityPublicData,
473473
drawer_keys: BcrKeys,
474474
timestamp: u64,
@@ -486,7 +486,7 @@ impl BillServiceApi for BillService {
486486
country_of_payment,
487487
city_of_payment,
488488
language,
489-
file_upload_id,
489+
file_upload_ids,
490490
drawer_public_data,
491491
drawer_keys,
492492
timestamp,

crates/bcr-ebill-api/src/service/bill_service/validation.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl BillService {
1717
pub(super) fn validate_bill_issue(
1818
&self,
1919
sum: &str,
20-
file_upload_id: &Option<String>,
20+
file_upload_ids: &Vec<String>,
2121
issue_date: &str,
2222
maturity_date: &str,
2323
drawee: &str,
@@ -26,8 +26,10 @@ impl BillService {
2626
) -> Result<(u64, BillType)> {
2727
let sum = util::currency::parse_sum(sum).map_err(|e| Error::Validation(e.to_string()))?;
2828

29-
util::file::validate_file_upload_id(file_upload_id)
30-
.map_err(|e| Error::Validation(e.to_string()))?;
29+
for file_upload_id in file_upload_ids {
30+
util::file::validate_file_upload_id(Some(file_upload_id))
31+
.map_err(|e| Error::Validation(e.to_string()))?;
32+
}
3133

3234
if util::date::date_string_to_i64_timestamp(issue_date, None).is_none() {
3335
return Err(Error::Validation(String::from("invalid issue date")));

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,15 @@ impl CompanyService {
145145
public_key: &str,
146146
) -> Result<Option<File>> {
147147
if let Some(upload_id) = upload_id {
148-
let files = self
148+
let (file_name, file_bytes) = &self
149149
.file_upload_store
150-
.read_temp_upload_files(upload_id)
150+
.read_temp_upload_file(upload_id)
151151
.await
152152
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
153-
if !files.is_empty() {
154-
let (file_name, file_bytes) = &files[0];
155-
let file = self
156-
.encrypt_and_save_uploaded_file(file_name, file_bytes, id, public_key)
157-
.await?;
158-
return Ok(Some(file));
159-
}
153+
let file = self
154+
.encrypt_and_save_uploaded_file(file_name, file_bytes, id, public_key)
155+
.await?;
156+
return Ok(Some(file));
160157
}
161158
Ok(None)
162159
}
@@ -907,13 +904,8 @@ pub mod tests {
907904
})
908905
});
909906
file_upload_store
910-
.expect_read_temp_upload_files()
911-
.returning(|_| {
912-
Ok(vec![(
913-
"some_file".to_string(),
914-
"hello_world".as_bytes().to_vec(),
915-
)])
916-
});
907+
.expect_read_temp_upload_file()
908+
.returning(|_| Ok(("some_file".to_string(), "hello_world".as_bytes().to_vec())));
917909
file_upload_store
918910
.expect_remove_temp_upload_folder()
919911
.returning(|_| Ok(()));
@@ -1062,13 +1054,8 @@ pub mod tests {
10621054
})
10631055
});
10641056
file_upload_store
1065-
.expect_read_temp_upload_files()
1066-
.returning(|_| {
1067-
Ok(vec![(
1068-
"some_file".to_string(),
1069-
"hello_world".as_bytes().to_vec(),
1070-
)])
1071-
});
1057+
.expect_read_temp_upload_file()
1058+
.returning(|_| Ok(("some_file".to_string(), "hello_world".as_bytes().to_vec())));
10721059
file_upload_store
10731060
.expect_remove_temp_upload_folder()
10741061
.returning(|_| Ok(()));

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,15 @@ impl ContactService {
108108
public_key: &str,
109109
) -> Result<Option<File>> {
110110
if let Some(upload_id) = upload_id {
111-
let files = self
111+
let (file_name, file_bytes) = &self
112112
.file_upload_store
113-
.read_temp_upload_files(upload_id)
113+
.read_temp_upload_file(upload_id)
114114
.await
115115
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
116-
if !files.is_empty() {
117-
let (file_name, file_bytes) = &files[0];
118-
let file = self
119-
.encrypt_and_save_uploaded_file(file_name, file_bytes, id, public_key)
120-
.await?;
121-
return Ok(Some(file));
122-
}
116+
let file = self
117+
.encrypt_and_save_uploaded_file(file_name, file_bytes, id, public_key)
118+
.await?;
119+
return Ok(Some(file));
123120
}
124121
Ok(None)
125122
}

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

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,13 @@ impl FileUploadServiceApi for FileUploadService {
106106
}
107107

108108
async fn get_temp_file(&self, file_upload_id: &str) -> Result<Option<(String, Vec<u8>)>> {
109-
let mut files = self
109+
let file = self
110110
.file_upload_store
111-
.read_temp_upload_files(file_upload_id)
111+
.read_temp_upload_file(file_upload_id)
112112
.await
113113
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
114-
// return the first file in the folder
115-
if !files.is_empty() {
116-
let (file_name, file_bytes) = files.remove(0);
117-
return Ok(Some((file_name, file_bytes)));
118-
}
119-
Ok(None)
114+
let (file_name, file_bytes) = file;
115+
return Ok(Some((file_name, file_bytes)));
120116
}
121117
}
122118

@@ -346,12 +342,9 @@ mod tests {
346342
#[tokio::test]
347343
async fn get_temp_file_baseline() {
348344
let mut storage = MockFileUploadStoreApiMock::new();
349-
storage.expect_read_temp_upload_files().returning(|_| {
350-
Ok(vec![(
351-
"some_file".to_string(),
352-
"hello_world".as_bytes().to_vec(),
353-
)])
354-
});
345+
storage
346+
.expect_read_temp_upload_file()
347+
.returning(|_| Ok(("some_file".to_string(), "hello_world".as_bytes().to_vec())));
355348
let service = get_service(storage);
356349

357350
let res = service.get_temp_file("1234").await;
@@ -362,24 +355,11 @@ mod tests {
362355
);
363356
}
364357

365-
#[tokio::test]
366-
async fn get_temp_file_no_file() {
367-
let mut storage = MockFileUploadStoreApiMock::new();
368-
storage
369-
.expect_read_temp_upload_files()
370-
.returning(|_| Ok(vec![]));
371-
let service = get_service(storage);
372-
373-
let res = service.get_temp_file("1234").await;
374-
assert!(res.is_ok());
375-
assert_eq!(res.unwrap(), None);
376-
}
377-
378358
#[tokio::test]
379359
async fn get_temp_file_err() {
380360
let mut storage = MockFileUploadStoreApiMock::new();
381361
storage
382-
.expect_read_temp_upload_files()
362+
.expect_read_temp_upload_file()
383363
.returning(|_| Err(persistence::Error::Io(std::io::Error::other("test error"))));
384364
let service = get_service(storage);
385365

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,15 @@ impl IdentityService {
9191
public_key: &str,
9292
) -> Result<Option<File>> {
9393
if let Some(upload_id) = upload_id {
94-
let files = self
94+
let (file_name, file_bytes) = &self
9595
.file_upload_store
96-
.read_temp_upload_files(upload_id)
96+
.read_temp_upload_file(upload_id)
9797
.await
9898
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
99-
if !files.is_empty() {
100-
let (file_name, file_bytes) = &files[0];
101-
let file = self
102-
.encrypt_and_save_uploaded_file(file_name, file_bytes, id, public_key)
103-
.await?;
104-
return Ok(Some(file));
105-
}
99+
let file = self
100+
.encrypt_and_save_uploaded_file(file_name, file_bytes, id, public_key)
101+
.await?;
102+
return Ok(Some(file));
106103
}
107104
Ok(None)
108105
}

0 commit comments

Comments
 (0)