Deploy Ladill Queue / deploy (push) Successful in 1m17s
Deleting the org mid afterResponse() reseed sent Care→Queue SSO into onboarding; preserve org/owner and reaffirm onboarded instead. Co-authored-by: Cursor <cursoragent@cursor.com>
207 lines
7.8 KiB
PHP
207 lines
7.8 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' => ['care' => false, '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' => [
|
|
'care' => (bool) data_get($organization->settings, 'integrations.care_enabled', false),
|
|
'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) {
|
|
'care' => 'healthcare',
|
|
'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' => [
|
|
'care_enabled' => false,
|
|
'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 (in_array($caller, ['care', 'frontdesk'], true)) {
|
|
$this->syncIntegrationFlag($organization->fresh(), $caller, true);
|
|
$organization = $organization->fresh();
|
|
}
|
|
|
|
// Care owns clinical stage graphs; still apply healthcare terminology/package metadata.
|
|
$packageResult = app(IndustryPackageInstaller::class)->install(
|
|
$organization->fresh(),
|
|
$branch,
|
|
$industry,
|
|
[
|
|
'actor_ref' => $owner,
|
|
'skip_stages' => $caller === 'care',
|
|
'include_optional' => true,
|
|
],
|
|
);
|
|
|
|
return response()->json([
|
|
'data' => [
|
|
'provisioned' => true,
|
|
'organization_name' => $organization->fresh()->name,
|
|
'industry' => $industry,
|
|
'industry_package' => $packageResult,
|
|
'integrations' => [
|
|
'care' => (bool) data_get($organization->fresh()->settings, 'integrations.care_enabled', false),
|
|
'frontdesk' => (bool) data_get($organization->fresh()->settings, 'integrations.frontdesk_enabled', false),
|
|
],
|
|
],
|
|
], $created ? 201 : 200);
|
|
}
|
|
|
|
public function update(Request $request): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'care_enabled' => ['nullable', 'boolean'],
|
|
'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('care_enabled', $validated) && in_array($caller, ['care', 'crm'], true)) {
|
|
$integrations['care_enabled'] = (bool) $validated['care_enabled'];
|
|
}
|
|
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' => [
|
|
'care' => (bool) ($integrations['care_enabled'] ?? false),
|
|
'frontdesk' => (bool) ($integrations['frontdesk_enabled'] ?? false),
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
protected function syncIntegrationFlag(Organization $organization, string $caller, bool $enabled): void
|
|
{
|
|
if (! in_array($caller, ['care', 'frontdesk'], true)) {
|
|
return;
|
|
}
|
|
|
|
$settings = $organization->settings ?? [];
|
|
$integrations = $settings['integrations'] ?? [];
|
|
$key = $caller.'_enabled';
|
|
$integrations[$key] = $enabled;
|
|
$settings['integrations'] = $integrations;
|
|
$organization->update(['settings' => $settings]);
|
|
}
|
|
}
|