Skip to content
Merged

Dev #3549

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
20 changes: 13 additions & 7 deletions app/Services/Support/SupportApprovalEmailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,22 @@ private function extractCaseIdFromSubject(?string $subject): ?int

public function isApprovalReply(string $body): bool
{
$keywords = config('support_gmail.approval_keywords', ['approve', 'yes', 'proceed']);
$firstLine = strtolower(trim(Str::before(str_replace("\r\n", "\n", $body), "\n")));
$keywords = config('support_gmail.approval_keywords', ['approve', 'approved', 'yes', 'proceed']);
$lines = explode("\n", str_replace("\r\n", "\n", $body));

foreach ($keywords as $keyword) {
$keyword = strtolower(trim((string) $keyword));
if ($keyword === '') {
foreach (array_slice($lines, 0, 8) as $line) {
$line = strtolower(trim($line));
if ($line === '') {
continue;
}
if ($firstLine === $keyword || str_starts_with($firstLine, $keyword.' ')) {
return true;
foreach ($keywords as $keyword) {
$keyword = strtolower(trim((string) $keyword));
if ($keyword === '') {
continue;
}
if ($line === $keyword || str_starts_with($line, $keyword.' ')) {
return true;
}
}
}

Expand Down
56 changes: 49 additions & 7 deletions app/Services/Support/UserProfileUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Support\SupportCase;
use App\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -66,12 +67,17 @@ public function updateProfile(
return SupportJson::fail($tool, $input, 'no_matching_user_found');
}

if ($matches->count() !== 1) {
return SupportJson::fail($tool, $input, 'ambiguous_user_match');
$user = $this->resolveUserForProfileUpdate($matches, $firstname, $lastname);
if ($user === null) {
return SupportJson::fail($tool, $input, [
'ambiguous_user_match',
'matched_user_ids: '.$matches->pluck('id')->implode(','),
]);
}

/** @var User $user */
$user = $matches->first();
$warnings = $matches->count() > 1
? ['resolved_duplicate_email_to_user_id:'.$user->id]
: [];

$before = [
'user_id' => $user->id,
Expand All @@ -95,7 +101,7 @@ public function updateProfile(
'before' => $before,
'after' => $before,
'note' => 'profile_already_matches_requested_values',
]);
], $warnings);
}

$planned = [
Expand All @@ -114,7 +120,7 @@ public function updateProfile(
'changes_applied' => [],
'before' => $before,
'after' => $after,
]);
], $warnings);
}

if (config('support_gmail.dry_run') && !$viaEmailApproval) {
Expand Down Expand Up @@ -145,7 +151,43 @@ public function updateProfile(
'changes_applied' => [$planned],
'before' => $before,
'after' => $after,
]);
], $warnings);
}

/**
* When multiple users share an email, pick the one that still needs the requested name change.
*
* @param Collection<int, User> $matches
*/
private function resolveUserForProfileUpdate(Collection $matches, ?string $firstname, ?string $lastname): ?User
{
if ($matches->count() === 1) {
return $matches->first();
}

$active = $matches->filter(fn (User $user) => $user->deleted_at === null)->values();
if ($active->count() === 1) {
return $active->first();
}

$candidates = $active->isNotEmpty() ? $active : $matches->values();

$needingUpdate = $candidates->filter(function (User $user) use ($firstname, $lastname) {
if ($firstname !== null && $firstname !== $user->firstname) {
return true;
}
if ($lastname !== null && $lastname !== $user->lastname) {
return true;
}

return false;
});

if ($needingUpdate->count() === 1) {
return $needingUpdate->first();
}

return null;
}

private function isValidEmail(string $email): bool
Expand Down
4 changes: 2 additions & 2 deletions config/support_gmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
// Default recipient for support:gmail:test and dry-run summaries when requester unknown.
'notify_email' => env('SUPPORT_GMAIL_NOTIFY_EMAIL', 'codeweek@matrixinternet.ie'),

// First line of a reply must match one of these (case-insensitive) to approve.
'approval_keywords' => ['approve', 'yes', 'proceed'],
// First non-empty line of a reply must match one of these (case-insensitive) to approve.
'approval_keywords' => ['approve', 'approved', 'yes', 'proceed'],

// Subject prefix for approval threads: "[CW-SUPPORT #123] ..."
'approval_subject_prefix' => '[CW-SUPPORT',
Expand Down
2 changes: 1 addition & 1 deletion docs/support-copilot-stakeholder-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ Read the summary email carefully.
APPROVE
```

(`YES` or `PROCEED` also work.)
(`APPROVED`, `YES`, or `PROCEED` also work. Must be on its own line near the top of the reply.)
3. Send from **`@matrixinternet.ie`** or **`@codeweek.eu`**
4. Wait up to ~1 minute for the system to process your reply
5. You will receive a **follow-up email** in the same thread:
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Support/SupportApprovalEmailServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ final class SupportApprovalEmailServiceTest extends TestCase
{
public function test_detects_approval_reply_keywords(): void
{
config()->set('support_gmail.approval_keywords', ['approve', 'yes', 'proceed']);
config()->set('support_gmail.approval_keywords', ['approve', 'approved', 'yes', 'proceed']);

$svc = app(SupportApprovalEmailService::class);

$this->assertTrue($svc->isApprovalReply("APPROVE\n\nSome quoted text"));
$this->assertTrue($svc->isApprovalReply("approved"));
$this->assertTrue($svc->isApprovalReply("\n\napproved\n\nOn Tue wrote:"));
$this->assertTrue($svc->isApprovalReply("yes"));
$this->assertFalse($svc->isApprovalReply("maybe approve later"));
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Support/UserProfileUpdateServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ public function test_profile_update_dry_run_plans_change(): void
$this->assertSame('Bernard Hanna', $u->firstname);
}

public function test_profile_update_resolves_duplicate_email_to_user_needing_change(): void
{
User::factory()->create([
'email' => 'dup@example.com',
'firstname' => 'Bernard',
'lastname' => 'Hanna',
]);
User::factory()->create([
'email' => 'dup@example.com',
'firstname' => 'Bernard Hanna',
'lastname' => '',
]);

$case = SupportCase::create([
'source_channel' => 'manual',
'processing_mode' => 'manual',
'subject' => 'test',
'raw_message' => 'test',
'status' => 'investigating',
'risk_level' => 'low',
'correlation_id' => 'cid',
]);

$payload = app(UserProfileUpdateService::class)->updateProfile(
$case,
'dup@example.com',
'Bernard',
'Hanna',
true,
);

$this->assertTrue($payload['ok']);
$this->assertSame('Bernard Hanna', $payload['result']['before']['firstname']);
$this->assertStringContainsString('resolved_duplicate_email_to_user_id:', (string) ($payload['warnings'][0] ?? ''));
}

public function test_profile_update_execute_changes_user(): void
{
config(['support_gmail.dry_run' => true]);
Expand Down
Loading