Deploy Ladill Queue / deploy (push) Successful in 58s
Queue orgs were created with care_enabled false, blocking counters API; provision now syncs the caller integration flag automatically. Co-authored-by: Cursor <cursoragent@cursor.com>
161 lines
5.9 KiB
PHP
161 lines
5.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\Department;
|
|
use App\Models\Organization;
|
|
use App\Services\Qms\AuditLogger;
|
|
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,
|
|
'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', '');
|
|
|
|
$validated = $request->validate([
|
|
'organization_name' => ['nullable', 'string', 'max:255'],
|
|
'branch_name' => ['nullable', 'string', 'max:255'],
|
|
'timezone' => ['nullable', 'timezone'],
|
|
'industry' => ['nullable', 'string'],
|
|
]);
|
|
|
|
$organization = Organization::owned($owner)->first();
|
|
$created = false;
|
|
|
|
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' => $validated['industry'] ?? ($caller === 'care' ? 'healthcare' : 'visitor'),
|
|
'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,
|
|
]);
|
|
|
|
Department::create([
|
|
'owner_ref' => $owner,
|
|
'branch_id' => $branch->id,
|
|
'name' => 'Reception',
|
|
'type' => 'reception',
|
|
'is_active' => true,
|
|
]);
|
|
|
|
AuditLogger::record($owner, 'organization.created', $organization->id, $owner, Organization::class, $organization->id);
|
|
}
|
|
|
|
if (in_array($caller, ['care', 'frontdesk'], true)) {
|
|
$this->syncIntegrationFlag($organization->fresh(), $caller, true);
|
|
}
|
|
|
|
return response()->json([
|
|
'data' => [
|
|
'provisioned' => true,
|
|
'organization_name' => $organization->fresh()->name,
|
|
'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]);
|
|
}
|
|
}
|