mako-gpke 0.20.0

GPKE process engine for German electricity market communication (Lieferbeginn, Lieferende, Netznutzungsabrechnung)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! GPKE Teil 2 § 3.1 — the NB's Bearbeitungsstand on Abrechnungsdaten.
//!
//! The Netzbetreiber sends the Abrechnungsdaten (Netznutzungs- and
//! Bilanzkreisabrechnung) to the Lieferant; the LF answers with a
//! Qualitätsrückmeldung or orders a change, and the NB owes a
//! **Bearbeitungsstandsmeldung** back.
//!
//! | PID | Process (AHB) | Direction |
//! |---|---|---|
//! | 55156 | Rückmeldung / Anfrage Abr.-Daten BK-Abr. verb. MaLo | LF → NB |
//! | 55220 | Rückmeldung / Anfrage Abr.-Daten Netznutzungsabrechnung | LF → NB |
//! | 55673 | Rückmeldung / Anfrage Abr.-Daten BK-Abr. erz. MaLo | LF → NB |
//! | 21047 | Bearbeitungsstandsmeldung (IFTSTA) | NB → LF |
//!
//! **Frist: der 2. Werktag nach dem ÜT** (§§ 3.1.1.2 / 3.1.2.2 / 3.1.3.2
//! Prozessschritt 2/3), from `mako_fristen::antwort`.
//!
//! # The answer is not an agreement
//!
//! `E_0595` „Bestellung prüfen" decides it, and its clusters are „Änderung der
//! Daten" / „keine Änderung der Daten" — whether a Stammdatenänderung follows,
//! not whether the LF's request was granted. `A06` sits in „Änderung der Daten"
//! while stating that no change is made, because the NB still sends its own data
//! back. The IFTSTA 21047 goes out either way, so nothing here derives a PID
//! from the cluster; it carries the code and the operator or ERP picks it.
//!
//! # Regulatory basis
//!
//! - BK6-24-174 GPKE Teil 2 §§ 3.1.1 / 3.1.2 / 3.1.3
//! - Entscheidungsbaum-Diagramme und Codelisten 4.3, Kap. 6.17.1 (`E_0595`)

use mako_engine::{
    deadline::Deadline,
    error::WorkflowError,
    ids::DeadlineId,
    outbox::PendingOutbox,
    types::{MaLo, MarktpartnerCode, MessageRef, Pruefidentifikator},
    workflow::{CommandPayload, EventPayload, Workflow, WorkflowOutput},
};

// ── PID set ───────────────────────────────────────────────────────────────────

/// Inbound PIDs this workflow answers — all LF → NB, GPKE Teil 2 § 3.1.
pub const ABRECHNUNGSDATEN_PIDS: &[u32] = &[55_156, 55_220, 55_673];

/// The IFTSTA Bearbeitungsstandsmeldung the NB answers with.
pub const BEARBEITUNGSSTAND_PID: u32 = 21_047;

/// The Entscheidungsbaum that decides the answer — `mako_pruefung::codes`
/// publishes its codes, and the tests hold the two in step.
pub const BESTELLUNG_EBD: &str = "E_0595";

/// Stable workflow name for process routing.
pub const WORKFLOW_NAME: &str = "gpke-abrechnungsdaten";

/// Deadline label for the 2-Werktage Bearbeitungsstand window.
pub const BEARBEITUNGSSTAND_WINDOW_LABEL: &str = "gpke-abrechnungsdaten-bearbeitungsstand";

// ── Events ────────────────────────────────────────────────────────────────────

