Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Monthly resource digest #591

Closed
wants to merge 15 commits into from
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Dusting
c380d69ba5f0813410af5eba98c3d3b83e4c288f
b7707e028a9838565a51d617382b0f2690bcd5cb
2ae654b39c858a304cc91ea72c8fc12deb43618c
8a091e69260ae3f2a92d3eecb43edac75a4a70ac
fd3f549866aaf85fe4e57e275fdbf149773ee635
4 changes: 1 addition & 3 deletions app/Completable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace App;

interface Completable
{
}
interface Completable {}
48 changes: 48 additions & 0 deletions app/Console/Commands/SendResourceDigestEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Console\Commands;

use App\Mail\ResourceDigestEmail;
use App\Models\Resource;
use App\Models\User;
use Carbon\Carbon;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class SendResourceDigestEmail extends Command
{
protected $signature = 'send:send-resource-digest-email';
protected $description = 'Send the monthly resource digest email';

public function __construct()
{
parent::__construct();
}

public function handle()
{
$resources = Resource::where('created_at', '>=', Carbon::now()->subDays(30))->get();

if ($resources->isEmpty()) {
$this->info('No resources created in the last 30 days. Email not sent.');

return;
}

$data = $resources->toArray();

User::where('is_subscriber', true)->chunk(100, function ($subscribedUsers) use ($data) {
foreach ($subscribedUsers as $user) {
try {
Mail::to($user->email)->queue(new ResourceDigestEmail($data));
} catch (Exception $e) {
Log::error('Failed to send email to ' . $user->email . ': ' . $e->getMessage());
}
}
});

$this->info('Monthly resource digest sent successfully to all subscribed users.');
}
}
5 changes: 4 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace App\Console;

use App\Console\Commands\SendResourceDigestEmail;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
protected $commands = [
//
SendResourceDigestEmail::class,
];

/**
Expand All @@ -18,6 +19,8 @@ protected function schedule(Schedule $schedule): void
{
$schedule->command('resource:expired -N')
->weeklyOn(Schedule::FRIDAY, '06:00');

$schedule->command('send:send-resource-digest-email')->monthly();
}

/**
Expand Down
43 changes: 43 additions & 0 deletions app/Mail/ResourceDigestEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class ResourceDigestEmail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

public $resources;

public function __construct($resources)
{
$this->resources = $resources;
}

public function envelope()
{
return new Envelope(
subject: 'New Onramp Resources!',
from: '[email protected]',
);
}

public function content()
{
return new Content(
markdown: 'emails.resource-digest',
with: ['resources' => $this->resources],
);
}

public function attachments(): array
{
return [];
}
}
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
'preferences' => 'object',
'is_subscriber' => 'boolean',
];

public function track()
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
"guzzlehttp/guzzle": "^7.2",
"kg-bot/laravel-localization-to-vue": "dev-l10-compatibility",
"laravel/framework": "^10.3",
"laravel/nova": "4.33.3",
"laravel/slack-notification-channel": "^2.5",
"laravel/socialite": "^5.6",
"laravel/tinker": "^2.8",
"laravel/ui": "^4.2",
"larswiegers/laravel-translations-checker": "^0.4",
"nathanheffley/laravel-slack-blocks": "^2.3",
"spatie/laravel-translatable": "^6.7",
"spatie/nova-translatable": "^4.0",
"stechstudio/laravel-ssh-tunnel": "^3.3",
"tightenco/nova-releases": "^1.0",
"tightenco/suggested-resources-shortcuts": "dev-mla/resource-digest",
"tightenco/ziggy": "^1.5"
},
"require-dev": {
Expand Down Expand Up @@ -95,7 +99,7 @@
"@php artisan key:generate --ansi"
],
"post-install-cmd": [
"[ $COMPOSER_DEV_MODE -eq 1 ] || composer require laravel/nova:^4.0 spatie/nova-translatable spatie/laravel-translatable tightenco/nova-releases tightenco/suggested-resources-shortcuts"
"[ $COMPOSER_DEV_MODE -eq 1 ] && composer require laravel/nova:4.33.3 spatie/nova-translatable spatie/laravel-translatable tightenco/nova-releases tightenco/suggested-resources-shortcuts"
],
"lint": [
"vendor/bin/duster lint"
Expand Down
Loading