use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use super::WarrantyStatus;
use super::AuditMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct WarrantyClaimId(pub Uuid);
impl WarrantyClaimId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for WarrantyClaimId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for WarrantyClaimId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for WarrantyClaimId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<WarrantyClaimId> for Uuid {
fn from(id: WarrantyClaimId) -> Self { id.0 }
}
impl AsRef<Uuid> for WarrantyClaimId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for WarrantyClaimId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct WarrantyClaim {
pub id: Uuid,
pub customer_id: Option<Uuid>,
pub item_id: Uuid,
pub serial_no: Option<String>,
pub claim_date: DateTime<Utc>,
pub warranty_expiry: Option<DateTime<Utc>>,
pub is_under_warranty: bool,
pub status: WarrantyStatus,
pub issue_id: Option<Uuid>,
pub description: Option<String>,
pub resolution: Option<String>,
#[serde(default)]
#[sqlx(json)]
pub metadata: AuditMetadata,
}
impl WarrantyClaim {
pub fn builder() -> WarrantyClaimBuilder {
<WarrantyClaimBuilder as Default>::default()
}
pub fn new(item_id: Uuid, claim_date: DateTime<Utc>, is_under_warranty: bool, status: WarrantyStatus) -> Self {
Self {
id: Uuid::new_v4(),
customer_id: None,
item_id,
serial_no: None,
claim_date,
warranty_expiry: None,
is_under_warranty,
status,
issue_id: None,
description: None,
resolution: None,
metadata: AuditMetadata::default(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> WarrantyClaimId {
WarrantyClaimId(self.id)
}
pub fn created_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.created_at.as_ref()
}
pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.updated_at.as_ref()
}
pub fn is_deleted(&self) -> bool {
self.metadata.deleted_at.is_some()
}
pub fn is_active(&self) -> bool {
self.metadata.deleted_at.is_none()
}
pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.deleted_at.as_ref()
}
pub fn created_by(&self) -> Option<&Uuid> {
self.metadata.created_by.as_ref()
}
pub fn updated_by(&self) -> Option<&Uuid> {
self.metadata.updated_by.as_ref()
}
pub fn deleted_by(&self) -> Option<&Uuid> {
self.metadata.deleted_by.as_ref()
}
pub fn status(&self) -> &WarrantyStatus {
&self.status
}
pub fn with_customer_id(mut self, value: Uuid) -> Self {
self.customer_id = Some(value);
self
}
pub fn with_serial_no(mut self, value: String) -> Self {
self.serial_no = Some(value);
self
}
pub fn with_warranty_expiry(mut self, value: DateTime<Utc>) -> Self {
self.warranty_expiry = Some(value);
self
}
pub fn with_issue_id(mut self, value: Uuid) -> Self {
self.issue_id = Some(value);
self
}
pub fn with_description(mut self, value: String) -> Self {
self.description = Some(value);
self
}
pub fn with_resolution(mut self, value: String) -> Self {
self.resolution = Some(value);
self
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"customer_id" => {
if let Ok(v) = serde_json::from_value(value) { self.customer_id = v; }
}
"item_id" => {
if let Ok(v) = serde_json::from_value(value) { self.item_id = v; }
}
"serial_no" => {
if let Ok(v) = serde_json::from_value(value) { self.serial_no = v; }
}
"claim_date" => {
if let Ok(v) = serde_json::from_value(value) { self.claim_date = v; }
}
"warranty_expiry" => {
if let Ok(v) = serde_json::from_value(value) { self.warranty_expiry = v; }
}
"is_under_warranty" => {
if let Ok(v) = serde_json::from_value(value) { self.is_under_warranty = v; }
}
"status" => {
if let Ok(v) = serde_json::from_value(value) { self.status = v; }
}
"issue_id" => {
if let Ok(v) = serde_json::from_value(value) { self.issue_id = v; }
}
"description" => {
if let Ok(v) = serde_json::from_value(value) { self.description = v; }
}
"resolution" => {
if let Ok(v) = serde_json::from_value(value) { self.resolution = v; }
}
_ => {} }
}
}
}
impl super::Entity for WarrantyClaim {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"WarrantyClaim"
}
}
impl backbone_core::PersistentEntity for WarrantyClaim {
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.created_at
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.created_at = Some(ts);
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.updated_at
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.updated_at = Some(ts);
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.deleted_at
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
self.metadata.deleted_at = ts;
}
}
impl backbone_orm::EntityRepoMeta for WarrantyClaim {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("customer_id".to_string(), "uuid".to_string());
m.insert("item_id".to_string(), "uuid".to_string());
m.insert("issue_id".to_string(), "uuid".to_string());
m.insert("status".to_string(), "warranty_status".to_string());
m.insert("claim_date".to_string(), "timestamptz".to_string());
m.insert("warranty_expiry".to_string(), "timestamptz".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&[]
}
}
#[derive(Debug, Clone, Default)]
pub struct WarrantyClaimBuilder {
customer_id: Option<Uuid>,
item_id: Option<Uuid>,
serial_no: Option<String>,
claim_date: Option<DateTime<Utc>>,
warranty_expiry: Option<DateTime<Utc>>,
is_under_warranty: Option<bool>,
status: Option<WarrantyStatus>,
issue_id: Option<Uuid>,
description: Option<String>,
resolution: Option<String>,
}
impl WarrantyClaimBuilder {
pub fn customer_id(mut self, value: Uuid) -> Self {
self.customer_id = Some(value);
self
}
pub fn item_id(mut self, value: Uuid) -> Self {
self.item_id = Some(value);
self
}
pub fn serial_no(mut self, value: String) -> Self {
self.serial_no = Some(value);
self
}
pub fn claim_date(mut self, value: DateTime<Utc>) -> Self {
self.claim_date = Some(value);
self
}
pub fn warranty_expiry(mut self, value: DateTime<Utc>) -> Self {
self.warranty_expiry = Some(value);
self
}
pub fn is_under_warranty(mut self, value: bool) -> Self {
self.is_under_warranty = Some(value);
self
}
pub fn status(mut self, value: WarrantyStatus) -> Self {
self.status = Some(value);
self
}
pub fn issue_id(mut self, value: Uuid) -> Self {
self.issue_id = Some(value);
self
}
pub fn description(mut self, value: String) -> Self {
self.description = Some(value);
self
}
pub fn resolution(mut self, value: String) -> Self {
self.resolution = Some(value);
self
}
pub fn build(self) -> Result<WarrantyClaim, String> {
let item_id = self.item_id.ok_or_else(|| "item_id is required".to_string())?;
let claim_date = self.claim_date.ok_or_else(|| "claim_date is required".to_string())?;
Ok(WarrantyClaim {
id: Uuid::new_v4(),
customer_id: self.customer_id,
item_id,
serial_no: self.serial_no,
claim_date,
warranty_expiry: self.warranty_expiry,
is_under_warranty: self.is_under_warranty.unwrap_or(false),
status: self.status.unwrap_or_default(),
issue_id: self.issue_id,
description: self.description,
resolution: self.resolution,
metadata: AuditMetadata::default(),
})
}
}