backbone_inventory/infrastructure/persistence/
delivery_note_item_repository.rs1use anyhow::Result;
12use rust_decimal::Decimal;
13use sqlx::{PgPool, Row};
14use uuid::Uuid;
15
16use backbone_orm::org_scope;
17
18use crate::domain::entity::DeliveryNoteItem;
19
20pub const TABLE_NAME: &str = "inventory.delivery_note_items";
22
23pub struct DeliveryNoteItemRepository(
28 backbone_orm::GenericCrudRepository<DeliveryNoteItem, backbone_orm::SoftDelete>,
29);
30
31impl std::ops::Deref for DeliveryNoteItemRepository {
32 type Target = backbone_orm::GenericCrudRepository<DeliveryNoteItem, backbone_orm::SoftDelete>;
33 fn deref(&self) -> &Self::Target { &self.0 }
34}
35
36impl DeliveryNoteItemRepository {
37 pub fn new(pool: PgPool) -> Self {
39 Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
40 }
41}
42
43pub struct NewDeliveryItemRow {
46 pub id: Uuid,
47 pub delivery_id: Uuid,
48 pub item_id: Uuid,
49 pub quantity: Decimal,
50}
51
52pub struct DeliveryItemRow {
55 pub id: Uuid,
56 pub item_id: Uuid,
57 pub quantity: Decimal,
58}
59
60pub struct DeliveryCancelItemRow {
64 pub item_id: Uuid,
65 pub quantity: Decimal,
66 pub cogs_amount: Decimal,
67}
68
69impl DeliveryNoteItemRepository {
71 pub async fn insert_item(
74 &self,
75 conn: &mut sqlx::PgConnection,
76 l: &NewDeliveryItemRow,
77 ) -> Result<(), sqlx::Error> {
78 sqlx::query(
79 r#"INSERT INTO inventory.delivery_note_items (id, delivery_id, item_id, quantity, valuation_rate, cogs_amount)
80 VALUES ($1,$2,$3,$4,0,0)"#,
81 )
82 .bind(l.id).bind(l.delivery_id).bind(l.item_id).bind(l.quantity)
83 .execute(conn)
84 .await?;
85 Ok(())
86 }
87
88 pub async fn fetch_items(
93 &self,
94 pool: &PgPool,
95 delivery_id: Uuid,
96 ) -> Result<Vec<DeliveryItemRow>, sqlx::Error> {
97 let rows = org_scope::fetch_all_rows_scoped(
98 pool,
99 sqlx::query(
100 r#"SELECT id, item_id, quantity FROM inventory.delivery_note_items
101 WHERE delivery_id=$1 AND (metadata->>'deleted_at') IS NULL ORDER BY id"#,
102 )
103 .bind(delivery_id),
104 )
105 .await?;
106 Ok(rows.iter().map(|it| DeliveryItemRow {
107 id: it.get("id"), item_id: it.get("item_id"), quantity: it.get("quantity"),
108 }).collect())
109 }
110
111 pub async fn update_valuation(
114 &self,
115 conn: &mut sqlx::PgConnection,
116 line_id: Uuid,
117 valuation_rate: Decimal,
118 cogs_amount: Decimal,
119 ) -> Result<(), sqlx::Error> {
120 sqlx::query("UPDATE inventory.delivery_note_items SET valuation_rate=$2, cogs_amount=$3 WHERE id=$1")
121 .bind(line_id).bind(valuation_rate).bind(cogs_amount)
122 .execute(conn)
123 .await?;
124 Ok(())
125 }
126
127 pub async fn fetch_cancel_items(
130 &self,
131 pool: &PgPool,
132 delivery_id: Uuid,
133 ) -> Result<Vec<DeliveryCancelItemRow>, sqlx::Error> {
134 let rows = org_scope::fetch_all_rows_scoped(
135 pool,
136 sqlx::query(
137 r#"SELECT item_id, quantity, cogs_amount FROM inventory.delivery_note_items
138 WHERE delivery_id=$1 AND (metadata->>'deleted_at') IS NULL ORDER BY id"#,
139 )
140 .bind(delivery_id),
141 )
142 .await?;
143 Ok(rows.iter().map(|it| DeliveryCancelItemRow {
144 item_id: it.get("item_id"), quantity: it.get("quantity"), cogs_amount: it.get("cogs_amount"),
145 }).collect())
146 }
147}
148
149backbone_core::impl_crud_repository!(DeliveryNoteItemRepository, DeliveryNoteItem, soft_delete);