Skip to main content

Overview

The ApproveResourceAction class handles the approval of pending resource requests. It creates the resource in NetBird, updates the request status, logs the approval, and sends an email notification to the requester.

Constructor Dependencies

The action is instantiated with the following dependency:
netbirdService
NetbirdService
required
Service class for interacting with the NetBird API

Method Signature

public function execute(
    PendingResource $pending,
    string $groupId,
    User $admin
): array

Parameters

pending
PendingResource
required
The pending resource request to approve (Eloquent model instance)
groupId
string
required
The NetBird group ID to associate the resource with
admin
User
required
The admin user approving the request (used for audit logging)

Return Value

return
array
Returns an array containing the created resource data from NetBird:
id
string
The unique NetBird resource ID
name
string
The resource name
address
string
The resource network address
description
string|null
The resource description (nullable)
enabled
bool
Whether the resource is enabled

Side Effects

This action performs the following side effects:
  1. NetBird API Call: Creates the resource in NetBird via NetbirdService::createResource() using data from the pending request
  2. Database Insert: Creates a new Resource record with:
    • netbird_id: The ID returned from NetBird
    • user_id: The ID of the user who originally requested the resource
    • created_by: The name of the user who originally requested the resource
  3. Database Update: Updates the PendingResource record:
    • Sets status to 'approved'
    • Sets decided_at to the current timestamp
  4. Audit Log: Creates a ResourceLog entry with action type Approved
  5. Email Notification: Sends a ResourceDecisionNotification email to the requester (if email is available)

Example Usage

use App\Actions\ApproveResourceAction;
use App\Services\NetbirdService;
use App\Models\{User, PendingResource};

// Instantiate the action with dependencies
$netbirdService = app(NetbirdService::class);
$action = new ApproveResourceAction($netbirdService);

// Fetch the pending request
$pending = PendingResource::find(1);
$admin = User::find(2);

// Execute the action
$result = $action->execute(
    pending: $pending,
    groupId: 'grp_abc123',
    admin: $admin
);

// Result contains the created resource data
echo $result['id']; // e.g., "res_xyz789"
echo $result['name']; // The approved resource name

// The pending request is now marked as approved
echo $pending->fresh()->status; // "approved"

Email Notification

The action automatically sends an email notification to the requester if the pending resource has a requested_email value. The email uses the ResourceDecisionNotification mailable with approved: true.
Email notifications are sent asynchronously if your application is configured to use queues. If the email fails to send, the approval operation still succeeds.

Error Scenarios

If the NetBird API call fails, the action will throw an exception before updating the pending request status or sending notifications. This ensures the pending request remains in its original state for retry.
The approved resource is associated with the original requester (via user_id and created_by), not the admin who approved it. The admin’s action is tracked in the audit log.

Source Reference

Implementation: /home/daytona/workspace/source/app/Actions/ApproveResourceAction.php:27 Email notification helper: /home/daytona/workspace/source/app/Actions/ApproveResourceAction.php:62

Build docs developers (and LLMs) love