fabricecw
9 months ago

Status template class

Posted 9 months ago by fabricecw

Hey

I try to implement a clean progress handling for orders. A order has many statuses and they can be completed via a button in the frontend or an API request. But a status should contain:

  • Name

  • Logic policies: For example, the user can only complete a status "start shipping" if a field "address" is defined in the order. Otherwise the status can't be completed.

  • Authorization policies: Like Laravel Policies, only authorized user for this status can complete it.

I thought about a custom PHP class, but I'm still unsure about the best approach.

An order has a statuses "template". When the order is created, all needed statuses will be created in a subtable in the right order. Because some orders needs an approval, others can be executed directly.

We already have a order progress handling, but it's a mess: An order has a "status" integer field. The user completes the status by submitting a "complete_status" integer to an update function. In the update function, I check for each status if it can be completed:

public function updateStatus()
{
    if(request('complete_status') == $order->status) {
        // For example status 2 is the "start shipping" status
        if($order->status == 2) {
            if(! $order->addressAvailable) {
                abort(403, 'You can not start shipping with no address.');
            }

            if($order->needsApproval()) {
                $order->status = 3;
            } else {
                $order->status = 4;
            }
        }

        if($order->status == 3) {
            // Code for status 3...
        }
    
    } else {
        // If a user tries to complete a status in which the order isn't.
        abort(403, 'This status can't be complete.');
    }
}

And in the frontend, I check the same logic to show/disable the buttons...

Any ideas or hints? Thanks in advance :-)

Please sign in or create an account to participate in this conversation.