Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions app/Console/Commands/Support/GmailDiagnoseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Console\Commands\Support;

use App\Models\Support\SupportCase;
use App\Services\Support\Gmail\GmailConnector;
use App\Services\Support\Gmail\SupportGmailPollQuery;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class GmailDiagnoseCommand extends Command
{
protected $signature = 'support:gmail:diagnose';

protected $description = 'Debug why support Gmail tickets are not processing (poll query, recent cases, failed jobs).';

public function handle(GmailConnector $connector): int
{
$this->line(json_encode([
'enabled' => (bool) config('support_gmail.enabled'),
'dry_run' => (bool) config('support_gmail.dry_run', true),
'notify_email' => config('support_gmail.notify_email'),
'subject_prefix' => config('support_gmail.subject_prefix'),
'label' => config('support_gmail.label'),
'effective_poll_query' => SupportGmailPollQuery::resolve(),
'queue_connection' => config('queue.default'),
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

if (!config('support_gmail.enabled')) {
$this->warn('SUPPORT_GMAIL_ENABLED is false.');

return self::FAILURE;
}

try {
$result = $connector->fetchNewMessages(
mailbox: (string) config('support_gmail.user', 'me'),
label: config('support_gmail.label'),
query: SupportGmailPollQuery::resolve(),
sinceHistoryId: null,
max: 5,
);

$preview = [];
foreach ($result['messages'] as $msg) {
$preview[] = [
'id' => $msg->id,
'subject' => $msg->subject,
'from' => $msg->from,
];
}

$this->line(json_encode([
'gmail_matches' => count($preview),
'warnings' => $result['warnings'] ?? [],
'messages' => $preview,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
} catch (\Throwable $e) {
$this->error('Gmail API error: '.$e->getMessage());

return self::FAILURE;
}

$cases = SupportCase::query()->orderByDesc('id')->limit(5)->get([
'id', 'subject', 'status', 'source_channel', 'created_at', 'forwarded_by_email',
]);

$this->line(json_encode([
'recent_support_cases' => $cases->toArray(),
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

if (DB::getSchemaBuilder()->hasTable('failed_jobs')) {
$failed = DB::table('failed_jobs')
->where('payload', 'like', '%ProcessSupportCase%')
->orderByDesc('id')
->limit(3)
->get(['id', 'failed_at']);

$this->line(json_encode([
'failed_support_jobs' => $failed->toArray(),
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion app/Jobs/Support/ProcessSupportCaseDiagnosticsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function handle(
'correlation_id' => $case->correlation_id,
]);

ProcessSupportCaseResolutionJob::dispatch($case->id);
ProcessSupportCaseResolutionJob::dispatchSync($case->id);
}
}

2 changes: 1 addition & 1 deletion app/Jobs/Support/ProcessSupportCaseTriageJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handle(TriageAgentService $triage, SupportActionLogger $logger):
correlationId: $case->correlation_id,
);

ProcessSupportCaseDiagnosticsJob::dispatch($case->id);
ProcessSupportCaseDiagnosticsJob::dispatchSync($case->id);
}
}

3 changes: 2 additions & 1 deletion app/Services/Support/Gmail/GmailIngestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public function pollAndIngest(int $max = 25): array
correlationId: $correlationId,
);

ProcessSupportCaseTriageJob::dispatch($case->id);
// Run pipeline synchronously so scheduler poll does not depend on queue workers.
ProcessSupportCaseTriageJob::dispatchSync($case->id);
} else {
$duplicates++;
}
Expand Down
Loading