From f92a796d8847cef1a498bc5731af9f3289c2c7cb Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Wed, 13 Jan 2021 13:30:53 +0100 Subject: [PATCH] attachemnt pdf on 2nd email, eneryone in subscriber list gets email --- app/EmailBatch.php | 88 ++++++++++++++++++- app/FileCollection.php | 3 + .../Controllers/Admin/PublishController.php | 38 ++++---- app/Jobs/ProcessEmailBatch.php | 4 +- app/Mail/BatchMail.php | 7 +- .../Metrics/RegistrationCompletePartition.php | 4 +- app/Nova/Metrics/TotalUsers.php | 2 +- app/Nova/Metrics/UsersPartition.php | 5 +- app/PdfFile.php | 18 ++-- app/User.php | 29 +++--- ..._13_000535_add_sends_pdf_to_collection.php | 33 +++++++ resources/views/emails/batch-pdf.blade.php | 1 + 12 files changed, 180 insertions(+), 52 deletions(-) create mode 100644 database/migrations/2021_01_13_000535_add_sends_pdf_to_collection.php diff --git a/app/EmailBatch.php b/app/EmailBatch.php index b999e1f..551b5f3 100644 --- a/app/EmailBatch.php +++ b/app/EmailBatch.php @@ -3,6 +3,7 @@ namespace App; use App\Mail\BatchMail; +use Arr; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use \Illuminate\Database\Eloquent\Relations; @@ -22,6 +23,8 @@ class EmailBatch extends TwillModel { protected $guarded = []; protected $softDeletes = false; + + use BelongsToPdfFile; @@ -34,6 +37,78 @@ class EmailBatch extends TwillModel 'sent_to' ]; + /** + * @param $data + * @return EmailBatch + */ + public static function createFromRequest($data): EmailBatch + { + $batch = new self; + + $recipientSlug = Arr::get($data, 'recipient_group.slug'); + $prefix = $recipientSlug === 'admin' ? + '** TEST ** ' : + ''; + + $batch->subject = $prefix . Arr::get($data, 'email.subject', ''); + $batch->content = [ + 'body' => Arr::get($data, 'email.content'), + 'link' => Arr::get($data, 'email.link'), + 'html' => Arr::get($data, 'email.html'), + 'type' => Arr::get($data, 'email.type', 0) //0: newsletter, 1: PDF + + ]; + $batch->file_id = Arr::get($data, 'file.id'); + $batch->sent_to = []; + + $batch->save(); + + + return $batch; + + } + + /** + * @param $data + * @return EmailBatch|null + */ + public static function createAttachmentMailFromRequest($data): ?EmailBatch + { + $fileId = Arr::get($data, 'file.id'); + /** @var PdfFile $file */ + + if($fileId === null) { + return null; + } + + $file = PdfFile::find($fileId); + + if(!$file || ! $file->collection->sends_pdf) { + return null; + } + + $batch = new self; + $recipientSlug = Arr::get($data, 'recipient_group.slug'); + $prefix = $recipientSlug === 'admin' ? + '** TEST ** [Version PDF]' : + '[Version PDF] '; + + $batch->subject = $prefix . Arr::get($data, 'email.subject', ''); + $batch->file_id = $fileId; + + $batch->content = [ + 'body' => "Bonjour, ci-joint la version PDF de {$file->title}", + 'type' => 0 + + ]; + $batch->sent_to = []; + + $batch->save(); + + return $batch; + + } + /** * @return Relations\HasMany */ @@ -82,10 +157,21 @@ class EmailBatch extends TwillModel $this->content['body'], $this->subject, $this->content['link'] ?? null, - optional($this->file)->coverUrl + optional($this->file)->coverUrl, + $this->getView() ))->render(); } + /** + * @return int + */ + public function getView() + { + if($this->content['type'] == 1) { + return BatchMail::MAIL_TYPE_PDF_FILE; + } + return BatchMail::MAIL_TYPE_NEWSLETTER; + } /** * @return string */ diff --git a/app/FileCollection.php b/app/FileCollection.php index 26334f4..06a59e3 100644 --- a/app/FileCollection.php +++ b/app/FileCollection.php @@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Model; * @property string $slug * @property string $name * @property string $short_name + * @property bool $sends_pdf * @method static FileCollection findOrFail($id) FileCollection */ class FileCollection extends Model @@ -19,6 +20,8 @@ class FileCollection extends Model public $timestamps = false; + protected $guarded = []; + public function files() { return $this->hasMany(PdfFile::class, 'collection_id'); diff --git a/app/Http/Controllers/Admin/PublishController.php b/app/Http/Controllers/Admin/PublishController.php index ec0f4dd..9973344 100644 --- a/app/Http/Controllers/Admin/PublishController.php +++ b/app/Http/Controllers/Admin/PublishController.php @@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Support\Arr; +use PharIo\Manifest\Email; class PublishController extends Controller @@ -40,34 +41,28 @@ class PublishController extends Controller */ public function publish(Request $request) { - $batch = new EmailBatch(); $data = $request->input('data'); - //0: newsletter, 1: PDF - $type = Arr::get($data, 'email.type', 0); - $recipientGroup = Arr::get($data, 'recipient_group'); - $recipientSlug = Arr::get($data, 'recipient_group.slug'); - $prefix = $recipientSlug === 'admin' ? - '** TEST ** ' : - ''; - - $batch->subject = $prefix . Arr::get($data, 'email.subject', ''); - $batch->content = [ - 'body' => Arr::get($data, 'email.content'), - 'link' => Arr::get($data, 'email.link'), - 'html' => Arr::get($data, 'email.html') - ]; - $batch->file_id = Arr::get($data, 'file.id'); - $batch->sent_to = []; - $batch->save(); + $recipientGroup = Arr::get($data, 'recipient_group'); - $builder = $recipientGroup === null ? + /** @var Builder $usersBuilder */ + $usersBuilder = $recipientGroup === null ? User::receivesEmails() : PublishController::sendGroups()[$recipientGroup['slug']]['builder']; - $processEmailBatch = new ProcessEmailBatch($batch, $builder); - dispatch($processEmailBatch->withFile()); + $userReceivesPdfBuilder = clone $usersBuilder; + + $batch = EmailBatch::createFromRequest($data); + $processEmailBatch = new ProcessEmailBatch($batch, $usersBuilder); + dispatch($processEmailBatch); + + if($pdfBatch = EmailBatch::createAttachmentMailFromRequest($data)) { + $userReceivesPdfBuilder->receivesPdf(); + $pdfProcessEmailBatch = new ProcessEmailBatch($pdfBatch, $userReceivesPdfBuilder); + dispatch($pdfProcessEmailBatch->withFile()); + + } return ['data' => $batch->id]; } @@ -160,6 +155,7 @@ class PublishController extends Controller $request->input('subject'), $request->input('link'), $request->input('image'), + BatchMail::MAIL_TYPE_PDF_FILE ))->render(); } diff --git a/app/Jobs/ProcessEmailBatch.php b/app/Jobs/ProcessEmailBatch.php index 4deefd0..f5ed9c8 100644 --- a/app/Jobs/ProcessEmailBatch.php +++ b/app/Jobs/ProcessEmailBatch.php @@ -80,7 +80,6 @@ class ProcessEmailBatch implements ShouldQueue $this->users = $usersBuilder->get(); - $this->html = $this->batch->content['html']; $this->replaceVariables(); } @@ -187,8 +186,7 @@ class ProcessEmailBatch implements ShouldQueue - $view = $this->html ?? $this->batch->renderFromContent(); - + $view = $this->batch->render(); $params = [ diff --git a/app/Mail/BatchMail.php b/app/Mail/BatchMail.php index 177be53..bc6a37c 100644 --- a/app/Mail/BatchMail.php +++ b/app/Mail/BatchMail.php @@ -44,8 +44,9 @@ class BatchMail extends Mailable * @param string $subject * @param string|null $link * @param null $image + * @param int $type */ - public function __construct($content, $subject, $link = null, $image = null) + public function __construct($content, $subject, $link = null, $image = null, $type = self::MAIL_TYPE_NEWSLETTER) { // $this->subject = $subject; @@ -53,7 +54,7 @@ class BatchMail extends Mailable $this->image = $image; - $this->emailView = self::getView(self::MAIL_TYPE_PDF_FILE); + $this->emailView = self::getView($type); $this->content = $this->emailView === 'batch-pdf' ? (Html2Markdown::convert($content)) : @@ -64,6 +65,8 @@ class BatchMail extends Mailable } } + + /** * @param $id * @return string diff --git a/app/Nova/Metrics/RegistrationCompletePartition.php b/app/Nova/Metrics/RegistrationCompletePartition.php index a2337db..8c2dbb0 100644 --- a/app/Nova/Metrics/RegistrationCompletePartition.php +++ b/app/Nova/Metrics/RegistrationCompletePartition.php @@ -20,8 +20,8 @@ class RegistrationCompletePartition extends Partition public function calculate(NovaRequest $request) { return $this->result([ - 'Compte activé' => User::registeredUser()->registrationComplete()->count(), - 'Compte non activé' => User::registeredUser()->registrationIncomplete()->count(), + 'Compte activé' => User::subscriber()->registrationComplete()->count(), + 'Compte non activé' => User::subscriber()->registrationIncomplete()->count(), ]); } diff --git a/app/Nova/Metrics/TotalUsers.php b/app/Nova/Metrics/TotalUsers.php index 6a70a84..389f96e 100644 --- a/app/Nova/Metrics/TotalUsers.php +++ b/app/Nova/Metrics/TotalUsers.php @@ -18,7 +18,7 @@ class TotalUsers extends Value */ public function calculate(NovaRequest $request) { - return $this->result(User::registeredUser()->count()); + return $this->result(User::subscriber()->count()); } diff --git a/app/Nova/Metrics/UsersPartition.php b/app/Nova/Metrics/UsersPartition.php index 0e271ef..59dc4b5 100644 --- a/app/Nova/Metrics/UsersPartition.php +++ b/app/Nova/Metrics/UsersPartition.php @@ -20,11 +20,10 @@ class UsersPartition extends Partition public function calculate(NovaRequest $request) { return $this->result([ - 'Abonnés (groupe)' => $g = User::hasOrgSubscription()->count(), - 'Abonnés (indiv)' => $i = User::hasIndSubscription()->count(), + 'Abonnés' => $g = User::subscriber()->count(), "Période d'essai" => $t = User::isOnTrial()->count(), "Découverte" => $d = User::isOnDiscovery()->count(), - "Reste" => User::count() - $i - $g - $t -$d + "Reste" => User::count() - $g - $t -$d ]); } diff --git a/app/PdfFile.php b/app/PdfFile.php index df1f31d..b323d8f 100644 --- a/app/PdfFile.php +++ b/app/PdfFile.php @@ -104,13 +104,15 @@ class PdfFile extends TwillModel implements Sortable /** @var PdfFile $pdfFile */ - $pdfFile = self::create([ - 'ref' => $ref, - 'collection_id' => $collection_id, + $pdfFile = self::query()->updateOrCreate([ 'slug' => Str::slug("{$collection->slug}_{$ref}"), + 'published' => 0, + 'collection_id' => $collection_id, + ], + [ + 'ref' => $ref, 'title' => "{$collection->short_name} - $ref", 'headlines' => $headlines, - 'published' => 0, ]); @@ -429,9 +431,9 @@ class PdfFile extends TwillModel implements Sortable */ public function getMailableUrl(User $user): string { - if($user->receives_pdf) { - return $this->getDownloadUrl(); - } +// if($user->receives_pdf) { +// return $this->getDownloadUrl(); +// } return ($user->type === User::TYPE_DISCOVER && !$user->reg_complete) ? $this->getSignedUrl($user) : $this->getUrlWithToken($user); @@ -669,6 +671,8 @@ class PdfFile extends TwillModel implements Sortable $builder->where('is_free', true); } + + /** * @return string */ diff --git a/app/User.php b/app/User.php index 90cb6f4..4634fba 100644 --- a/app/User.php +++ b/app/User.php @@ -138,6 +138,10 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails 'badge' => 'success', 'label' => 'Abonnement actif (indep)' ], + 'subscribed' => [ + 'badge' => 'success', + 'label' => 'Abonnement actif' + ], 'trial' => [ 'badge' => 'info', 'label' => "Période d'essai" @@ -318,7 +322,7 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails */ public function hasValidSubscription(): bool { - return $this->orgIsSubscribed() || $this->subscribed(); + return $this->type === self::TYPE_SUBSCRIBER; } /** @@ -431,10 +435,11 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails /** * @param Builder $builder + * @deprecated */ public function scopeHasActiveSubscription(Builder $builder): void { - $builder->hasOrgSubscription()->orWhere->hasIndSubscription(); + $builder->subscriber(); } /** @@ -473,7 +478,7 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails /** * @param Builder $builder */ - public function scopeRegisteredUser(Builder $builder): void + public function scopeSubscriber(Builder $builder): void { $builder->where('type', self::TYPE_SUBSCRIBER); } @@ -523,6 +528,14 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails } + /** + * @param Builder $builder + */ + public function scopeReceivesPdf(Builder $builder): void + { + $builder->where('receives_pdf', 1); + } + @@ -562,15 +575,7 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails switch($this->type) { case self::TYPE_SUBSCRIBER: - if($this->orgIsSubscribed) { - $id = 'org_subscribed'; - } - if($this->subscribed()) { - $id = 'ind_subscribed'; - } - if($this->onTrial()) { - $id = 'trial'; - } + $id = 'subscribed'; break; case self::TYPE_PROSPECT: diff --git a/database/migrations/2021_01_13_000535_add_sends_pdf_to_collection.php b/database/migrations/2021_01_13_000535_add_sends_pdf_to_collection.php new file mode 100644 index 0000000..51e3d51 --- /dev/null +++ b/database/migrations/2021_01_13_000535_add_sends_pdf_to_collection.php @@ -0,0 +1,33 @@ +boolean('sends_pdf')->default(0); + }); + \App\FileCollection::query()->first()->update(['sends_pdf' => true]); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('collections', function (Blueprint $table) { + $table->boolean('sends_pdf')->default(0); + }); + } +} diff --git a/resources/views/emails/batch-pdf.blade.php b/resources/views/emails/batch-pdf.blade.php index c6099d4..c75360c 100644 --- a/resources/views/emails/batch-pdf.blade.php +++ b/resources/views/emails/batch-pdf.blade.php @@ -12,6 +12,7 @@ +
{!! $content !!} -- 2.39.5