/// Events emitted by the Abrechnungsdaten workflow.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum AbrechnungsdatenEvent {
    /// A Rückmeldung / Bestellung arrived from the Lieferant.
    RueckmeldungErhalten {
        /// 55156, 55220 or 55673.
        pruefidentifikator: Pruefidentifikator,
        /// MP-ID of the Lieferant.
        sender: MarktpartnerCode,
        /// MP-ID of this Netzbetreiber.
        receiver: MarktpartnerCode,
        /// The Marktlokation the Abrechnungsdaten belong to.
        location_id: MaLo,
        /// EDIFACT document date (`YYYYMMDD`).
        document_date: String,
        /// EDIFACT message reference.
        message_ref: MessageRef,
        /// `true` if the message passed AHB validation.
        validation_passed: bool,
        /// Validation issues, for the `Abgebrochen` event.
        validation_errors: Vec<String>,
    },
    /// The IFTSTA 21047 Bearbeitungsstandsmeldung went out.
    BearbeitungsstandGesendet {
        /// The `E_0595` code it states.
        antwort_code: String,
        /// `true` when a Stammdatenänderung follows — the code's own cluster.
        sendet_stammdatenaenderung: bool,
        /// `FTX` Bemerkung, where one was given.
        bemerkung: Option<String>,
    },
    /// The message could not be processed.
    Abgebrochen {
        /// Why.
        reason: String,
    },
    /// The 2-Werktage window expired without a Bearbeitungsstand.
    DeadlineExpired {
        /// Which deadline.
        deadline_id: DeadlineId,
        /// Its label.
        label: Box<str>,
    },
}

impl EventPayload for AbrechnungsdatenEvent {
    fn event_type(&self) -> &'static str {
        match self {
            Self::RueckmeldungErhalten { .. } => "AbrechnungsdatenRueckmeldungErhalten",
            Self::BearbeitungsstandGesendet { .. } => "AbrechnungsdatenBearbeitungsstandGesendet",
            Self::Abgebrochen { .. } => "AbrechnungsdatenAbgebrochen",
            Self::DeadlineExpired { .. } => "AbrechnungsdatenDeadlineExpired",
        }
    }
}

// ── State ─────────────────────────────────────────────────────────────────────

/// What the process knows about the inbound Rückmeldung.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AbrechnungsdatenData {
    /// The inbound PID.
    pub pruefidentifikator: Pruefidentifikator,
    /// The Lieferant that sent it.
    pub sender: MarktpartnerCode,
    /// This Netzbetreiber.
    pub receiver: MarktpartnerCode,
    /// The Marktlokation.
    pub location_id: MaLo,
    /// EDIFACT document date.
    pub document_date: String,
    /// EDIFACT message reference.
    pub message_ref: MessageRef,
}

/// State of an Abrechnungsdaten Bearbeitungsstand process.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(tag = "status", content = "data")]
pub enum AbrechnungsdatenState {
    /// No events yet.
    #[default]
    New,
    /// Rückmeldung received; the 2-Werktage window is running.
    Eingegangen(AbrechnungsdatenData),
    /// The Bearbeitungsstand went out. Terminal.
    Beantwortet {
        /// The inbound message.
        data: AbrechnungsdatenData,
        /// The `E_0595` code.
        antwort_code: String,
    },
    /// The process ended without an answer. Terminal.
    Abgebrochen {
        /// Why.
        reason: String,
    },
}

impl AbrechnungsdatenState {
    /// Stable label for logs and error messages.
    #[must_use]
    pub const fn label(&self) -> &'static str {
        match self {
            Self::New => "New",
            Self::Eingegangen(_) => "Eingegangen",
            Self::Beantwortet { .. } => "Beantwortet",
            Self::Abgebrochen { .. } => "Abgebrochen",
        }
    }

    /// `true` when no further command can move this process.
    #[must_use]
    pub const fn is_terminal(&self) -> bool {
        matches!(self, Self::Beantwortet { .. } | Self::Abgebrochen { .. })
    }
}

// ── Commands ──────────────────────────────────────────────────────────────────

/// Commands the Abrechnungsdaten workflow accepts.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "command", content = "data")]
pub enum AbrechnungsdatenCommand {
    /// An inbound 55156 / 55220 / 55673 arrived.
    ReceiveRueckmeldung {
        /// The inbound PID.
        pid: Pruefidentifikator,
        /// The Lieferant.
        sender: MarktpartnerCode,
        /// This Netzbetreiber.
        receiver: MarktpartnerCode,
        /// The Marktlokation.
        location_id: MaLo,
        /// EDIFACT document date.
        document_date: String,
        /// EDIFACT message reference.
        message_ref: MessageRef,
        /// AHB validation outcome.
        validation_passed: bool,
        /// Validation issues.
        validation_errors: Vec<String>,
    },
    /// Send the IFTSTA 21047 Bearbeitungsstandsmeldung.
    ///
    /// The `E_0595` code is the caller's — `makod` resolves it against the tree
    /// and refuses one the tree does not publish. Its cluster says whether a
    /// Stammdatenänderung follows, which the NB then sends through
    /// `gpke-stammdatenaenderung`; it does **not** select the answer PID, since
    /// 21047 carries both outcomes.
    SendBearbeitungsstand {
        /// The `E_0595` Antwortcode.
        antwort_code: String,
        /// `true` when the code's cluster is „Änderung der Daten".
        sendet_stammdatenaenderung: bool,
        /// `FTX` Bemerkung — required alongside `A99`.
        bemerkung: Option<String>,
    },
    /// A registered deadline fired.
    TimeoutExpired {
        /// Which deadline.
        deadline_id: DeadlineId,
        /// Its label.
        label: Box<str>,
    },
}

