From 5efa8d9de0bcbb84bd7320f8c775a64eca3ed2eb Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 25 Jan 2023 08:26:32 +1100 Subject: [PATCH] Fixes for notifications --- app/Jobs/Inventory/AdjustProductInventory.php | 18 ++--- app/Jobs/Quote/QuoteCheckExpired.php | 2 +- .../Credit/CreditCreatedNotification.php | 2 +- .../Credit/CreditEmailedNotification.php | 2 +- .../Invoice/InvoiceCreatedNotification.php | 2 +- .../Invoice/InvoiceEmailedNotification.php | 2 +- .../InvoiceFailedEmailNotification.php | 2 +- .../Misc/InvitationViewedListener.php | 3 +- .../PurchaseOrderAcceptedListener.php | 2 +- .../PurchaseOrderCreatedListener.php | 2 +- .../PurchaseOrderEmailedNotification.php | 2 +- .../Quote/QuoteApprovedNotification.php | 2 +- .../Quote/QuoteCreatedNotification.php | 2 +- .../Quote/QuoteEmailedNotification.php | 2 +- .../Traits/Notifications/UserNotifies.php | 36 +++++----- tests/Feature/Notify/NotificationTest.php | 65 ++++++++++++++++++- 16 files changed, 104 insertions(+), 42 deletions(-) diff --git a/app/Jobs/Inventory/AdjustProductInventory.php b/app/Jobs/Inventory/AdjustProductInventory.php index 4673367b4b02..cfe674f19cf6 100644 --- a/app/Jobs/Inventory/AdjustProductInventory.php +++ b/app/Jobs/Inventory/AdjustProductInventory.php @@ -148,21 +148,21 @@ class AdjustProductInventory implements ShouldQueue $nmo->company = $this->company; $nmo->settings = $this->company->settings; - // $product->company_users->each(function ($cu) use($product, $nmo){ + $this->company->company_users->each(function ($cu) use($product, $nmo){ - // if($this->checkNotificationExists($cu, $product, ['inventory_all', 'inventory_user'])) - // { + if($this->checkNotificationExists($cu, $product, ['inventory_all', 'inventory_user'])) + { - // $nmo->to_user = $cu->user; - // NinjaMailerJob::dispatch($nmo); + $nmo->to_user = $cu->user; + NinjaMailerJob::dispatch($nmo); - // } + } - // }); + }); - $nmo->to_user = $this->company->owner(); + // $nmo->to_user = $this->company->owner(); - NinjaMailerJob::dispatch($nmo); + // NinjaMailerJob::dispatch($nmo); } } diff --git a/app/Jobs/Quote/QuoteCheckExpired.php b/app/Jobs/Quote/QuoteCheckExpired.php index 36126a957b3a..bdbb31258772 100644 --- a/app/Jobs/Quote/QuoteCheckExpired.php +++ b/app/Jobs/Quote/QuoteCheckExpired.php @@ -117,7 +117,7 @@ class QuoteCheckExpired implements ShouldQueue } /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_expired', 'quote_expired_all']); + $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_expired', 'quote_expired_all', 'quote_expired_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/Credit/CreditCreatedNotification.php b/app/Listeners/Credit/CreditCreatedNotification.php index d66e3f7c21ec..ca8b569afd40 100644 --- a/app/Listeners/Credit/CreditCreatedNotification.php +++ b/app/Listeners/Credit/CreditCreatedNotification.php @@ -57,7 +57,7 @@ class CreditCreatedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'credit'); /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($credit->invitations()->first(), $company_user, 'credit', ['all_notifications', 'credit_created', 'credit_created_all']); + $methods = $this->findUserNotificationTypes($credit->invitations()->first(), $company_user, 'credit', ['all_notifications', 'credit_created', 'credit_created_all', 'credit_created_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/Credit/CreditEmailedNotification.php b/app/Listeners/Credit/CreditEmailedNotification.php index 7d94f864fdbc..f9b124ff3ade 100644 --- a/app/Listeners/Credit/CreditEmailedNotification.php +++ b/app/Listeners/Credit/CreditEmailedNotification.php @@ -54,7 +54,7 @@ class CreditEmailedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'credit'); - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'credit', ['all_notifications', 'credit_sent', 'credit_sent_all']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'credit', ['all_notifications', 'credit_sent', 'credit_sent_all', 'credit_sent_user']); if (($key = array_search('mail', $methods)) !== false) { // if (($key = array_search('mail', $methods))) { diff --git a/app/Listeners/Invoice/InvoiceCreatedNotification.php b/app/Listeners/Invoice/InvoiceCreatedNotification.php index f3c71c1b7b53..4ab01495f026 100644 --- a/app/Listeners/Invoice/InvoiceCreatedNotification.php +++ b/app/Listeners/Invoice/InvoiceCreatedNotification.php @@ -63,7 +63,7 @@ class InvoiceCreatedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'invoice'); /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($invoice->invitations()->first(), $company_user, 'invoice', ['all_notifications', 'invoice_created', 'invoice_created_all']); + $methods = $this->findUserNotificationTypes($invoice->invitations()->first(), $company_user, 'invoice', ['all_notifications', 'invoice_created', 'invoice_created_all', 'invoice_created_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index 9dd91d0115d0..9e59e46453f5 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -61,7 +61,7 @@ class InvoiceEmailedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'invoice'); /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent', 'invoice_sent_all']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent', 'invoice_sent_all', 'invoice_sent_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php index a684b680cef3..27439e229cc0 100644 --- a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php +++ b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php @@ -56,7 +56,7 @@ class InvoiceFailedEmailNotification foreach ($event->invitation->company->company_users as $company_user) { $user = $company_user->user; - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent', 'invoice_sent_all']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent', 'invoice_sent_all', 'invoice_sent_user']); if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); diff --git a/app/Listeners/Misc/InvitationViewedListener.php b/app/Listeners/Misc/InvitationViewedListener.php index ab7d88f05aba..327aca2325b1 100644 --- a/app/Listeners/Misc/InvitationViewedListener.php +++ b/app/Listeners/Misc/InvitationViewedListener.php @@ -68,8 +68,9 @@ class InvitationViewedListener implements ShouldQueue foreach ($invitation->company->company_users as $company_user) { $entity_viewed = "{$entity_name}_viewed"; $entity_viewed_all = "{$entity_name}_viewed_all"; + $entity_viewed_user = "{$entity_name}_viewed_user"; - $methods = $this->findUserNotificationTypes($invitation, $company_user, $entity_name, ['all_notifications', $entity_viewed, $entity_viewed_all]); + $methods = $this->findUserNotificationTypes($invitation, $company_user, $entity_name, ['all_notifications', $entity_viewed, $entity_viewed_all, $entity_viewed_user]); if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderAcceptedListener.php b/app/Listeners/PurchaseOrder/PurchaseOrderAcceptedListener.php index ff64295ee7b5..802a82582530 100644 --- a/app/Listeners/PurchaseOrder/PurchaseOrderAcceptedListener.php +++ b/app/Listeners/PurchaseOrder/PurchaseOrderAcceptedListener.php @@ -59,7 +59,7 @@ class PurchaseOrderAcceptedListener implements ShouldQueue } /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($purchase_order->invitations()->first(), $company_user, 'purchase_order', ['all_notifications', 'purchase_order_accepted', 'purchase_order_accepted_all']); + $methods = $this->findUserNotificationTypes($purchase_order->invitations()->first(), $company_user, 'purchase_order', ['all_notifications', 'purchase_order_accepted', 'purchase_order_accepted_all', 'purchase_order_accepted_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderCreatedListener.php b/app/Listeners/PurchaseOrder/PurchaseOrderCreatedListener.php index 088aaf81c380..3d22d4fb056e 100644 --- a/app/Listeners/PurchaseOrder/PurchaseOrderCreatedListener.php +++ b/app/Listeners/PurchaseOrder/PurchaseOrderCreatedListener.php @@ -63,7 +63,7 @@ class PurchaseOrderCreatedListener implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'purchase_order'); /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($purchase_order->invitations()->first(), $company_user, 'purchase_order', ['all_notifications', 'purchase_order_created', 'purchase_order_created_all']); + $methods = $this->findUserNotificationTypes($purchase_order->invitations()->first(), $company_user, 'purchase_order', ['all_notifications', 'purchase_order_created', 'purchase_order_created_all', 'purchase_order_created_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php b/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php index 36cc203526cf..9def7f062112 100644 --- a/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php +++ b/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php @@ -61,7 +61,7 @@ class PurchaseOrderEmailedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'purchase_order'); /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'purchase_order', ['all_notifications', 'purchase_order_sent', 'purchase_order_sent_all']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'purchase_order', ['all_notifications', 'purchase_order_sent', 'purchase_order_sent_all', 'purchase_order_sent_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/Quote/QuoteApprovedNotification.php b/app/Listeners/Quote/QuoteApprovedNotification.php index 92611258467e..47bb68867fc0 100644 --- a/app/Listeners/Quote/QuoteApprovedNotification.php +++ b/app/Listeners/Quote/QuoteApprovedNotification.php @@ -61,7 +61,7 @@ class QuoteApprovedNotification implements ShouldQueue } /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_approved', 'quote_approved_all']); + $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_approved', 'quote_approved_all', 'quote_approved_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/Quote/QuoteCreatedNotification.php b/app/Listeners/Quote/QuoteCreatedNotification.php index 9ae90d50cb0e..dfecf70cd2bb 100644 --- a/app/Listeners/Quote/QuoteCreatedNotification.php +++ b/app/Listeners/Quote/QuoteCreatedNotification.php @@ -63,7 +63,7 @@ class QuoteCreatedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'quote'); /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_created', 'quote_created_all']); + $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_created', 'quote_created_all', 'quote_created_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/Quote/QuoteEmailedNotification.php b/app/Listeners/Quote/QuoteEmailedNotification.php index a4bd87845459..490248310417 100644 --- a/app/Listeners/Quote/QuoteEmailedNotification.php +++ b/app/Listeners/Quote/QuoteEmailedNotification.php @@ -56,7 +56,7 @@ class QuoteEmailedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'quote'); - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'quote', ['all_notifications', 'quote_sent', 'quote_sent_all']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'quote', ['all_notifications', 'quote_sent', 'quote_sent_all', 'quote_sent_user']); if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); diff --git a/app/Utils/Traits/Notifications/UserNotifies.php b/app/Utils/Traits/Notifications/UserNotifies.php index d4fd338971b1..23c45146088d 100644 --- a/app/Utils/Traits/Notifications/UserNotifies.php +++ b/app/Utils/Traits/Notifications/UserNotifies.php @@ -43,7 +43,7 @@ trait UserNotifies //if a user owns this record or is assigned to it, they are attached the permission for notification. if ($invitation->{$entity_name}->user_id == $company_user->user_id || $invitation->{$entity_name}->assigned_user_id == $company_user->user_id) { - $required_permissions = $this->addSpecialUserPermissionForEntity($invitation->{$entity_name}, $required_permissions); + // $required_permissions = $this->addSpecialUserPermissionForEntity($invitation->{$entity_name}, $required_permissions); } else { $required_permissions = $this->removeSpecialUserPermissionForEntity($invitation->{$entity_name}, $required_permissions); } @@ -83,24 +83,24 @@ trait UserNotifies private function addSpecialUserPermissionForEntity($entity, array $required_permissions) :array { - array_merge($required_permissions, ['all_notifications']); + return array_merge($required_permissions, ['all_notifications', 'all_user_notifications']); - switch ($entity) { - case $entity instanceof Payment || $entity instanceof Client: //we pass client also as this is the proxy for Payment Failures (ie, there is no payment) - return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'payment_failure_user', 'payment_success_user']); - case $entity instanceof Invoice: - return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'invoice_created_user', 'invoice_sent_user', 'invoice_viewed_user', 'invoice_late_user']); - case $entity instanceof Quote: - return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'quote_created_user', 'quote_sent_user', 'quote_viewed_user', 'quote_approved_user', 'quote_expired_user']); - case $entity instanceof Credit: - return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'credit_created_user', 'credit_sent_user', 'credit_viewed_user']); - case $entity instanceof PurchaseOrder: - return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'purchase_order_created_user', 'purchase_order_sent_user', 'purchase_order_viewed_user']); - case $entity instanceof Product: - return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'inventory_user', 'inventory_all']); - default: - return []; - } + // switch ($entity) { + // case $entity instanceof Payment || $entity instanceof Client: //we pass client also as this is the proxy for Payment Failures (ie, there is no payment) + // return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'payment_failure_user', 'payment_success_user']); + // case $entity instanceof Invoice: + // return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'invoice_created_user', 'invoice_sent_user', 'invoice_viewed_user', 'invoice_late_user']); + // case $entity instanceof Quote: + // return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'quote_created_user', 'quote_sent_user', 'quote_viewed_user', 'quote_approved_user', 'quote_expired_user']); + // case $entity instanceof Credit: + // return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'credit_created_user', 'credit_sent_user', 'credit_viewed_user']); + // case $entity instanceof PurchaseOrder: + // return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'purchase_order_created_user', 'purchase_order_sent_user', 'purchase_order_viewed_user']); + // case $entity instanceof Product: + // return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'inventory_user', 'inventory_all']); + // default: + // return []; + // } } private function removeSpecialUserPermissionForEntity($entity, $required_permissions) diff --git a/tests/Feature/Notify/NotificationTest.php b/tests/Feature/Notify/NotificationTest.php index f82b105ad01e..0afee0a28c1a 100644 --- a/tests/Feature/Notify/NotificationTest.php +++ b/tests/Feature/Notify/NotificationTest.php @@ -15,6 +15,8 @@ namespace Tests\Feature\Notify; use App\DataMapper\CompanySettings; use App\Models\CompanyToken; use App\Models\CompanyUser; +use App\Models\Invoice; +use App\Models\InvoiceInvitation; use App\Models\Product; use App\Models\User; use App\Utils\Traits\Notifications\UserNotifies; @@ -42,6 +44,65 @@ class NotificationTest extends TestCase $this->makeTestData(); } + + public function testEntityViewedNotificationWithEntityLate() + { + // ['all_notifications', 'all_user_notifications', 'invoice_created_user', 'invoice_sent_user', 'invoice_viewed_user', 'invoice_late_user']; + $u = User::factory()->create([ + 'account_id' => $this->account->id, + 'email' => $this->faker->safeEmail(), + 'confirmation_code' => uniqid("st",true), + ]); + + $company_token = new CompanyToken; + $company_token->user_id = $u->id; + $company_token->company_id = $this->company->id; + $company_token->account_id = $this->account->id; + $company_token->name = 'test token'; + $company_token->token = Str::random(64); + $company_token->is_system = true; + $company_token->save(); + + $u->companies()->attach($this->company->id, [ + 'account_id' => $this->account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'is_locked' => 0, + 'notifications' => CompanySettings::notificationDefaults(), + 'settings' => null, + ]); + + $company_user = CompanyUser::where('user_id', $u->id)->where('company_id', $this->company->id)->first(); + + $notifications = new \stdClass; + $notifications->email = ["invoice_late_user","quote_approved_user"]; + $company_user->update(['notifications' => (array)$notifications]); + + $i = Invoice::factory()->create([ + 'user_id' => $u->id, + 'company_id' => $this->company->id, + 'number' => uniqid("st",true), + 'client_id' => $this->client->id, + ]); + + $invitation = InvoiceInvitation::factory()->create([ + 'user_id' => $u->id, + 'company_id' => $this->company->id, + 'invoice_id' => $i->id, + 'client_contact_id' => $this->client->contacts->first()->id, + ]); + + $methods = $this->findUserNotificationTypes($invitation, $company_user, 'invoice', ['all_notifications', 'invoice_late_user']); + + $this->assertCount(1, $methods); + + $methods = $this->findUserNotificationTypes($invitation, $company_user, 'invoice', ['all_notifications', 'invoice_viewed', 'invoice_viewed_all']); + + $this->assertCount(0, $methods); + + } + + public function testNotificationFound() { $notifications = new \stdClass; @@ -101,7 +162,6 @@ class NotificationTest extends TestCase } - public function testAllNotificationsDoesNotFiresForUser() { $u = User::factory()->create([ @@ -170,12 +230,13 @@ class NotificationTest extends TestCase 'company_id' => $this->company->id ]); - $methods = $this->findUserEntityNotificationType($p, $cu, []); + $methods = $this->findUserEntityNotificationType($p, $cu, ['inventory_user']); nlog($methods); $this->assertCount(1, $methods); + $this->assertTrue($this->checkNotificationExists($cu, $p, ['inventory_all', 'inventory_user'])); }