feat: constructor to create BillingServices obj

This commit is contained in:
Aravinth Manivannan 2024-12-18 18:10:27 +05:30
parent 0ffda24b2a
commit 44a3362679
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::sync::Arc;
use derive_builder::Builder;
use mockall::predicate::*;
@ -20,6 +21,23 @@ pub mod update_line_item_service;
pub mod update_store_service;
// TODO: 2. reset token number for store_id cronjob
use add_bill_service::AddBillServiceBuilder;
use add_line_item_service::AddLineItemServiceBuilder;
use add_store_service::AddStoreServiceBuilder;
use compute_bill_total_price_service::ComputeBillTotalPriceBillServiceBuilder;
use delete_bill_service::DeleteBillServiceBuilder;
use delete_line_item_service::DeleteLineItemServiceBuilder;
use update_bill_service::UpdateBillServiceBuilder;
use update_line_item_service::UpdateLineItemServiceBuilder;
use update_store_service::UpdateStoreServiceBuilder;
use crate::billing::application::port::output::db::{
bill_id_exists::BillIDExistsDBPortObj,
get_line_items_for_bill_id::GetLineItemsForBillIDDBPortObj,
line_item_id_exists::LineItemIDExistsDBPortObj, next_token_id::NextTokenIDDBPortObj,
store_id_exists::StoreIDExistsDBPortObj, store_name_exists::StoreNameExistsDBPortObj,
};
#[automock]
pub trait BillingServicesInterface: Send + Sync {
fn add_bill(&self) -> add_bill_service::AddBillServiceObj;
@ -35,6 +53,8 @@ pub trait BillingServicesInterface: Send + Sync {
) -> compute_bill_total_price_service::ComputeBillTotalPriceBillServiceObj;
}
pub type BillingServicesObj = std::sync::Arc<dyn BillingServicesInterface>;
#[derive(Clone, Builder)]
pub struct BillingServices {
add_bill: add_bill_service::AddBillServiceObj,
@ -81,3 +101,109 @@ impl BillingServicesInterface for BillingServices {
self.compute_total_price_for_bill.clone()
}
}
impl BillingServices {
pub fn new(
db_bill_id_exists: BillIDExistsDBPortObj,
db_line_item_id_exists: LineItemIDExistsDBPortObj,
db_store_id_exists: StoreIDExistsDBPortObj,
db_store_name_exists: StoreNameExistsDBPortObj,
db_get_line_items_for_bill_id: GetLineItemsForBillIDDBPortObj,
db_next_token_id: NextTokenIDDBPortObj,
) -> Self {
let services = BillingServicesBuilder::default()
.add_bill(Arc::new(
AddBillServiceBuilder::default()
.db_next_token_id(db_next_token_id.clone())
.db_bill_id_exists(db_bill_id_exists.clone())
.build()
.unwrap(),
))
.add_store(Arc::new(
AddStoreServiceBuilder::default()
.db_store_id_exists(db_store_id_exists.clone())
.db_store_name_exists(db_store_name_exists.clone())
.build()
.unwrap(),
))
.update_store(Arc::new(
UpdateStoreServiceBuilder::default()
.db_store_id_exists(db_store_id_exists.clone())
.db_store_name_exists(db_store_name_exists.clone())
.build()
.unwrap(),
))
.add_line_item(Arc::new(
AddLineItemServiceBuilder::default()
.db_line_item_id_exists(db_line_item_id_exists.clone())
.db_bill_id_exists(db_bill_id_exists.clone())
.build()
.unwrap(),
))
.update_line_item(Arc::new(
UpdateLineItemServiceBuilder::default()
.db_line_item_id_exists(db_line_item_id_exists.clone())
.db_bill_id_exists(db_bill_id_exists.clone())
.build()
.unwrap(),
))
.delete_line_item(Arc::new(
DeleteLineItemServiceBuilder::default()
.db_line_item_id_exists(db_line_item_id_exists.clone())
.build()
.unwrap(),
))
.update_bill(Arc::new(
UpdateBillServiceBuilder::default()
.db_bill_id_exists(db_bill_id_exists.clone())
.build()
.unwrap(),
))
.delete_bill(Arc::new(
DeleteBillServiceBuilder::default()
.db_bill_id_exists(db_bill_id_exists.clone())
.build()
.unwrap(),
))
.compute_total_price_for_bill(Arc::new(
ComputeBillTotalPriceBillServiceBuilder::default()
.db_bill_id_exists(db_bill_id_exists.clone())
.db_get_line_items_for_bill_id(db_get_line_items_for_bill_id)
.build()
.unwrap(),
))
.build()
.unwrap();
services
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
billing::application::port::output::db::{
bill_id_exists::mock_bill_id_exists_db_port_true,
get_line_items_for_bill_id::mock_get_line_items_for_bill_id_db_port_no_line_items,
line_item_id_exists::mock_line_item_id_exists_db_port_true,
next_token_id::mock_next_token_id_db_port,
store_id_exists::mock_store_id_exists_db_port_true,
store_name_exists::mock_store_name_exists_db_port_true,
},
db::migrate::*,
tests::bdd::IS_NEVER_CALLED,
};
#[test]
fn billing_services_builder_works() {
BillingServices::new(
mock_bill_id_exists_db_port_true(IS_NEVER_CALLED),
mock_line_item_id_exists_db_port_true(IS_NEVER_CALLED),
mock_store_id_exists_db_port_true(IS_NEVER_CALLED),
mock_store_name_exists_db_port_true(IS_NEVER_CALLED),
mock_get_line_items_for_bill_id_db_port_no_line_items(IS_NEVER_CALLED),
mock_next_token_id_db_port(IS_NEVER_CALLED),
);
}
}