Skip to content

Commit

Permalink
Updated active card multiple accounts to give more info
Browse files Browse the repository at this point in the history
Jnesselr committed Dec 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3cd7536 commit 199b617
Showing 2 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/Issues/Checkers/AccessCards/ActiveCardIssues.php
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ public function generateIssues(): void
}

if ($membersWithCard->count() > 1) {
$this->issues->add(new ActiveCardMultipleAccounts($card_holder));
$this->issues->add(new ActiveCardMultipleAccounts($card_holder, $membersWithCard));

return;
}
59 changes: 53 additions & 6 deletions app/Issues/Types/AccessCards/ActiveCardMultipleAccounts.php
Original file line number Diff line number Diff line change
@@ -2,19 +2,23 @@

namespace App\Issues\Types\AccessCards;

use App\External\WooCommerce\Api\WooCommerceApi;
use App\Issues\Data\MemberData;
use App\Issues\Types\ICanFixThem;
use App\Issues\Types\IssueBase;
use Illuminate\Support\Collection;

/**
* This _technically_ should never ever happen. WinDSX should rip the card away from the original card holder on an
* update. But if it did ever happen, we would absolutely want to know about it.
*/
class ActiveCardMultipleAccounts extends IssueBase
{
use ICanFixThem;

private $cardHolder;
private Collection $membersWithCard;

public function __construct($cardHolder)
public function __construct($cardHolder, $membersWithCard)
{
$this->cardHolder = $cardHolder;
$this->membersWithCard = $membersWithCard;
}

public static function getIssueNumber(): int
@@ -29,6 +33,49 @@ public static function getIssueTitle(): string

public function getIssueText(): string
{
return "{$this->cardHolder['first_name']} {$this->cardHolder['last_name']} has the active card ({$this->cardHolder['card_num']}) but is connected to multiple accounts.";
$otherAccounts = $this->membersWithCard->implode(fn($md) => "$md->first_name $md->last_name ($md->id)", ",");
return "{$this->cardHolder['first_name']} {$this->cardHolder['last_name']} has the active card ({$this->cardHolder['card_num']}) but is connected to multiple accounts: $otherAccounts";
}

public function fix(): bool
{
$choiceHelper = $this->issueFixChoice();

foreach ($this->membersWithCard as $memberData) {
/** @var MemberData $memberData */
$choiceHelper->option(
"Keep {$this->cardHolder['card_num']} ONLY for customer $memberData->first_name, $memberData->last_name ($memberData->id) Member: {$memberData->isMember}",
fn() => $this->keepOnlyCardHolder($memberData)
);
}

return $choiceHelper->run();
}

private function keepOnlyCardHolder(MemberData $winnerMemberData)
{
$cardNum = $this->cardHolder['card_num'];

/** @var WooCommerceApi $wooCommerceApi */
$wooCommerceApi = app(WooCommerceApi::class);

foreach ($this->membersWithCard as $memberData) {
/** @var MemberData $memberData */
if ($memberData->id == $winnerMemberData->id) {
continue; // They're keeping the card, nothing to do for them.
}

$memberData->cards->forget($cardNum);

$wooCommerceApi->customers
->update($memberData->id, [
'meta_data' => [
[
'key' => 'access_card_number',
'value' => $memberData->cards->implode(','),
],
],
]);
}
}
}

0 comments on commit 199b617

Please sign in to comment.