refactor(sales): replace service with command and action pattern
- Replace SaleService with CreateSaleCommand and CreateSaleAction for better separation of concerns - Move sale creation logic into dedicated command class following command pattern - Update CreateSale.php to use new action instead of direct service call - Wrap sale creation in database transaction for data consistency
This commit is contained in:
22
app/Actions/Sales/CreateSaleAction.php
Normal file
22
app/Actions/Sales/CreateSaleAction.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Sales;
|
||||
|
||||
use App\Commands\Sales\CreateSaleCommand;
|
||||
use App\Models\Sale;
|
||||
|
||||
class CreateSaleAction
|
||||
{
|
||||
/**
|
||||
* Create a new class instance.
|
||||
*/
|
||||
public function __construct(private CreateSaleCommand $createSaleCommand)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function __invoke(array $data): Sale
|
||||
{
|
||||
return $this->createSaleCommand->execute($data);
|
||||
}
|
||||
}
|
||||
27
app/Commands/Sales/CreateSaleCommand.php
Normal file
27
app/Commands/Sales/CreateSaleCommand.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands\Sales;
|
||||
|
||||
use App\Commands\Command;
|
||||
use App\DataObjects\CreateSaleDTO;
|
||||
use App\Models\Sale;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateSaleCommand implements Command
|
||||
{
|
||||
public function execute(array $data): Sale
|
||||
{
|
||||
return DB::transaction(function () use ($data, &$sale) {
|
||||
$tData = new CreateSaleDTO(
|
||||
reference_number: $data['current_series'],
|
||||
happened_on: \Carbon\Carbon::parse($data['happened_on']),
|
||||
branch_id: $data['branch_id'],
|
||||
user_id: Auth::user()->id,
|
||||
);
|
||||
|
||||
return Sale::create($tData->toArray());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Filament\Resources\SaleResource\Pages;
|
||||
|
||||
use App\Actions\Sales\CreateSaleAction;
|
||||
use App\Actions\Transactions\CreateTransactionAction;
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use App\Filament\Resources\ClientResource;
|
||||
@@ -83,7 +84,7 @@ class CreateSale extends CreateRecord
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$record = app(SaleService::class)->create($this->getFormDataMutation($data));
|
||||
$record = app(CreateSaleAction::class)($this->getFormDataMutation($data));
|
||||
$branch = $record->branch;
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Sales;
|
||||
|
||||
use App\DataObjects\CreateSaleDTO;
|
||||
use App\Models\Sale;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class SaleService
|
||||
{
|
||||
/**
|
||||
* Create a new class instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function create(array $data): Sale
|
||||
{
|
||||
|
||||
$tData = new CreateSaleDTO(
|
||||
reference_number: $data['current_series'],
|
||||
happened_on: Carbon::parse($data['happened_on']),
|
||||
branch_id: $data['branch_id'],
|
||||
user_id: Auth::user()->id,
|
||||
);
|
||||
return Sale::create($tData->toArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user