Skip to content

Commit

Permalink
task: refactor rules (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 authored Feb 24, 2024
1 parent 72e9e30 commit 84f27c5
Show file tree
Hide file tree
Showing 25 changed files with 557 additions and 117 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions openubl/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
openubl-entity = {path = "../entity"}
openubl-common = {path = "../common"}
openubl-migration = {path = "../migration"}
openubl-storage = { path = "../storage" }

xsender = {path = "../../xsender"}

Expand Down
52 changes: 52 additions & 0 deletions openubl/api/src/system/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ impl ProjectContext {
}

// Documents
pub async fn get_document(
&self,
id: i32,
tx: Transactional<'_>,
) -> Result<Option<UblDocumentContext>, Error> {
Ok(entity::ubl_document::Entity::find()
.join(
JoinType::InnerJoin,
entity::credentials::Relation::Project.def(),
)
.filter(entity::credentials::Column::Id.eq(id))
.filter(entity::credentials::Column::ProjectId.eq(self.project.id))
.one(&self.system.connection(tx))
.await?
.map(|e| (&self.system, e).into()))
}

pub async fn get_document_by_ubl_params(
&self,
Expand Down Expand Up @@ -205,9 +221,31 @@ impl ProjectContext {
.map(|e| (&self.system, e).into()))
}

pub async fn get_credential_for_supplier_id(
&self,
supplier_id: &str,
tx: Transactional<'_>,
) -> Result<Option<CredentialsContext>, Error> {
Ok(entity::credentials::Entity::find()
.join(
JoinType::InnerJoin,
entity::credentials::Relation::Project.def(),
)
.join(
JoinType::InnerJoin,
entity::credentials::Relation::SendRule.def(),
)
.filter(entity::send_rule::Column::SupplierId.eq(supplier_id))
.filter(entity::credentials::Column::ProjectId.eq(self.project.id))
.one(&self.system.connection(tx))
.await?
.map(|e| (&self.system, e).into()))
}

pub async fn create_credentials(
&self,
model: &entity::credentials::Model,
supplier_ids: &[String],
tx: Transactional<'_>,
) -> Result<CredentialsContext, Error> {
let entity = entity::credentials::ActiveModel {
Expand All @@ -221,6 +259,20 @@ impl ProjectContext {
};

let result = entity.insert(&self.system.connection(tx)).await?;

let rules = supplier_ids
.iter()
.map(|supplier_id| entity::send_rule::ActiveModel {
supplier_id: Set(supplier_id.clone()),
credentials_id: Set(result.id),
project_id: Set(self.project.id),
..Default::default()
})
.collect::<Vec<_>>();
let _rules = entity::send_rule::Entity::insert_many(rules)
.exec(&self.system.connection(tx))
.await?;

Ok((&self.system, result).into())
}

Expand Down
30 changes: 15 additions & 15 deletions openubl/api/src/system/ubl_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ impl From<(&InnerSystem, entity::ubl_document::Model)> for UblDocumentContext {
}
}

// impl UblDocumentContext {
// fn send_to_sunat(credentials: &entity::credentials::Model) {
// FileSender {
// urls: Urls {
// invoice: credentials.url_invoice.clone(),
// perception_retention: credentials.url_perception_retention.clone(),
// despatch: credentials.url_despatch.clone(),
// },
// credentials: Credentials {
// username: credentials.username_sol.clone(),
// password: credentials.password_sol.clone(),
// },
// };
// }
// }
impl UblDocumentContext {
// fn send_to_sunat(credentials: &entity::credentials::Model) {
// FileSender {
// urls: Urls {
// invoice: credentials.url_invoice.clone(),
// perception_retention: credentials.url_perception_retention.clone(),
// despatch: credentials.url_despatch.clone(),
// },
// credentials: Credentials {
// username: credentials.username_sol.clone(),
// password: credentials.password_sol.clone(),
// },
// };
// }
}
6 changes: 5 additions & 1 deletion openubl/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use std::process::{ExitCode, Termination};

use clap::Parser;

mod sender;

#[allow(clippy::large_enum_variant)]
#[derive(clap::Subcommand, Debug)]
pub enum Command {
Server(openubl_server::Run),
Server(openubl_server::ServerRun),
// Sender(SenderRun),
}

#[derive(clap::Parser, Debug)]
Expand Down Expand Up @@ -41,6 +44,7 @@ impl Cli {
async fn run_command(self) -> anyhow::Result<ExitCode> {
match self.command {
Command::Server(run) => run.run().await,
// Command::Sender(run) => run.run().await
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions openubl/cli/src/sender.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(clap::Args, Debug)]
pub struct SenderRun {
#[arg(id = "input", long)]
pub input: Vec<String>,
}

impl SenderRun {
// pub async fn run(self) -> anyhow::Result<ExitCode> {
// Ok(ExitCode::SUCCESS)
// }
}
3 changes: 1 addition & 2 deletions openubl/entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ edition = "2021"
publish = false

[dependencies]
sea-orm = { version = "0.12.10", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"] }
serde = { version = "1.0.193", features = ["derive"] }
sea-orm = { version = "0.12.10", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"] }
18 changes: 12 additions & 6 deletions openubl/entity/src/credentials.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "credentials")]
pub struct Model {
#[serde(skip_deserializing)]
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub url_invoice: String,
pub url_despatch: String,
pub url_perception_retention: String,
pub username_sol: String,
pub password_sol: String,
pub client_id: String,
pub client_secret: String,
pub url_invoice: String,
pub url_despatch: String,
pub url_perception_retention: String,
pub project_id: i32,
}

Expand All @@ -30,6 +28,8 @@ pub enum Relation {
on_delete = "Cascade"
)]
Project,
#[sea_orm(has_many = "super::send_rule::Entity")]
SendRule,
}

impl Related<super::project::Entity> for Entity {
Expand All @@ -38,4 +38,10 @@ impl Related<super::project::Entity> for Entity {
}
}

impl Related<super::send_rule::Entity> for Entity {
fn to() -> RelationDef {
Relation::SendRule.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
1 change: 1 addition & 0 deletions openubl/entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod credentials;
pub mod keystore;
pub mod keystore_config;
pub mod project;
pub mod send_rule;
pub mod ubl_document;
pub mod user_role;
1 change: 1 addition & 0 deletions openubl/entity/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod credentials;
pub mod keystore;
pub mod keystore_config;
pub mod project;
pub mod send_rule;
pub mod ubl_document;
pub mod user_role;
1 change: 1 addition & 0 deletions openubl/entity/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub use super::credentials::Entity as Credentials;
pub use super::keystore::Entity as Keystore;
pub use super::keystore_config::Entity as KeystoreConfig;
pub use super::project::Entity as Project;
pub use super::send_rule::Entity as SendRule;
pub use super::ubl_document::Entity as UblDocument;
pub use super::user_role::Entity as UserRole;
12 changes: 9 additions & 3 deletions openubl/entity/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "project")]
pub struct Model {
#[serde(skip_deserializing)]
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
Expand All @@ -19,6 +17,8 @@ pub enum Relation {
Credentials,
#[sea_orm(has_many = "super::keystore::Entity")]
Keystore,
#[sea_orm(has_many = "super::send_rule::Entity")]
SendRule,
#[sea_orm(has_many = "super::ubl_document::Entity")]
UblDocument,
#[sea_orm(has_many = "super::user_role::Entity")]
Expand All @@ -37,6 +37,12 @@ impl Related<super::keystore::Entity> for Entity {
}
}

impl Related<super::send_rule::Entity> for Entity {
fn to() -> RelationDef {
Relation::SendRule.def()
}
}

impl Related<super::ubl_document::Entity> for Entity {
fn to() -> RelationDef {
Relation::UblDocument.def()
Expand Down
47 changes: 47 additions & 0 deletions openubl/entity/src/send_rule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "send_rule")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub supplier_id: String,
pub credentials_id: i32,
pub project_id: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::credentials::Entity",
from = "Column::CredentialsId",
to = "super::credentials::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
Credentials,
#[sea_orm(
belongs_to = "super::project::Entity",
from = "Column::ProjectId",
to = "super::project::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
Project,
}

impl Related<super::credentials::Entity> for Entity {
fn to() -> RelationDef {
Relation::Credentials.def()
}
}

impl Related<super::project::Entity> for Entity {
fn to() -> RelationDef {
Relation::Project.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
4 changes: 1 addition & 3 deletions openubl/entity/src/ubl_document.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "ubl_document")]
pub struct Model {
#[serde(skip_deserializing)]
#[sea_orm(primary_key)]
pub id: i32,
pub project_id: i32,
Expand Down
2 changes: 2 additions & 0 deletions openubl/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod m20240101_104121_create_ubl_document;
mod m20240113_213636_create_keystore;
mod m20240113_213657_create_keystore_config;
mod m20240114_154538_create_credentials;
mod m20240117_142858_create_send_rule;
pub struct Migrator;

#[async_trait::async_trait]
Expand All @@ -18,6 +19,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240113_213636_create_keystore::Migration),
Box::new(m20240113_213657_create_keystore_config::Migration),
Box::new(m20240114_154538_create_credentials::Migration),
Box::new(m20240117_142858_create_send_rule::Migration),
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl MigrationTrait for Migration {
}

#[derive(DeriveIden)]
enum Credentials {
pub enum Credentials {
Table,
Id,
Name,
Expand Down
Loading

0 comments on commit 84f27c5

Please sign in to comment.