]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6626 @6
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 16 Jan 2024 12:01:50 +0000 (13:01 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 16 Jan 2024 12:01:50 +0000 (13:01 +0100)
.env.alpha
.env.dev
.env.prod
app/Jobs/MailjetSyncList.php [new file with mode: 0644]
app/Jobs/ProcessTotals.php
app/Logging/SlackFormatter.php
composer.json
composer.lock

index f6c4d4506a0d34cce2a2d86f5bb88553c12f5955..45f5c554c5a8c8d67b8f827a8976bcf33c0475f2 100644 (file)
@@ -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}"
 
index 317e1cf0cbd1c77cbd4536f30fb1f9cbe1d8378c..ea8965487070b2fba73d39bde6559ce191819c0a 100644 (file)
--- 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}"
 
index 8d0ca4017174e9e94191c5ec4a43d24ada08da4d..759cfe0833927a7c11315672252004b2c606f17c 100644 (file)
--- 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 (file)
index 0000000..ff4744a
--- /dev/null
@@ -0,0 +1,144 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\Company;
+use App\Models\User;
+use Cubedesigners\UserDatabase\Permissions;
+use DateTimeInterface;
+use Egulias\EmailValidator\EmailValidator;
+use Egulias\EmailValidator\Validation\DNSCheckValidation;
+use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
+use Egulias\EmailValidator\Validation\RFCValidation;
+use Illuminate\Support\Facades\Cache;
+use Mailjet\Client;
+use Mailjet\Resources;
+
+class MailjetSyncList extends Base
+{
+    const LIST_ID = '10296433';
+
+    /**
+     * @throws \Exception
+     */
+    public function handle()
+    {
+
+
+        $skippedCompanies = [];
+        $companies = [];
+        $contacts = ['addnoforce' => [], '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]);
+    }
+}
index bafdcb688e2bd7be0ac08a77d3beca622ea423d1..d9aa612b8cf094227ca73e5e453ec03656f5ac33 100644 (file)
@@ -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;
             }
index e31958c1ad3f73b45c311213eb9c7804f51e8c55..f5647728f1d03debcc3df73f16d13fb7519f01ec 100644 (file)
@@ -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;
     }
 }
index 2a1219e6995abbde8315306f351d1af11c248524..e344d4bacf20bde38e8bd3c152d8659212d8c346 100644 (file)
@@ -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",
index 498b11a058c6812c595141cd8a3ac2bfa8f41aad..cd1af0ca729fb875fecfa98ee062b8e63e55d591 100644 (file)
@@ -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",