backbone-inventory 0.17.2

Inventory: stock ledger of record (SLE) + moving-average valuation; emits COGS/asset AccountingPost (Delivery/Receipt -> GL)
Documentation
//! Repository for StockEntryItem entities
//!
//! Originally generated by metaphor-schema; now **user-owned** — this exact path is declared under
//! `user_owned` in `metaphor.codegen.yaml`, so the generator skips it wholesale. The custom methods
//! below hold the hand-written StockEntryItem SQL
//! (4-layer rule: services orchestrate, repositories hold the SQL).
//!
//! Thin newtype over `backbone_orm::GenericCrudRepository<StockEntryItem, backbone_orm::SoftDelete>`.
//! All standard CRUD methods are available via `Deref`.

use anyhow::Result;
use rust_decimal::Decimal;
use sqlx::PgPool;
use uuid::Uuid;

use crate::domain::entity::StockEntryItem;

/// Table name for StockEntryItem entities
pub const TABLE_NAME: &str = "inventory.stock_entry_items";

/// Repository for StockEntryItem entities.
///
/// All standard CRUD, soft-delete, pagination, and bulk methods are
/// provided automatically via `Deref` to `backbone_orm::GenericCrudRepository`.
pub struct StockEntryItemRepository(
    backbone_orm::GenericCrudRepository<StockEntryItem, backbone_orm::SoftDelete>,
);

impl std::ops::Deref for StockEntryItemRepository {
    type Target = backbone_orm::GenericCrudRepository<StockEntryItem, backbone_orm::SoftDelete>;
    fn deref(&self) -> &Self::Target { &self.0 }
}

impl StockEntryItemRepository {
    /// Create a new repository instance.
    pub fn new(pool: PgPool) -> Self {
        Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
    }
}

/// The exact row a transfer line insert writes.
pub struct NewStockEntryItemRow {
    pub id: Uuid,
    pub entry_id: Uuid,
    pub item_id: Uuid,
    pub quantity: Decimal,
}

/// Hand-written StockEntryItem SQL. Lives here per the module's 4-layer rule.
impl StockEntryItemRepository {
    /// Insert one transfer line. Takes the CALLER'S connection so it commits with the paired
    /// out/in movement; the caller has already relayed the ambient org scope onto it — don't
    /// re-bind here.
    pub async fn insert_item(
        &self,
        conn: &mut sqlx::PgConnection,
        l: &NewStockEntryItemRow,
    ) -> Result<(), sqlx::Error> {
        sqlx::query("INSERT INTO inventory.stock_entry_items (id, entry_id, item_id, quantity) VALUES ($1,$2,$3,$4)")
            .bind(l.id).bind(l.entry_id).bind(l.item_id).bind(l.quantity)
            .execute(conn)
            .await?;
        Ok(())
    }
}

backbone_core::impl_crud_repository!(StockEntryItemRepository, StockEntryItem, soft_delete);