backbone-inventory 0.17.2

Inventory: stock ledger of record (SLE) + moving-average valuation; emits COGS/asset AccountingPost (Delivery/Receipt -> GL)
Documentation
//! Package DTOs (Data Transfer Objects)
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! DTOs provide a clean separation between domain entities and API
//! representations, with OpenAPI documentation support.

use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc, NaiveDate};

#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;

use crate::domain::entity::Package;
use crate::domain::entity::AuditMetadata;

// =============================================================================
// Create DTO
// =============================================================================

/// Request DTO for creating a new Package
///
/// Used in POST requests to create new entities.
/// Excludes auto-generated fields (id, created_at, updated_at, deleted_at).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreatePackageDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "complete_name")]
    pub complete_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "location_id")]
    pub location_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "parent_package_id")]
    pub parent_package_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "parent_path")]
    pub parent_path: String,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "pack_date")]
    pub pack_date: Option<NaiveDate>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "package_type_id")]
    pub package_type_id: Option<Uuid>,
}

// =============================================================================
// Update DTO
// =============================================================================

/// Request DTO for full update of a Package
///
/// Used in PUT requests for full entity replacement.
/// All fields are required (except auto-generated ones).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePackageDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "complete_name")]
    pub complete_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "location_id")]
    pub location_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "parent_package_id")]
    pub parent_package_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "parent_path")]
    pub parent_path: String,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "pack_date")]
    pub pack_date: Option<NaiveDate>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "package_type_id")]
    pub package_type_id: Option<Uuid>,
}

// =============================================================================
// Patch DTO
// =============================================================================

/// Request DTO for partial update of a Package
///
/// Used in PATCH requests for partial updates.
/// All fields are optional - only provided fields will be updated.
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchPackageDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "complete_name")]
    pub complete_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "location_id")]
    pub location_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "parent_package_id")]
    pub parent_package_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "parent_path")]
    pub parent_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "pack_date")]
    pub pack_date: Option<NaiveDate>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "package_type_id")]
    pub package_type_id: Option<Uuid>,
}

impl PatchPackageDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.name.is_some() || self.complete_name.is_some() || self.location_id.is_some() || self.parent_package_id.is_some() || self.parent_path.is_some() || self.pack_date.is_some() || self.package_type_id.is_some()
    }
}

// =============================================================================
// Response DTO
// =============================================================================

/// Response DTO for Package entity
///
/// Used in API responses for single entity.
/// Includes all fields including metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PackageResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub complete_name: String,
    pub location_id: Option<Uuid>,
    pub parent_package_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub parent_path: String,
    pub pack_date: Option<NaiveDate>,
    pub package_type_id: Option<Uuid>,
    pub metadata: AuditMetadata,
}

// =============================================================================
// List Response DTO
// =============================================================================

/// Paginated list response for Package entities
///
/// Used in API responses for list endpoints.
/// Includes pagination metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PackageListResponseDto {
    /// List of items
    pub items: Vec<PackageResponseDto>,
    /// Total number of items (across all pages)
    pub total: u64,
    /// Current page number (1-indexed)
    pub page: u32,
    /// Number of items per page
    pub per_page: u32,
    /// Total number of pages
    pub total_pages: u32,
    /// Whether there is a next page
    pub has_next: bool,
    /// Whether there is a previous page
    pub has_prev: bool,
}

impl PackageListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<PackageResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
        let total_pages = if per_page > 0 {
            ((total as f64) / (per_page as f64)).ceil() as u32
        } else {
            0
        };
        Self {
            items,
            total,
            page,
            per_page,
            total_pages,
            has_next: page < total_pages,
            has_prev: page > 1,
        }
    }
}

/// Summary DTO for list views (compact version)
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PackageSummaryDto {
    pub id: Uuid,
    pub name: String,
    pub complete_name: String,
    pub location_id: Option<Uuid>,
    pub created_at: Option<DateTime<Utc>>,
}

// =============================================================================
// Conversions
// =============================================================================

impl From<Package> for PackageResponseDto {
    fn from(entity: Package) -> Self {
        Self {
            id: entity.id,
            name: entity.name,
            complete_name: entity.complete_name,
            location_id: entity.location_id,
            parent_package_id: entity.parent_package_id,
            parent_path: entity.parent_path,
            pack_date: entity.pack_date,
            package_type_id: entity.package_type_id,
            metadata: entity.metadata,
        }
    }
}

impl From<Package> for PackageSummaryDto {
    fn from(entity: Package) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            name: entity.name,
            complete_name: entity.complete_name,
            location_id: entity.location_id,
            created_at,
        }
    }
}

impl From<CreatePackageDto> for Package {
    fn from(dto: CreatePackageDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: dto.name,
            complete_name: dto.complete_name,
            location_id: dto.location_id,
            parent_package_id: dto.parent_package_id,
            parent_path: dto.parent_path,
            pack_date: dto.pack_date,
            package_type_id: dto.package_type_id,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&Package> for PackageResponseDto {
    fn from(entity: &Package) -> Self {
        Self {
            id: entity.id.clone(),
            name: entity.name.clone(),
            complete_name: entity.complete_name.clone(),
            location_id: entity.location_id.clone(),
            parent_package_id: entity.parent_package_id.clone(),
            parent_path: entity.parent_path.clone(),
            pack_date: entity.pack_date.clone(),
            package_type_id: entity.package_type_id.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

impl backbone_core::FromCreateDto<CreatePackageDto> for Package {
    fn from_create_dto(dto: CreatePackageDto) -> backbone_core::ServiceResult<Self> {
        Ok(Package::from(dto))
    }
}

impl backbone_core::ApplyUpdateDto<UpdatePackageDto> for Package {
    fn apply_update(mut self, dto: UpdatePackageDto) -> backbone_core::ServiceResult<Self> {
        self.name = dto.name;
        self.complete_name = dto.complete_name;
        self.location_id = dto.location_id;
        self.parent_package_id = dto.parent_package_id;
        self.parent_path = dto.parent_path;
        self.pack_date = dto.pack_date;
        self.package_type_id = dto.package_type_id;
        Ok(self)
    }
}

// =============================================================================
// Custom DTOs
// =============================================================================

// Add custom DTOs specific to Package here.
// This section will be preserved during regeneration.
// >>> END CUSTOM DTOs