feat: wrapup worker handler + auto-generate job
This commit is contained in:
@@ -14,6 +14,7 @@ tokio = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
hex = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
[features]
|
||||
xlsx = []
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::Datelike;
|
||||
use domain::{errors::DomainError, events::DomainEvent, ports::PeriodicJob};
|
||||
|
||||
use crate::context::AppContext;
|
||||
@@ -85,3 +86,78 @@ impl PeriodicJob for EnrichmentStalenessJob {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WrapUpAutoGenerateJob {
|
||||
ctx: AppContext,
|
||||
}
|
||||
|
||||
impl WrapUpAutoGenerateJob {
|
||||
pub fn new(ctx: AppContext) -> Self {
|
||||
Self { ctx }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl PeriodicJob for WrapUpAutoGenerateJob {
|
||||
fn interval(&self) -> Duration {
|
||||
Duration::from_secs(86400)
|
||||
}
|
||||
|
||||
async fn run(&self) -> Result<(), DomainError> {
|
||||
let now = chrono::Utc::now().naive_utc();
|
||||
// Only run in January
|
||||
if now.month() != 1 {
|
||||
return Ok(());
|
||||
}
|
||||
let year = now.year() - 1;
|
||||
let start = chrono::NaiveDate::from_ymd_opt(year, 1, 1)
|
||||
.ok_or_else(|| DomainError::ValidationError("invalid date".into()))?;
|
||||
let end = chrono::NaiveDate::from_ymd_opt(year + 1, 1, 1)
|
||||
.ok_or_else(|| DomainError::ValidationError("invalid date".into()))?;
|
||||
|
||||
let users = self.ctx.repos.user.list_with_stats().await?;
|
||||
for user in &users {
|
||||
if user.total_movies > 0 {
|
||||
let existing = self
|
||||
.ctx
|
||||
.repos
|
||||
.wrapup_repo
|
||||
.find_existing(Some(user.user_id.value()), start, end)
|
||||
.await?;
|
||||
if existing.is_none() {
|
||||
let cmd = crate::wrapup::commands::RequestWrapUpCommand {
|
||||
user_id: Some(user.user_id.value()),
|
||||
start_date: start,
|
||||
end_date: end,
|
||||
};
|
||||
if let Err(e) = crate::wrapup::generate::execute(&self.ctx, cmd).await {
|
||||
tracing::warn!(
|
||||
"auto-generate wrapup for user {} failed: {e}",
|
||||
user.user_id.value()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Global wrap-up
|
||||
let existing = self
|
||||
.ctx
|
||||
.repos
|
||||
.wrapup_repo
|
||||
.find_existing(None, start, end)
|
||||
.await?;
|
||||
if existing.is_none() {
|
||||
let cmd = crate::wrapup::commands::RequestWrapUpCommand {
|
||||
user_id: None,
|
||||
start_date: start,
|
||||
end_date: end,
|
||||
};
|
||||
if let Err(e) = crate::wrapup::generate::execute(&self.ctx, cmd).await {
|
||||
tracing::warn!("auto-generate global wrapup failed: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
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