120 lines
4.4 KiB
PHP
120 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Client;
|
|
use App\Models\ClientProjectActivities;
|
|
use App\Models\ClientUserAssignation;
|
|
use App\Models\User;
|
|
use App\Services\UserHierarchyService;
|
|
use Inertia\Inertia;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log as FacadesLog;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ActivityController extends Controller
|
|
{
|
|
public function __construct(private UserHierarchyService $hierarchyService)
|
|
{
|
|
}
|
|
|
|
public function completeActivity(Request $request, int $activity_id)
|
|
{
|
|
$activity = ClientProjectActivities::findOrFail($activity_id);
|
|
abort_unless($this->hierarchyService->canViewClient(Auth::user(), $activity->client), 403);
|
|
|
|
$activity->completed_at = Carbon::now();
|
|
$activity->save();
|
|
return redirect()->back()
|
|
->with('message-info', 'Activity has been completed.');
|
|
}
|
|
|
|
public function storeActivity(Request $request, int $project_id): RedirectResponse
|
|
{
|
|
$request->validate([
|
|
'task_description' => ['required', 'string'],
|
|
'category' => ['required_if:activity_type,report,scheduled'],
|
|
'activity_type' => ['required', 'in:report,scheduled,customer_notes'],
|
|
'estimated_completed_at' => [
|
|
'nullable',
|
|
'required_if:activity_type,scheduled',
|
|
],
|
|
]);
|
|
|
|
$project = Client::findOrFail($project_id);
|
|
abort_unless($this->hierarchyService->canViewClient(Auth::user(), $project), 403);
|
|
|
|
$activity = $project->activitiesList()->create([
|
|
'task_description' => $request->task_description,
|
|
'activity_type' => $request->category,
|
|
'estimated_completed_at' => $request->activity_type == 'report' ? null : $request->estimated_completed_at,
|
|
'user_id' => auth()->id(),
|
|
'completed_at' => $request->activity_type == 'report' ? now() : null,
|
|
]);
|
|
|
|
return redirect()
|
|
->route('google-ads.accounts.show', ['id' => $project->customer_id, 'tab' => 'activities'])
|
|
->with('message-info', 'Activity has been created successfully.');
|
|
}
|
|
|
|
public function updateActivity($id, Request $request): RedirectResponse
|
|
{
|
|
try {
|
|
$activity = ClientProjectActivities::findOrFail($id);
|
|
abort_unless($this->hierarchyService->canViewClient(Auth::user(), $activity->client), 403);
|
|
|
|
$request->validate([
|
|
'task_description' => ['required', 'string', 'min:0'],
|
|
'category' => ['required_if:activity_type,report,scheduled'],
|
|
'activity_type' => ['required', 'in:report,scheduled,customer_notes'],
|
|
'estimated_completed_at' => [
|
|
'nullable',
|
|
'required_if:activity_type,scheduled',
|
|
],
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
|
|
$activity->update([
|
|
'task_description' => $request->task_description,
|
|
'activity_type' => $request->category,
|
|
'estimated_completed_at' => $request->activity_type == 'report' ? null : $request->estimated_completed_at,
|
|
'completed_at' => $request->activity_type == 'report' ? (! empty($activity->completed_at) ? $activity->completed_at : now()) : null,
|
|
]);
|
|
|
|
DB::commit();
|
|
|
|
return redirect()
|
|
->back()
|
|
->with('message-info', 'Activity has been updated successfully.');
|
|
|
|
} catch (\Throwable $e) {
|
|
DB::rollBack();
|
|
FacadesLog::error('Error updated project activity: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return redirect()
|
|
->back()
|
|
->withInput()
|
|
->with('message-error', 'Something went wrong while updating the project activity.');
|
|
}
|
|
}
|
|
|
|
public function deleteActivity($id): RedirectResponse
|
|
{
|
|
$activity = ClientProjectActivities::findOrFail($id);
|
|
abort_unless($this->hierarchyService->canViewClient(Auth::user(), $activity->client), 403);
|
|
|
|
$clientId = $activity->client_id;
|
|
$activity->delete();
|
|
|
|
return redirect()
|
|
->route('google-ads.accounts.show', ['id' => $clientId, 'tab' => 'activities'])
|
|
->with('message-info', 'Activity has been deleted successfully.');
|
|
}
|
|
}
|