-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Slack][Equipment Authorization] Refactor batch authorization into qu…
…euable action(s) (#48) * Add AddUserMembership Action * Add BatchAuthorizeEquipment Action * Use BatchAuthorizeEquipment in `EquipmentAuthorization->handle`
- Loading branch information
Showing
3 changed files
with
120 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Actions\WordPress; | ||
|
||
use App\External\WooCommerce\Api\WooCommerceApi; | ||
use Spatie\QueueableAction\QueueableAction; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class AddUserMembership | ||
{ | ||
use QueueableAction; | ||
|
||
/** | ||
* Create a new action instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(WooCommerceApi $wpApi) | ||
{ | ||
$this->wpApi = $wpApi; | ||
} | ||
|
||
/** | ||
* Execute the action. | ||
* | ||
* @return mixed | ||
*/ | ||
public function execute($actorId, $memberId, $planId) | ||
{ | ||
$this->wpApi->members->addMembership($memberId, $planId); | ||
Log::info('AddUserMembership: Customer '.$actorId.' granted user plan id '.$planId.' to Customer '.$memberId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace App\Actions\WordPress; | ||
|
||
use App\Models\Customer; | ||
use App\Actions\WordPress\AddUserMembershipo; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class BatchAuthorizeEquipment | ||
{ | ||
/** | ||
* Create a new action instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct( | ||
AddUserMembership $addUserMembership | ||
) | ||
{ | ||
$this->authorizeAction = $addUserMembership; | ||
} | ||
|
||
/** | ||
* Execute the action. | ||
* @param Customer $trainer - The person who is submitting the authorization | ||
* @param Collection<Customer> $members - The members to be authorized on the equipment | ||
* @param Collection<TrainableEquipment> $equipment - The pieces of equipment for which the members will be authorized | ||
* @param Boolean $makeTrainers - Default false. Whether the members should be authorized to trainers others on this equipment. | ||
* @return mixed | ||
*/ | ||
public function execute(Customer $trainer, $members, $equipment, $makeTrainers=false) | ||
{ | ||
// Validate that trainer is an authorized trainer for all equipment | ||
foreach($equipment as $e) { | ||
if (!$trainer->hasMembership($e->trainer_plan_id)) { | ||
Log::info('BatchAuthorizeEquipment: Customer attempted to submit authorization without trainer role. '.json_encode([ | ||
'trainer_id' => $trainer->id, | ||
'equipment_id' => $e->id | ||
])); | ||
|
||
throw new \Exception('NotAuthorized'); | ||
} | ||
} | ||
|
||
Log::info('BatchAuthorizeEquipment: Submitting batch: '.json_encode([ | ||
'trainer_id' => $trainer->id, | ||
'member_ids' => $members->map(fn($member) => $member->id), | ||
'equipment_ids' => $equipment->map(fn($e) => $e->id), | ||
'make_trainers' => $makeTrainers | ||
])); | ||
|
||
$planIds = $equipment->map(fn($e) => $e->user_plan_id); | ||
|
||
if ($makeTrainers) { | ||
$planIds = $planIds->concat($equipment->map(fn ($e) => $e->trainer_plan_id)); | ||
} | ||
|
||
foreach($members->crossjoin($planIds) as [$member, $planId]){ | ||
if ($member->hasMembership($planId)) { | ||
continue; | ||
} | ||
$this->authorizeAction->onQueue()->execute($trainer->id, $member->id, $planId); | ||
} | ||
return 'ok'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters