Skip to content

Commit

Permalink
create service with ref to db conn
Browse files Browse the repository at this point in the history
  • Loading branch information
mxfactorial committed Apr 24, 2024
1 parent 7f3629c commit 6985874
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion services/auto-confirm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async fn func(event: LambdaEvent<CognitoEventUserPoolsPreSignup>) -> Result<Valu
Error::from(e)
})?;

let svc = Service::new(conn);
let svc = Service::new(&conn);

let _ = svc
.create_account_from_cognito_trigger(fake_profile, decimal_balance, 0)
Expand Down
4 changes: 3 additions & 1 deletion services/balance-by-account/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ async fn handle_event(
) -> Result<String, StatusCode> {
let client_request = event.0;

let svc = Service::new(pool.get_conn().await);
let conn = pool.get_conn().await;

let svc = Service::new(&conn);

let account = client_request.account_name;

Expand Down
4 changes: 3 additions & 1 deletion services/request-approve/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ async fn handle_event(
) -> Result<axum::Json<IntraTransaction>, StatusCode> {
let client_request = event.0;

let svc = Service::new(pool.get_conn().await);
let conn = pool.get_conn().await;

let svc = Service::new(&conn);

let request_id = client_request.id.parse::<i32>().unwrap();

Expand Down
4 changes: 3 additions & 1 deletion services/request-by-id/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ async fn handle_event(
) -> Result<axum::Json<IntraTransaction>, StatusCode> {
let client_request = event.0;

let svc = Service::new(pool.get_conn().await);
let conn = pool.get_conn().await;

let svc = Service::new(&conn);

let request_id = client_request.id.parse::<i32>().unwrap();

Expand Down
16 changes: 11 additions & 5 deletions services/request-create/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
};
use httpclient::HttpClient as Client;
use pg::postgres::{ConnectionPool, DatabaseConnection, DB};
use service::Service;
use service::{Service, TxService};
use shutdown::shutdown_signal;
use std::{env, error::Error, net::ToSocketAddrs};
use thiserror::Error;
Expand Down Expand Up @@ -101,9 +101,9 @@ async fn test_values(req: IntraTransaction) -> Result<IntraTransaction, Box<dyn
Ok(response)
}

async fn create_request(
async fn create_request<'a>(
rule_tested: IntraTransaction,
svc: &mut Service<DatabaseConnection>,
svc: &mut TxService<'a, DatabaseConnection>,
) -> Result<Transaction, RequestCreateError> {
let accounts = rule_tested.transaction.transaction_items.list_accounts();

Expand Down Expand Up @@ -144,16 +144,22 @@ async fn handle_event(
})
.unwrap();

let mut svc = Service::new(pool.get_conn().await);
let mut tx_conn = pool.get_conn().await;

let inserted_transaction_request = create_request(rule_tested.clone(), &mut svc)
let mut tx_svc = TxService::new(&mut tx_conn);

let inserted_transaction_request = create_request(rule_tested.clone(), &mut tx_svc)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

// auth_account and approver_role are different on request-approve
let auth_account = rule_tested.clone().auth_account.unwrap();
let approver_role = rule_tested.transaction.author_role.unwrap();

let conn = pool.get_conn().await;

let svc = Service::new(&conn);

let approved_transaction_request = svc
.approve(auth_account, approver_role, inserted_transaction_request)
.await
Expand Down
4 changes: 3 additions & 1 deletion services/requests-by-account/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ async fn handle_event(
) -> Result<axum::Json<IntraTransactions>, StatusCode> {
let client_request = event.0;

let svc = Service::new(pool.get_conn().await);
let conn = pool.get_conn().await;

let svc = Service::new(&conn);

let account = client_request.account_name;

Expand Down
10 changes: 5 additions & 5 deletions services/rule/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ mod rules;
// used by lambda to test for service availability
const READINESS_CHECK_PATH: &str = "READINESS_CHECK_PATH";

async fn apply_transaction_item_rules(
svc: &Service<DatabaseConnection>,
async fn apply_transaction_item_rules<'a>(
svc: &'a Service<'a, DatabaseConnection>,
role_sequence: RoleSequence,
transaction_items: &TransactionItems,
) -> TransactionItems {
Expand Down Expand Up @@ -112,8 +112,8 @@ async fn apply_transaction_item_rules(
response
}

async fn apply_approval_rules(
svc: &Service<DatabaseConnection>,
async fn apply_approval_rules<'a>(
svc: &'a Service<'a, DatabaseConnection>,
role_sequence: RoleSequence,
transaction_items: &mut TransactionItems,
approval_time: &TZTime,
Expand Down Expand Up @@ -206,7 +206,7 @@ async fn apply_rules(
let conn = pool.get_conn().await;

// create service with conn
let svc = Service::new(conn);
let svc = Service::new(&conn);

let mut rule_applied_tr_items =
apply_transaction_item_rules(&svc, role_sequence, &transaction_items).await;
Expand Down
4 changes: 3 additions & 1 deletion services/transaction-by-id/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ async fn handle_event(
) -> Result<axum::Json<IntraTransaction>, StatusCode> {
let client_request = event.0;

let svc = Service::new(pool.get_conn().await);
let conn = pool.get_conn().await;

let svc = Service::new(&conn);

let transaction_id = client_request.id.parse::<i32>().unwrap();

Expand Down
4 changes: 3 additions & 1 deletion services/transactions-by-account/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ async fn handle_event(
) -> Result<axum::Json<IntraTransactions>, StatusCode> {
let client_request = event.0;

let svc = Service::new(pool.get_conn().await);
let conn = pool.get_conn().await;

let svc = Service::new(&conn);

let account = client_request.account_name;

Expand Down

0 comments on commit 6985874

Please sign in to comment.