import fix for update so it does not insert person and client case
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\StorePermissionRequest;
|
||||
use App\Http\Requests\UpdatePermissionRequest;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
@@ -14,7 +16,7 @@ class PermissionController extends Controller
|
||||
public function index(): Response
|
||||
{
|
||||
$permissions = Permission::query()
|
||||
->select('id','name','slug','description','created_at')
|
||||
->select('id', 'name', 'slug', 'description', 'created_at')
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
@@ -22,15 +24,51 @@ public function index(): Response
|
||||
'permissions' => $permissions,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(): Response
|
||||
{
|
||||
return Inertia::render('Admin/Permissions/Create');
|
||||
$roles = Role::orderBy('name')->get(['id', 'name', 'slug']);
|
||||
|
||||
return Inertia::render('Admin/Permissions/Create', [
|
||||
'roles' => $roles,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(StorePermissionRequest $request): RedirectResponse
|
||||
{
|
||||
Permission::create($request->validated());
|
||||
$data = $request->validated();
|
||||
$roleIds = $data['roles'] ?? [];
|
||||
unset($data['roles']);
|
||||
|
||||
return redirect()->route('admin.index')->with('success', 'Dovoljenje ustvarjeno.');
|
||||
$permission = Permission::create($data);
|
||||
if (! empty($roleIds)) {
|
||||
$permission->roles()->sync($roleIds);
|
||||
}
|
||||
|
||||
return redirect()->route('admin.permissions.index')->with('success', 'Dovoljenje ustvarjeno.');
|
||||
}
|
||||
|
||||
public function edit(Permission $permission): Response
|
||||
{
|
||||
$roles = Role::orderBy('name')->get(['id', 'name', 'slug']);
|
||||
$selected = $permission->roles()->pluck('roles.id');
|
||||
|
||||
return Inertia::render('Admin/Permissions/Edit', [
|
||||
'permission' => $permission->only('id', 'name', 'slug', 'description'),
|
||||
'roles' => $roles,
|
||||
'selectedRoleIds' => $selected,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(UpdatePermissionRequest $request, Permission $permission): RedirectResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
$roleIds = $data['roles'] ?? [];
|
||||
unset($data['roles']);
|
||||
|
||||
$permission->update($data);
|
||||
$permission->roles()->sync($roleIds);
|
||||
|
||||
return redirect()->route('admin.permissions.index')->with('success', 'Dovoljenje posodobljeno.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ public function rules(): array
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'slug' => ['required', 'string', 'max:255', 'alpha_dash', 'unique:permissions,slug'],
|
||||
'description' => ['nullable', 'string', 'max:500'],
|
||||
'roles' => ['sometimes', 'array'],
|
||||
'roles.*' => ['integer', 'exists:roles,id'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdatePermissionRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()?->hasPermission('manage-settings') || $this->user()?->hasRole('admin');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
$permissionId = $this->route('permission')->id ?? null;
|
||||
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'slug' => ['required', 'string', 'max:255', 'alpha_dash', Rule::unique('permissions', 'slug')->ignore($permissionId)],
|
||||
'description' => ['nullable', 'string', 'max:500'],
|
||||
'roles' => ['sometimes', 'array'],
|
||||
'roles.*' => ['integer', 'exists:roles,id'],
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'name.required' => 'Ime je obvezno.',
|
||||
'slug.required' => 'Slug je obvezen.',
|
||||
'slug.unique' => 'Slug že obstaja.',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1490,6 +1490,9 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
|
||||
return ['action' => 'invalid', 'message' => 'Missing contract.reference'];
|
||||
}
|
||||
|
||||
// Determine mapping mode for contract.reference (e.g., keyref)
|
||||
$refMode = $this->mappingMode($mappings, 'contract.reference');
|
||||
|
||||
// Determine client_case_id: prefer provided, else derive via person/client
|
||||
$clientCaseId = $contractData['client_case_id'] ?? null;
|
||||
$clientId = $import->client_id; // may be null
|
||||
@@ -1513,6 +1516,19 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
|
||||
->first();
|
||||
}
|
||||
|
||||
// If contract.reference is keyref and contract not found, do not create any entities
|
||||
if (! $existing && $refMode === 'keyref') {
|
||||
ImportEvent::create([
|
||||
'import_id' => $import->id,
|
||||
'user_id' => null,
|
||||
'event' => 'row_skipped',
|
||||
'level' => 'warning',
|
||||
'message' => 'Contract reference '.$reference.' does not exist (keyref); row skipped.',
|
||||
]);
|
||||
|
||||
return ['action' => 'skipped', 'message' => 'contract.reference keyref lookup failed: not found'];
|
||||
}
|
||||
|
||||
// If we still need to insert, we must resolve clientCaseId, but avoid creating new person/case unless necessary
|
||||
if (! $existing && ! $clientCaseId) {
|
||||
$clientRef = $mapped['client_case']['client_ref'] ?? null;
|
||||
@@ -1573,7 +1589,7 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
|
||||
continue;
|
||||
}
|
||||
$parts = explode('.', $map->target_field);
|
||||
if ($parts[0] !== 'contract') {
|
||||
if (($parts[0] ?? null) !== 'contract') {
|
||||
continue;
|
||||
}
|
||||
$field = $parts[1] ?? null;
|
||||
@@ -1588,31 +1604,16 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
|
||||
// keyref: used as lookup and applied on insert, but not on update
|
||||
if ($mode === 'keyref') {
|
||||
$applyInsert[$field] = $value;
|
||||
|
||||
continue;
|
||||
}
|
||||
if (in_array($mode, ['insert', 'both'])) {
|
||||
if (in_array($mode, ['insert', 'both'], true)) {
|
||||
$applyInsert[$field] = $value;
|
||||
}
|
||||
if (in_array($mode, ['update', 'both'])) {
|
||||
if (in_array($mode, ['update', 'both'], true)) {
|
||||
$applyUpdate[$field] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// If contract not found and contract.reference is keyref, skip without creating entities
|
||||
$refMode = $this->mappingMode($mappings, 'contract.reference');
|
||||
if (! $existing && $refMode === 'keyref') {
|
||||
ImportEvent::create([
|
||||
'import_id' => $import->id,
|
||||
'user_id' => null,
|
||||
'event' => 'row_skipped',
|
||||
'level' => 'warning',
|
||||
'message' => 'Contract reference '.$reference.' does not exist (keyref); row skipped.',
|
||||
]);
|
||||
|
||||
return ['action' => 'skipped', 'message' => 'contract.reference keyref lookup failed: not found'];
|
||||
}
|
||||
|
||||
if ($existing) {
|
||||
// 1) Prepare contract field changes (non-null)
|
||||
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
|
||||
|
||||
Reference in New Issue
Block a user