Deploy Ladill Care / deploy (push) Successful in 1m7s
Drop wallet/brand marketing language so credential setup reads as neutral configuration. Co-authored-by: Cursor <cursoragent@cursor.com>
153 lines
5.3 KiB
PHP
153 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Care;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
|
|
use App\Services\Care\AuditLogger;
|
|
use App\Services\Care\CarePermissions;
|
|
use App\Services\Messaging\CustomerSmsClient;
|
|
use App\Services\Messaging\MessagingCredentialsService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class IntegrationsController extends Controller
|
|
{
|
|
use ScopesToAccount;
|
|
|
|
public function edit(Request $request, MessagingCredentialsService $credentials): View
|
|
{
|
|
$this->authorizeAbility($request, 'settings.view');
|
|
$organization = $this->organization($request);
|
|
$canManage = app(CarePermissions::class)->can($this->member($request), 'settings.manage');
|
|
|
|
return view('care.integrations.edit', [
|
|
'organization' => $organization,
|
|
'canManage' => $canManage,
|
|
'credential' => $credentials->forOrganization($organization),
|
|
]);
|
|
}
|
|
|
|
public function saveSms(Request $request, MessagingCredentialsService $credentials): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'settings.manage');
|
|
$organization = $this->organization($request);
|
|
|
|
$data = $request->validate([
|
|
'sms_api_key' => ['required', 'string', 'max:200'],
|
|
'sms_sender_id' => ['required', 'string', 'max:11'],
|
|
]);
|
|
|
|
$result = $credentials->validateAndSaveSms(
|
|
$organization,
|
|
$data['sms_api_key'],
|
|
$data['sms_sender_id'],
|
|
);
|
|
|
|
AuditLogger::record(
|
|
$this->ownerRef($request),
|
|
($result['ok'] ?? false) ? 'integrations.sms_connected' : 'integrations.sms_failed',
|
|
$organization->id,
|
|
$this->ownerRef($request),
|
|
\App\Models\Organization::class,
|
|
$organization->id,
|
|
['prefix' => substr(trim($data['sms_api_key']), 0, 16)],
|
|
);
|
|
|
|
if (! ($result['ok'] ?? false)) {
|
|
return back()->withInput()->with('error', $result['error'] ?? 'Could not save SMS credentials.');
|
|
}
|
|
|
|
return back()->with('success', 'SMS credentials saved.');
|
|
}
|
|
|
|
public function disconnectSms(Request $request, MessagingCredentialsService $credentials): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'settings.manage');
|
|
$organization = $this->organization($request);
|
|
$credentials->disconnectSms($organization);
|
|
|
|
AuditLogger::record(
|
|
$this->ownerRef($request),
|
|
'integrations.sms_disconnected',
|
|
$organization->id,
|
|
$this->ownerRef($request),
|
|
\App\Models\Organization::class,
|
|
$organization->id,
|
|
);
|
|
|
|
return back()->with('success', 'SMS disconnected.');
|
|
}
|
|
|
|
public function saveBird(Request $request, MessagingCredentialsService $credentials): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'settings.manage');
|
|
$organization = $this->organization($request);
|
|
|
|
$data = $request->validate([
|
|
'bird_api_key' => ['required', 'string', 'max:200'],
|
|
'bird_from_email' => ['required', 'email', 'max:255'],
|
|
'bird_from_name' => ['nullable', 'string', 'max:100'],
|
|
]);
|
|
|
|
$result = $credentials->validateAndSaveBird(
|
|
$organization,
|
|
$data['bird_api_key'],
|
|
$data['bird_from_email'],
|
|
$data['bird_from_name'] ?? null,
|
|
);
|
|
|
|
AuditLogger::record(
|
|
$this->ownerRef($request),
|
|
($result['ok'] ?? false) ? 'integrations.bird_connected' : 'integrations.bird_failed',
|
|
$organization->id,
|
|
$this->ownerRef($request),
|
|
\App\Models\Organization::class,
|
|
$organization->id,
|
|
['prefix' => substr(trim($data['bird_api_key']), 0, 16)],
|
|
);
|
|
|
|
if (! ($result['ok'] ?? false)) {
|
|
return back()->withInput()->with('error', $result['error'] ?? 'Could not save Bird credentials.');
|
|
}
|
|
|
|
return back()->with('success', 'Email credentials saved.');
|
|
}
|
|
|
|
public function disconnectBird(Request $request, MessagingCredentialsService $credentials): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'settings.manage');
|
|
$organization = $this->organization($request);
|
|
$credentials->disconnectBird($organization);
|
|
|
|
AuditLogger::record(
|
|
$this->ownerRef($request),
|
|
'integrations.bird_disconnected',
|
|
$organization->id,
|
|
$this->ownerRef($request),
|
|
\App\Models\Organization::class,
|
|
$organization->id,
|
|
);
|
|
|
|
return back()->with('success', 'Email disconnected.');
|
|
}
|
|
|
|
public function previewSenders(Request $request, CustomerSmsClient $sms): JsonResponse
|
|
{
|
|
$this->authorizeAbility($request, 'settings.manage');
|
|
|
|
$data = $request->validate([
|
|
'sms_api_key' => ['required', 'string', 'max:200'],
|
|
]);
|
|
|
|
$result = $sms->senders(trim($data['sms_api_key']));
|
|
if (! ($result['ok'] ?? false)) {
|
|
return response()->json(['error' => $result['error'] ?? 'Could not load senders.'], 422);
|
|
}
|
|
|
|
return response()->json($result['data'] ?? []);
|
|
}
|
|
}
|