Преглед на файлове

Merge branch 'master' of https://rav.triplestart.com/tigerphp/lemon-admin

Peter Muturi преди 7 месеца
родител
ревизия
aeaa24ef75

+ 13 - 38
app/Http/Controllers/AdminController.php

@@ -524,50 +524,25 @@ class AdminController extends Controller
   }
 
   public function emailAttachment(Request $request){
-    $params = [
-        'to' => [
-            ['email' => $request->get('toEmail'), 'name' => $request->get('toName')]
-        ],
-        'template' => 'attachment',
-        'subject' => $request->get('subject'),
-        'template_params' => [
-            'toEmail' => $request->get('toEmail'),
-            'toName' => $request->get('toName'),
-            'message_body' => $request->get('message')
-        ],
-        'file' => $request->get('attachmentPath')
-    ];
-
-    try{
-        $this->sendEmailNotification($params);
-        return $this->pass();
-    }catch(Exception $e){
-        return $this->fail($e->getMessage());
-    }
+    $params = $request->only(['toEmail', 'toName', 'subject', 'message', 'attachmentPath']);
+    $response = $this->emailService->sendEmailWithAttachment($params);
+    return $this->pass();
   }
 
   public function emailReport(Request $request, StoreOrder $order){
     $results_attachment = $order->getDetailJsonValue('results_attachment');
-    $params = [
-        'to' => [
-            ['email' => $order->user->getEmail(), 'name' => $order->user->displayName()]
-        ],
-        'template' => 'results-report',
-        'subject' => 'Test Results for Order #' . $order->iid,
-        'template_params' => [
-            'user' => $order->user,
-            'order' => $order,
-            'message_body' => $request->get('message')
-        ],
-        'file' => $results_attachment['path']
+
+    $emailParams = [
+        'toEmail' => $order->user->getEmail(), 
+        'toName' => $order->user->displayName(), 
+        'subject' => 'Test Results for Order #' . $order->iid, 
+        'message' => $request->get('message'), 
+        'attachmentPath' => $results_attachment['path']
     ];
 
-    try{
-        $this->sendEmailNotification($params);
-        return $this->pass();
-    }catch(Exception $e){
-        return $this->fail($e->getMessage());
-    }
+    $response = $this->emailService->sendEmailWithAttachment($emailParams);
+    return $this->pass();
+
   }
 
   public function smsReport(Request $request, StoreOrder $order){

+ 60 - 3
app/Http/Services/EmailService.php

@@ -6,6 +6,7 @@ use App\Models\PromoCode;
 use App\Models\StoreOrder;
 use Illuminate\Support\Facades\Http;
 use Illuminate\Support\Facades\View;
+use Illuminate\Support\Facades\Storage;
 
 use App\Models\User;
 
@@ -29,16 +30,31 @@ class EmailService
         $this->stringMappingConfig = config('constants.' . $this->appInternalName);
     }
 
-    protected function callJava($endPoint, $data, $sessionKey)
+    protected function callJava($endPoint, $data, $sessionKey, $attachment = null)
     {
         $data['secret'] = $this->secret;
         $url = config('app.backendUrl') . $endPoint;
-        $response =   Http::asForm()
+        if($attachment){
+            $response =   Http::asMultipart()
+            ->attach(
+                $attachment['fileName'],
+                $attachment['contents'],
+                $attachment['name']
+            )
             ->withHeaders([
                 'sessionKey' => $sessionKey
             ])
             ->post($url, $data)
             ->body();
+        }else{
+            $response =   Http::asForm()
+            ->withHeaders([
+                'sessionKey' => $sessionKey
+            ])
+            ->post($url, $data)
+            ->body();
+        }
+        
 
         return json_decode($response, true);
     }
@@ -54,7 +70,7 @@ class EmailService
         $appUrl = $this->appUrl;
         $emailFromName = $this->emailFromName;
         $html = (string) view('emails.templates.user-reset-password', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
-        $plainText = (string) view('emails.templates.user-reset-password-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
+        $plainText = (string) null;
 
         $params = [
             'fromEmailAddress' => $this->fromEmailAddress,
@@ -215,4 +231,45 @@ class EmailService
 
         $response = $this->callJava('/api/email/send', $params, null);
     }
+
+    public function sendEmailWithAttachment($params)
+    {
+        if (!@$params['toEmail']) return;
+        $appInternalName = $this->appInternalName;
+        $stringMappingConfig = $this->stringMappingConfig;
+        $appUrl = $this->appUrl;
+        $emailFromName = $this->emailFromName;
+        $html = (string) view('emails.templates.attachment', compact('params', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
+        $plainText = (string) '';
+
+        $attachment = $this->getAttachmentFromStoragePath($params['attachmentPath'], 'attachment1');
+
+
+        $emailParams = [
+            'fromEmailAddress' => $this->fromEmailAddress,
+            'fromName' => $this->emailFromName,
+            'toEmailAddress' => @$params['toEmail'],
+            'subject' => @$params['subject'],
+            'contentHtml' => $html,
+            'contentText' => $plainText,
+            'entityType' => null,
+            'entityUid' => null,
+            'attachment1' => $attachment
+        ];
+
+        $response = $this->callJava('/api/email/send', $emailParams, null, $attachment);
+    }
+
+    protected function getAttachmentFromStoragePath($path, $fileName){
+        if (Storage::exists($path)) {
+            return [
+                'fileName' => $fileName,
+                'name' => basename($path),
+                'contents' => Storage::get($path),
+                'mime' => Storage::mimeType($path),
+            ];
+        }
+    
+        return null;
+    }
 }

+ 13 - 1
resources/views/app/my-account/admin/orders/partials/lab-requests.blade.php

@@ -8,10 +8,22 @@
         @include("app.my-account.admin.orders.forms.upload-lab-tests-request-attachment")
     @else
     <div class="d-flex align-items-center">
-        <span class="me-1">Request PDF: <i class="fas fa-paperclip fa-fw"></i> <a href="{{ $lab_tests_request_attachment['attachmentAccessTokenUrl'] .'?fileType=lab_tests_request_attachment' }}" target="_blank">{{ $lab_tests_request_attachment['orginalFileName'] }}</a></span>
+        <span class="me-1">Lab Request PDF: <i class="fas fa-paperclip fa-fw"></i> <a href="{{ $lab_tests_request_attachment['attachmentAccessTokenUrl'] .'?fileType=lab_tests_request_attachment' }}" target="_blank">{{ $lab_tests_request_attachment['orginalFileName'] }}</a></span>
         <div>
             @include("app.my-account.admin.orders.forms.upload-lab-tests-request-attachment", ['edit' => true])
         </div>
+        <span class="text-muted mx-1">|</span>
+        <div>
+            @include('app.my-account.admin.orders.forms.email-attachment', [
+                'title' => 'Email Lab Request PDF',
+                'toEmail' => $order->user->getEmail(),
+                'toName' => $order->user->displayName(),
+                'subject' => 'Lab Request PDF',
+                'attachmentUrl' => $lab_tests_request_attachment['attachmentAccessTokenUrl'] . '?fileType=lab_tests_request_attachment',
+                'attachmentName' => $lab_tests_request_attachment['orginalFileName'],
+                'attachmentPath' => $lab_tests_request_attachment['path']
+            ])
+        </div>
     </div>
     @endif
 </div>

+ 16 - 0
resources/views/app/my-account/admin/orders/sub/dashboard.blade.php

@@ -57,6 +57,7 @@
                         <div class="d-flex align-items-center">
                             <div>
                                 <?php
+                                    $lab_tests_request_attachment = $order->getDetailJsonValue('lab_tests_request_attachment');
                                     $partner_results_attachment = $order->getDetailJsonValue('partner_results_attachment');
                                 ?>
                                 @if($partner_results_attachment)
@@ -85,6 +86,21 @@
                                     ])
                                 </div>
                             @endif
+
+                            @if($lab_tests_request_attachment)
+                                <span class="text-muted mx-1">|</span>
+                                <div>
+                                    @include('app.my-account.admin.orders.forms.email-attachment', [
+                                        'title' => 'Email Lab Request PDF',
+                                        'toEmail' => $order->getPartnerEmail(),
+                                        'toName' => '',
+                                        'subject' => 'Lab Request PDF',
+                                        'attachmentUrl' => $lab_tests_request_attachment['attachmentAccessTokenUrl'] . '?fileType=lab_tests_request_attachment',
+                                        'attachmentName' => $lab_tests_request_attachment['orginalFileName'],
+                                        'attachmentPath' => $lab_tests_request_attachment['path']
+                                    ])
+                                </div>
+                            @endif
                         </div>
                     </div>
                 </div>

+ 3 - 2
resources/views/emails/templates/attachment.blade.php

@@ -1,14 +1,15 @@
 <?php
     $appUrl = config('app.url');
+
 ?>
 
 @extends('emails.layout')
 @section('salutation')
-    Hi, {{$toName}}!
+    Hi, {{$params['toName']}}!
 @endsection
 
 @section('content')
     <p class="f-fallback" style="color: #000; font-size: 15px; padding: 0 15px; line-height: 24px; margin: .4em 0 0.1875em;">
-    <?= nl2br($message_body) ?>
+    <?= nl2br($params['message']) ?>
     </p>
 @endsection