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
56 changes: 56 additions & 0 deletions app/Console/Commands/Support/UserUpdateProfileCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Console\Commands\Support;

use App\Models\Support\SupportCase;
use App\Services\Support\SupportJson;
use App\Services\Support\UserProfileUpdateService;
use Illuminate\Console\Command;

class UserUpdateProfileCommand extends Command
{
protected $signature = 'support:user-update-profile
{email : User email to update}
{--firstname= : New first name (firstname field)}
{--lastname= : New last name (lastname field)}
{--dry-run : Plan only; do not write}
{--json : Output JSON only}';

protected $description = 'Support tool: update a user profile first/last name (dry-run supported)';

public function handle(UserProfileUpdateService $service): int
{
$email = (string) $this->argument('email');
$firstname = $this->option('firstname');
$lastname = $this->option('lastname');
$dryRun = (bool) $this->option('dry-run');

$firstname = is_string($firstname) && trim($firstname) !== '' ? trim($firstname) : null;
$lastname = is_string($lastname) && trim($lastname) !== '' ? trim($lastname) : null;

$case = SupportCase::create([
'source_channel' => 'manual',
'processing_mode' => 'manual',
'subject' => 'CLI: support:user-update-profile',
'raw_message' => json_encode(['email' => $email, 'firstname' => $firstname, 'lastname' => $lastname]),
'status' => 'investigating',
'risk_level' => 'low',
'target_email' => $email,
'correlation_id' => SupportJson::correlationId(),
]);

$payload = $service->updateProfile(
case: $case,
email: $email,
firstname: $firstname,
lastname: $lastname,
dryRun: $dryRun,
viaEmailApproval: !$dryRun,
);

$json = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$this->output->writeln($json);

return ($payload['ok'] ?? false) ? self::SUCCESS : self::FAILURE;
}
}
36 changes: 36 additions & 0 deletions app/Http/Controllers/Internal/Support/ToolController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Services\Support\SupportActionLogger;
use App\Services\Support\SupportJson;
use App\Services\Support\UserAuditService;
use App\Services\Support\UserProfileUpdateService;
use App\Services\Support\UserRestoreService;
use Illuminate\Http\Request;

Expand All @@ -16,6 +17,7 @@ public function __construct(
private readonly SupportActionLogger $logger,
private readonly UserAuditService $userAudit,
private readonly UserRestoreService $userRestore,
private readonly UserProfileUpdateService $userProfileUpdate,
private readonly EventAuditService $eventAudit,
) {
}
Expand Down Expand Up @@ -78,6 +80,40 @@ public function userRestore(Request $request)
return SupportJson::json($payload, ($payload['ok'] ?? false) ? 200 : 422);
}

public function userProfileUpdate(Request $request)
{
$data = $request->validate([
'support_case_id' => ['required', 'integer'],
'email' => ['required', 'string'],
'firstname' => ['nullable', 'string', 'max:255'],
'lastname' => ['nullable', 'string', 'max:255'],
'dry_run' => ['required', 'boolean'],
]);

$case = SupportCase::findOrFail((int) $data['support_case_id']);
$payload = $this->userProfileUpdate->updateProfile(
case: $case,
email: $data['email'],
firstname: $data['firstname'] ?? null,
lastname: $data['lastname'] ?? null,
dryRun: (bool) $data['dry_run'],
);

$this->logger->log(
case: $case,
actionName: 'user_profile_update',
actionType: 'write',
input: $data,
output: $payload,
succeeded: (bool) ($payload['ok'] ?? false),
executedBy: 'system',
correlationId: $case->correlation_id,
errorMessage: ($payload['ok'] ?? false) ? null : implode(';', (array) ($payload['errors'] ?? [])),
);

return SupportJson::json($payload, ($payload['ok'] ?? false) ? 200 : 422);
}

