Skip to content

Commit d5e39cf

Browse files
authored
refactor to Validate trait,add baseline issue and is paid test and fix zip2 deps for utoipa (#466)
1 parent 5357ae9 commit d5e39cf

File tree

12 files changed

+696
-594
lines changed

12 files changed

+696
-594
lines changed

clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
too-many-arguments-threshold=13
1+
too-many-arguments-threshold=12

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

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,40 @@ use super::{BillAction, BillServiceApi, Result, error::Error, service::BillServi
22
use crate::util;
33
use bcr_ebill_core::{
44
File, Validate,
5-
bill::{BillKeys, BillType, BitcreditBill, validation::validate_bill_issue},
5+
bill::{BillIssueData, BillKeys, BillType, BitcreditBill, validation::validate_bill_issue},
66
blockchain::{
77
Blockchain,
88
bill::{BillBlockchain, block::BillIssueBlockData},
99
},
10-
contact::IdentityPublicData,
1110
util::BcrKeys,
1211
};
1312
use bcr_ebill_transport::BillChainEvent;
1413
use log::{debug, error};
1514

1615
impl BillService {
17-
#[allow(clippy::too_many_arguments)]
18-
pub(super) async fn issue_bill(
19-
&self,
20-
t: u64,
21-
country_of_issuing: String,
22-
city_of_issuing: String,
23-
issue_date: String,
24-
maturity_date: String,
25-
drawee: String,
26-
payee: String,
27-
sum: String,
28-
currency: String,
29-
country_of_payment: String,
30-
city_of_payment: String,
31-
language: String,
32-
file_upload_ids: Vec<String>,
33-
drawer_public_data: IdentityPublicData,
34-
drawer_keys: BcrKeys,
35-
timestamp: u64,
36-
) -> Result<BitcreditBill> {
37-
debug!("issuing bill with type {t}");
38-
let (sum, bill_type) = validate_bill_issue(
39-
&sum,
40-
&file_upload_ids,
41-
&issue_date,
42-
&maturity_date,
43-
&drawee,
44-
&payee,
45-
t,
46-
)?;
16+
pub(super) async fn issue_bill(&self, data: BillIssueData) -> Result<BitcreditBill> {
17+
debug!("issuing bill with type {}", &data.t);
18+
let (sum, bill_type) = validate_bill_issue(&data)?;
4719

4820
let (public_data_drawee, public_data_payee) = match bill_type {
4921
// Drawer is payee
5022
BillType::SelfDrafted => {
51-
let public_data_drawee = match self.contact_store.get(&drawee).await {
23+
let public_data_drawee = match self.contact_store.get(&data.drawee).await {
5224
Ok(Some(drawee)) => drawee.into(),
5325
Ok(None) | Err(_) => {
5426
return Err(Error::DraweeNotInContacts);
5527
}
5628
};
5729

58-
let public_data_payee = drawer_public_data.clone();
30+
let public_data_payee = data.drawer_public_data.clone();
5931

6032
(public_data_drawee, public_data_payee)
6133
}
6234
// Drawer is drawee
6335
BillType::PromissoryNote => {
64-
let public_data_drawee = drawer_public_data.clone();
36+
let public_data_drawee = data.drawer_public_data.clone();
6537

66-
let public_data_payee = match self.contact_store.get(&payee).await {
38+
let public_data_payee = match self.contact_store.get(&data.payee).await {
6739
Ok(Some(drawee)) => drawee.into(),
6840
Ok(None) | Err(_) => {
6941
return Err(Error::PayeeNotInContacts);
@@ -74,14 +46,14 @@ impl BillService {
7446
}
7547
// Drawer is neither drawee nor payee
7648
BillType::ThreeParties => {
77-
let public_data_drawee = match self.contact_store.get(&drawee).await {
49+
let public_data_drawee = match self.contact_store.get(&data.drawee).await {
7850
Ok(Some(drawee)) => drawee.into(),
7951
Ok(None) | Err(_) => {
8052
return Err(Error::DraweeNotInContacts);
8153
}
8254
};
8355

84-
let public_data_payee = match self.contact_store.get(&payee).await {
56+
let public_data_payee = match self.contact_store.get(&data.payee).await {
8557
Ok(Some(drawee)) => drawee.into(),
8658
Ok(None) | Err(_) => {
8759
return Err(Error::PayeeNotInContacts);
@@ -104,7 +76,7 @@ impl BillService {
10476
};
10577

10678
let mut bill_files: Vec<File> = vec![];
107-
for file_upload_id in file_upload_ids.iter() {
79+
for file_upload_id in data.file_upload_ids.iter() {
10880
let (file_name, file_bytes) = &self
10981
.file_upload_store
11082
.read_temp_upload_file(file_upload_id)
@@ -118,25 +90,29 @@ impl BillService {
11890

11991
let bill = BitcreditBill {
12092
id: bill_id.clone(),
121-
country_of_issuing,
122-
city_of_issuing,
123-
currency,
93+
country_of_issuing: data.country_of_issuing,
94+
city_of_issuing: data.city_of_issuing,
95+
currency: data.currency,
12496
sum,
125-
maturity_date,
126-
issue_date,
127-
country_of_payment,
128-
city_of_payment,
129-
language,
97+
maturity_date: data.maturity_date,
98+
issue_date: data.issue_date,
99+
country_of_payment: data.country_of_payment,
100+
city_of_payment: data.city_of_payment,
101+
language: data.language,
130102
drawee: public_data_drawee,
131-
drawer: drawer_public_data.clone(),
103+
drawer: data.drawer_public_data.clone(),
132104
payee: public_data_payee,
133105
endorsee: None,
134106
files: bill_files,
135107
};
136108

137-
let signing_keys = self.get_bill_signing_keys(&drawer_public_data, &drawer_keys, &identity);
138-
let block_data =
139-
BillIssueBlockData::from(bill.clone(), signing_keys.signatory_identity, timestamp);
109+
let signing_keys =
110+
self.get_bill_signing_keys(&data.drawer_public_data, &data.drawer_keys, &identity);
111+
let block_data = BillIssueBlockData::from(
112+
bill.clone(),
113+
signing_keys.signatory_identity,
114+
data.timestamp,
115+
);
140116
block_data.validate()?;
141117

142118
self.store.save_keys(&bill_id, &bill_keys).await?;
@@ -145,19 +121,19 @@ impl BillService {
145121
signing_keys.signatory_keys,
146122
signing_keys.company_keys,
147123
keys.clone(),
148-
timestamp,
124+
data.timestamp,
149125
)?;
150126

151127
let block = chain.get_first_block();
152128
self.blockchain_store.add_block(&bill.id, block).await?;
153129

154130
self.add_identity_and_company_chain_blocks_for_signed_bill_action(
155-
&drawer_public_data,
131+
&data.drawer_public_data,
156132
&bill_id,
157133
block,
158134
&identity.key_pair,
159-
&drawer_keys,
160-
timestamp,
135+
&data.drawer_keys,
136+
data.timestamp,
161137
)
162138
.await?;
163139

@@ -167,13 +143,13 @@ impl BillService {
167143
&chain,
168144
&bill_keys,
169145
&identity.identity,
170-
&drawer_public_data.node_id,
171-
timestamp,
146+
&data.drawer_public_data.node_id,
147+
data.timestamp,
172148
)
173149
.await?;
174150

175151
// clean up temporary file uploads, if there are any, logging any errors
176-
for file_upload_id in file_upload_ids.iter() {
152+
for file_upload_id in data.file_upload_ids.iter() {
177153
if let Err(e) = self
178154
.file_upload_store
179155
.remove_temp_upload_folder(file_upload_id)
@@ -209,9 +185,9 @@ impl BillService {
209185
self.execute_bill_action(
210186
&bill_id,
211187
BillAction::Accept,
212-
&drawer_public_data,
213-
&drawer_keys,
214-
timestamp + 1,
188+
&data.drawer_public_data,
189+
&data.drawer_keys,
190+
data.timestamp + 1,
215191
)
216192
.await?;
217193
}

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

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::data::{
1111
use crate::util::BcrKeys;
1212
use async_trait::async_trait;
1313
use bcr_ebill_core::ServiceTraitBounds;
14-
use bcr_ebill_core::bill::BillAction;
14+
use bcr_ebill_core::bill::{BillAction, BillIssueData};
1515

1616
pub use error::Error;
1717
#[cfg(test)]
@@ -97,26 +97,7 @@ pub trait BillServiceApi: ServiceTraitBounds {
9797
) -> Result<File>;
9898

9999
/// issues a new bill
100-
#[allow(clippy::too_many_arguments)]
101-
async fn issue_new_bill(
102-
&self,
103-
t: u64,
104-
country_of_issuing: String,
105-
city_of_issuing: String,
106-
issue_date: String,
107-
maturity_date: String,
108-
drawee: String,
109-
payee: String,
110-
sum: String,
111-
currency: String,
112-
country_of_payment: String,
113-
city_of_payment: String,
114-
language: String,
115-
file_upload_ids: Vec<String>,
116-
drawer_public_data: IdentityPublicData,
117-
drawer_keys: BcrKeys,
118-
timestamp: u64,
119-
) -> Result<BitcreditBill>;
100+
async fn issue_new_bill(&self, data: BillIssueData) -> Result<BitcreditBill>;
120101

121102
/// executes the given bill action
122103
async fn execute_bill_action(
@@ -428,24 +409,24 @@ pub mod tests {
428409
payee.node_id = BcrKeys::new().get_public_key();
429410

430411
let bill = service
431-
.issue_new_bill(
432-
2,
433-
String::from("UK"),
434-
String::from("London"),
435-
String::from("2030-01-01"),
436-
String::from("2030-04-01"),
437-
drawee.node_id,
438-
payee.node_id,
439-
String::from("100"),
440-
String::from("sat"),
441-
String::from("AT"),
442-
String::from("Vienna"),
443-
String::from("en-UK"),
444-
vec![TEST_BILL_ID.to_string()],
445-
IdentityPublicData::new(drawer.identity).unwrap(),
446-
drawer.key_pair,
447-
1731593928,
448-
)
412+
.issue_new_bill(BillIssueData {
413+
t: 2,
414+
country_of_issuing: String::from("UK"),
415+
city_of_issuing: String::from("London"),
416+
issue_date: String::from("2030-01-01"),
417+
maturity_date: String::from("2030-04-01"),
418+
drawee: drawee.node_id,
419+
payee: payee.node_id,
420+
sum: String::from("100"),
421+
currency: String::from("sat"),
422+
country_of_payment: String::from("AT"),
423+
city_of_payment: String::from("Vienna"),
424+
language: String::from("en-UK"),
425+
file_upload_ids: vec![TEST_BILL_ID.to_string()],
426+
drawer_public_data: IdentityPublicData::new(drawer.identity).unwrap(),
427+
drawer_keys: drawer.key_pair,
428+
timestamp: 1731593928,
429+
})
449430
.await
450431
.unwrap();
451432

@@ -485,24 +466,24 @@ pub mod tests {
485466
payee.node_id = BcrKeys::new().get_public_key();
486467

487468
let bill = service
488-
.issue_new_bill(
489-
2,
490-
String::from("UK"),
491-
String::from("London"),
492-
String::from("2030-01-01"),
493-
String::from("2030-04-01"),
494-
drawee.node_id,
495-
payee.node_id,
496-
String::from("100"),
497-
String::from("sat"),
498-
String::from("AT"),
499-
String::from("Vienna"),
500-
String::from("en-UK"),
501-
vec![TEST_BILL_ID.to_string()],
502-
IdentityPublicData::from(drawer.1.0), // public company data
503-
BcrKeys::from_private_key(&drawer.1.1.private_key).unwrap(), // company keys
504-
1731593928,
505-
)
469+
.issue_new_bill(BillIssueData {
470+
t: 2,
471+
country_of_issuing: String::from("UK"),
472+
city_of_issuing: String::from("London"),
473+
issue_date: String::from("2030-01-01"),
474+
maturity_date: String::from("2030-04-01"),
475+
drawee: drawee.node_id,
476+
payee: payee.node_id,
477+
sum: String::from("100"),
478+
currency: String::from("sat"),
479+
country_of_payment: String::from("AT"),
480+
city_of_payment: String::from("Vienna"),
481+
language: String::from("en-UK"),
482+
file_upload_ids: vec![TEST_BILL_ID.to_string()],
483+
drawer_public_data: IdentityPublicData::from(drawer.1.0),
484+
drawer_keys: BcrKeys::from_private_key(&drawer.1.1.private_key).unwrap(),
485+
timestamp: 1731593928,
486+
})
506487
.await
507488
.unwrap();
508489

0 commit comments

Comments
 (0)