From b56167da3946eb60d48bcddec523a4b159c11f89 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 11 Dec 2021 21:12:48 +1100 Subject: [PATCH] Fixes for duplicate quote approved notification --- app/Http/Controllers/Auth/LoginController.php | 106 ++++++++++++++---- .../Quote/QuoteApprovedNotification.php | 9 -- app/Services/Quote/QuoteService.php | 8 +- 3 files changed, 88 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 2d8ea8fef702..c0c53faa0bd3 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -102,7 +102,6 @@ class LoginController extends BaseController * * @return Response|User Process user login. * - * * @throws \Illuminate\Validation\ValidationException * @OA\Post( * path="/api/v1/login", @@ -386,13 +385,31 @@ class LoginController extends BaseController $cu = CompanyUser::query() ->where('user_id', auth()->user()->id); - $cu->first()->account->companies->each(function ($company) use($cu){ + // $cu->first()->account->companies->each(function ($company) use($cu){ - if($company->tokens()->where('is_system', true)->count() == 0) + // if($company->tokens()->where('is_system', true)->count() == 0) + // { + // CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); + // } + // }); + + + if($existing_user->company_users()->count() != $existing_user->tokens()->count()) { - CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); + + $existing_user->companies->each(function($company) use($existing_user){ + + if(!CompanyToken::where('user_id', $existing_user->id)->where('company_id', $company->id)->exists()){ + + CreateCompanyToken::dispatchNow($company, $existing_user, "Google_O_Auth"); + + } + + }); + } - }); + + if(Ninja::isHosted() && !$cu->first()->is_owner && !$existing_user->account->isEnterpriseClient()) return response()->json(['message' => 'Pro / Free accounts only the owner can log in. Please upgrade'], 403); @@ -420,13 +437,30 @@ class LoginController extends BaseController $cu = CompanyUser::query() ->where('user_id', auth()->user()->id); - $cu->first()->account->companies->each(function ($company) use($cu){ + // $cu->first()->account->companies->each(function ($company) use($cu){ - if($company->tokens()->where('is_system', true)->count() == 0) + // if($company->tokens()->where('is_system', true)->count() == 0) + // { + // CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); + // } + // }); + + if($existing_login_user->company_users()->count() != $existing_login_user->tokens()->count()) { - CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); + + $existing_login_user->companies->each(function($company) use($existing_login_user){ + + if(!CompanyToken::where('user_id', $existing_login_user->id)->where('company_id', $company->id)->exists()){ + + CreateCompanyToken::dispatchNow($company, $existing_login_user, "Google_O_Auth"); + + } + + }); + } - }); + + if(Ninja::isHosted() && !$cu->first()->is_owner && !$existing_login_user->account->isEnterpriseClient()) return response()->json(['message' => 'Pro / Free accounts only the owner can log in. Please upgrade'], 403); @@ -458,13 +492,30 @@ class LoginController extends BaseController $cu = CompanyUser::query() ->where('user_id', auth()->user()->id); - $cu->first()->account->companies->each(function ($company) use($cu){ + // $cu->first()->account->companies->each(function ($company) use($cu){ - if($company->tokens()->where('is_system', true)->count() == 0) + // if($company->tokens()->where('is_system', true)->count() == 0) + // { + // CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); + // } + // }); + + + if($existing_login_user->company_users()->count() != $existing_login_user->tokens()->count()) { - CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); + + $existing_login_user->companies->each(function($company) use($existing_login_user){ + + if(!CompanyToken::where('user_id', $existing_login_user->id)->where('company_id', $company->id)->exists()){ + + CreateCompanyToken::dispatchNow($company, $existing_login_user, "Google_O_Auth"); + + } + + }); + } - }); + if(Ninja::isHosted() && !$cu->first()->is_owner && !$existing_login_user->account->isEnterpriseClient()) return response()->json(['message' => 'Pro / Free accounts only the owner can log in. Please upgrade'], 403); @@ -502,13 +553,30 @@ class LoginController extends BaseController $cu = CompanyUser::whereUserId(auth()->user()->id); - $cu->first()->account->companies->each(function ($company) use($cu){ + // $cu->first()->account->companies->each(function ($company) use($cu){ + + // if($company->tokens()->where('is_system', true)->count() == 0) + // { + // CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); + // } + // }); + + if(auth()->user()->company_users()->count() != auth()->user()->tokens()->count()) + { + + auth()->user()->companies->each(function($company) { + + if(!CompanyToken::where('user_id', auth()->user()->id)->where('company_id', $company->id)->exists()){ + + CreateCompanyToken::dispatchNow($company, auth()->user(), "Google_O_Auth"); + + } + + }); + + } + - if($company->tokens()->where('is_system', true)->count() == 0) - { - CreateCompanyToken::dispatchNow($company, $cu->first()->user, request()->server('HTTP_USER_AGENT')); - } - }); if(Ninja::isHosted() && !$cu->first()->is_owner && !auth()->user()->account->isEnterpriseClient()) return response()->json(['message' => 'Pro / Free accounts only the owner can log in. Please upgrade'], 403); diff --git a/app/Listeners/Quote/QuoteApprovedNotification.php b/app/Listeners/Quote/QuoteApprovedNotification.php index 59199d0c535d..591e2a8b8102 100644 --- a/app/Listeners/Quote/QuoteApprovedNotification.php +++ b/app/Listeners/Quote/QuoteApprovedNotification.php @@ -57,9 +57,6 @@ class QuoteApprovedNotification implements ShouldQueue if(!$user) continue; - /* This is only here to handle the alternate message channels - ie Slack */ - // $notification = new EntitySentNotification($event->invitation, 'quote'); - /* Returns an array of notification methods */ $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_approved', 'quote_approved_all']); @@ -67,7 +64,6 @@ class QuoteApprovedNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); - $nmo->to_user = $user; NinjaMailerJob::dispatch($nmo); @@ -76,11 +72,6 @@ class QuoteApprovedNotification implements ShouldQueue $first_notification_sent = false; } - /* Override the methods in the Notification Class */ - // $notification->method = $methods; - - // Notify on the alternate channels - // $user->notify($notification); } } } diff --git a/app/Services/Quote/QuoteService.php b/app/Services/Quote/QuoteService.php index dcc4016e802b..f830ba6bc76f 100644 --- a/app/Services/Quote/QuoteService.php +++ b/app/Services/Quote/QuoteService.php @@ -110,7 +110,7 @@ class QuoteService $contact = $this->quote->invitations->first()->contact; } - event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars())); + // event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars())); if ($this->quote->client->getSetting('auto_convert_quote')) { $this->convert(); @@ -123,12 +123,6 @@ class QuoteService } - - // if ($this->quote->client->getSetting('auto_archive_quote')) { - // $quote_repo = new QuoteRepository(); - // $quote_repo->archive($this->quote); - // } - return $this; }