Samson Mutunga 1 год назад
Родитель
Сommit
9b74ac6e97

+ 35 - 0
app/Http/Controllers/Api/ApiAccountController.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use Illuminate\Http\Request;
+use App\Http\Controllers\Api\ApiBaseController;
+use App\Http\Services\EmailService;
+use App\Models\AccountInvite;
+
+
+class ApiAccountController extends ApiBaseController
+{
+    public $emailService;
+
+    public function __construct(Request $request, EmailService $emailService)
+    {
+        parent::__construct($request);
+        $this->emailService = $emailService;
+    }
+
+    public function create(Request $request){
+        $data = $this->getFormData($request);
+        $response = $this->callJavaApi('/accountInvite/create', $data);
+        if(!@$response['success']){
+            return $this->fail($response['message']);
+        }
+
+        $accountInvite = AccountInvite::where('uid', $response['data'])->first();
+        if($accountInvite){
+            $this->emailService->notifyAccountInvite($accountInvite);
+        }
+        return $this->pass($response['data']);
+    }
+    
+}

+ 4 - 2
app/Http/Controllers/EmailController.php

@@ -6,7 +6,7 @@ use Illuminate\Http\Request;
 use Illuminate\Support\Facades\File;
 
 use App\Http\Services\EmailService;
-use App\Models\Account;
+use App\Models\AccountInvite;
 use App\Models\AppSession;
 use App\Models\Invoice;
 use App\Models\FinancialTransaction;
@@ -57,6 +57,7 @@ class EmailController extends Controller
                 $toName = $this->pro ? $this->pro->displayName() : 'John Doe';
                 $message = 'Client information has been updated';
                 $appointment = Appointment::first();
+                $accountInvite = AccountInvite::first();
                 return view('emails/templates/' . $email, compact(
                     'appUrl', 
                     'toEmailAddress', 
@@ -66,7 +67,8 @@ class EmailController extends Controller
                     'appInternalName', 
                     'emailFromName',
 
-                    'appointment'
+                    'appointment',
+                    'accountInvite'
                 ));
             } else {
                 abort(404);

+ 27 - 0
app/Http/Services/EmailService.php

@@ -3,6 +3,7 @@
 namespace App\Http\Services;
 
 use App\Models\Appointment;
+use App\Models\AccountInvite;
 use Illuminate\Support\Facades\Http;
 use Illuminate\Support\Facades\View;
 
@@ -131,4 +132,30 @@ class EmailService
         return $response;
     }
 
+    public function notifyAccountInvite(AccountInvite $accountInvite)
+    {
+        $toEmailAddress = $this->getValidToEmailAddress($accountInvite->to_email_address);
+        if(!$toEmailAddress) return;
+
+        $appInternalName = $this->appInternalName;
+        $stringMappingConfig = $this->stringMappingConfig;
+        $appUrl = $this->appUrl;
+        $emailFromName = $this->emailFromName;
+        $toEmailAddress = $toEmailAddress;
+        $html = (string) view('emails.templates.account-invite', compact('toEmailAddress', 'accountInvite', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
+        $plainText = '';
+
+        $params = [
+            'subject' => 'Account Invite',
+            'htmlBody' => $html,
+            'plainTextBody' => $plainText,
+            'toAddress' => $toEmailAddress,
+            'fromEmail' => $this->fromEmailAddress,
+            'fromName' => $this->emailFromName,
+        ];
+
+        $response = $this->callJava('/email/send', $params);
+        return $response;
+    }
+
 }

+ 12 - 0
app/Models/Account.php

@@ -8,4 +8,16 @@ class Account extends Model
 {
     protected $table = 'account';
 
+    public function displayName($_flat = true)
+    {
+        $result = '';
+        if($_flat) {
+            $result = $this->name_first . ' ' . $this->name_last;
+        }
+        else {
+            $result = $this->name_last . ', ' . $this->name_first;
+        }
+        return $result;
+    }
+
 }

+ 12 - 0
app/Models/AccountInvite.php

@@ -18,4 +18,16 @@ class AccountInvite extends Model
     public function client() {
         return $this->hasOne(Client::class, 'id', 'for_client_id');
     }
+
+    public function displayName($_flat = true)
+    {
+        $result = '';
+        if($_flat) {
+            $result = $this->first_name . ' ' . $this->last_name;
+        }
+        else {
+            $result = $this->last_name . ', ' . $this->first_name;
+        }
+        return $result;
+    }
 }

+ 2 - 1
resources/views/app/patient/accounts.blade.php

@@ -11,7 +11,8 @@
                 <a href="#" start show class="col-2-button">
                     + Add
                 </a>
-                <form url="/api/accountInvite/create" class="mcp-theme-1">
+                <form url="{{ route('api.account.create') }}" class="mcp-theme-1">
+                    @csrf
                     <input type="hidden" name="forClientUid" value="{{$patient->uid}}">
                     <div class="mb-2">
                         <label class="text-secondary text-sm">Email Address</label>

+ 18 - 0
resources/views/emails/templates/account-invite.blade.php

@@ -0,0 +1,18 @@
+@extends('emails.layout')
+<?php
+$appInternalName = config('app.internalName');
+$appConfig = config('constants.' . $appInternalName);
+?>
+@section('salutation')
+    Hi, {{ $accountInvite->displayName(true) }}!
+@endsection
+
+@section('content')
+    <p class="f-fallback" style="color: #000; font-size: 16px; padding: 0 15px; line-height: 30px; margin: .4em 0 0.1875em;">
+  You have been invited to access a patient record by the name <b>{{ $accountInvite->client->displayName() }}</b>. Please click the link below to create your account.
+</p>
+@include("emails.call-to-action-button", [
+  'link' => config('app.stagfe6_url') . "/create-account/" . $accountInvite->access_token,
+  'label' => 'Create Account'
+])
+@endsection

+ 5 - 0
routes/api.php

@@ -3,6 +3,7 @@
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Route;
 use App\Http\Controllers\Api\ApiAppointmentController;
+use App\Http\Controllers\Api\ApiAccountController;
 
 /*
 |--------------------------------------------------------------------------
@@ -25,5 +26,9 @@ Route::name('api.')->group(function () {
        Route::post('create', [ApiAppointmentController::class, 'create'])->name('create');
        Route::post('request-confirmation', [ApiAppointmentController::class, 'requestConfirmation'])->name('request-confirmation');
     });
+    //Account
+    Route::name('account.')->prefix('account')->group(function () {
+        Route::post('create', [ApiAccountController::class, 'create'])->name('create');
+     });
     
 });