feat(messaging): suite-channel patient email/SMS and entitlement sync
Deploy Ladill Care / deploy (push) Successful in 40s

Prefer platform suite messaging for patient email/SMS, fall back to product
keys; write suite entitlements on Pro wallet, prepaid, and renewal.
This commit is contained in:
isaacclad
2026-07-16 11:24:21 +00:00
parent 7a0a7fcb88
commit c9f5df6ed7
9 changed files with 409 additions and 128 deletions
@@ -5,6 +5,8 @@ namespace App\Http\Controllers\Care;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Models\Patient;
use App\Services\Billing\PlatformEmailClient;
use App\Services\Billing\PlatformSmsClient;
use App\Services\Care\AuditLogger;
use App\Services\Care\OrganizationResolver;
use App\Services\Messaging\CustomerEmailClient;
@@ -23,6 +25,7 @@ class PatientMessageController extends Controller
Patient $patient,
MessagingCredentialsService $credentials,
CustomerEmailClient $email,
PlatformEmailClient $platformEmail,
): RedirectResponse {
$this->authorizeAbility($request, 'patients.manage');
$this->authorizePatient($request, $patient);
@@ -31,16 +34,6 @@ class PatientMessageController extends Controller
return back()->with('error', 'This patient has no valid email on file.');
}
$credential = $credentials->forOrganization($this->organization($request));
if (! $credential->hasValidBird()) {
return back()->with('error', 'Connect Ladill Bird in Integrations before sending patient email.');
}
$apiKey = $credential->birdApiKey();
if (! $apiKey) {
return back()->with('error', 'Bird credentials could not be decrypted. Reconnect Bird in Integrations.');
}
$data = $request->validate([
'subject' => ['required', 'string', 'max:200'],
'body' => ['required', 'string', 'max:5000'],
@@ -49,15 +42,39 @@ class PatientMessageController extends Controller
$owner = $this->ownerRef($request);
$organization = $this->organization($request);
$html = OrganizationBranding::wrapEmailHtml(nl2br(e($data['body'])), $organization);
$sent = $email->send(
$apiKey,
(string) $credential->bird_from_email,
$credential->bird_from_name,
(string) $patient->email,
$data['subject'],
$html,
$data['body'],
);
$sent = false;
$error = null;
$via = null;
if ($platformEmail->isConfigured()) {
$sent = $platformEmail->send($owner, (string) $patient->email, $data['subject'], $html, $data['body']);
$via = 'suite';
if (! $sent) {
$error = $platformEmail->lastError();
}
}
if (! $sent) {
$credential = $credentials->forOrganization($organization);
if ($credential->hasValidBird() && ($apiKey = $credential->birdApiKey())) {
$sent = $email->send(
$apiKey,
(string) $credential->bird_from_email,
$credential->bird_from_name,
(string) $patient->email,
$data['subject'],
$html,
$data['body'],
);
$via = 'bird';
if (! $sent) {
$error = $email->lastError() ?: $error;
}
} elseif (! $platformEmail->isConfigured()) {
$error = 'Set a Ladill mailbox in Account → Messaging, or connect Ladill Bird in Integrations.';
}
}
AuditLogger::record(
$owner,
@@ -69,12 +86,13 @@ class PatientMessageController extends Controller
[
'to' => $patient->email,
'subject' => $data['subject'],
'error' => $sent ? null : $email->lastError(),
'via' => $via,
'error' => $sent ? null : $error,
],
);
if (! $sent) {
return back()->with('error', $email->lastError() ?: 'Email could not be sent.');
return back()->with('error', $error ?: 'Email could not be sent.');
}
return back()->with('success', 'Email sent to '.$patient->email);
@@ -85,6 +103,7 @@ class PatientMessageController extends Controller
Patient $patient,
MessagingCredentialsService $credentials,
CustomerSmsClient $sms,
PlatformSmsClient $platformSms,
): RedirectResponse {
$this->authorizeAbility($request, 'patients.manage');
$this->authorizePatient($request, $patient);
@@ -93,22 +112,34 @@ class PatientMessageController extends Controller
return back()->with('error', 'This patient has no phone number on file.');
}
$credential = $credentials->forOrganization($this->organization($request));
if (! $credential->hasValidSms()) {
return back()->with('error', 'Connect Ladill SMS in Integrations before sending patient SMS.');
}
$apiKey = $credential->smsApiKey();
if (! $apiKey) {
return back()->with('error', 'SMS credentials could not be decrypted. Reconnect SMS in Integrations.');
}
$data = $request->validate([
'message' => ['required', 'string', 'max:480'],
]);
$owner = $this->ownerRef($request);
$sent = $sms->send($apiKey, (string) $patient->phone, $data['message'], (string) $credential->sms_sender_id);
$organization = $this->organization($request);
$credential = $credentials->forOrganization($organization);
$sent = false;
$error = null;
if ($platformSms->isConfigured()) {
$sender = $credential->sms_sender_id ?: null;
$sent = $platformSms->send($owner, (string) $patient->phone, $data['message'], $sender ? (string) $sender : null);
if (! $sent) {
$error = $platformSms->lastError();
}
}
if (! $sent && $credential->hasValidSms() && ($apiKey = $credential->smsApiKey())) {
$sent = $sms->send($apiKey, (string) $patient->phone, $data['message'], (string) $credential->sms_sender_id);
if (! $sent) {
$error = $sms->lastError() ?? $error;
}
} elseif (! $sent && ! $platformSms->isConfigured()) {
$error = 'Connect Ladill SMS in Integrations, or ensure platform SMS is configured (SMS_API_KEY_CARE).';
}
AuditLogger::record(
$owner,
@@ -119,12 +150,12 @@ class PatientMessageController extends Controller
$patient->id,
[
'to' => $patient->phone,
'error' => $sent ? null : $sms->lastError(),
'error' => $sent ? null : $error,
],
);
if (! $sent) {
return back()->with('error', $sms->lastError() ?: 'SMS could not be sent.');
return back()->with('error', $error ?: 'SMS could not be sent.');
}
return back()->with('success', 'SMS sent to '.$patient->phone);