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

Feature - Notify users on presentation/company update #562

Merged
merged 9 commits into from
Oct 28, 2024
5 changes: 5 additions & 0 deletions app/Http/Controllers/Crew/CompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace App\Http\Controllers\Crew;

use App\Events\CompanyRolesNotified;
use App\Http\Controllers\Controller;
use App\Jobs\NotifyCompanyRoles;
use App\Mail\CompanyApprovedMailable;
use App\Mail\CompanyDeletedMailable;
use App\Mail\CompanyDisapprovedMailable;
use App\Mail\CompanyRepInvitation;
use App\Models\Company;
Expand Down Expand Up @@ -133,6 +136,8 @@ public function destroy(Company $company)
abort(403);
}

NotifyCompanyRoles::dispatchSync('company representative', $company, CompanyDeletedMailable::class);

foreach ($company->users as $user) {
$user->syncRoles(Role::findByName('participant', 'web'));
}
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Crew/PresentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\StorePresentationRequest;
use App\Jobs\NotifyPresentationRoles;
use App\Mail\PresentationApprovedMailable;
use App\Mail\PresentationDeletedMailable;
use App\Mail\PresentationDisapprovedMailable;
use App\Models\Company;
use App\Models\Presentation;
Expand Down Expand Up @@ -141,6 +143,8 @@ public function destroy(Presentation $presentation)
abort(403);
}

NotifyPresentationRoles::dispatch('speaker', $presentation, PresentationDeletedMailable::class);

$presentation->delete();

return redirect(route('moderator.presentations.index'))
Expand Down
51 changes: 51 additions & 0 deletions app/Jobs/NotifyCompanyRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Jobs;

use App\Models\Company;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class NotifyCompanyRoles implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(
public string $receiver,
public Company $company,
public string $emailTemplate,
) {
}

/**
* Execute the job.
*/
public function handle(): void
{
if (!$this->company->is_approved) {
return;
}

$users = collect();
if ($this->receiver == 'crew') {
$users = User::role(['event organizer', 'assistant organizer'])->get();
} else if ($this->receiver == 'company representative') {
if ($representative = $this->company->representative) {
$users->push($representative);
}
}

// Send emails to the users
foreach ($users as $user) {
Mail::to($user->email)->send(new $this->emailTemplate($user, $this->company));
}
}
}
49 changes: 49 additions & 0 deletions app/Jobs/NotifyPresentationRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Jobs;

use App\Models\Presentation;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class NotifyPresentationRoles implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(
public string $receiver,
public Presentation $presentation,
public string $emailTemplate,
) {
}

/**
* Execute the job.
*/
public function handle(): void
{
if (!$this->presentation->is_approved) {
return;
}

$users = collect();
if ($this->receiver == 'crew') {
$users = User::role(['event organizer', 'speakers supervisor', 'assistant organizer'])->get();
} else if ($this->receiver == 'speaker') {
$users = $this->presentation->speakers;
}

// Send emails to the users
foreach ($users as $user) {
Mail::to($user->email)->send(new $this->emailTemplate($user, $this->presentation));
}
}
}
10 changes: 10 additions & 0 deletions app/Livewire/Company/EditCompanyModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Livewire\Company;

use App\Jobs\NotifyCompanyRoles;
use App\Livewire\Forms\CompanyForm;
use App\Mail\CompanyUpdatedMailable;
use App\Models\Company;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\View;
Expand Down Expand Up @@ -42,9 +44,17 @@ public function save()
$this->form->update();

if (Auth::user()->id == $this->company->representative->id) {
if (!$this->company->isSameCompany($this->form->company)) {
NotifyCompanyRoles::dispatch('crew', $this->company, CompanyUpdatedMailable::class);
}

return redirect(route('company.details'))
->with('status', 'Company successfully updated.');
} else {
if (!$this->company->isSameCompany($this->form->company)) {
NotifyCompanyRoles::dispatch('company representative', $this->company, CompanyUpdatedMailable::class);
}

return redirect(route('moderator.companies.show', $this->company))
->with('status', 'Company successfully updated.');
}
Expand Down
10 changes: 10 additions & 0 deletions app/Livewire/Presentation/EditPresentationModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Livewire\Presentation;

use App\Jobs\NotifyPresentationRoles;
use App\Livewire\Forms\PresentationForm;
use App\Mail\PresentationUpdatedMailable;
use App\Models\Presentation;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -38,9 +40,17 @@ public function save()
$this->form->update();

if (Auth::user()->presenter_of) {
if (!$this->presentation->isSamePresentation($this->form->presentation)) {
NotifyPresentationRoles::dispatch('crew', $this->presentation, PresentationUpdatedMailable::class);
}

return redirect(route('presentations.show', $this->presentation))
->with('status', 'Presentation successfully updated.');
} else {
if (!$this->presentation->isSamePresentation($this->form->presentation)) {
NotifyPresentationRoles::dispatch('speaker', $this->presentation, PresentationUpdatedMailable::class);
}

return redirect(route('moderator.presentations.show', $this->presentation))
->with('status', 'Presentation successfully updated.');
}
Expand Down
56 changes: 56 additions & 0 deletions app/Mail/CompanyDeletedMailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Mail;

use App\Models\Company;
use App\Models\User;
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 CompanyDeletedMailable extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(
public User $user,
public Company $company,
) {
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Your Company Was Deleted',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.company-deleted',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
56 changes: 56 additions & 0 deletions app/Mail/CompanyUpdatedMailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Mail;

use App\Models\Company;
use App\Models\User;
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 CompanyUpdatedMailable extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(
public User $user,
public Company $company,
) {
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Company Details Updated',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.company-updated',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
56 changes: 56 additions & 0 deletions app/Mail/PresentationDeletedMailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Mail;

use App\Models\Presentation;
use App\Models\User;
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 PresentationDeletedMailable extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(
public User $user,
public Presentation $presentation,
) {
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Your Presentation Was Deleted',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.presentation-deleted',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Loading
Loading