mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 01:17:30 -05:00 
			
		
		
		
	Fixes for notifications
This commit is contained in:
		
							parent
							
								
									c3561dc58a
								
							
						
					
					
						commit
						5efa8d9de0
					
				@ -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);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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))) {
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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]);
 | 
			
		||||
 | 
			
		||||
@ -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]);
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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]);
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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']));
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user