Skip to content

Commit c43d734

Browse files
authored
Fix maturity date, issue date validation, add testing docs, up version (#479)
1 parent de01b4d commit c43d734

File tree

15 files changed

+80
-13
lines changed

15 files changed

+80
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.3.6
2+
3+
* Add validation for maturity date
4+
* Add docs for testing
5+
16
# 0.3.5
27

38
* Properly propagate and log errors when getting a file (e.g. an avatar)

crates/bcr-ebill-api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bcr-ebill-api"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
edition = "2024"
55

66
[lib]

crates/bcr-ebill-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bcr-ebill-core"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
edition = "2024"
55

66
[lib]

crates/bcr-ebill-core/src/bill/validation.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
},
99
},
1010
constants::{ACCEPT_DEADLINE_SECONDS, PAYMENT_DEADLINE_SECONDS, RECOURSE_DEADLINE_SECONDS},
11-
util,
11+
util::{self, date::start_of_day_as_timestamp},
1212
};
1313

1414
use super::{BillAction, BillIssueData, BillType, BillValidateActionData, RecourseReason};
@@ -34,8 +34,17 @@ pub fn validate_bill_issue(data: &BillIssueData) -> Result<(u64, BillType), Vali
3434
));
3535
}
3636

37-
util::date::date_string_to_timestamp(&data.issue_date, None)?;
38-
util::date::date_string_to_timestamp(&data.maturity_date, None)?;
37+
let issue_date_ts = util::date::date_string_to_timestamp(&data.issue_date, None)?;
38+
let maturity_date_ts = util::date::date_string_to_timestamp(&data.maturity_date, None)?;
39+
let start_of_day = start_of_day_as_timestamp(data.timestamp);
40+
41+
if maturity_date_ts < start_of_day {
42+
return Err(ValidationError::MaturityDateInThePast);
43+
}
44+
45+
if issue_date_ts > maturity_date_ts {
46+
return Err(ValidationError::IssueDateAfterMaturityDate);
47+
}
3948

4049
let bill_type = match data.t {
4150
0 => BillType::PromissoryNote,
@@ -591,8 +600,8 @@ mod tests {
591600
t: 0,
592601
country_of_issuing: "AT".into(),
593602
city_of_issuing: "Vienna".into(),
594-
issue_date: "2024-08-12".into(),
595-
maturity_date: "2024-11-12".into(),
603+
issue_date: "2025-08-12".into(),
604+
maturity_date: "2025-11-12".into(),
596605
drawee: TEST_PUB_KEY_SECP.into(),
597606
payee: OTHER_TEST_PUB_KEY_SECP.into(),
598607
sum: "500".into(),
@@ -618,6 +627,8 @@ mod tests {
618627
#[case::invalid_file_id( BillIssueData { file_upload_ids: vec!["".into()], ..valid_bill_issue_data() }, ValidationError::InvalidFileUploadId)]
619628
#[case::invalid_issue_date( BillIssueData { issue_date: "invaliddate".into(), ..valid_bill_issue_data() }, ValidationError::InvalidDate)]
620629
#[case::invalid_maturity_date( BillIssueData { maturity_date: "invaliddate".into(), ..valid_bill_issue_data() }, ValidationError::InvalidDate)]
630+
#[case::maturity_date_before_now( BillIssueData { maturity_date: "2004-01-12".into(), ..valid_bill_issue_data() }, ValidationError::MaturityDateInThePast)]
631+
#[case::issue_date_after_maturity_date( BillIssueData { issue_date: "2028-01-12".into(), ..valid_bill_issue_data() }, ValidationError::IssueDateAfterMaturityDate)]
621632
#[case::invalid_bill_type( BillIssueData { t: 5, ..valid_bill_issue_data() }, ValidationError::InvalidBillType)]
622633
#[case::drawee_equals_payee( BillIssueData { drawee: TEST_PUB_KEY_SECP.into(), payee: TEST_PUB_KEY_SECP.into(), ..valid_bill_issue_data() }, ValidationError::DraweeCantBePayee)]
623634
#[case::invalid_payee( BillIssueData { payee: "invalidkey".into(), ..valid_bill_issue_data() }, ValidationError::InvalidSecp256k1Key("invalidkey".into()))]

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ pub enum ValidationError {
180180
#[error("invalid date")]
181181
InvalidDate,
182182

183+
/// error returned if the maturity date is in the past
184+
#[error("maturity date can't be in the past")]
185+
MaturityDateInThePast,
186+
187+
/// error returned if the issue date is after the maturity date
188+
#[error("issue date after maturity date")]
189+
IssueDateAfterMaturityDate,
190+
183191
/// error returned if the currency was invalid
184192
#[error("invalid currency")]
185193
InvalidCurrency,

crates/bcr-ebill-core/src/util/date.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ pub fn seconds(timestamp: u64) -> DateTimeUtc {
2121
}
2222
}
2323

24+
/// Returns the start of day timestamp for the given timestamp
25+
pub fn start_of_day_as_timestamp(timestamp: u64) -> u64 {
26+
let dt = seconds(timestamp);
27+
let date = dt.date_naive();
28+
let end_of_day_time =
29+
NaiveTime::from_hms_micro_opt(00, 00, 00, 000_000).expect("is a valid time");
30+
let date_time = date.and_time(end_of_day_time);
31+
let date_utc = Utc.from_utc_datetime(&date_time);
32+
date_utc.timestamp() as u64
33+
}
34+
2435
/// Returns the end of day timestamp for the given timestamp
2536
pub fn end_of_day_as_timestamp(timestamp: u64) -> u64 {
2637
let dt = seconds(timestamp);
@@ -88,6 +99,16 @@ mod tests {
8899
);
89100
}
90101

102+
#[test]
103+
fn test_start_of_day() {
104+
let ts = Utc
105+
.with_ymd_and_hms(2025, 1, 15, 5, 10, 45)
106+
.unwrap()
107+
.timestamp() as u64;
108+
let start_of_day = start_of_day_as_timestamp(ts);
109+
assert!(start_of_day < ts,);
110+
}
111+
91112
#[test]
92113
fn test_end_of_day() {
93114
let ts = Utc

crates/bcr-ebill-persistence/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bcr-ebill-persistence"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
edition = "2024"
55

66
[lib]

crates/bcr-ebill-transport/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bcr-ebill-transport"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
edition = "2024"
55

66
[lib]

crates/bcr-ebill-wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bcr-ebill-wasm"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
edition = "2024"
55

66
[lib]

crates/bcr-ebill-wasm/main.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ async function triggerContact() {
218218
async function triggerBill() {
219219
let measured = measure(async () => {
220220
console.log("creating bill");
221+
222+
const now = new Date();
223+
const issue_date = now.toISOString().split('T')[0];
224+
const threeMonthsLater = new Date(now);
225+
threeMonthsLater.setMonth(now.getMonth() + 3);
226+
const maturity_date = threeMonthsLater.toISOString().split('T')[0];
227+
221228
let file_upload_id = document.getElementById("file_upload_id").value || undefined;
222229
let node_id = document.getElementById("node_id_bill").value;
223230
let identity = await identityApi.detail();
@@ -226,8 +233,8 @@ async function triggerBill() {
226233
t: 1,
227234
country_of_issuing: "AT",
228235
city_of_issuing: "Vienna",
229-
issue_date: "2024-01-22",
230-
maturity_date: "2024-06-22",
236+
issue_date,
237+
maturity_date,
231238
payee: identity.node_id,
232239
drawee: node_id,
233240
sum: "1500",

0 commit comments

Comments
 (0)