impl CommandPayload for AbrechnungsdatenCommand {}

// ── Workflow ──────────────────────────────────────────────────────────────────

/// The NB's Bearbeitungsstand on inbound Abrechnungsdaten (GPKE Teil 2 § 3.1).
#[derive(Debug, Clone, Copy, Default)]
pub struct GpkeAbrechnungsdatenWorkflow;

impl Workflow for GpkeAbrechnungsdatenWorkflow {
    type State = AbrechnungsdatenState;
    type Command = AbrechnungsdatenCommand;
    type Event = AbrechnungsdatenEvent;

    fn on_deadline(deadline: &Deadline, state: &Self::State) -> Option<Self::Command> {
        // Only the **business** window ends this process. `makod` registers a
        // second deadline on the same stream — the APERAK 45-minute *delivery*
        // window — which the outbox worker discharges the moment the APERAK
        // goes out. Accepting any label made a late technical acknowledgement
        // fail the Abrechnungsdaten process, which is neither what the Frist
        // means nor what GPKE Teil 2 §§ 3.1.1.2 / 3.1.2.2 / 3.1.3.2 sanction.
        (deadline.label() == BEARBEITUNGSSTAND_WINDOW_LABEL && !state.is_terminal()).then(|| {
            AbrechnungsdatenCommand::TimeoutExpired {
                deadline_id: deadline.deadline_id(),
                label: deadline.label().into(),
            }
        })
    }

    fn handle(
        state: &Self::State,
        command: Self::Command,
    ) -> Result<WorkflowOutput<Self::Event>, WorkflowError> {
        match command {
            AbrechnungsdatenCommand::ReceiveRueckmeldung {
                pid,
                sender,
                receiver,
                location_id,
                document_date,
                message_ref,
                validation_passed,
                validation_errors,
            } => {
                if !matches!(state, AbrechnungsdatenState::New) {
                    return Err(WorkflowError::invalid_state("New", state.label()));
                }
                if !ABRECHNUNGSDATEN_PIDS.contains(&pid.as_u32()) {
                    return Err(WorkflowError::other(format!(
                        "PID {} is not a GPKE Teil 2 § 3.1 Abrechnungsdaten-Rückmeldung",
                        pid.as_u32()
                    )));
                }
                Ok(vec![AbrechnungsdatenEvent::RueckmeldungErhalten {
                    pruefidentifikator: pid,
                    sender,
                    receiver,
                    location_id,
                    document_date,
                    message_ref,
                    validation_passed,
                    validation_errors,
                }]
                .into())
            }

            AbrechnungsdatenCommand::SendBearbeitungsstand {
                antwort_code,
                sendet_stammdatenaenderung,
                bemerkung,
            } => {
                let AbrechnungsdatenState::Eingegangen(data) = state else {
                    return Err(WorkflowError::invalid_state("Eingegangen", state.label()));
                };
                let events = vec![AbrechnungsdatenEvent::BearbeitungsstandGesendet {
                    antwort_code: antwort_code.clone(),
                    sendet_stammdatenaenderung,
                    bemerkung: bemerkung.clone(),
                }];
                // The answer is a message, not just an event.
                let outbox = vec![PendingOutbox::new(
                    "IFTSTA",
                    data.sender.as_str(),
                    serde_json::json!({
                        "pid":          BEARBEITUNGSSTAND_PID,
                        "anfrage_pid":  data.pruefidentifikator.as_u32(),
                        "sender":       data.receiver.as_str(),
                        "receiver":     data.sender.as_str(),
                        "malo":         data.location_id.as_str(),
                        // `SG15 STS` — the E_0595 code and the tree it comes from.
                        "antwort_code": antwort_code,
                        "antwort_codeliste":  BESTELLUNG_EBD,
                        // „Änderung der Daten" means a Stammdatenänderung
                        // follows; the receiver needs to know one is coming.
                        "sendet_stammdatenaenderung": sendet_stammdatenaenderung,
                        "bemerkung":    bemerkung,
                    }),
                )];
                Ok(WorkflowOutput::with_outbox(events, outbox))
            }

            AbrechnungsdatenCommand::TimeoutExpired { deadline_id, label } => {
                if state.is_terminal() {
                    // A deadline is never cancelled, so it fires for settled
                    // processes too. Nothing went wrong.
                    return Ok(Vec::new().into());
                }
                Ok(vec![AbrechnungsdatenEvent::DeadlineExpired { deadline_id, label }].into())
            }
        }
    }

