Files
ladill-queue/app/Http/Controllers/Api/IntegrationController.php
T
isaaccladandCursor b208e59bd0
Deploy Ladill Queue / deploy (push) Successful in 3m12s
Strip healthcare packaging and Care integration from Queue.
Queue is general-purpose QMS only; Care runs its own native engine.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-18 10:15:32 +00:00

195 lines
6.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\Concerns\ScopesApiToAccount;
use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\Organization;
use App\Services\Qms\AuditLogger;
use App\Services\Qms\Industry\IndustryPackageInstaller;
use App\Services\Qms\Industry\IndustryPackageRegistry;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class IntegrationController extends Controller
{
use ScopesApiToAccount;
public function status(Request $request): JsonResponse
{
$owner = $this->ownerRef($request);
$organization = Organization::owned($owner)->first();
if (! $organization) {
return response()->json([
'data' => [
'provisioned' => false,
'onboarded' => false,
'integrations' => ['frontdesk' => false],
],
]);
}
return response()->json([
'data' => [
'provisioned' => true,
'onboarded' => (bool) data_get($organization->settings, 'onboarded', false),
'organization_name' => $organization->name,
'industry' => data_get($organization->settings, 'industry'),
'industry_package' => data_get($organization->settings, 'industry_package.key'),
'integrations' => [
'frontdesk' => (bool) data_get($organization->settings, 'integrations.frontdesk_enabled', false),
],
],
]);
}
public function provision(Request $request): JsonResponse
{
$owner = $this->ownerRef($request);
$caller = (string) $request->attributes->get('service_caller', '');
$registry = app(IndustryPackageRegistry::class);
$validated = $request->validate([
'organization_name' => ['nullable', 'string', 'max:255'],
'branch_name' => ['nullable', 'string', 'max:255'],
'timezone' => ['nullable', 'timezone'],
'industry' => ['nullable', 'string'],
]);
$defaultIndustry = match ($caller) {
'frontdesk' => 'visitor',
'pos' => 'restaurant',
default => 'custom',
};
$industry = $registry->resolveKey($validated['industry'] ?? $defaultIndustry);
$organization = Organization::owned($owner)->first();
$created = false;
$branch = null;
if (! $organization) {
$created = true;
$name = $validated['organization_name'] ?? 'Service queues';
$organization = Organization::create([
'owner_ref' => $owner,
'name' => $name,
'slug' => Str::slug($name).'-'.substr($owner, 0, 6),
'timezone' => $validated['timezone'] ?? config('app.timezone', 'UTC'),
'settings' => [
'onboarded' => true,
'industry' => $industry,
'appointment_mode' => 'hybrid',
'integrations' => [
'frontdesk_enabled' => false,
],
],
]);
$branch = Branch::create([
'owner_ref' => $owner,
'organization_id' => $organization->id,
'name' => $validated['branch_name'] ?? 'Main branch',
'is_active' => true,
]);
AuditLogger::record($owner, 'organization.created', $organization->id, $owner, Organization::class, $organization->id);
} else {
$settings = $organization->settings ?? [];
$settings['onboarded'] = true;
if (! empty($validated['organization_name'])) {
$organization->name = $validated['organization_name'];
}
if (! empty($validated['timezone'])) {
$organization->timezone = $validated['timezone'];
}
$settings['industry'] = $industry;
$organization->forceFill(['settings' => $settings])->save();
$branch = Branch::query()
->where('organization_id', $organization->id)
->where('is_active', true)
->orderBy('id')
->first();
if (! $branch) {
$branch = Branch::create([
'owner_ref' => $owner,
'organization_id' => $organization->id,
'name' => $validated['branch_name'] ?? 'Main branch',
'is_active' => true,
]);
}
}
if ($caller === 'frontdesk') {
$this->syncIntegrationFlag($organization->fresh(), $caller, true);
$organization = $organization->fresh();
}
$packageResult = app(IndustryPackageInstaller::class)->install(
$organization->fresh(),
$branch,
$industry,
[
'actor_ref' => $owner,
'include_optional' => true,
],
);
return response()->json([
'data' => [
'provisioned' => true,
'organization_name' => $organization->fresh()->name,
'industry' => $industry,
'industry_package' => $packageResult,
'integrations' => [
'frontdesk' => (bool) data_get($organization->fresh()->settings, 'integrations.frontdesk_enabled', false),
],
],
], $created ? 201 : 200);
}
public function update(Request $request): JsonResponse
{
$validated = $request->validate([
'frontdesk_enabled' => ['nullable', 'boolean'],
]);
$organization = $this->organization($request);
$caller = (string) $request->attributes->get('service_caller', '');
$settings = $organization->settings ?? [];
$integrations = $settings['integrations'] ?? [];
if (array_key_exists('frontdesk_enabled', $validated) && in_array($caller, ['frontdesk', 'crm'], true)) {
$integrations['frontdesk_enabled'] = (bool) $validated['frontdesk_enabled'];
}
$settings['integrations'] = $integrations;
$organization->update(['settings' => $settings]);
return response()->json([
'data' => [
'integrations' => [
'frontdesk' => (bool) ($integrations['frontdesk_enabled'] ?? false),
],
],
]);
}
protected function syncIntegrationFlag(Organization $organization, string $caller, bool $enabled): void
{
if ($caller !== 'frontdesk') {
return;
}
$settings = $organization->settings ?? [];
$integrations = $settings['integrations'] ?? [];
$integrations['frontdesk_enabled'] = $enabled;
$settings['integrations'] = $integrations;
$organization->update(['settings' => $settings]);
}
}