mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 17:24:10 +00:00
Removes three layered gates that kept the Danger Zone completely hidden unless the current user had more than one company: 1. SettingsLayoutView's showDangerZone computed no longer checks companies.length > 1 — just is_owner. 2. DangerZoneView drops the v-if that wrapped the delete button with the same check. 3. Admin\\CompaniesController::destroy() drops the companies_count <= 1 early-return that was enforcing the rule server-side (translation key You_cannot_delete_all_companies was inline in the controller, not in lang files or tests, so nothing else needs cleanup). The reasoning behind the old gate was that a user with zero companies would be stranded. That's a misread of how the app degrades: /admin/no-company already exists as a graceful fallback view, and the user can create a fresh company from there to recover. Hiding the entire delete flow just to avoid that fallback UX was overkill — the name-confirmation modal already prevents accidental deletion.
114 lines
3.2 KiB
PHP
114 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Facades\Hashids;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\AdminCompanyUpdateRequest;
|
|
use App\Http\Requests\CompaniesRequest;
|
|
use App\Http\Resources\CompanyResource;
|
|
use App\Models\Company;
|
|
use App\Services\CompanyService;
|
|
use Illuminate\Http\Request;
|
|
use Silber\Bouncer\BouncerFacade;
|
|
|
|
class CompaniesController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly CompanyService $companyService,
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$companies = Company::query()
|
|
->with(['owner', 'address'])
|
|
->when($request->has('search'), function ($query) use ($request) {
|
|
$query->where('name', 'like', '%'.$request->search.'%');
|
|
})
|
|
->when($request->has('orderByField') && $request->has('orderBy'), function ($query) use ($request) {
|
|
$query->orderBy($request->orderByField, $request->orderBy);
|
|
}, function ($query) {
|
|
$query->orderBy('name', 'asc');
|
|
})
|
|
->paginate($request->input('limit', 10));
|
|
|
|
return CompanyResource::collection($companies);
|
|
}
|
|
|
|
public function show(Company $company)
|
|
{
|
|
$company->load(['owner', 'address']);
|
|
|
|
return new CompanyResource($company);
|
|
}
|
|
|
|
public function update(AdminCompanyUpdateRequest $request, Company $company)
|
|
{
|
|
$company->update([
|
|
'name' => $request->name,
|
|
'vat_id' => $request->vat_id,
|
|
'tax_id' => $request->tax_id,
|
|
'owner_id' => $request->owner_id,
|
|
]);
|
|
|
|
if ($request->has('address')) {
|
|
$company->address()->updateOrCreate(
|
|
['company_id' => $company->id],
|
|
$request->address,
|
|
);
|
|
}
|
|
|
|
$company->load(['owner', 'address']);
|
|
|
|
return new CompanyResource($company);
|
|
}
|
|
|
|
public function store(CompaniesRequest $request)
|
|
{
|
|
$this->authorize('create company');
|
|
|
|
$user = $request->user();
|
|
|
|
$company = Company::create($request->getCompanyPayload());
|
|
$company->unique_hash = Hashids::connection(Company::class)->encode($company->id);
|
|
$company->save();
|
|
$this->companyService->setupDefaults($company);
|
|
$user->companies()->attach($company->id);
|
|
|
|
BouncerFacade::scope()->to($company->id);
|
|
$user->assign('owner');
|
|
|
|
if ($request->address) {
|
|
$company->address()->create($request->address);
|
|
}
|
|
|
|
return new CompanyResource($company);
|
|
}
|
|
|
|
public function destroy(Request $request)
|
|
{
|
|
$company = Company::find($request->header('company'));
|
|
|
|
$this->authorize('delete company', $company);
|
|
|
|
$user = $request->user();
|
|
|
|
if ($request->name !== $company->name) {
|
|
return respondJson('company_name_must_match_with_given_name', 'Company name must match with given name');
|
|
}
|
|
|
|
$this->companyService->delete($company, $user);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
]);
|
|
}
|
|
|
|
public function userCompanies(Request $request)
|
|
{
|
|
$companies = $request->user()->companies;
|
|
|
|
return CompanyResource::collection($companies);
|
|
}
|
|
}
|