feat: wrapup worker handler + auto-generate job
This commit is contained in:
40
crates/application/src/wrapup/event_handler.rs
Normal file
40
crates/application/src/wrapup/event_handler.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use async_trait::async_trait;
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::ports::EventHandler;
|
||||
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub struct WrapUpEventHandler {
|
||||
ctx: AppContext,
|
||||
}
|
||||
|
||||
impl WrapUpEventHandler {
|
||||
pub fn new(ctx: AppContext) -> Self {
|
||||
Self { ctx }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for WrapUpEventHandler {
|
||||
async fn handle(&self, event: &DomainEvent) -> Result<(), DomainError> {
|
||||
match event {
|
||||
DomainEvent::WrapUpRequested {
|
||||
wrapup_id,
|
||||
user_id,
|
||||
start_date,
|
||||
end_date,
|
||||
} => {
|
||||
super::handle_requested::execute(
|
||||
&self.ctx,
|
||||
wrapup_id.clone(),
|
||||
user_id.as_ref().map(|u| u.value()),
|
||||
*start_date,
|
||||
*end_date,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
51
crates/application/src/wrapup/handle_requested.rs
Normal file
51
crates/application/src/wrapup/handle_requested.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use crate::context::AppContext;
|
||||
use crate::wrapup::{compute, queries::ComputeWrapUpQuery};
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::models::wrapup::{DateRange, WrapUpScope, WrapUpStatus};
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
wrapup_id: WrapUpId,
|
||||
user_id: Option<uuid::Uuid>,
|
||||
start_date: chrono::NaiveDate,
|
||||
end_date: chrono::NaiveDate,
|
||||
) -> Result<(), DomainError> {
|
||||
ctx.repos
|
||||
.wrapup_repo
|
||||
.update_status(&wrapup_id, &WrapUpStatus::Generating, None)
|
||||
.await?;
|
||||
|
||||
let scope = match user_id {
|
||||
Some(uid) => WrapUpScope::User(uid),
|
||||
None => WrapUpScope::Global,
|
||||
};
|
||||
let query = ComputeWrapUpQuery {
|
||||
scope,
|
||||
date_range: DateRange {
|
||||
start: start_date,
|
||||
end: end_date,
|
||||
},
|
||||
};
|
||||
|
||||
match compute::execute(ctx, query).await {
|
||||
Ok(report) => {
|
||||
let json = serde_json::to_string(&report)
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?;
|
||||
ctx.repos.wrapup_repo.set_complete(&wrapup_id, &json).await?;
|
||||
ctx.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::WrapUpCompleted { wrapup_id })
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
ctx.repos
|
||||
.wrapup_repo
|
||||
.update_status(&wrapup_id, &WrapUpStatus::Failed, Some(&e.to_string()))
|
||||
.await?;
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
pub mod commands;
|
||||
pub mod compute;
|
||||
pub mod event_handler;
|
||||
pub mod generate;
|
||||
pub mod get_wrapup;
|
||||
pub mod handle_requested;
|
||||
pub mod list_wrapups;
|
||||
pub mod queries;
|
||||
|
||||
Reference in New Issue
Block a user