feat(accound deactivated notification)

This commit is contained in:
2026-04-07 17:09:25 +02:00
parent 6754d8684a
commit ca464e8e06
10 changed files with 236 additions and 14 deletions

View File

@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
/**
@@ -33,7 +34,7 @@ use Illuminate\Notifications\Notifiable;
* @property string|null $website_url
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string|null $deleted_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read string $full_name
* @property-read \App\Models\MemberGroup|null $group
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\IspconfigMember> $ispconfigs
@@ -80,7 +81,7 @@ use Illuminate\Notifications\Notifiable;
*/
class Member extends Model
{
use HasFactory, Notifiable;
use HasFactory, Notifiable, SoftDeletes;
protected $fillable = [
'user_id',
@@ -151,7 +152,7 @@ class Member extends Model
return $this->hasMany(NextCloudMember::class, 'member_id');
}
public function lastMembership(): Membership
public function lastMembership(): ?Membership
{
return $this->memberships()->where('status', 'active')->first();
}
@@ -160,14 +161,21 @@ class Member extends Model
{
$membership = $this->lastMembership();
if ($membership === null) {
return false;
}
return $membership->services()->where('identifier', $serviceIdentifier)->exists();
}
public function isExpired(): bool
{
// Member ayant leur dernière adhésion non renouvellée depuis plus d'un mois
$lastMembership = $this->lastMembership();
if ($lastMembership === null) {
return true;
}
return $lastMembership->status === 'expired' || $lastMembership->created_at->addMonths(1) < now();
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Notifications;
use App\Models\Contact;
use App\Models\NotificationTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ContactNewRequestNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(public readonly Contact $contact) {}
/**
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
public function toMail(object $notifiable): MailMessage
{
$template = NotificationTemplate::findByIdentifier('contact_new_request');
$vars = [
'contact_name' => $this->contact->full_name,
'contact_email' => $this->contact->email ?? '',
'contact_subject' => $this->contact->subject ?? '',
'contact_message' => $this->contact->message ?? '',
'app_name' => config('app.name'),
];
return (new MailMessage)
->subject($template->renderSubject($vars))
->view('notifications.mail-template', [
'body' => $template->renderBody($vars),
]);
}
/**
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [];
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Notifications;
use App\Models\Member;
use App\Models\NotificationTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class MemberDeactivatedAdminNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(public readonly Member $member) {}
/**
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
public function toMail(object $notifiable): MailMessage
{
$template = NotificationTemplate::findByIdentifier('member_deactivated_admin');
$vars = [
'member_name' => $this->member->full_name,
'member_email' => $this->member->email ?? '',
'app_name' => config('app.name'),
];
return (new MailMessage)
->subject($template->renderSubject($vars))
->view('notifications.mail-template', [
'body' => $template->renderBody($vars),
]);
}
/**
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [];
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Notifications;
use App\Models\Member;
use App\Models\NotificationTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class MemberDeactivatedMemberNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(public readonly Member $member) {}
/**
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
public function toMail(object $notifiable): MailMessage
{
$template = NotificationTemplate::findByIdentifier('member_deactivated_member');
$vars = [
'member_name' => $this->member->full_name,
'app_name' => config('app.name'),
];
return (new MailMessage)
->subject($template->renderSubject($vars))
->view('notifications.mail-template', [
'body' => $template->renderBody($vars),
]);
}
/**
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [];
}
}

View File

@@ -3,23 +3,22 @@
namespace App\Services;
use App\Models\Contact;
use App\Notifications\ContactNewRequestNotification;
use Illuminate\Support\Facades\Notification;
class ContactService
{
public function __construct()
{
//
}
public function __construct() {}
public function registerNewContactRequest(array $data): Contact
{
$contact = new Contact();
$contact = new Contact;
$contact->fill($data);
$contact->save();
// Envoyer un email à l'administrateur
Notification::route('mail', config('app.admin_email'))
->notify(new ContactNewRequestNotification($contact));
return $contact;
}
}

View File

@@ -6,6 +6,9 @@ use App\Events\MemberRegistered;
use App\Models\Member;
use App\Models\MemberGroup;
use App\Models\Package;
use App\Notifications\MemberDeactivatedAdminNotification;
use App\Notifications\MemberDeactivatedMemberNotification;
use Illuminate\Support\Facades\Notification;
class MemberService
{
@@ -58,14 +61,16 @@ class MemberService
*/
public function deactivateMember(Member $member): void
{
// todo: send email to member + admin
$member->update(['status' => 'excluded']);
$membership = $member->memberships()
->where('status', 'active')->first();
$membership->update(['status' => 'inactive']);
// On détache les services côté Roxane - à tester
$membership->services()->detach();
$member->notify(new MemberDeactivatedMemberNotification($member));
Notification::route('mail', config('app.admin_email'))
->notify(new MemberDeactivatedAdminNotification($member));
}
}