2525
2626use OCA \Dossiq \Kernel \AppIdentity ;
2727use OCA \Dossiq \Kernel \Contracts \ComplaintServiceInterface ;
28+ use OCA \Dossiq \Service \Complaint \ComplaintDeadlineAlerts ;
2829use OCA \Dossiq \Service \Support \CallerIdentity ;
2930use OCA \Dossiq \Service \Support \SearchesObjects ;
30- use OCA \Dossiq \Service \Term \DayCount ;
3131use OCA \Dossiq \Service \Term \TermResolution ;
3232use OCA \OpenRegister \Kernel \Contracts \Value \ObjectFindTarget ;
3333use OCA \OpenRegister \Kernel \Contracts \Value \ObjectSaveOptions ;
3838/**
3939 * Service for complaint (klacht) management per Awb chapter 9.
4040 *
41- * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) The total is the sum of small private steps; each method stays under the method thresholds.
42- *
4341 * @spec openspec/changes/archive/2026-06-13-complaint-management/tasks.md#task-TASK-CM-02
4442 */
4543class ComplaintService implements ComplaintServiceInterface {
@@ -99,6 +97,13 @@ class ComplaintService implements ComplaintServiceInterface {
9997 */
10098 private const AWB_VERDAGING_WEEKS = 4 ;
10199
100+ /**
101+ * Sorts complaints by their deadlines and projects the overdue list.
102+ *
103+ * @var ComplaintDeadlineAlerts
104+ */
105+ private readonly ComplaintDeadlineAlerts $ alerts ;
106+
102107 /**
103108 * Constructor.
104109 *
@@ -110,16 +115,19 @@ class ComplaintService implements ComplaintServiceInterface {
110115 * @param TermResolution|null $resolution The case type's own first-response
111116 * term, which wins over the Awb
112117 * default when one is declared.
113- * @param DayCount $days Counts the days between two moments.
118+ * @param ComplaintDeadlineAlerts|null $alerts Sorts complaints by their
119+ * deadlines; built over the same
120+ * date path when not given.
114121 */
115122 public function __construct (
116123 private readonly SettingsService $ settingsService ,
117124 private readonly LoggerInterface $ logger ,
118125 private readonly WorkingDayCalculator $ workingDays ,
119126 private readonly CaseDateNormaliser $ dates ,
120127 private readonly ?TermResolution $ resolution = null ,
121- private readonly DayCount $ days = new DayCount () ,
128+ ? ComplaintDeadlineAlerts $ alerts = null ,
122129 ) {
130+ $ this ->alerts = ($ alerts ?? new ComplaintDeadlineAlerts (dates: $ this ->dates ));
123131 }//end __construct()
124132
125133 /**
@@ -473,52 +481,17 @@ public function linkEscalatedCase(string $complaintId, string $caseId): array {
473481 */
474482 public function getDeadlineAlerts (int $ warningDays = 3 ): array {
475483 $ activeStatuses = ['received ' , 'receipt_confirmed ' , 'in_handling ' , 'hoorgesprek_planned ' , 'hoorgesprek_completed ' ];
476- $ all = $ this ->listComplaints (filters: ['status ' => $ activeStatuses ]);
477- $ today = $ this ->dates ->today ();
478- $ overdue = [];
479- $ warning = [];
480-
481- foreach ($ all as $ complaint ) {
482- $ deadlineDate = $ this ->dates ->tryParse ($ complaint ['afhandelDeadline ' ] ?? null );
483- if ($ deadlineDate === null ) {
484- continue ;
485- }
486484
487- $ diff = $ this ->days ->between (from: $ today , to: $ deadlineDate );
488- $ isPast = $ today > $ deadlineDate ;
489-
490- if ($ isPast === true ) {
491- $ overdue [] = $ complaint ;
492- } elseif ($ diff <= $ warningDays ) {
493- $ warning [] = $ complaint ;
494- }
495- }
496-
497- return ['overdue ' => $ overdue , 'warning ' => $ warning ];
485+ return $ this ->alerts ->grouped (
486+ complaints: $ this ->listComplaints (filters: ['status ' => $ activeStatuses ]),
487+ warningDays: $ warningDays
488+ );
498489 }//end getDeadlineAlerts()
499490
500- /**
501- * Fields of a complaint an assistant may see in a list (REQ-MCP-203).
502- *
503- * `complainant` is the citizen and is never one of them.
504- *
505- * @var array<string>
506- */
507- private const OVERDUE_PROJECTION = [
508- 'id ' ,
509- 'complaintNumber ' ,
510- 'subject ' ,
511- 'category ' ,
512- 'status ' ,
513- 'handler ' ,
514- 'acknowledgementOfReceiptDeadline ' ,
515- 'afhandelDeadline ' ,
516- ];
517-
518491 /**
519492 * The complaints past their handling deadline, without the complainant.
520493 *
521- * The projection is applied HERE, on the way out, because the dialect has
494+ * The projection is applied on the way out (in ComplaintDeadlineAlerts) , because the dialect has
522495 * no field projection and `complaint` stays get-only there. A row is
523496 * rebuilt from an allowlist rather than stripped of a denylist, so a field
524497 * added to the schema later stays out until someone names it.
@@ -538,25 +511,7 @@ public function getDeadlineAlerts(int $warningDays = 3): array {
538511 annotations: ['outsideAgent ' => true ]
539512 )]
540513 public function listOverdueComplaints (): array {
541- $ today = $ this ->dates ->today ();
542- $ rows = [];
543- foreach ($ this ->getDeadlineAlerts ()['overdue ' ] as $ complaint ) {
544- $ row = [];
545- foreach (self ::OVERDUE_PROJECTION as $ field ) {
546- if (array_key_exists ($ field , $ complaint ) === true ) {
547- $ row [$ field ] = $ complaint [$ field ];
548- }
549- }
550-
551- $ ackDeadline = $ this ->dates ->tryParse ($ complaint ['acknowledgementOfReceiptDeadline ' ] ?? null );
552- $ row ['handlingOverdue ' ] = true ;
553- $ row ['acknowledgementOverdue ' ] = $ ackDeadline !== null
554- && $ today > $ ackDeadline
555- && ($ complaint ['status ' ] ?? null ) === 'received ' ;
556- $ rows [] = $ row ;
557- }
558-
559- return ['complaints ' => $ rows , 'count ' => count ($ rows )];
514+ return $ this ->alerts ->overdueRows (overdue: $ this ->getDeadlineAlerts ()['overdue ' ]);
560515 }//end listOverdueComplaints()
561516
562517 /**
0 commit comments