add history logs for missions
This commit is contained in:
@@ -11,6 +11,7 @@ use App\Models\Mission;
|
|||||||
use App\Services\Cartables\Mission\TransportationUnitService;
|
use App\Services\Cartables\Mission\TransportationUnitService;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class TransportationUnitController extends Controller
|
class TransportationUnitController extends Controller
|
||||||
{
|
{
|
||||||
@@ -23,27 +24,49 @@ class TransportationUnitController extends Controller
|
|||||||
|
|
||||||
public function allocate(AllocateRequest $request, Mission $mission): JsonResponse
|
public function allocate(AllocateRequest $request, Mission $mission): JsonResponse
|
||||||
{
|
{
|
||||||
$mission->machines()->attach($request->machines);
|
DB::transaction(function () use ($request, $mission) {
|
||||||
|
$mission->machines()->attach($request->machines);
|
||||||
|
|
||||||
$state = MissionStates::PENDING_CONFIRMATION->value;
|
$mission->histories()->create([
|
||||||
$mission->update([
|
'user_id' => auth()->user()->id,
|
||||||
'state_id' => $state,
|
'previous_state_id' => $mission->state_id,
|
||||||
'state_name' => MissionStates::name($state),
|
'previous_state_name' => $mission->state_name
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$state = MissionStates::PENDING_CONFIRMATION->value;
|
||||||
|
$mission->update([
|
||||||
|
'state_id' => $state,
|
||||||
|
'state_name' => MissionStates::name($state),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
return $this->successResponse();
|
return $this->successResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deallocate(DeallocateRequest $request, Mission $mission): JsonResponse
|
public function deallocate(DeallocateRequest $request, Mission $mission): JsonResponse
|
||||||
{
|
{
|
||||||
$state = MissionStates::REJECT_BY_TRANSPORTATION->value;
|
DB::transaction(function () use ($request, $mission) {
|
||||||
|
$mission->histories()->create([
|
||||||
|
'user_id' => auth()->user()->id,
|
||||||
|
'previous_state_id' => $mission->state_id,
|
||||||
|
'previous_state_name' => $mission->state_name,
|
||||||
|
'user_description' => $request->description
|
||||||
|
]);
|
||||||
|
|
||||||
$mission->update([
|
$state = MissionStates::REJECT_BY_TRANSPORTATION->value;
|
||||||
'state_id' => $state,
|
|
||||||
'state_name' => MissionStates::name($state),
|
$mission->update([
|
||||||
'description' => $request->description
|
'state_id' => $state,
|
||||||
]);
|
'state_name' => MissionStates::name($state),
|
||||||
|
'description' => $request->description
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
return $this->successResponse();
|
return $this->successResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function rahdaran(Mission $mission): JsonResponse
|
||||||
|
{
|
||||||
|
return $this->successResponse($mission->rahdaran()->get(['rahdaran.id', 'rahdaran.name', 'rahdaran.code']));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
|||||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||||
|
|
||||||
class Mission extends Model
|
class Mission extends Model
|
||||||
@@ -20,6 +21,11 @@ class Mission extends Model
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function histories(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(MissionHistory::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function roadItems(): MorphToMany
|
public function roadItems(): MorphToMany
|
||||||
{
|
{
|
||||||
return $this->morphedByMany(RoadItemsProject::class, 'missionable');
|
return $this->morphedByMany(RoadItemsProject::class, 'missionable');
|
||||||
|
|||||||
Reference in New Issue
Block a user