Skip to content

Commit

Permalink
Add digestvalue extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Jan 13, 2024
1 parent 1f79f2e commit c3386c9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions openubl/api/src/system/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl ProjectContext {
document_id: Set(model.document_id.clone()),
supplier_id: Set(model.supplier_id.clone()),
voided_document_code: Set(model.voided_document_code.clone()),
digest_value: Set(model.digest_value.clone()),
sha256: Set(model.sha256.clone()),
..Default::default()
};
Expand Down
1 change: 1 addition & 0 deletions openubl/entity/src/ubl_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Model {
pub document_id: String,
pub supplier_id: String,
pub voided_document_code: Option<String>,
pub digest_value: Option<String>,
pub sha256: String,
}

Expand Down
2 changes: 2 additions & 0 deletions openubl/migration/src/m20240101_104121_create_ubl_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl MigrationTrait for Migration {
.not_null(),
)
.col(ColumnDef::new(UblDocument::VoidedDocumentCode).string())
.col(ColumnDef::new(UblDocument::DigestValue).string())
.col(ColumnDef::new(UblDocument::Sha256).string().not_null())
.foreign_key(
ForeignKey::create()
Expand Down Expand Up @@ -58,5 +59,6 @@ enum UblDocument {
DocumentId,
SupplierId,
VoidedDocumentCode,
DigestValue,
Sha256,
}
1 change: 1 addition & 0 deletions openubl/server/src/server/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub async fn upload_file(
document_id: file_metadata.document_id,
document_type: file_metadata.document_type,
voided_document_code: file_metadata.voided_line_document_type_code,
digest_value: file_metadata.digest_value,
sha256: file_sha256,
};

Expand Down
14 changes: 14 additions & 0 deletions xsender/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ mod tests {
document_id: String::from("F001-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand All @@ -160,6 +161,7 @@ mod tests {
document_id: String::from("F001-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -188,6 +190,7 @@ mod tests {
document_id: String::from("FC01-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -216,6 +219,7 @@ mod tests {
document_id: String::from("FD01-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -246,6 +250,7 @@ mod tests {
document_type: String::from(DocumentType::VOIDED_DOCUMENTS),
document_id: String::from("RA-20200328-1"),
ruc: String::from("123456789012"),
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -280,6 +285,7 @@ mod tests {
document_type: String::from(DocumentType::VOIDED_DOCUMENTS),
document_id: String::from("RA-20200328-1"),
ruc: String::from("123456789012"),
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -314,6 +320,7 @@ mod tests {
document_type: String::from(DocumentType::VOIDED_DOCUMENTS),
document_id: String::from("RA-20200328-1"),
ruc: String::from("123456789012"),
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -348,6 +355,7 @@ mod tests {
document_type: String::from(DocumentType::VOIDED_DOCUMENTS),
document_id: String::from("RA-20200328-1"),
ruc: String::from("123456789012"),
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -381,6 +389,7 @@ mod tests {
document_id: String::from("RA-20200328-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: Some(String::from(Catalog1::RETENCION)),
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -414,6 +423,7 @@ mod tests {
document_id: String::from("RA-20200328-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: Some(String::from(Catalog1::GUIA_REMISION_REMITENTE)),
digest_value: None,
};

let document_delivery = send_file_target(
Expand All @@ -438,6 +448,7 @@ mod tests {
document_id: String::from("S001-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -473,6 +484,7 @@ mod tests {
document_id: String::from("S001-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -504,6 +516,7 @@ mod tests {
document_id: String::from("R001-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down Expand Up @@ -535,6 +548,7 @@ mod tests {
document_id: String::from("D001-1"),
ruc: String::from("123456789012"),
voided_line_document_type_code: None,
digest_value: None,
};

let document_delivery = send_file_target(
Expand Down
1 change: 1 addition & 0 deletions xsender/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use regex::Regex;
use reqwest::Client;
use tera::Tera;

pub const DS: &str = "http://www.w3.org/2000/09/xmldsig#";
pub const CBC_NS: &str = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
pub const CAC_NS: &str = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
pub const SAC_NS: &str =
Expand Down
30 changes: 29 additions & 1 deletion xsender/src/ubl_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use xml::name::OwnedName;
use xml::reader::XmlEvent;
use xml::EventReader;

use crate::constants::{CAC_NS, CBC_NS, SAC_NS};
use crate::constants::{CAC_NS, CBC_NS, DS, SAC_NS};

pub struct UblFile {
pub file_content: String,
Expand All @@ -30,13 +30,15 @@ pub struct UblMetadata {
pub document_id: String,
pub ruc: String,
pub voided_line_document_type_code: Option<String>,
pub digest_value: Option<String>,
}

impl UblFile {
pub fn metadata(&self) -> anyhow::Result<UblMetadata> {
let event_reader = EventReader::from_str(&self.file_content);

enum Wrapper {
Signature,
AccountingSupplierParty,
VoidedDocumentsLine,
}
Expand All @@ -48,6 +50,7 @@ impl UblFile {
let mut document_id: Option<String> = None;
let mut ruc: Option<String> = None;
let mut voided_line_document_type_code: Option<String> = None;
let mut digest_value: Option<String> = None;

fn set_wrapper(
is_start: bool,
Expand All @@ -59,6 +62,13 @@ impl UblFile {
let local_name = name.local_name.as_str();

match (namespace, prefix, local_name) {
(Some(DS), Some("ds"), "Signature") => {
current_wrapper = if is_start {
Some(Wrapper::Signature)
} else {
None
}
}
(Some(CAC_NS), Some("cac"), "AccountingSupplierParty") => {
current_wrapper = if is_start {
Some(Wrapper::AccountingSupplierParty)
Expand Down Expand Up @@ -100,6 +110,11 @@ impl UblFile {
document_id = Some(current_text.clone());
}
}
(Some(Wrapper::Signature), Some(DS), Some("ds"), "DigestValue") => {
if digest_value.is_none() {
digest_value = Some(current_text.clone());
}
}
(
Some(Wrapper::AccountingSupplierParty),
Some(CBC_NS),
Expand Down Expand Up @@ -148,6 +163,7 @@ impl UblFile {
ruc: ruc.trim().to_string(),
voided_line_document_type_code: voided_line_document_type_code
.map(|e| e.trim().to_string()),
digest_value: digest_value.map(|e| e.trim().to_string()),
}),
_ => Err(anyhow!(
"document_type, document_id, and ruc were not found"
Expand Down Expand Up @@ -178,6 +194,10 @@ mod tests {
assert_eq!(metadata1.document_id, "F001-1");
assert_eq!(metadata1.ruc, "12345678912");
assert_eq!(metadata1.voided_line_document_type_code, None);
assert_eq!(
metadata1.digest_value.as_deref(),
Some("dUdjbsfAegqmI/hBripxnCim3XM=")
);

let file2 = UblFile::from_path(Path::new(&format!("{RESOURCES}/RA-20200328-1.xml")));
let metadata2 = file2.unwrap().metadata().unwrap();
Expand All @@ -188,12 +208,20 @@ mod tests {
metadata2.voided_line_document_type_code.as_deref(),
Some("01")
);
assert_eq!(
metadata2.digest_value.as_deref(),
Some("JGeIFq43Y1Ajg3sJsOCD8faxoIM=")
);

let file3 = UblFile::from_path(Path::new(&format!("{RESOURCES}/150101-F001-11.xml")));
let metadata3 = file3.unwrap().metadata().unwrap();
assert_eq!(metadata3.document_type, "Invoice");
assert_eq!(metadata3.document_id, "F001-11");
assert_eq!(metadata3.ruc, "20602516025");
assert_eq!(metadata3.voided_line_document_type_code, None);
assert_eq!(
metadata3.digest_value.as_deref(),
Some("kLZdGqZX/X/f+g9+BerSzCT1aSU=")
);
}
}

0 comments on commit c3386c9

Please sign in to comment.