1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Repository for Warehouse 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 Warehouse registration SQL
//! (4-layer rule: services orchestrate, repositories hold the SQL).
//!
//! Thin newtype over `backbone_orm::GenericCrudRepository<Warehouse, backbone_orm::SoftDelete>`.
//! All standard CRUD methods are available via `Deref`.
use anyhow::Result;
use sqlx::{PgPool, Row};
use uuid::Uuid;
use backbone_orm::org_scope;
use crate::domain::entity::Warehouse;
/// Table name for Warehouse entities
pub const TABLE_NAME: &str = "inventory.warehouses";
/// Repository for Warehouse entities.
///
/// All standard CRUD, soft-delete, pagination, and bulk methods are
/// provided automatically via `Deref` to `backbone_orm::GenericCrudRepository`.
pub struct WarehouseRepository(
backbone_orm::GenericCrudRepository<Warehouse, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for WarehouseRepository {
type Target = backbone_orm::GenericCrudRepository<Warehouse, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl WarehouseRepository {
/// Create a new repository instance.
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
/// The exact row a warehouse registration writes.
///
/// Mirrors the raw column shape rather than the `Warehouse` entity: `warehouse_type` is cast at the
/// DB (`$5::warehouse_type`) so a bad value fails as a DB error, not a deserialize panic.
pub struct NewWarehouseRow<'a> {
pub id: Uuid,
pub code: &'a str,
pub name: &'a str,
pub warehouse_type: &'a str,
pub parent_warehouse_id: Option<Uuid>,
pub is_group: bool,
}
/// Hand-written Warehouse SQL. Lives here per the module's 4-layer rule.
impl WarehouseRepository {
/// Register a warehouse.
///
/// A write outside any transaction: takes the pool and runs `org_scope::execute_scoped` so
/// the write rides the ambient org scope's request-dedicated connection — the composing
/// service sets it per request, and it satisfies the decorator's WITH CHECK fence
/// (ADR-0029). Undecorated (module tests) the insert runs plain.
///
/// Returns the raw `sqlx::Error` deliberately: the caller inspects it for a unique violation
/// (the composing decorator's org-scoped code arbiter) to turn a duplicate warehouse code
/// into a domain error.
pub async fn insert_warehouse(
&self,
pool: &PgPool,
w: &NewWarehouseRow<'_>,
) -> Result<(), sqlx::Error> {
org_scope::execute_scoped(
pool,
sqlx::query(
r#"INSERT INTO inventory.warehouses
(id, code, name, warehouse_type, parent_warehouse_id, is_group)
VALUES ($1,$2,$3,$4::warehouse_type,$5,$6)"#,
)
.bind(w.id).bind(w.code).bind(w.name).bind(w.warehouse_type)
.bind(w.parent_warehouse_id).bind(w.is_group),
)
.await?;
Ok(())
}
/// Resolve a warehouse-pivot candidate: does this warehouse exist and is it a concrete stock
/// warehouse (not a grouping node)? Backs the availability scope read's
/// fail-loud pivot validation — a typo'd warehouse id must refuse typed, never
/// project a silently-empty (all-sold-out) storefront. `Ok(None)` = no such live warehouse;
/// `Some(is_group)` = found, with the grouping flag for the caller to refuse.
///
/// Pool-based and org-scoped: the read rides the ambient org scope the composing service set
/// per request (ADR-0029).
pub async fn fetch_pivot_warehouse(
&self,
pool: &PgPool,
warehouse_id: Uuid,
) -> Result<Option<bool>, sqlx::Error> {
let row = org_scope::fetch_optional_row_scoped(
pool,
sqlx::query(
r#"SELECT is_group FROM inventory.warehouses
WHERE id = $1
AND (metadata->>'deleted_at') IS NULL"#,
)
.bind(warehouse_id),
)
.await?;
Ok(row.map(|r| r.get("is_group")))
}
}
backbone_core::impl_crud_repository!(WarehouseRepository, Warehouse, soft_delete);