// SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later use uuid::Uuid; use super::BillingDBPostgresAdapter; use crate::billing::application::port::output::db::{bill_id_exists::*, errors::*}; #[async_trait::async_trait] impl BillIDExistsDBPort for BillingDBPostgresAdapter { async fn bill_id_exists(&self, bill_id: &Uuid) -> BillingDBResult { let res = sqlx::query!( "SELECT EXISTS ( SELECT 1 FROM cqrs_billing_bill_query WHERE bill_id = $1 );", bill_id ) .fetch_one(&self.pool) .await?; if let Some(x) = res.exists { Ok(x) } else { Ok(false) } } } #[cfg(test)] pub mod tests { use super::*; // use crate::billing::domain::add_product_command::tests::get_customizations; use crate::billing::domain::bill_aggregate::*; pub async fn create_dummy_bill(bill: &Bill, db: &BillingDBPostgresAdapter) { sqlx::query!( "INSERT INTO cqrs_billing_bill_query ( version, store_id, bill_id, token_number, deleted ) VALUES ( $1, $2, $3, $4, $5 );", 1, *bill.store_id(), *bill.bill_id(), *bill.token_number() as i32, bill.deleted().clone(), ) .execute(&db.pool) .await .unwrap(); } #[actix_rt::test] async fn test_postgres_product_exists() { let settings = crate::settings::tests::get_settings().await; settings.create_db().await; let db = super::BillingDBPostgresAdapter::new( sqlx::postgres::PgPool::connect(&settings.database.url) .await .unwrap(), ); let bill = Bill::default(); // state doesn't exist assert!(!db.bill_id_exists(bill.bill_id()).await.unwrap()); create_dummy_bill(&bill, &db).await; // state exists assert!(db.bill_id_exists(bill.bill_id()).await.unwrap()); settings.drop_db().await; } }