    fn apply(state: Self::State, event: &Self::Event) -> Self::State {
        match event {
            AbrechnungsdatenEvent::RueckmeldungErhalten {
                pruefidentifikator,
                sender,
                receiver,
                location_id,
                document_date,
                message_ref,
                validation_passed,
                validation_errors,
            } => {
                if *validation_passed {
                    AbrechnungsdatenState::Eingegangen(AbrechnungsdatenData {
                        pruefidentifikator: *pruefidentifikator,
                        sender: sender.clone(),
                        receiver: receiver.clone(),
                        location_id: location_id.clone(),
                        document_date: document_date.clone(),
                        message_ref: message_ref.clone(),
                    })
                } else {
                    AbrechnungsdatenState::Abgebrochen {
                        reason: format!("AHB validation failed: {}", validation_errors.join("; ")),
                    }
                }
            }
            AbrechnungsdatenEvent::BearbeitungsstandGesendet { antwort_code, .. } => match state {
                AbrechnungsdatenState::Eingegangen(data) => AbrechnungsdatenState::Beantwortet {
                    data,
                    antwort_code: antwort_code.clone(),
                },
                other => other,
            },
            AbrechnungsdatenEvent::Abgebrochen { reason } => AbrechnungsdatenState::Abgebrochen {
                reason: reason.clone(),
            },
            AbrechnungsdatenEvent::DeadlineExpired { label, .. } => {
                if state.is_terminal() {
                    state
                } else {
                    AbrechnungsdatenState::Abgebrochen {
                        reason: format!(
                            "{label} expired without a Bearbeitungsstandsmeldung — GPKE Teil 2 \
                             § 3.1 gives the NB until the 2. WT nach dem ÜT"
                        ),
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn pid(code: u32) -> Pruefidentifikator {
        Pruefidentifikator::new(code).expect("valid PID")
    }

    fn receive(code: u32) -> AbrechnungsdatenCommand {
        AbrechnungsdatenCommand::ReceiveRueckmeldung {
            pid: pid(code),
            sender: MarktpartnerCode::new("4012345000023"),
            receiver: MarktpartnerCode::new("9900357000004"),
            location_id: MaLo::new("51238696012"),
            document_date: "20260304".to_owned(),
            message_ref: MessageRef::new("ABR-001"),
            validation_passed: true,
            validation_errors: vec![],
        }
    }

    fn eingegangen(code: u32) -> AbrechnungsdatenState {
        let out =
            GpkeAbrechnungsdatenWorkflow::handle(&AbrechnungsdatenState::default(), receive(code))
                .expect("accepted");
        out.events.iter().fold(
            AbrechnungsdatenState::default(),
            GpkeAbrechnungsdatenWorkflow::apply,
        )
    }

    #[test]
    fn every_routed_pid_is_accepted_and_nothing_else_is() {
        for code in ABRECHNUNGSDATEN_PIDS {
            assert!(matches!(
                eingegangen(*code),
                AbrechnungsdatenState::Eingegangen(_)
            ));
        }
        // 55001 is the Anmeldung; it belongs to another workflow.
        assert!(
            GpkeAbrechnungsdatenWorkflow::handle(
                &AbrechnungsdatenState::default(),
                receive(55_001)
            )
            .is_err()
        );
    }

    /// The answer is an IFTSTA 21047 carrying its `E_0595` code — an event with
    /// no outbox entry records the process as answered while the Lieferant
    /// watches its 2-Werktage window expire.
    #[test]
    fn the_bearbeitungsstand_reaches_the_wire_with_its_code() {
        let out = GpkeAbrechnungsdatenWorkflow::handle(
            &eingegangen(55_156),
            AbrechnungsdatenCommand::SendBearbeitungsstand {
                antwort_code: "A05".to_owned(),
                sendet_stammdatenaenderung: true,
                bemerkung: None,
            },
        )
        .expect("answered");

        let iftsta = out
            .outbox
            .iter()
            .find(|o| &*o.message_type == "IFTSTA")
            .expect("the answer must produce an outbound IFTSTA");
        assert_eq!(iftsta.payload["pid"], BEARBEITUNGSSTAND_PID);
        assert_eq!(iftsta.payload["antwort_code"], "A05");
        assert_eq!(iftsta.payload["antwort_codeliste"], "E_0595");
        assert_eq!(iftsta.payload["sendet_stammdatenaenderung"], true);
        // The answer goes back to the Lieferant that asked.
        assert_eq!(iftsta.payload["sender"], "9900357000004");
        assert_eq!(iftsta.payload["receiver"], "4012345000023");
    }

    /// Both clusters ride the same PID — `E_0595` does not select one, which is
    /// why the cluster stayed off the agreement axis.
    #[test]
    fn both_clusters_ride_the_same_pid() {
        for (code, sendet) in [("A02", true), ("A03", false)] {
            let out = GpkeAbrechnungsdatenWorkflow::handle(
                &eingegangen(55_220),
                AbrechnungsdatenCommand::SendBearbeitungsstand {
                    antwort_code: code.to_owned(),
                    sendet_stammdatenaenderung: sendet,
                    bemerkung: None,
                },
            )
            .expect("answered");
            let iftsta = out
                .outbox
                .iter()
                .find(|o| &*o.message_type == "IFTSTA")
                .expect("outbound IFTSTA");
            assert_eq!(iftsta.payload["pid"], BEARBEITUNGSSTAND_PID, "{code}");
        }
    }

    /// Every code this workflow can send comes from the tree's Clearing branch.
    #[test]
    fn the_codes_belong_to_the_clearing_branch() {
        assert_eq!(BESTELLUNG_EBD, mako_pruefung::codes::EBD_BESTELLUNG);
        for code in mako_pruefung::codes::E_0595_CLEARING_CODES {
            assert!(
                mako_pruefung::codes::lookup(BESTELLUNG_EBD, code).is_some(),
                "{code}"
            );
        }
    }

    /// A deadline fires for settled processes too, and that is not a failure.
    #[test]
    fn a_deadline_on_a_settled_process_is_a_no_op() {
        let answered = {
            let out = GpkeAbrechnungsdatenWorkflow::handle(
                &eingegangen(55_673),
                AbrechnungsdatenCommand::SendBearbeitungsstand {
                    antwort_code: "A01".to_owned(),
                    sendet_stammdatenaenderung: false,
                    bemerkung: None,
                },
            )
            .expect("answered");
            out.events
                .iter()
                .fold(eingegangen(55_673), GpkeAbrechnungsdatenWorkflow::apply)
        };
        let out = GpkeAbrechnungsdatenWorkflow::handle(
            &answered,
            AbrechnungsdatenCommand::TimeoutExpired {
                deadline_id: DeadlineId::new(),
                label: BEARBEITUNGSSTAND_WINDOW_LABEL.into(),
            },
        )
        .expect("no-op");
        assert!(out.events.is_empty());
    }

    /// An unanswered window closes the process, so the operator sees a missed
    /// Frist rather than an open one.
    #[test]
    fn an_expired_window_closes_the_process() {
        let out = GpkeAbrechnungsdatenWorkflow::handle(
            &eingegangen(55_156),
            AbrechnungsdatenCommand::TimeoutExpired {
                deadline_id: DeadlineId::new(),
                label: BEARBEITUNGSSTAND_WINDOW_LABEL.into(),
            },
        )
        .expect("expired");
        let state = out
            .events
            .iter()
            .fold(eingegangen(55_156), GpkeAbrechnungsdatenWorkflow::apply);
        assert!(matches!(state, AbrechnungsdatenState::Abgebrochen { .. }));
    }
}