From 15d31162d2a5b7ca55f1285217730d03cdf81d77 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 16 Jan 2024 13:01:50 +0100 Subject: [PATCH] wip #6626 @6 --- .env.alpha | 3 + .env.dev | 3 + .env.prod | 3 + app/Jobs/MailjetSyncList.php | 144 +++++++++++++++++++++++++++++++++ app/Jobs/ProcessTotals.php | 34 ++++---- app/Logging/SlackFormatter.php | 2 + composer.json | 1 + composer.lock | 2 +- 8 files changed, 174 insertions(+), 18 deletions(-) create mode 100644 app/Jobs/MailjetSyncList.php diff --git a/.env.alpha b/.env.alpha index f6c4d4506..45f5c554c 100644 --- a/.env.alpha +++ b/.env.alpha @@ -51,6 +51,9 @@ MAIL_FROM_NAME="[ALPHA] Fluidbook Toolbox" MAIL_BCC_ALL=test+toolboxalpha@cubedesigners.com MAIL_TEAM_NAME=Fluidbook +MAILJET_API_KEY=ca110b35f8735c223d69c9987c2ac47d +MAILJET_API_SECRET=b289d0acb08e0fe56ce98ccf0dd1ed8b + MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" diff --git a/.env.dev b/.env.dev index 317e1cf0c..ea8965487 100644 --- a/.env.dev +++ b/.env.dev @@ -51,6 +51,9 @@ MAIL_FROM_NAME="[DEV] Fluidbook Toolbox" MAIL_BCC_ALL=test+toolboxdev@cubedesigners.com MAIL_TEAM_NAME=Fluidbook +MAILJET_API_KEY=ca110b35f8735c223d69c9987c2ac47d +MAILJET_API_SECRET=b289d0acb08e0fe56ce98ccf0dd1ed8b + MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" diff --git a/.env.prod b/.env.prod index 8d0ca4017..759cfe083 100644 --- a/.env.prod +++ b/.env.prod @@ -51,6 +51,9 @@ MAIL_FROM_NAME="Fluidbook Toolbox" MAIL_BCC_ALL=test+toolbox@cubedesigners.com MAIL_TEAM_NAME=Fluidbook +MAILJET_API_KEY=ca110b35f8735c223d69c9987c2ac47d +MAILJET_API_SECRET=b289d0acb08e0fe56ce98ccf0dd1ed8b + MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" diff --git a/app/Jobs/MailjetSyncList.php b/app/Jobs/MailjetSyncList.php new file mode 100644 index 000000000..ff4744a2e --- /dev/null +++ b/app/Jobs/MailjetSyncList.php @@ -0,0 +1,144 @@ + [], 'unsub' => [], 'remove' => []]; + + $validator = new EmailValidator(); + $multipleValidations = new MultipleValidationWithAnd([ + new RFCValidation(), + new DNSCheckValidation() + ]); + + foreach (Company::withoutGlobalScopes()->get() as $company) { + if (!$company->created_ok) { + $skippedCompanies[] = $company->id; + continue; + } + /** @var Company $company */ + $adminCompany = Permissions::getCompanyByUser($company->admin); + // Skip companies not directly managed by Cubedesigners + if ($adminCompany != 7) { + $skippedCompanies[] = $company->id; + continue; + } + $companies[$company->id] = $company; + } + + foreach (User::withoutGlobalScopes()->where('created_ok', '1')->where('enabled', '1')->get() as $user) { + if (in_array($user->company, $skippedCompanies) || !isset($companies[$user->company])) { + continue; + } + + $c = $companies[$user->company]; + $address = $user->address; + if (is_string($address)) { + $address = json_decode($address); + } + if (null === $address) { + $country = ''; + } else { + $country = is_array($address) ? $address['country'] : $address->country; + } + + $action = 'addnoforce'; + + if (str_ends_with($user->email, '@extranet.cubedesigners.com')) { + $action = 'remove'; + } + + if (!Cache::rememberForever('email_valid_' . $user->email, function () use ($user, $validator, $multipleValidations) { + return $validator->isValid($user->email, $multipleValidations); + })) { + continue; + } + + if (!$user->marketing) { + $action = 'unsub'; + } + + $contact = [ + 'Email' => $user->email, + 'IsExcludedFromCampaigns' => 'false', + 'Name' => trim($user->firstname . ' ' . $user->lastname), + 'Properties' => [ + 'toolbox_id' => $user->id, + 'firstname' => trim($user->firstname), + 'lastname' => trim($user->lastname), + 'company' => trim($c->name), + 'fluidbook' => $c->e1_ws_grade > 0 && $c->toolbox_access, + 'elearning' => (bool)$c->permissions_elearning, + 'locale' => $user->locale, + 'country' => $country, + 'last_project' => $c->c_last_project_date ? (new \DateTime($c->c_last_project_date))->format(DateTimeInterface::RFC3339) : '', + ] + ]; + + $contacts[$action][] = $contact; + } + + + foreach ($contacts as $action => $list) { + $this->addContactsToList($list, $action); + } + } + + protected function addContactsToList($contacts, $action = 'addnoforce') + { + if (empty($contacts)) { + return; + } + $mj = static::_api(); + $body = [ + 'Action' => $action, + 'Contacts' => $contacts + ]; + $response = $mj->post(Resources::$ContactslistManagemanycontacts, ['id' => self::LIST_ID, 'body' => $body]); + $jobId = $response->getData()[0]['JobID']; + + while (true) { + $check = $mj->get(Resources::$ContactslistManagemanycontacts, ['id' => self::LIST_ID, 'actionid' => (string)$jobId]); + $status = ($check->getData()[0]['Status']); + if ($status === 'Error') { + dd($check->getData()[0]); + } else if ($status === 'Completed') { + return; + } + dump($status); + sleep(2); + } + } + + /** + * @return Client + */ + protected static function _api($call = true, $version = 'v3') + { + return new \Mailjet\Client(env('MAILJET_API_KEY'), env('MAILJET_API_SECRET'), $call, ['version' => $version]); + } +} diff --git a/app/Jobs/ProcessTotals.php b/app/Jobs/ProcessTotals.php index bafdcb688..d9aa612b8 100644 --- a/app/Jobs/ProcessTotals.php +++ b/app/Jobs/ProcessTotals.php @@ -5,6 +5,7 @@ namespace App\Jobs; use App\Models\Company; use App\Models\FluidbookQuote; use App\Models\User; +use Cubedesigners\UserDatabase\Permissions; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; @@ -22,12 +23,13 @@ class ProcessTotals extends Base protected $companyYears = []; protected $unpaidYears = []; protected $unpaid = []; + + protected $lastProject = []; protected static $_wstable = 'extranet_clean'; public function handle() { $this->processInvoices(); - //$this->processProjects(); $this->processCompanies(); $this->processFluidbookCounts(); Artisan::command('ws:precache', function () { @@ -37,7 +39,6 @@ class ProcessTotals extends Base protected function processInvoices() { foreach (DB::table(self::$_wstable . '.factures')->whereIn('status', [1, 2])->get() as $e) { - $this->invoices[$e->facture_id] = ['status' => $e->status, 'amount' => $e->total_ht, 'project' => $e->projet, 'year' => date('Y', $e->date_creation), 'paid' => $e->status == 2]; if (!isset($this->invoicesByProject[$e->projet])) { @@ -47,16 +48,6 @@ class ProcessTotals extends Base } } -// protected function processProjects() -// { -// foreach (DB::table(self::$_wstable . '.taches')->get() as $e) { -// if (!isset($this->projects[$e->projet])) { -// $this->projects[$e->projet] = 0; -// } -// $this->projects[$e->projet] += $e->budget; -// } -// -// } protected function processCompanies() { @@ -72,6 +63,7 @@ class ProcessTotals extends Base } foreach (DB::table(self::$_wstable . '.projets')->get() as $e) { + if (!isset($this->companyOfUser[$e->client])) { continue; } @@ -79,14 +71,19 @@ class ProcessTotals extends Base if (!isset($this->invoicesByProject[$e->projet_id])) { continue; } + + $company = $this->companyOfUser[$e->client]; + if (!isset($this->companies[$company])) { + $this->companies[$company] = 0; + $this->unpaid[$company] = 0; + $this->lastProject[$company] = 0; + } + + $this->lastProject[$company] = max($this->lastProject[$company], $e->date_creation, $e->date_debut, $e->date_fin); + foreach ($this->invoicesByProject[$e->projet_id] as $invoice_id) { $invoice = $this->invoices[$invoice_id]; - $company = $this->companyOfUser[$e->client]; - if (!isset($this->companies[$company])) { - $this->companies[$company] = 0; - $this->unpaid[$company] = 0; - } if (!isset($this->companyYears[$company])) { $this->companyYears[$company] = []; $this->unpaidYears[$company] = []; @@ -111,7 +108,10 @@ class ProcessTotals extends Base if (null === $c) { continue; } + $d = new \DateTime(); + $d->setTimestamp($this->lastProject[$company]); $c->c_ca = $ca; + $c->c_last_project_date = $d; foreach ($this->companyYears[$company] as $y => $yca) { $c->{'c_ca_' . $y} = $yca; } diff --git a/app/Logging/SlackFormatter.php b/app/Logging/SlackFormatter.php index e31958c1a..f5647728f 100644 --- a/app/Logging/SlackFormatter.php +++ b/app/Logging/SlackFormatter.php @@ -38,6 +38,7 @@ class SlackFormatter 'URL' => $this->request->url(), ]; } + /** @var User $user */ $user = backpack_user(); if ($user) { @@ -46,6 +47,7 @@ class SlackFormatter ]; } + return $record; } } diff --git a/composer.json b/composer.json index 2a1219e69..e344d4bac 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "cubist/matomo": "dev-master", "cubist/pdf": "dev-master", "cubist/scorm": "dev-master", + "egulias/email-validator": "^4.0", "fluidbook/tools": "dev-master", "hollodotme/fast-cgi-client": "^3.1.7", "jolicode/slack-php-api": "^v4.6.0", diff --git a/composer.lock b/composer.lock index 498b11a05..cd1af0ca7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e4b75781593f63e2769630b55d6cfd1f", + "content-hash": "a2c4e7e7e43bf89fa3a5c783830cb261", "packages": [ { "name": "archtechx/enums", -- 2.39.5