signature; // Log command start logNotificationStart($commandName); $this->info('Checking deleted joints for missing comments...'); $notificationCode = 'notification_deleted_joint_missing_comment'; try { // Skip if checked recently (within 5 minutes) if (!shouldCheckNotification($notificationCode)) { $this->info('Skipping: Last notification sent less than 5 minutes ago.'); logNotificationSkip($commandName, 'Last notification sent less than 5 minutes ago'); return 0; } // Get last check time (null = first run, check all records) $lastCheck = getLastCheckTimestamp($notificationCode); // Build query $query = DB::table('deleted_joints') ->where(function($q) { $q->whereNull('comment') ->orWhere('comment', ''); }); // If not first run, only check new records if ($lastCheck !== null) { $query->where('created_at', '>', $lastCheck); // Get already notified IDs to prevent duplicates $alreadyNotifiedIds = getAlreadyNotifiedIds($notificationCode); if (!empty($alreadyNotifiedIds)) { $query->whereNotIn('id', $alreadyNotifiedIds); } } // If first run (lastCheck === null), check ALL records (no where clause added) // Get IDs of records that need notification $ids = $query->pluck('id')->toArray(); if (empty($ids)) { $this->info('No new deleted joints with missing comments found.'); $duration = microtime(true) - $startTime; logNotificationComplete($commandName, 0, 0, 0, $duration); return 0; } // Prepare filter parameters $filterParams = [ 'table' => 'deleted_joints', 'conditions' => [ 'or' => [ ['comment' => null], ['comment' => ''] ] ] ]; // If not first run, merge with existing IDs (additive approach) if ($lastCheck !== null) { $existingIds = getAlreadyNotifiedIds($notificationCode); if (!empty($existingIds)) { // Merge new IDs with existing ones $allIds = array_values(array_unique(array_merge($existingIds, $ids))); // Check if there are actually new IDs $newIds = array_diff($ids, $existingIds); if (empty($newIds)) { $this->info('Skipping: No new records since last notification.'); $duration = microtime(true) - $startTime; logNotificationSkip($commandName, 'No new records since last notification'); return 0; } $filterParams['conditions']['id'] = $allIds; } else { $filterParams['conditions']['id'] = $ids; } } else { // First run: use all IDs $filterParams['conditions']['id'] = $ids; } // Check for duplicate (same issue already notified and no new records) if ($lastCheck !== null && isNotificationDuplicate($notificationCode, $filterParams)) { $this->info('Skipping: Same issue already notified and no new records.'); $duration = microtime(true) - $startTime; logNotificationSkip($commandName, 'Same issue already notified and no new records'); return 0; } $title = 'Deleted Joints Missing Comments'; $newCount = $lastCheck !== null ? count(array_diff($ids, getAlreadyNotifiedIds($notificationCode))) : count($ids); $message = sprintf( '%d deleted joint(s) have no comment. Please add deletion reason for each joint.', count($filterParams['conditions']['id']) ); // Send notification with all IDs (existing + new) $sentCount = sendNotification( $notificationCode, $message, null, // Link will be generated automatically $title, $filterParams, count($filterParams['conditions']['id']) ); $duration = microtime(true) - $startTime; $totalRecords = count($filterParams['conditions']['id']); // Log completion logNotificationComplete($commandName, $sentCount, $totalRecords, $newCount, $duration); $this->info("Sent {$sentCount} notification(s) for {$totalRecords} deleted joints with missing comments ({$newCount} new)."); return 0; } catch (\Exception $e) { $duration = microtime(true) - $startTime; logNotificationError($commandName, $e->getMessage(), [ 'file' => $e->getFile(), 'line' => $e->getLine(), 'duration' => $duration, ]); $this->error("Error: " . $e->getMessage()); return 1; } } }