use mako_engine::{
error::WorkflowError,
ids::DeadlineId,
outbox::PendingOutbox,
types::{MarktpartnerCode, MessageRef, Pruefidentifikator},
workflow::{CommandPayload, EventPayload, Workflow, WorkflowOutput},
};
pub const WORKFLOW_NAME: &str = "wim-technik-aenderung";
pub const ORDERS_PIDS: &[u32] = &[17003, 17007, 17008, 17118];
pub const ORDRSP_PIDS: &[u32] = &[19003, 19004, 19005, 19006, 19007, 19011, 19012];
const ORDRSP_BESTAETIGUNG_PIDS: &[u32] = &[19003, 19005, 19011];
pub const ANTWORT_WINDOW_LABEL: &str = "wim-technik-aenderung-antwort";
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum TechnikAenderungEvent {
AuftragGesendet {
orders_pid: Pruefidentifikator,
recipient: MarktpartnerCode,
location_id: Option<String>,
message_ref: MessageRef,
},
AuftragBestaetigt {
ordrsp_pid: Pruefidentifikator,
message_ref: MessageRef,
},
AuftragAbgelehnt {
ordrsp_pid: Pruefidentifikator,
reason: Option<String>,
message_ref: MessageRef,
},
DeadlineExpired {
deadline_id: DeadlineId,
label: Box<str>,
},
}
impl EventPayload for TechnikAenderungEvent {
fn event_type(&self) -> &'static str {
match self {
Self::AuftragGesendet { .. } => "TechnikAenderungAuftragGesendet",
Self::AuftragBestaetigt { .. } => "TechnikAenderungAuftragBestaetigt",
Self::AuftragAbgelehnt { .. } => "TechnikAenderungAuftragAbgelehnt",
Self::DeadlineExpired { .. } => "TechnikAenderungDeadlineExpired",
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuftragData {
pub orders_pid: Pruefidentifikator,
pub recipient: MarktpartnerCode,
pub location_id: Option<String>,
pub message_ref: MessageRef,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "status", content = "data")]
pub enum TechnikAenderungState {
New,
AuftragGesendet(AuftragData),
Bestaetigt {
auftrag: AuftragData,
ordrsp_pid: Pruefidentifikator,
},
Abgelehnt {
auftrag: AuftragData,
ordrsp_pid: Pruefidentifikator,
reason: Option<String>,
},
DeadlineExpired {
auftrag: AuftragData,
},
}
impl Default for TechnikAenderungState {
fn default() -> Self {
Self::New
}
}
impl TechnikAenderungState {
#[must_use]
pub fn label(&self) -> &'static str {
match self {
Self::New => "New",
Self::AuftragGesendet(_) => "AuftragGesendet",
Self::Bestaetigt { .. } => "Bestaetigt",
Self::Abgelehnt { .. } => "Abgelehnt",
Self::DeadlineExpired { .. } => "DeadlineExpired",
}
}
#[must_use]
pub fn is_terminal(&self) -> bool {
matches!(
self,
Self::Bestaetigt { .. } | Self::Abgelehnt { .. } | Self::DeadlineExpired { .. }
)
}
}
#[derive(Clone)]
pub enum TechnikAenderungCommand {
SendAuftrag {
orders_pid: Pruefidentifikator,
recipient: MarktpartnerCode,
location_id: Option<String>,
message_ref: MessageRef,
payload: serde_json::Value,
},
ReceiveOrdrsp {
ordrsp_pid: Pruefidentifikator,
reason: Option<String>,
message_ref: MessageRef,
},
TimeoutExpired {
deadline_id: DeadlineId,
label: Box<str>,
},
}
impl CommandPayload for TechnikAenderungCommand {}
pub struct WimTechnikAenderungWorkflow;
impl Workflow for WimTechnikAenderungWorkflow {
type State = TechnikAenderungState;
type Event = TechnikAenderungEvent;
type Command = TechnikAenderungCommand;
fn on_deadline(
deadline: &mako_engine::deadline::Deadline,
state: &Self::State,
) -> Option<Self::Command> {
match (deadline.label(), state) {
(ANTWORT_WINDOW_LABEL, TechnikAenderungState::AuftragGesendet(_)) => {
Some(TechnikAenderungCommand::TimeoutExpired {
deadline_id: deadline.deadline_id(),
label: deadline.label().into(),
})
}
_ => None,
}
}
fn apply(state: Self::State, event: &Self::Event) -> Self::State {
match event {
TechnikAenderungEvent::AuftragGesendet {
orders_pid,
recipient,
location_id,
message_ref,
} => TechnikAenderungState::AuftragGesendet(AuftragData {
orders_pid: *orders_pid,
recipient: recipient.clone(),
location_id: location_id.clone(),
message_ref: message_ref.clone(),
}),
TechnikAenderungEvent::AuftragBestaetigt { ordrsp_pid, .. } => match state {
TechnikAenderungState::AuftragGesendet(auftrag) => {
TechnikAenderungState::Bestaetigt {
auftrag,
ordrsp_pid: *ordrsp_pid,
}
}
other => other,
},
TechnikAenderungEvent::AuftragAbgelehnt {
ordrsp_pid, reason, ..
} => match state {
TechnikAenderungState::AuftragGesendet(auftrag) => {
TechnikAenderungState::Abgelehnt {
auftrag,
ordrsp_pid: *ordrsp_pid,
reason: reason.clone(),
}
}
other => other,
},
TechnikAenderungEvent::DeadlineExpired { .. } => match state {
TechnikAenderungState::AuftragGesendet(auftrag) => {
TechnikAenderungState::DeadlineExpired { auftrag }
}
other => other,
},
}
}
fn handle(
state: &Self::State,
command: Self::Command,
) -> Result<WorkflowOutput<Self::Event>, WorkflowError> {
match command {
TechnikAenderungCommand::SendAuftrag {
orders_pid,
recipient,
location_id,
message_ref,
payload,
} => {
if !matches!(state, TechnikAenderungState::New) {
return Err(WorkflowError::invalid_state("New", state.label()));
}
if !ORDERS_PIDS.contains(&orders_pid.as_u32()) {
return Err(WorkflowError::rejected(format!(
"not a valid WiM Technikänderung ORDERS PID: {orders_pid}",
)));
}
let event = TechnikAenderungEvent::AuftragGesendet {
orders_pid,
recipient: recipient.clone(),
location_id: location_id.clone(),
message_ref: message_ref.clone(),
};
let outbox = vec![PendingOutbox::new(
"ORDERS",
recipient.as_str(),
serde_json::json!({
"pid": orders_pid.as_u32(),
"location": location_id,
"orders_ref": message_ref.as_str(),
"payload": payload,
}),
)];
Ok(WorkflowOutput::with_outbox(vec![event], outbox))
}
TechnikAenderungCommand::ReceiveOrdrsp {
ordrsp_pid,
reason,
message_ref,
} => {
if state.is_terminal() {
return Ok(WorkflowOutput::events(vec![]));
}
if !matches!(state, TechnikAenderungState::AuftragGesendet(_)) {
return Err(WorkflowError::invalid_state(
"AuftragGesendet",
state.label(),
));
}
if !ORDRSP_PIDS.contains(&ordrsp_pid.as_u32()) {
return Err(WorkflowError::rejected(format!(
"not a valid WiM Technikänderung ORDRSP PID: {ordrsp_pid}",
)));
}
let event = if ORDRSP_BESTAETIGUNG_PIDS.contains(&ordrsp_pid.as_u32()) {
TechnikAenderungEvent::AuftragBestaetigt {
ordrsp_pid,
message_ref,
}
} else {
TechnikAenderungEvent::AuftragAbgelehnt {
ordrsp_pid,
reason,
message_ref,
}
};
Ok(vec![event].into())
}
TechnikAenderungCommand::TimeoutExpired { deadline_id, label } => {
if state.is_terminal() {
return Ok(WorkflowOutput::events(vec![]));
}
Ok(vec![TechnikAenderungEvent::DeadlineExpired { deadline_id, label }].into())
}
}
}
}