mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-28 17:03:59 +00:00
Laravel Vapor - test attaching a file in an email + upload file to S3 (code sync from shipping portal)
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Jobs\Commands\V2;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\DocumentType;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Models\User;
|
||||
use ZipArchive;
|
||||
|
||||
class TestAttachingS3FileAndSendEmailV2CommandJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
Log::info(Carbon::now() . ': Start job - Test to attach a file from S3 bucket and trigger an email send.');
|
||||
$start = new Carbon();
|
||||
|
||||
Auth::login(User::findOrFail(1));
|
||||
$zip_file = 'do_'.Carbon::yesterday()->format('Y_m_d').'.zip';
|
||||
$attachment = storage_path().'/'.$zip_file;
|
||||
Log::info("[TESTING] attachment: $attachment");
|
||||
|
||||
$startDate = Carbon::now()->subMonth();
|
||||
Log::info("[TESTING] startDate: ".$startDate->toDateTimeString());
|
||||
$endDate = Carbon::yesterday();
|
||||
|
||||
$bookings = \App\Models\Booking::where('status', ApprovalStatus::COMPLETED)->whereDate('updated_at', '>=', $startDate)->whereDate('updated_at', '<=', $endDate)
|
||||
->whereHas('transactions', function ($query){
|
||||
return $query->where('type', TransactionType::PAYMENT)->whereHas('transactions', function ($query){
|
||||
return $query->where('issuer', 2);
|
||||
});
|
||||
})->get();
|
||||
Log::info("[TESTING] total bookings: " . count($bookings));
|
||||
|
||||
if(!count($bookings)){ return; }
|
||||
|
||||
$filesystemDriver = Storage::getDefaultDriver();
|
||||
if($filesystemDriver === 's3'){
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($attachment, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
|
||||
//STEP 1: Go through each of the booking from query
|
||||
$counter = 0;
|
||||
foreach ($bookings as $booking) {
|
||||
$file = $booking->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()->files()->first();
|
||||
$fileContent = Storage::disk('s3')->get('documents/'.$file->file->file_info->original->file);
|
||||
$zip->addFromString($booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf', $fileContent);
|
||||
|
||||
$counter++;
|
||||
if ($counter >= 5) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
|
||||
//STEP 2: Upload the zip file to S3
|
||||
$filePathForS3 = 'temp/'.$zip_file;
|
||||
Storage::disk('s3')->put($filePathForS3, file_get_contents($attachment));
|
||||
|
||||
Log::info('DownloadBookingDocumentLogic finished processing files to zip count: '.count($bookings));
|
||||
$temporaryUrl = Storage::disk('s3')->temporaryUrl(
|
||||
$filePathForS3,
|
||||
Carbon::now()->addMinutes(10)
|
||||
);
|
||||
Log::info("[TESTING] total bookings: $temporaryUrl");
|
||||
|
||||
//STEP 3: Send Email
|
||||
// Mail::raw( "Attention to VT Admin team:\r\n\r\nKindly refer to the attachment for our DAILY DO COMPILATION ".$startDate->format('d-m-Y')." - ".$endDate->format('d-m-Y').".\r\n\r\n**This is an automatically generated email – please do not reply to it. If you have any queries kindly contact our admin team through Wechat.\r\n\r\n\r\nCIEF WORLDWIDE SDN BHD", function($message) use ($attachment, $startDate, $endDate){
|
||||
// $message->from('exchange@cief-malaysia.com');
|
||||
// $message->to(['vtnation16@gmail.com', 'vtnation@gmail.com', 'atvantic04@gmail.com', 'vtnation2@gmail.com']);
|
||||
// $message->cc(['shafiqa_sukeri@cief-malaysia.com', 'frontendcief@gmail.com', 'pm@cief-malaysia.com', 'hasan@cief-malaysia.com', 'shipping_admin@cief-malaysia.com', 'hasanakbar27@gmail.com', 'pmwong2019@gmail.com', 'uldvstar@gmail.com']);
|
||||
// $message->subject('CIEF DO COMPILATION '.$startDate->format('d-m-Y').' - '.$endDate->format('d-m-Y'));
|
||||
|
||||
// $message->attach($attachment);
|
||||
// });
|
||||
|
||||
File::delete($attachment);
|
||||
Log::info('success');
|
||||
}
|
||||
}
|
||||
|
||||
$end = new Carbon();
|
||||
$elapsedTime = $start->diff($end)->format('%H:%I:%S');
|
||||
Log::info(Carbon::now() . ': End job - Test to attach a file from S3 bucket and trigger an email send. ElapsedTime: ' . $elapsedTime . '.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\V2;
|
||||
|
||||
|
||||
use App\Classes\Jobs\Commands\V2\TestAttachingS3FileAndSendEmailV2CommandJob;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class TestAttachingS3FileAndSendEmailV2Command extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'test-attach-s3-file-in-email-command';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'This is a test to attach a file from S3 bucket and trigger an email send';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
TestAttachingS3FileAndSendEmailV2CommandJob::dispatch();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user