feat(messaging): suite-channel patient email/SMS and entitlement sync
Deploy Ladill Care / deploy (push) Successful in 40s
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:
@@ -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);
|
||||
|
||||
@@ -258,12 +258,24 @@ class ProController extends Controller
|
||||
$settings['auto_renew'] = true;
|
||||
$settings['billing_method'] = 'wallet_monthly';
|
||||
$settings['billed_branches'] = $billedBranches;
|
||||
$settings['plan_expires_at'] = now()
|
||||
->addDays((int) config('care.pro.period_days', 30))
|
||||
->toIso8601String();
|
||||
$expires = now()->addDays((int) config('care.pro.period_days', 30));
|
||||
$settings['plan_expires_at'] = $expires->toIso8601String();
|
||||
unset($settings['plan_renewal_error'], $settings['enterprise_billed_branches']);
|
||||
$organization->update(['settings' => $settings]);
|
||||
|
||||
$billing->syncSuiteEntitlement(
|
||||
$organization->owner_ref,
|
||||
$plan === 'enterprise' ? 'enterprise' : 'pro',
|
||||
'active',
|
||||
$reference,
|
||||
[
|
||||
'period_starts_at' => now()->toIso8601String(),
|
||||
'period_ends_at' => $expires->toIso8601String(),
|
||||
'source' => 'wallet_debit',
|
||||
'metadata' => ['billed_branches' => $billedBranches],
|
||||
],
|
||||
);
|
||||
|
||||
return redirect()->route('care.pro.index')->with('success', $successMessage);
|
||||
}
|
||||
|
||||
@@ -278,8 +290,23 @@ class ProController extends Controller
|
||||
$settings['auto_renew'] = false;
|
||||
$settings['billing_method'] = 'paystack_prepaid';
|
||||
$settings['billed_branches'] = $billedBranches;
|
||||
$settings['plan_expires_at'] = now()->addMonths($months)->toIso8601String();
|
||||
$expires = now()->addMonths($months);
|
||||
$settings['plan_expires_at'] = $expires->toIso8601String();
|
||||
unset($settings['plan_renewal_error'], $settings['enterprise_billed_branches']);
|
||||
$organization->update(['settings' => $settings]);
|
||||
|
||||
// Prepaid already writes entitlements on platform checkout; sync as repair.
|
||||
app(BillingClient::class)->syncSuiteEntitlement(
|
||||
$organization->owner_ref,
|
||||
$plan === 'enterprise' ? 'enterprise' : 'pro',
|
||||
'active',
|
||||
'care-prepaid-sync-'.$organization->id.'-'.now()->format('YmdHis'),
|
||||
[
|
||||
'period_starts_at' => now()->toIso8601String(),
|
||||
'period_ends_at' => $expires->toIso8601String(),
|
||||
'source' => 'checkout',
|
||||
'metadata' => ['billed_branches' => $billedBranches],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user