intial ticket fix partial delete fix

This commit is contained in:
Mark Gregory
2026-06-18 17:15:56 +01:00
parent 70cd7149aa
commit 5064365af2
7 changed files with 325 additions and 25 deletions
+46 -24
View File
@@ -9,6 +9,7 @@ use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Component;
use Modules\Helpdesk\Models\AutomationRule;
use Modules\Helpdesk\Models\CannedResponse;
@@ -134,7 +135,7 @@ final class SettingsManager extends Component
$data = $this->guardDefaultColumn('helpdesk_statuses', $data);
if ((bool) ($data['is_default'] ?? false)) {
Status::query()->whereKeyNot($this->statusForm['id'])->update(['is_default' => false]);
$this->clearOtherDefaults(Status::class, $this->statusForm['id']);
}
$this->persistSettingModel(Status::class, $this->statusForm['id'], $data);
@@ -173,7 +174,7 @@ final class SettingsManager extends Component
$data = $this->guardDefaultColumn('helpdesk_priorities', $data);
if ((bool) ($data['is_default'] ?? false)) {
Priority::query()->whereKeyNot($this->priorityForm['id'])->update(['is_default' => false]);
$this->clearOtherDefaults(Priority::class, $this->priorityForm['id']);
}
$this->persistSettingModel(Priority::class, $this->priorityForm['id'], $data);
$this->resetPriorityForm();
@@ -214,10 +215,7 @@ final class SettingsManager extends Component
$data = $this->guardDefaultColumn('helpdesk_categories', $data);
if ((bool) ($data['is_default'] ?? false)) {
Category::query()
->whereKeyNot($this->categoryForm['id'])
->where('company_id', $data['company_id'])
->update(['is_default' => false]);
$this->clearOtherDefaults(Category::class, $this->categoryForm['id'], $data['company_id'] ?? null);
}
$this->persistSettingModel(Category::class, $this->categoryForm['id'], $data);
$this->resetCategoryForm();
@@ -258,10 +256,7 @@ final class SettingsManager extends Component
$data = $this->guardDefaultColumn('helpdesk_teams', $data);
if ((bool) ($data['is_default'] ?? false)) {
Team::query()
->whereKeyNot($this->teamForm['id'])
->where('company_id', $data['company_id'])
->update(['is_default' => false]);
$this->clearOtherDefaults(Team::class, $this->teamForm['id'], $data['company_id'] ?? null);
}
$this->persistSettingModel(Team::class, $this->teamForm['id'], $data);
$this->resetTeamForm();
@@ -303,10 +298,7 @@ final class SettingsManager extends Component
$data = $this->guardDefaultColumn('helpdesk_projects', $data);
if ((bool) ($data['is_default'] ?? false)) {
Project::query()
->whereKeyNot($this->projectForm['id'])
->where('company_id', $data['company_id'])
->update(['is_default' => false]);
$this->clearOtherDefaults(Project::class, $this->projectForm['id'], $data['company_id'] ?? null);
}
$this->persistSettingModel(Project::class, $this->projectForm['id'], $data);
$this->resetProjectForm();
@@ -352,7 +344,7 @@ final class SettingsManager extends Component
$data['slug'] = $this->slug($data['slug'] ?: $data['name']);
if ((bool) ($data['is_default'] ?? false)) {
KanbanBoard::query()->whereKeyNot($this->boardForm['id'])->update(['is_default' => false]);
$this->clearOtherDefaults(KanbanBoard::class, $this->boardForm['id']);
}
$board = $this->persistSettingModel(KanbanBoard::class, $this->boardForm['id'], $data);
@@ -530,7 +522,7 @@ final class SettingsManager extends Component
])['slaForm'];
$data['slug'] = $this->slug($data['slug'] ?: $data['name']);
if ((bool) ($data['is_default'] ?? false)) {
SlaPolicy::query()->whereKeyNot($this->slaForm['id'])->update(['is_default' => false]);
$this->clearOtherDefaults(SlaPolicy::class, $this->slaForm['id']);
}
$this->persistSettingModel(SlaPolicy::class, $this->slaForm['id'], $data);
$this->resetSlaPolicyForm();
@@ -797,6 +789,7 @@ final class SettingsManager extends Component
private function orderedCategories(): mixed
{
return Category::query()
->with('company')
->withCount('tickets')
->when($this->hasDefaultColumn('helpdesk_categories'), fn ($query) => $query->orderByDesc('is_default'))
->orderBy('name')
@@ -806,6 +799,7 @@ final class SettingsManager extends Component
private function orderedTeams(): mixed
{
return Team::query()
->with('company')
->withCount(['projects', 'tickets'])
->when($this->hasDefaultColumn('helpdesk_teams'), fn ($query) => $query->orderByDesc('is_default'))
->orderBy('name')
@@ -815,7 +809,7 @@ final class SettingsManager extends Component
private function orderedProjects(): mixed
{
return Project::query()
->with(['team'])
->with(['company', 'team'])
->withCount('tickets')
->when($this->hasDefaultColumn('helpdesk_projects'), fn ($query) => $query->orderByDesc('is_default'))
->orderBy('title')
@@ -837,19 +831,47 @@ final class SettingsManager extends Component
*/
private function persistSettingModel(string $modelClass, int|string|null $id, array $data): object
{
if (filled($id)) {
unset($data['id']);
try {
if (filled($id)) {
/** @var TModel $model */
$model = $modelClass::query()->findOrFail((int) $id);
$model->fill($data);
$model->save();
return $model;
}
/** @var TModel $model */
$model = $modelClass::query()->findOrFail((int) $id);
$model->fill($data);
$model->save();
$model = $modelClass::query()->create($data);
return $model;
} catch (\Illuminate\Database\QueryException $exception) {
throw ValidationException::withMessages([
'settings' => 'The record could not be saved. Check the slug is unique for this section/company and try again.',
]);
}
}
/**
* Clear the default flag on every other record without generating invalid null-id SQL.
*
* @param class-string<\Illuminate\Database\Eloquent\Model> $modelClass
*/
private function clearOtherDefaults(string $modelClass, int|string|null $currentId, int|string|null $companyId = null): void
{
$query = $modelClass::query();
if (filled($currentId)) {
$query->whereKeyNot((int) $currentId);
}
/** @var TModel $model */
$model = $modelClass::query()->create($data);
if (Schema::hasColumn((new $modelClass())->getTable(), 'company_id')) {
$query->where('company_id', filled($companyId) ? (int) $companyId : null);
}
return $model;
$query->update(['is_default' => false]);
}
/**