use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::ProcessingJob;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::JobStatus;
use crate::domain::entity::ProcessingJobType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateProcessingJobDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "file_id")]
pub file_id: Uuid,
#[serde(alias = "job_type")]
pub job_type: ProcessingJobType,
pub status: JobStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub priority: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "input_data")]
pub input_data: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "result_data")]
pub result_data: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "error_message")]
pub error_message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "started_at")]
pub started_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "completed_at")]
pub completed_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "retry_count")]
pub retry_count: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "max_retries")]
pub max_retries: i32,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateProcessingJobDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "file_id")]
pub file_id: Uuid,
#[serde(alias = "job_type")]
pub job_type: ProcessingJobType,
pub status: JobStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub priority: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "input_data")]
pub input_data: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "result_data")]
pub result_data: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "error_message")]
pub error_message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "started_at")]
pub started_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "completed_at")]
pub completed_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "retry_count")]
pub retry_count: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "max_retries")]
pub max_retries: i32,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchProcessingJobDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "file_id")]
pub file_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "job_type")]
pub job_type: Option<ProcessingJobType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<JobStatus>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", alias = "input_data")]
pub input_data: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none", alias = "result_data")]
pub result_data: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none", alias = "error_message")]
pub error_message: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "started_at")]
pub started_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "completed_at")]
pub completed_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "retry_count")]
pub retry_count: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "max_retries")]
pub max_retries: Option<i32>,
}
impl PatchProcessingJobDto {
pub fn has_changes(&self) -> bool {
self.file_id.is_some() || self.job_type.is_some() || self.status.is_some() || self.priority.is_some() || self.input_data.is_some() || self.result_data.is_some() || self.error_message.is_some() || self.started_at.is_some() || self.completed_at.is_some() || self.retry_count.is_some() || self.max_retries.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ProcessingJobResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub file_id: Uuid,
pub job_type: ProcessingJobType,
pub status: JobStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub priority: i32,
pub input_data: Option<serde_json::Value>,
pub result_data: Option<serde_json::Value>,
pub error_message: Option<String>,
pub started_at: Option<DateTime<Utc>>,
pub completed_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub retry_count: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub max_retries: i32,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ProcessingJobListResponseDto {
pub items: Vec<ProcessingJobResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl ProcessingJobListResponseDto {
pub fn new(items: Vec<ProcessingJobResponseDto>, 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,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ProcessingJobSummaryDto {
pub id: Uuid,
pub file_id: Uuid,
pub job_type: ProcessingJobType,
pub status: JobStatus,
pub created_at: Option<DateTime<Utc>>,
}
impl From<ProcessingJob> for ProcessingJobResponseDto {
fn from(entity: ProcessingJob) -> Self {
Self {
id: entity.id,
file_id: entity.file_id,
job_type: entity.job_type,
status: entity.status,
priority: entity.priority,
input_data: entity.input_data,
result_data: entity.result_data,
error_message: entity.error_message,
started_at: entity.started_at,
completed_at: entity.completed_at,
retry_count: entity.retry_count,
max_retries: entity.max_retries,
metadata: entity.metadata,
}
}
}
impl From<ProcessingJob> for ProcessingJobSummaryDto {
fn from(entity: ProcessingJob) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
file_id: entity.file_id,
job_type: entity.job_type,
status: entity.status,
created_at,
}
}
}
impl From<CreateProcessingJobDto> for ProcessingJob {
fn from(dto: CreateProcessingJobDto) -> Self {
Self {
id: Uuid::new_v4(),
file_id: dto.file_id,
job_type: dto.job_type,
status: dto.status,
priority: dto.priority,
input_data: dto.input_data,
result_data: dto.result_data,
error_message: dto.error_message,
started_at: dto.started_at,
completed_at: dto.completed_at,
retry_count: dto.retry_count,
max_retries: dto.max_retries,
metadata: AuditMetadata::default(),
}
}
}
impl From<&ProcessingJob> for ProcessingJobResponseDto {
fn from(entity: &ProcessingJob) -> Self {
Self {
id: entity.id.clone(),
file_id: entity.file_id.clone(),
job_type: entity.job_type.clone(),
status: entity.status.clone(),
priority: entity.priority.clone(),
input_data: entity.input_data.clone(),
result_data: entity.result_data.clone(),
error_message: entity.error_message.clone(),
started_at: entity.started_at.clone(),
completed_at: entity.completed_at.clone(),
retry_count: entity.retry_count.clone(),
max_retries: entity.max_retries.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateProcessingJobDto> for ProcessingJob {
fn from_create_dto(dto: CreateProcessingJobDto) -> backbone_core::ServiceResult<Self> {
Ok(ProcessingJob::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateProcessingJobDto> for ProcessingJob {
fn apply_update(mut self, dto: UpdateProcessingJobDto) -> backbone_core::ServiceResult<Self> {
self.file_id = dto.file_id;
self.job_type = dto.job_type;
self.status = dto.status;
self.priority = dto.priority;
self.input_data = dto.input_data;
self.result_data = dto.result_data;
self.error_message = dto.error_message;
self.started_at = dto.started_at;
self.completed_at = dto.completed_at;
self.retry_count = dto.retry_count;
self.max_retries = dto.max_retries;
Ok(self)
}
}