mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-16 12:12:15 +01:00
Added usage check to delete endpoints
This commit is contained in:
committed by
Constantin Graf
parent
40f1159ea5
commit
ad6146c483
33
app/Exceptions/Api/EntityStillInUseApiException.php
Normal file
33
app/Exceptions/Api/EntityStillInUseApiException.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exceptions\Api;
|
||||
|
||||
class EntityStillInUseApiException extends ApiException
|
||||
{
|
||||
private string $modelToDelete;
|
||||
|
||||
private string $modelInUse;
|
||||
|
||||
public function __construct(string $modelToDelete, string $modelInUse)
|
||||
{
|
||||
parent::__construct('', 0, null);
|
||||
$this->modelToDelete = $modelToDelete;
|
||||
$this->modelInUse = $modelInUse;
|
||||
}
|
||||
|
||||
public const string KEY = 'entity_still_in_use';
|
||||
|
||||
/**
|
||||
* Get the translated message for the exception.
|
||||
*/
|
||||
#[\Override]
|
||||
public function getTranslatedMessage(): string
|
||||
{
|
||||
return __('exceptions.api.'.$this->getKey(), [
|
||||
'modelToDelete' => __('validation.entities.'.$this->modelToDelete),
|
||||
'modelInUse' => __('validation.entities.'.$this->modelInUse),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\Api\EntityStillInUseApiException;
|
||||
use App\Http\Requests\V1\Tag\TagStoreRequest;
|
||||
use App\Http\Requests\V1\Tag\TagUpdateRequest;
|
||||
use App\Http\Resources\V1\Client\ClientCollection;
|
||||
@@ -83,7 +84,7 @@ class ClientController extends Controller
|
||||
/**
|
||||
* Delete client
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
* @throws AuthorizationException|EntityStillInUseApiException
|
||||
*
|
||||
* @operationId deleteClient
|
||||
*/
|
||||
@@ -91,6 +92,10 @@ class ClientController extends Controller
|
||||
{
|
||||
$this->checkPermission($organization, 'clients:delete', $client);
|
||||
|
||||
if ($client->projects()->exists()) {
|
||||
throw new EntityStillInUseApiException('client', 'project');
|
||||
}
|
||||
|
||||
$client->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\Api\EntityStillInUseApiException;
|
||||
use App\Exceptions\Api\UserNotPlaceholderApiException;
|
||||
use App\Http\Requests\V1\Member\MemberIndexRequest;
|
||||
use App\Http\Requests\V1\Member\MemberUpdateRequest;
|
||||
@@ -12,6 +13,8 @@ use App\Http\Resources\V1\Member\MemberPivotResource;
|
||||
use App\Http\Resources\V1\Member\MemberResource;
|
||||
use App\Models\Membership;
|
||||
use App\Models\Organization;
|
||||
use App\Models\ProjectMember;
|
||||
use App\Models\TimeEntry;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -68,7 +71,7 @@ class MemberController extends Controller
|
||||
/**
|
||||
* Remove a member of the organization.
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
* @throws AuthorizationException|EntityStillInUseApiException
|
||||
*
|
||||
* @operationId removeMember
|
||||
*/
|
||||
@@ -76,6 +79,13 @@ class MemberController extends Controller
|
||||
{
|
||||
$this->checkPermission($organization, 'members:delete', $membership);
|
||||
|
||||
if (TimeEntry::query()->where('user_id', $membership->user_id)->whereBelongsTo($organization, 'organization')->exists()) {
|
||||
throw new EntityStillInUseApiException('member', 'time_entry');
|
||||
}
|
||||
if (ProjectMember::query()->whereBelongsToOrganization($organization)->where('user_id', $membership->user_id)->exists()) {
|
||||
throw new EntityStillInUseApiException('member', 'project_member');
|
||||
}
|
||||
|
||||
$membership->delete();
|
||||
|
||||
return response()
|
||||
|
||||
@@ -4,17 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\Api\EntityStillInUseApiException;
|
||||
use App\Http\Requests\V1\Project\ProjectStoreRequest;
|
||||
use App\Http\Requests\V1\Project\ProjectUpdateRequest;
|
||||
use App\Http\Resources\V1\Project\ProjectCollection;
|
||||
use App\Http\Resources\V1\Project\ProjectResource;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectMember;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
@@ -113,7 +116,7 @@ class ProjectController extends Controller
|
||||
/**
|
||||
* Delete project
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
* @throws AuthorizationException|EntityStillInUseApiException
|
||||
*
|
||||
* @operationId deleteProject
|
||||
*/
|
||||
@@ -121,7 +124,20 @@ class ProjectController extends Controller
|
||||
{
|
||||
$this->checkPermission($organization, 'projects:delete', $project);
|
||||
|
||||
$project->delete();
|
||||
if ($project->tasks()->exists()) {
|
||||
throw new EntityStillInUseApiException('project', 'task');
|
||||
}
|
||||
if ($project->timeEntries()->exists()) {
|
||||
throw new EntityStillInUseApiException('project', 'time_entry');
|
||||
}
|
||||
|
||||
DB::transaction(function () use (&$project) {
|
||||
$project->members()->each(function (ProjectMember $member) {
|
||||
$member->delete();
|
||||
});
|
||||
|
||||
$project->delete();
|
||||
});
|
||||
|
||||
return response()
|
||||
->json(null, 204);
|
||||
|
||||
@@ -4,12 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\Api\EntityStillInUseApiException;
|
||||
use App\Http\Requests\V1\Tag\TagStoreRequest;
|
||||
use App\Http\Requests\V1\Tag\TagUpdateRequest;
|
||||
use App\Http\Resources\V1\Tag\TagCollection;
|
||||
use App\Http\Resources\V1\Tag\TagResource;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Tag;
|
||||
use App\Models\TimeEntry;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
@@ -83,7 +85,7 @@ class TagController extends Controller
|
||||
/**
|
||||
* Delete tag
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
* @throws AuthorizationException|EntityStillInUseApiException
|
||||
*
|
||||
* @operationId deleteTag
|
||||
*/
|
||||
@@ -91,6 +93,10 @@ class TagController extends Controller
|
||||
{
|
||||
$this->checkPermission($organization, 'tags:delete', $tag);
|
||||
|
||||
if (TimeEntry::query()->hasTag($tag)->whereBelongsTo($organization, 'organization')->exists()) {
|
||||
throw new EntityStillInUseApiException('tag', 'time_entry');
|
||||
}
|
||||
|
||||
$tag->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\Api\EntityStillInUseApiException;
|
||||
use App\Http\Requests\V1\Task\TaskIndexRequest;
|
||||
use App\Http\Requests\V1\Task\TaskStoreRequest;
|
||||
use App\Http\Requests\V1\Task\TaskUpdateRequest;
|
||||
@@ -104,7 +105,7 @@ class TaskController extends Controller
|
||||
/**
|
||||
* Delete task
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
* @throws AuthorizationException|EntityStillInUseApiException
|
||||
*
|
||||
* @operationId deleteTask
|
||||
*/
|
||||
@@ -112,6 +113,10 @@ class TaskController extends Controller
|
||||
{
|
||||
$this->checkPermission($organization, 'tasks:delete', $task);
|
||||
|
||||
if ($task->timeEntries()->exists()) {
|
||||
throw new EntityStillInUseApiException('task', 'time_entry');
|
||||
}
|
||||
|
||||
$task->delete();
|
||||
|
||||
return response()
|
||||
|
||||
@@ -74,6 +74,14 @@ class Project extends Model
|
||||
return $this->hasMany(Task::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<TimeEntry>
|
||||
*/
|
||||
public function timeEntries(): HasMany
|
||||
{
|
||||
return $this->hasMany(TimeEntry::class, 'project_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Project> $builder
|
||||
*/
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace App\Models;
|
||||
use App\Service\BillableRateService;
|
||||
use Carbon\CarbonInterval;
|
||||
use Database\Factories\TimeEntryFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -31,6 +32,7 @@ use Korridor\LaravelComputedAttributes\ComputedAttributes;
|
||||
* @property string|null $task_id
|
||||
* @property-read Task|null $task
|
||||
*
|
||||
* @method Builder<TimeEntry> hasTag(Tag $tag)
|
||||
* @method static TimeEntryFactory factory()
|
||||
*/
|
||||
class TimeEntry extends Model
|
||||
@@ -73,6 +75,14 @@ class TimeEntry extends Model
|
||||
return $this->end === null ? null : $this->start->diffAsCarbonInterval($this->end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<TimeEntry> $builder
|
||||
*/
|
||||
public function scopeHasTag(Builder $builder, Tag $tag): void
|
||||
{
|
||||
$builder->whereJsonContains('tags', $tag->getKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, TimeEntry>
|
||||
*/
|
||||
|
||||
@@ -149,6 +149,14 @@ class User extends Authenticatable implements FilamentUser, MustVerifyEmail
|
||||
return $this->hasMany(TimeEntry::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<ProjectMember>
|
||||
*/
|
||||
public function projectMembers(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProjectMember::class, 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<User> $builder
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user