public function eventAudit(Request $request)
{
$data = $request->validate([
Expand Down
17 changes: 16 additions & 1 deletion app/Jobs/Support/ExecuteApprovedSupportActionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Support\SupportApproval;
use App\Models\Support\SupportCase;
use App\Services\Support\SupportActionLogger;
use App\Services\Support\UserProfileUpdateService;
use App\Services\Support\UserRestoreService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -20,7 +21,11 @@ public function __construct(public int $supportApprovalId)
{
}

public function handle(UserRestoreService $userRestore, SupportActionLogger $logger): void
public function handle(
UserRestoreService $userRestore,
UserProfileUpdateService $userProfileUpdate,
SupportActionLogger $logger,
): void
{
$approval = SupportApproval::findOrFail($this->supportApprovalId);
$case = SupportCase::findOrFail($approval->support_case_id);
Expand Down Expand Up @@ -67,6 +72,16 @@ public function handle(UserRestoreService $userRestore, SupportActionLogger $log
email: (string) ($payload['email'] ?? ''),
dryRun: false,
confidence: isset($payload['confidence']) ? (float) $payload['confidence'] : null,
viaEmailApproval: true,
);
} elseif ($action === 'user_profile_update') {
$result = $userProfileUpdate->updateProfile(
case: $case,
email: (string) ($payload['email'] ?? ''),
firstname: isset($payload['firstname']) ? (string) $payload['firstname'] : null,
lastname: isset($payload['lastname']) ? (string) $payload['lastname'] : null,
dryRun: false,
viaEmailApproval: true,
);
} else {
$result = [
Expand Down
16 changes: 16 additions & 0 deletions app/Jobs/Support/ProcessSupportCaseDiagnosticsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Support\SupportCaseMessage;
use App\Services\Support\Agents\DiagnosticsAgentService;
use App\Services\Support\SupportActionLogger;
use App\Services\Support\UserProfileUpdateService;
use App\Services\Support\UserRestoreService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -25,6 +26,7 @@ public function handle(
DiagnosticsAgentService $diagnostics,
SupportActionLogger $logger,
UserRestoreService $userRestore,
UserProfileUpdateService $userProfileUpdate,
): void {
$case = SupportCase::findOrFail($this->supportCaseId);
$case->update(['status' => 'investigating']);
Expand Down Expand Up @@ -56,6 +58,20 @@ public function handle(
);
}

if ($case->case_type === 'profile_update' && $case->target_email) {
$dryRunResult = $userProfileUpdate->updateFromCase($case, dryRun: true);
$logger->log(
case: $case,
actionName: 'user_profile_update',
actionType: 'write',
input: ['email' => $case->target_email, 'dry_run' => true],
output: $dryRunResult,
succeeded: (bool) ($dryRunResult['ok'] ?? false),
executedBy: 'agent',
correlationId: $case->correlation_id,
);
}

// Persist diagnostics snapshot as a message for UI/debugging (stable storage for later external orchestrator).
SupportCaseMessage::create([
'support_case_id' => $case->id,
Expand Down
35 changes: 31 additions & 4 deletions app/Services/Support/Agents/TriageAgentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,39 @@
namespace App\Services\Support\Agents;

use App\Models\Support\SupportCase;
use App\Services\Support\SupportProfileRequestParser;
use Illuminate\Support\Str;

class TriageAgentService
{
public function __construct(
private readonly SupportProfileRequestParser $profileParser,
) {
}

public function triage(SupportCase $case): array
{
$text = Str::lower((string) ($case->normalized_message ?? $case->raw_message ?? ''));
$rawText = (string) ($case->normalized_message ?? $case->raw_message ?? '');
$text = Str::lower($rawText);
$profile = $this->profileParser->parse($rawText);

// V1 heuristic placeholder (replace with LLM later, keep output schema stable).
$caseType = 'unknown';
$runbook = 'unknown';
if (Str::contains($text, ['soft-deleted', 'deleted', 'restore account', 'account missing'])) {
$caseType = 'account_restore';
$runbook = 'restore_deleted_account';
} elseif (Str::contains($text, [
'update profile',
'profile name',
'your details',
'first name',
'last name',
'change name',
'rename profile',
]) || ($profile['firstname'] !== null || $profile['lastname'] !== null)) {
$caseType = 'profile_update';
$runbook = 'update_user_profile';
} elseif (Str::contains($text, ['duplicate', 'two accounts', 'split across'])) {
$caseType = 'duplicate_account';
$runbook = 'duplicate_account_investigation';
Expand All @@ -31,22 +50,30 @@ public function triage(SupportCase $case): array
$runbook = 'role_problem';
}

$targetEmail = $this->extractFirstEmail($text);
$targetEmail = $profile['email'] ?? $this->extractFirstEmail($text);
$secondary = $this->extractAllEmails($text);
$secondary = array_values(array_filter($secondary, fn ($e) => $targetEmail ? $e !== $targetEmail : true));

$risk = Str::contains($text, ['password reset', 'merge', 'ownership', 'privileged']) ? 'high' : 'low';
if ($caseType === 'profile_update') {
$risk = 'low';
}

$needsHuman = $targetEmail === null
|| ($caseType === 'profile_update' && $profile['firstname'] === null && $profile['lastname'] === null);

return [
'case_type' => $caseType,
'confidence' => 0.50,
'target_email' => $targetEmail,
'secondary_emails' => $secondary,
'target_user_id' => null,
'requested_action' => null,
'requested_action' => $caseType === 'profile_update' ? 'user_profile_update' : null,
'profile_firstname' => $profile['firstname'],
'profile_lastname' => $profile['lastname'],
'risk_level' => $risk,
'recommended_runbook' => $runbook,
'needs_human_review' => $targetEmail === null,
'needs_human_review' => $needsHuman,
'reasoning_summary' => 'V1 heuristic triage (LLM integration pending).',
];
}
Expand Down
36 changes: 31 additions & 5 deletions app/Services/Support/SupportApprovalEmailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class SupportApprovalEmailService
public function __construct(
private readonly GmailOutboundService $gmail,
private readonly SupportSenderAllowlist $allowlist,
private readonly SupportProfileRequestParser $profileParser,
) {
}

Expand Down Expand Up @@ -175,6 +176,20 @@ private function proposedActionForCase(SupportCase $case): array
];
}

if ($case->case_type === 'profile_update' && $case->target_email) {
$profile = $this->profileParser->parse((string) ($case->normalized_message ?? $case->raw_message ?? ''));
if ($profile['firstname'] !== null || $profile['lastname'] !== null) {
return [
'action' => 'user_profile_update',
'payload' => [
'email' => $case->target_email,
'firstname' => $profile['firstname'],
'lastname' => $profile['lastname'],
],
];
}
}

return ['action' => 'none', 'payload' => []];
}

Expand All @@ -200,11 +215,22 @@ private function buildDryRunBody(SupportCase $case, array $proposedAction): stri
$lines[] = '';
}

$dryRun = $case->actions()->where('action_name', 'user_restore')->where('action_type', 'write')->latest()->first()?->output_json;
if (is_array($dryRun)) {
$lines[] = 'Planned changes (dry run):';
$lines[] = json_encode($dryRun['changes_planned'] ?? $dryRun, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$lines[] = '';
foreach (['user_restore', 'user_profile_update'] as $writeAction) {
$dryRun = $case->actions()->where('action_name', $writeAction)->where('action_type', 'write')->latest()->first()?->output_json;
if (!is_array($dryRun)) {
continue;
}
$result = $dryRun['result'] ?? $dryRun;
if (is_array($result) && isset($result['before'], $result['after'])) {
$lines[] = 'Planned profile/account changes (dry run):';
$lines[] = 'Before: '.json_encode($result['before'], JSON_UNESCAPED_SLASHES);
$lines[] = 'After: '.json_encode($result['after'], JSON_UNESCAPED_SLASHES);
$lines[] = '';
} elseif (is_array($result)) {
$lines[] = 'Planned changes (dry run):';
$lines[] = json_encode($result['changes_planned'] ?? $result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$lines[] = '';
}
}

$action = $proposedAction['action'] ?? 'none';
Expand Down
Loading
Loading