You've already forked isop-mirror
feat: implement company deletion functionality with confirmation dialog
This commit is contained in:
@@ -4,7 +4,10 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use App\Models\Internship;
|
||||
use App\Models\InternshipStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CompanyController extends Controller
|
||||
{
|
||||
@@ -152,4 +155,67 @@ class CompanyController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a company, its contact person and all related data.
|
||||
*/
|
||||
public function delete(int $id)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
// Admin kontrola
|
||||
if ($user->role !== 'ADMIN') {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
$company = Company::find($id);
|
||||
|
||||
if (!$company) {
|
||||
return response()->json([
|
||||
'message' => 'No such company exists.'
|
||||
], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
// 1. Získaj všetky internship IDs firmy
|
||||
$internshipIds = Internship::where('company_id', $company->id)
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
|
||||
// 2. Vymaž všetky internship statuses
|
||||
if (!empty($internshipIds)) {
|
||||
InternshipStatus::whereIn('internship_id', $internshipIds)->delete();
|
||||
}
|
||||
|
||||
// 3. Vymaž všetky internships firmy
|
||||
Internship::where('company_id', $company->id)->delete();
|
||||
|
||||
// 4. Získaj contact usera
|
||||
$contactUser = User::find($company->contact);
|
||||
|
||||
// 5. Vymaž company
|
||||
$company->delete();
|
||||
|
||||
// 6. Vymaž contact usera (EMPLOYER)
|
||||
if ($contactUser && $contactUser->role === 'EMPLOYER') {
|
||||
$contactUser->delete();
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Company successfully deleted.'
|
||||
], 200);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Error deleting company.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,20 @@ class Company extends Model
|
||||
'contact',
|
||||
'hiring'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the internships for the company.
|
||||
*/
|
||||
public function internships()
|
||||
{
|
||||
return $this->hasMany(Internship::class, 'company_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contact person (user) for the company.
|
||||
*/
|
||||
public function contactPerson()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'contact');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,4 +54,5 @@ Route::prefix('/companies')->middleware("auth:sanctum")->group(function () {
|
||||
Route::get("/simple", [CompanyController::class, 'all_simple']);
|
||||
Route::get("/{id}", [CompanyController::class, 'get']);
|
||||
Route::post("/{id}", [CompanyController::class, 'update_all']);
|
||||
Route::delete("/{id}", [CompanyController::class, 'delete']);
|
||||
});
|
||||
@@ -13,6 +13,13 @@ const companyId = route.params.id;
|
||||
const loading = ref(true);
|
||||
const saving = ref(false);
|
||||
|
||||
// Delete state
|
||||
const deleteDialog = ref(false);
|
||||
const deleteLoading = ref(false);
|
||||
const deleteError = ref<string | null>(null);
|
||||
const deleteSuccess = ref(false);
|
||||
const company = ref<CompanyData | null>(null);
|
||||
|
||||
const form = ref({
|
||||
name: '',
|
||||
address: '',
|
||||
@@ -30,6 +37,7 @@ const { data } = await useSanctumFetch<CompanyData>(`/api/companies/${companyId}
|
||||
|
||||
watch(data, (newData) => {
|
||||
if (newData) {
|
||||
company.value = newData;
|
||||
form.value.name = newData.name;
|
||||
form.value.address = newData.address;
|
||||
form.value.ico = newData.ico;
|
||||
@@ -65,6 +73,49 @@ async function saveChanges() {
|
||||
function cancel() {
|
||||
navigateTo('/dashboard/admin/companies');
|
||||
}
|
||||
|
||||
// Funkcia na otvorenie delete dialogu
|
||||
const openDeleteDialog = () => {
|
||||
deleteDialog.value = true;
|
||||
deleteError.value = null;
|
||||
};
|
||||
|
||||
// Funkcia na zatvorenie dialogu
|
||||
const closeDeleteDialog = () => {
|
||||
deleteDialog.value = false;
|
||||
deleteError.value = null;
|
||||
deleteSuccess.value = false;
|
||||
};
|
||||
|
||||
// Funkcia na vymazanie firmy
|
||||
const deleteCompany = async () => {
|
||||
if (!companyId) return;
|
||||
|
||||
deleteLoading.value = true;
|
||||
deleteError.value = null;
|
||||
|
||||
try {
|
||||
await client(`/api/companies/${companyId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
deleteSuccess.value = true;
|
||||
|
||||
// Presmeruj na zoznam po 1.5 sekundách
|
||||
setTimeout(() => {
|
||||
navigateTo('/dashboard/admin/companies');
|
||||
}, 1500);
|
||||
|
||||
} catch (e) {
|
||||
if (e instanceof FetchError) {
|
||||
deleteError.value = e.response?._data?.message || 'Chyba pri mazaní firmy.';
|
||||
} else {
|
||||
deleteError.value = 'Neznáma chyba pri mazaní firmy.';
|
||||
}
|
||||
} finally {
|
||||
deleteLoading.value = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -120,11 +171,53 @@ function cancel() {
|
||||
<v-btn @click="cancel" :disabled="saving">
|
||||
Zrušiť
|
||||
</v-btn>
|
||||
<v-spacer></v-spacer>
|
||||
<v-btn color="red" variant="outlined" @click="openDeleteDialog" :disabled="saving">
|
||||
Vymazať firmu
|
||||
</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-col>
|
||||
</v-row>
|
||||
</div>
|
||||
|
||||
<!-- Delete Confirmation Dialog -->
|
||||
<v-dialog v-model="deleteDialog" max-width="500px">
|
||||
<v-card>
|
||||
<v-card-title class="text-h5">
|
||||
Potvrdiť vymazanie
|
||||
</v-card-title>
|
||||
<v-card-text>
|
||||
<p v-if="!deleteSuccess">
|
||||
Naozaj chcete vymazať firmu <strong>{{ company?.name }}</strong>?
|
||||
</p>
|
||||
<p v-if="!deleteSuccess" class="text-error mt-2">
|
||||
Táto akcia vymaže aj kontaktnú osobu (EMPLOYER), všetky praxe a statusy spojené s touto firmou a
|
||||
<strong>nie je možné ju vrátiť späť</strong>.
|
||||
</p>
|
||||
|
||||
<!-- Error message -->
|
||||
<v-alert v-if="deleteError" type="error" density="compact" class="mt-3">
|
||||
{{ deleteError }}
|
||||
</v-alert>
|
||||
|
||||
<!-- Success message -->
|
||||
<v-alert v-if="deleteSuccess" type="success" density="compact" class="mt-3">
|
||||
Firma bola úspešne vymazaná. Presmerovanie...
|
||||
</v-alert>
|
||||
</v-card-text>
|
||||
<v-card-actions>
|
||||
<v-spacer></v-spacer>
|
||||
<v-btn color="grey" variant="text" @click="closeDeleteDialog" :disabled="deleteLoading">
|
||||
Zrušiť
|
||||
</v-btn>
|
||||
<v-btn color="red" variant="text" @click="deleteCompany" :loading="deleteLoading"
|
||||
:disabled="deleteSuccess">
|
||||
Vymazať
|
||||
</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-dialog>
|
||||
</v-container>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -22,7 +22,65 @@ const headers = [
|
||||
{ title: 'Operácie', key: 'ops', align: 'middle' },
|
||||
];
|
||||
|
||||
const client = useSanctumClient();
|
||||
|
||||
const { data, error } = await useSanctumFetch<CompanyData[]>('/api/companies/simple');
|
||||
|
||||
// State pre delete dialog
|
||||
const deleteDialog = ref(false);
|
||||
const companyToDelete = ref<CompanyData | null>(null);
|
||||
const deleteLoading = ref(false);
|
||||
const deleteError = ref<string | null>(null);
|
||||
const deleteSuccess = ref(false);
|
||||
|
||||
// Funkcia na otvorenie delete dialogu
|
||||
const openDeleteDialog = (company: CompanyData) => {
|
||||
companyToDelete.value = company;
|
||||
deleteDialog.value = true;
|
||||
deleteError.value = null;
|
||||
};
|
||||
|
||||
// Funkcia na zatvorenie dialogu
|
||||
const closeDeleteDialog = () => {
|
||||
deleteDialog.value = false;
|
||||
companyToDelete.value = null;
|
||||
deleteError.value = null;
|
||||
deleteSuccess.value = false;
|
||||
};
|
||||
|
||||
// Funkcia na vymazanie firmy
|
||||
const deleteCompany = async () => {
|
||||
if (!companyToDelete.value) return;
|
||||
|
||||
deleteLoading.value = true;
|
||||
deleteError.value = null;
|
||||
|
||||
try {
|
||||
await client(`/api/companies/${companyToDelete.value.id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
// Odstránime firmu zo zoznamu
|
||||
if (data.value) {
|
||||
const index = data.value.findIndex(c => c.id === companyToDelete.value!.id);
|
||||
if (index > -1) {
|
||||
data.value.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
deleteSuccess.value = true;
|
||||
|
||||
setTimeout(() => {
|
||||
closeDeleteDialog();
|
||||
}, 1500);
|
||||
|
||||
} catch (err: any) {
|
||||
deleteError.value = err.data?.message || 'Chyba pri mazaní firmy.';
|
||||
} finally {
|
||||
deleteLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -62,14 +120,52 @@ const { data, error } = await useSanctumFetch<CompanyData[]>('/api/companies/sim
|
||||
<td class="text-left">
|
||||
<v-btn class="m-1 op-btn" density="compact" append-icon="mdi-pencil" base-color="orange"
|
||||
:to="'/dashboard/admin/companies/edit/' + item.id">Editovať</v-btn>
|
||||
<v-btn class="m-1 op-btn" density="compact" append-icon="mdi-trash-can-outline"
|
||||
base-color="red" @click="async () => { }">Zmazať</v-btn>
|
||||
<v-btn class="m-1 op-btn" density="compact" append-icon="mdi-delete" base-color="red"
|
||||
@click="openDeleteDialog(item)">Vymazať</v-btn>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</v-table>
|
||||
</div>
|
||||
</v-card>
|
||||
|
||||
<!-- Delete Confirmation Dialog -->
|
||||
<v-dialog v-model="deleteDialog" max-width="500px">
|
||||
<v-card>
|
||||
<v-card-title class="text-h5">
|
||||
Potvrdiť vymazanie
|
||||
</v-card-title>
|
||||
<v-card-text>
|
||||
<p v-if="!deleteSuccess">
|
||||
Naozaj chcete vymazať firmu <strong>{{ companyToDelete?.name }}</strong>?
|
||||
</p>
|
||||
<p v-if="!deleteSuccess" class="text-error mt-2">
|
||||
Táto akcia vymaže aj kontaktnú osobu (EMPLOYER), všetky praxe a statusy spojené s touto firmou a
|
||||
<strong>nie je možné ju vrátiť späť</strong>.
|
||||
</p>
|
||||
|
||||
<!-- Error message -->
|
||||
<v-alert v-if="deleteError" type="error" density="compact" class="mt-3">
|
||||
{{ deleteError }}
|
||||
</v-alert>
|
||||
|
||||
<!-- Success message -->
|
||||
<v-alert v-if="deleteSuccess" type="success" density="compact" class="mt-3">
|
||||
Firma bola úspešne vymazaná.
|
||||
</v-alert>
|
||||
</v-card-text>
|
||||
<v-card-actions>
|
||||
<v-spacer></v-spacer>
|
||||
<v-btn color="grey" variant="text" @click="closeDeleteDialog" :disabled="deleteLoading">
|
||||
Zrušiť
|
||||
</v-btn>
|
||||
<v-btn color="red" variant="text" @click="deleteCompany" :loading="deleteLoading"
|
||||
:disabled="deleteSuccess">
|
||||
Vymazať
|
||||
</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-dialog>
|
||||
</v-container>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user