128 lines
5.2 KiB
PHP
128 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Business;
|
|
use App\Models\BusinessDomain;
|
|
use App\Services\Businesses\BusinessProvisioningService;
|
|
use App\Services\Tenant\DomainNormalizer;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\View\View;
|
|
use InvalidArgumentException;
|
|
|
|
class ManagerBusinessController extends Controller
|
|
{
|
|
public function create(): View
|
|
{
|
|
return view('manager.businesses.create');
|
|
}
|
|
|
|
public function store(Request $request, BusinessProvisioningService $provisioner, DomainNormalizer $domains): RedirectResponse
|
|
{
|
|
$data = $request->validate([
|
|
'name' => ['required', 'string', 'max:160'],
|
|
'shortName' => ['nullable', 'string', 'max:80'],
|
|
'slug' => ['required', 'alpha_dash', 'max:80', 'unique:Business,slug'],
|
|
'domain' => ['required', 'string', 'max:253'],
|
|
'email' => ['nullable', 'email', 'max:190'],
|
|
'phone' => ['nullable', 'string', 'max:40'],
|
|
'timezone' => ['required', 'timezone'],
|
|
'currency' => ['required', 'string', 'size:3'],
|
|
'ownerName' => ['required', 'string', 'max:160'],
|
|
'ownerEmail' => ['required', 'email', 'max:190', 'unique:User,email'],
|
|
]);
|
|
|
|
try {
|
|
$data['domain'] = $domains->normalize($data['domain']);
|
|
} catch (InvalidArgumentException $exception) {
|
|
return back()->withErrors(['domain' => $exception->getMessage()])->withInput();
|
|
}
|
|
|
|
if (BusinessDomain::query()->where('hostname', $data['domain'])->exists()) {
|
|
return back()->withErrors(['domain' => 'That hostname is already assigned to another business.'])->withInput();
|
|
}
|
|
|
|
try {
|
|
$result = $provisioner->provision($data, [
|
|
'name' => $data['ownerName'],
|
|
'email' => $data['ownerEmail'],
|
|
], $request->user());
|
|
} catch (InvalidArgumentException $exception) {
|
|
return back()->withErrors(['domain' => $exception->getMessage()])->withInput();
|
|
}
|
|
|
|
$hostname = $result->business->primaryDomain()->value('hostname');
|
|
$invitationUrl = request()->getScheme().'://'.$hostname.'/invite/'.$result->ownerInvitation->token;
|
|
|
|
return redirect()->route('manager.businesses.show', $result->business)
|
|
->with('status', 'Business created. The owner can finish setup using the invitation link below.')
|
|
->with('owner_invitation_url', $invitationUrl);
|
|
}
|
|
|
|
public function show(Business $business): View
|
|
{
|
|
$business->load(['domains', 'settings', 'bookingRule', 'hours', 'staff.user', 'staff.role']);
|
|
|
|
return view('manager.businesses.show', compact('business'));
|
|
}
|
|
|
|
public function edit(Business $business): View
|
|
{
|
|
$business->load('primaryDomain');
|
|
|
|
return view('manager.businesses.edit', compact('business'));
|
|
}
|
|
|
|
public function update(Request $request, Business $business, DomainNormalizer $domains): RedirectResponse
|
|
{
|
|
$data = $request->validate([
|
|
'name' => ['required', 'string', 'max:160'],
|
|
'shortName' => ['nullable', 'string', 'max:80'],
|
|
'slug' => ['required', 'alpha_dash', 'max:80', 'unique:Business,slug,'.$business->id.',id'],
|
|
'domain' => ['required', 'string', 'max:253'],
|
|
'email' => ['nullable', 'email', 'max:190'],
|
|
'phone' => ['nullable', 'string', 'max:40'],
|
|
'timezone' => ['required', 'timezone'],
|
|
'currency' => ['required', 'string', 'size:3'],
|
|
]);
|
|
|
|
try {
|
|
$hostname = $domains->normalize($data['domain']);
|
|
} catch (InvalidArgumentException $exception) {
|
|
return back()->withErrors(['domain' => $exception->getMessage()])->withInput();
|
|
}
|
|
|
|
$domainTaken = BusinessDomain::query()
|
|
->where('hostname', $hostname)
|
|
->where('businessId', '!=', $business->id)
|
|
->exists();
|
|
|
|
if ($domainTaken) {
|
|
return back()->withErrors(['domain' => 'That hostname is already assigned to another business.'])->withInput();
|
|
}
|
|
|
|
DB::transaction(function () use ($business, $data, $hostname): void {
|
|
$business->update([
|
|
'name' => trim($data['name']),
|
|
'shortName' => filled($data['shortName'] ?? null) ? trim($data['shortName']) : null,
|
|
'slug' => strtolower(trim($data['slug'])),
|
|
'email' => filled($data['email'] ?? null) ? strtolower(trim($data['email'])) : null,
|
|
'phone' => filled($data['phone'] ?? null) ? trim($data['phone']) : null,
|
|
'timezone' => $data['timezone'],
|
|
'currency' => strtoupper($data['currency']),
|
|
]);
|
|
|
|
$primary = $business->domains()->where('isPrimary', true)->first();
|
|
if ($primary) {
|
|
$primary->update(['hostname' => $hostname]);
|
|
} else {
|
|
BusinessDomain::create(['businessId' => $business->id, 'hostname' => $hostname, 'isPrimary' => true, 'isActive' => true]);
|
|
}
|
|
});
|
|
|
|
return redirect()->route('manager.businesses.show', $business)->with('status', 'Business details updated.');
|
|
}
|
|
}
|