Add patient SMS/email messaging and fix Meet video visit errors.
Deploy Ladill Care / deploy (push) Successful in 43s
Deploy Ladill Care / deploy (push) Successful in 43s
Wire Care to Ladill SMS and Bird management APIs on the patient page, and surface Meet auth/config failures as friendly validation errors instead of a 502 page. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -27,6 +27,7 @@ class AppointmentMeetController extends Controller
|
||||
$appointment->loadMissing('patient');
|
||||
$patient = $appointment->patient;
|
||||
$owner = $this->ownerRef($request);
|
||||
$user = $request->user();
|
||||
|
||||
$response = MeetClient::for($owner)->createRoom([
|
||||
'title' => 'Video visit — '.$patient->fullName(),
|
||||
@@ -34,6 +35,8 @@ class AppointmentMeetController extends Controller
|
||||
'scheduled_at' => ($appointment->scheduled_at ?? now()->addHour())->toIso8601String(),
|
||||
'duration_minutes' => 30,
|
||||
'branch_id' => $appointment->branch_id,
|
||||
'host_name' => $user?->name,
|
||||
'host_email' => $user?->email,
|
||||
'source' => [
|
||||
'app' => 'care',
|
||||
'entity_type' => 'appointment',
|
||||
@@ -60,11 +63,14 @@ class AppointmentMeetController extends Controller
|
||||
|
||||
$owner = $this->ownerRef($request);
|
||||
$appointment->loadMissing('patient');
|
||||
$user = $request->user();
|
||||
|
||||
if (! $appointment->meet_room_uuid) {
|
||||
$response = MeetClient::for($owner)->createRoom([
|
||||
'title' => 'Video visit — '.$appointment->patient->fullName(),
|
||||
'description' => $appointment->reason,
|
||||
'host_name' => $user?->name,
|
||||
'host_email' => $user?->email,
|
||||
'source' => [
|
||||
'app' => 'care',
|
||||
'entity_type' => 'appointment',
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
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 Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PatientMessageController extends Controller
|
||||
{
|
||||
use ScopesToAccount;
|
||||
|
||||
public function sendEmail(Request $request, Patient $patient, PlatformEmailClient $email): RedirectResponse
|
||||
{
|
||||
$this->authorizeAbility($request, 'patients.manage');
|
||||
$this->authorizePatient($request, $patient);
|
||||
|
||||
if (! filter_var((string) $patient->email, FILTER_VALIDATE_EMAIL)) {
|
||||
return back()->with('error', 'This patient has no valid email on file.');
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'subject' => ['required', 'string', 'max:200'],
|
||||
'body' => ['required', 'string', 'max:5000'],
|
||||
]);
|
||||
|
||||
$owner = $this->ownerRef($request);
|
||||
$html = nl2br(e($data['body']));
|
||||
$sent = $email->send($owner, (string) $patient->email, $data['subject'], $html, $data['body']);
|
||||
|
||||
AuditLogger::record(
|
||||
$owner,
|
||||
$sent ? 'patient.email_sent' : 'patient.email_failed',
|
||||
$patient->organization_id,
|
||||
$owner,
|
||||
Patient::class,
|
||||
$patient->id,
|
||||
[
|
||||
'to' => $patient->email,
|
||||
'subject' => $data['subject'],
|
||||
'error' => $sent ? null : $email->lastError(),
|
||||
],
|
||||
);
|
||||
|
||||
if (! $sent) {
|
||||
return back()->with('error', $email->lastError() ?: 'Email could not be sent.');
|
||||
}
|
||||
|
||||
return back()->with('success', 'Email sent to '.$patient->email);
|
||||
}
|
||||
|
||||
public function sendSms(Request $request, Patient $patient, PlatformSmsClient $sms): RedirectResponse
|
||||
{
|
||||
$this->authorizeAbility($request, 'patients.manage');
|
||||
$this->authorizePatient($request, $patient);
|
||||
|
||||
if (trim((string) $patient->phone) === '') {
|
||||
return back()->with('error', 'This patient has no phone number on file.');
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'message' => ['required', 'string', 'max:480'],
|
||||
]);
|
||||
|
||||
$owner = $this->ownerRef($request);
|
||||
$sent = $sms->send($owner, (string) $patient->phone, $data['message']);
|
||||
|
||||
AuditLogger::record(
|
||||
$owner,
|
||||
$sent ? 'patient.sms_sent' : 'patient.sms_failed',
|
||||
$patient->organization_id,
|
||||
$owner,
|
||||
Patient::class,
|
||||
$patient->id,
|
||||
[
|
||||
'to' => $patient->phone,
|
||||
'error' => $sent ? null : $sms->lastError(),
|
||||
],
|
||||
);
|
||||
|
||||
if (! $sent) {
|
||||
return back()->with('error', $sms->lastError() ?: 'SMS could not be sent.');
|
||||
}
|
||||
|
||||
return back()->with('success', 'SMS sent to '.$patient->phone);
|
||||
}
|
||||
|
||||
protected function authorizePatient(Request $request, Patient $patient): void
|
||||
{
|
||||
$this->authorizeOwner($request, $patient);
|
||||
abort_unless($patient->organization_id === $this->organization($request)->id, 404);
|
||||
|
||||
$branchId = app(OrganizationResolver::class)->branchScope($this->member($request));
|
||||
if ($branchId !== null && $patient->branch_id !== $branchId) {
|
||||
abort(404);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Billing;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PlatformEmailClient
|
||||
{
|
||||
private ?string $lastError = null;
|
||||
|
||||
public function lastError(): ?string
|
||||
{
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
private function base(): string
|
||||
{
|
||||
return rtrim((string) config('smtp.platform_api_url', 'https://ladill.com/api/smtp'), '/');
|
||||
}
|
||||
|
||||
private function token(): string
|
||||
{
|
||||
return (string) config('smtp.platform_api_key', '');
|
||||
}
|
||||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return $this->token() !== '';
|
||||
}
|
||||
|
||||
public function send(string $ownerPublicId, string $to, string $subject, string $html, ?string $text = null): bool
|
||||
{
|
||||
$this->lastError = null;
|
||||
|
||||
if (! $this->isConfigured()) {
|
||||
$this->lastError = 'Outbound email is not configured on this Care instance. Set SMTP_API_KEY_CARE.';
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$res = Http::withToken($this->token())->acceptJson()->timeout(30)
|
||||
->post($this->base().'/messages/send', array_filter([
|
||||
'user' => $ownerPublicId,
|
||||
'from' => config('smtp.from'),
|
||||
'from_name' => config('smtp.from_name'),
|
||||
'to' => $to,
|
||||
'subject' => $subject,
|
||||
'html' => $html,
|
||||
'text' => $text,
|
||||
], fn ($value) => $value !== null && $value !== ''));
|
||||
|
||||
if ($res->status() === 402) {
|
||||
$this->lastError = (string) ($res->json('error') ?: 'Insufficient Bird email credits. Top up Bird and try again.');
|
||||
Log::warning('Care platform email send: insufficient balance', ['user' => $ownerPublicId]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($res->failed()) {
|
||||
$this->lastError = (string) ($res->json('error') ?: $res->json('message') ?: 'The email could not be sent.');
|
||||
Log::warning('Care platform email send failed', ['status' => $res->status(), 'body' => $res->body()]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) ($res->json('success') ?? true);
|
||||
} catch (\Throwable $e) {
|
||||
$this->lastError = 'Could not reach the email service. Please try again.';
|
||||
Log::warning('Care platform email send error', ['error' => $e->getMessage()]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Billing;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PlatformSmsClient
|
||||
{
|
||||
private ?string $lastError = null;
|
||||
|
||||
public function lastError(): ?string
|
||||
{
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
private function base(): string
|
||||
{
|
||||
return rtrim((string) config('sms.platform_api_url', 'https://ladill.com/api'), '/');
|
||||
}
|
||||
|
||||
private function token(): string
|
||||
{
|
||||
return (string) config('sms.platform_api_key', '');
|
||||
}
|
||||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return $this->token() !== '';
|
||||
}
|
||||
|
||||
/** @return list<array<string, mixed>> */
|
||||
public function services(string $ownerPublicId): array
|
||||
{
|
||||
if (! $this->isConfigured()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
$res = Http::withToken($this->token())->acceptJson()->timeout(15)
|
||||
->get($this->base().'/sms/services', ['user' => $ownerPublicId]);
|
||||
|
||||
if ($res->failed()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return (array) ($res->json('data') ?? []);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Care platform SMS services lookup failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function ensureServiceId(string $ownerPublicId): ?int
|
||||
{
|
||||
$services = $this->services($ownerPublicId);
|
||||
if ($services !== []) {
|
||||
return (int) ($services[0]['id'] ?? 0) ?: null;
|
||||
}
|
||||
|
||||
if (! $this->isConfigured()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$res = Http::withToken($this->token())->acceptJson()->timeout(15)
|
||||
->post($this->base().'/sms/services', [
|
||||
'user' => $ownerPublicId,
|
||||
'name' => 'Ladill Care',
|
||||
'brand_name' => 'Care',
|
||||
]);
|
||||
|
||||
if ($res->failed()) {
|
||||
$this->lastError = (string) ($res->json('error') ?: 'Could not create an SMS service for this account.');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int) ($res->json('data.id') ?? 0) ?: null;
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Care platform SMS service create failed', ['error' => $e->getMessage()]);
|
||||
$this->lastError = 'Could not reach the SMS service.';
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function send(string $ownerPublicId, string $to, string $message, ?string $senderId = null): bool
|
||||
{
|
||||
$this->lastError = null;
|
||||
|
||||
if (! $this->isConfigured()) {
|
||||
$this->lastError = 'SMS is not configured on this Care instance. Set SMS_API_KEY_CARE.';
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$serviceId = $this->ensureServiceId($ownerPublicId);
|
||||
if (! $serviceId) {
|
||||
$this->lastError ??= 'No SMS service is available for this account. Open sms.ladill.com once, then retry.';
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$res = Http::withToken($this->token())->acceptJson()->timeout(30)
|
||||
->post($this->base().'/sms/messages/send', array_filter([
|
||||
'user' => $ownerPublicId,
|
||||
'sms_service_id' => $serviceId,
|
||||
'to' => $to,
|
||||
'text' => $message,
|
||||
'sender_id' => $senderId ?? config('sms.default_sender_id'),
|
||||
]));
|
||||
|
||||
if ($res->status() === 402) {
|
||||
$this->lastError = (string) ($res->json('error') ?: 'Insufficient SMS credit. Top up your Ladill SMS wallet and try again.');
|
||||
Log::warning('Care platform SMS send: insufficient balance', ['user' => $ownerPublicId]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($res->failed()) {
|
||||
$this->lastError = (string) ($res->json('error') ?: $res->json('message') ?: 'The SMS could not be sent.');
|
||||
Log::warning('Care platform SMS send failed', ['status' => $res->status(), 'body' => $res->body()]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) ($res->json('success') ?? true);
|
||||
} catch (\Throwable $e) {
|
||||
$this->lastError = 'Could not reach the SMS service. Please try again.';
|
||||
Log::warning('Care platform SMS send error', ['error' => $e->getMessage()]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,12 @@ class MeetClient
|
||||
*/
|
||||
private function post(string $path, array $data): array
|
||||
{
|
||||
if (trim((string) config('meet.key')) === '') {
|
||||
throw ValidationException::withMessages([
|
||||
'meet' => ['Video visits are not configured. Set MEET_API_KEY_CARE on Care and Meet to the same value.'],
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->client()->post($path, $data);
|
||||
} catch (ConnectionException) {
|
||||
@@ -70,8 +76,26 @@ class MeetClient
|
||||
);
|
||||
}
|
||||
|
||||
if ($response->status() === 401 || $response->status() === 403) {
|
||||
throw ValidationException::withMessages([
|
||||
'meet' => ['Video visits are not authorized. Set the same MEET_API_KEY_CARE on Care and Meet, then clear config cache.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($response->status() === 404) {
|
||||
throw ValidationException::withMessages([
|
||||
'meet' => ['No Meet workspace was found for this account. Sign into meet.ladill.com once to create one, then retry.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
abort($response->status() === 404 ? 404 : 502, 'Meet service error.');
|
||||
$message = $response->json('error')
|
||||
?? $response->json('message')
|
||||
?? 'Meet could not schedule this video visit. Please try again.';
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'meet' => [is_string($message) ? $message : 'Meet service error.'],
|
||||
]);
|
||||
}
|
||||
|
||||
return (array) $response->json();
|
||||
|
||||
Reference in New Issue
Block a user