use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::ProcessingJob;
use crate::infrastructure::persistence::ProcessingJobRepository;
#[derive(Debug, Clone)]
pub struct UpdateProcessingJobInput {
pub id: Uuid,
pub file_id: Option<Uuid>,
pub job_type: Option<ProcessingJobType>,
pub status: Option<JobStatus>,
pub priority: Option<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>>,
pub retry_count: Option<i32>,
pub max_retries: Option<i32>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct UpdateProcessingJobOutput {
pub processing_job: ProcessingJob,
}
pub struct UpdateProcessingJobUseCase {
repository: Arc<ProcessingJobRepository>,
}
impl UpdateProcessingJobUseCase {
pub fn new(repository: Arc<ProcessingJobRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateProcessingJobUseCase {
type Input = UpdateProcessingJobInput;
type Output = UpdateProcessingJobOutput;
async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
let mut entity = self.repository.find_by_id(input.id).await?
.ok_or_else(|| anyhow!("ProcessingJob not found"))?;
if let Some(val) = input.file_id {
entity.file_id = val;
}
if let Some(val) = input.job_type {
entity.job_type = val;
}
if let Some(val) = input.status {
entity.status = val;
}
if let Some(val) = input.priority {
entity.priority = val;
}
if input.input_data.is_some() {
entity.input_data = input.input_data;
}
if input.result_data.is_some() {
entity.result_data = input.result_data;
}
if input.error_message.is_some() {
entity.error_message = input.error_message;
}
if input.started_at.is_some() {
entity.started_at = input.started_at;
}
if input.completed_at.is_some() {
entity.completed_at = input.completed_at;
}
if let Some(val) = input.retry_count {
entity.retry_count = val;
}
if let Some(val) = input.max_retries {
entity.max_retries = val;
}
if input.metadata.is_some() {
entity.metadata = input.metadata;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateProcessingJobOutput { processing_job: updated })
}
}