feat: ✨ sync daily unsubscribed contacts from mailjet
This commit is contained in:
parent
98ef3c826e
commit
6fabb40ab6
11 changed files with 384 additions and 11 deletions
|
|
@ -49,3 +49,7 @@ SAML2_SP_CERT_x509=
|
|||
SAML2_SP_CERT_PRIVATEKEY=
|
||||
SAML2_CONTACT_TECHNICAL_EMAIL=
|
||||
SAML2_CONTACT_SUPPORT_EMAIL=
|
||||
|
||||
# MJ - API
|
||||
MJ_APIKEY_PUBLIC=
|
||||
MJ_APIKEY_PRIVATE=
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel
|
|||
// @codeCoverageIgnoreStart
|
||||
$schedule->job(StartStatJobs::class)->dailyAt('03:30');
|
||||
$schedule->job(SendTransferredToDigitalCountMail::class)->weekdays()->at('07:00');
|
||||
$schedule->job(SyncEmailOptOuts::class)->dailyAt('00:00');
|
||||
$schedule->command(DeleteUnconfirmedCustomers::class)->dailyAt('05:30');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,10 +37,11 @@ class StoreBrandRequest extends FormRequest
|
|||
public function rules(): array
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|unique:brands,name|string|min:5|max:255',
|
||||
'url' => 'required|url|min:5|max:255',
|
||||
'url_claim' => 'required|url|min:5|max:255',
|
||||
'domain' => 'required|unique:brands,domain|string|min:5|max:255',
|
||||
'name' => 'required|unique:brands,name|string|min:5|max:255',
|
||||
'url' => 'required|url|min:5|max:255',
|
||||
'url_claim' => 'required|url|min:5|max:255',
|
||||
'domain' => 'required|unique:brands,domain|string|min:5|max:255',
|
||||
'newsletter_email' => 'nullable|email:rfc,strict,dns,spoof,filter',
|
||||
];
|
||||
return \array_merge(
|
||||
$rules,
|
||||
|
|
@ -56,11 +57,12 @@ class StoreBrandRequest extends FormRequest
|
|||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'name' => trans('validation.attributes.name'),
|
||||
'logo' => trans('validation.custom.logo'),
|
||||
'url' => trans('validation.custom.url'),
|
||||
'url_claim' => trans('validation.custom.url_claim'),
|
||||
'domain' => trans('validation.custom.domain'),
|
||||
'name' => trans('validation.attributes.name'),
|
||||
'logo' => trans('validation.custom.logo'),
|
||||
'url' => trans('validation.custom.url'),
|
||||
'url_claim' => trans('validation.custom.url_claim'),
|
||||
'domain' => trans('validation.custom.domain'),
|
||||
'newsletter_email' => trans('validation.custom.newsletter_email'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
58
app/Jobs/SyncEmailOptOuts.php
Normal file
58
app/Jobs/SyncEmailOptOuts.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Lib\Helpers\MailjetHelper;
|
||||
use App\Models\Brand;
|
||||
use App\Models\CustomerInfo;
|
||||
use App\Models\SingleOptin;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
|
||||
class SyncEmailOptOuts implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$campaigns = MailjetHelper::getCampaigns();
|
||||
|
||||
foreach ($campaigns as $campaign) {
|
||||
$contactListId = $campaign['ListID'];
|
||||
$brand = Brand::query()->where('newsletter_email', $campaign['FromEmail'])->first();
|
||||
$unsubscribedContacts = MailjetHelper::getUnsubscribedContacts($contactListId, Carbon::now()->subDay());
|
||||
|
||||
if (empty($unsubscribedContacts)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($unsubscribedContacts as $unsubscribedContact) {
|
||||
$contact = MailjetHelper::getContact($unsubscribedContact['ContactID']);
|
||||
$crmCustomer = CustomerInfo::getFromEmail($brand, $contact[0]["Email"])->first();
|
||||
|
||||
if ($crmCustomer) {
|
||||
$crmCustomer->opt_in = false;
|
||||
|
||||
if (!$crmCustomer->saveOrFail()) {
|
||||
throw new \Exception(sprintf('Optin status not changed for customer (%s) and brand (%d).', $crmCustomer->getKey()));
|
||||
}
|
||||
} else {
|
||||
$singleOptin = SingleOptin::query()->where('email', $contact[0]['Email'])
|
||||
->whereHas('brand', function ($q) use ($brand) {
|
||||
$q->whereId($brand->getKey());
|
||||
})->first();
|
||||
|
||||
if ($singleOptin) {
|
||||
if (!$singleOptin->delete()) {
|
||||
throw new \Exception(sprintf('SingleOptin (%s) not deleted for brand (%d).', $singleOptin->hashcode, $brand->name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
94
app/Lib/Helpers/MailjetHelper.php
Normal file
94
app/Lib/Helpers/MailjetHelper.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace App\Lib\Helpers;
|
||||
|
||||
use Mailjet\Resources;
|
||||
|
||||
class MailjetHelper
|
||||
{
|
||||
/**
|
||||
* Get campaigns sent
|
||||
*
|
||||
* @param \Carbon\Carbon|null $fromTimestamp
|
||||
* @return array
|
||||
*/
|
||||
public static function getCampaigns(\Carbon\Carbon|null $fromTimestamp = null): array
|
||||
{
|
||||
$filters = [
|
||||
'Limit' => 1000
|
||||
];
|
||||
|
||||
if (!is_null($fromTimestamp)) {
|
||||
$filters = array_merge(
|
||||
$filters,
|
||||
[
|
||||
'FromTS' => $fromTimestamp->toRfc3339String()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$response = app('mailjetClient')->get(Resources::$Campaign, [
|
||||
'filters' => $filters
|
||||
]);
|
||||
|
||||
if ($response->success()) {
|
||||
return $response->getData();
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unsubscribed contacts for a given contact list
|
||||
*
|
||||
* @param integer $contactListId
|
||||
* @param \Carbon\Carbon|null $lastActivity
|
||||
* @return array
|
||||
*/
|
||||
public static function getUnsubscribedContacts(int $contactListId, \Carbon\Carbon|null $lastActivity = null): array
|
||||
{
|
||||
$filters = [
|
||||
'ContactsList' => $contactListId,
|
||||
'Unsub' => true,
|
||||
'Limit' => 1000,
|
||||
];
|
||||
|
||||
if (!is_null($lastActivity)) {
|
||||
$filters = array_merge(
|
||||
$filters,
|
||||
[
|
||||
'LastActivityAt' => $lastActivity->toRfc3339String()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$response = app('mailjetClient')->get(Resources::$Listrecipient, [
|
||||
'filters' => $filters
|
||||
]);
|
||||
|
||||
if ($response->success()) {
|
||||
return $response->getData();
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get contact for a given contact id
|
||||
*
|
||||
* @param integer $contactId
|
||||
* @return array
|
||||
*/
|
||||
public static function getContact(int $contactId): array
|
||||
{
|
||||
$response = app('mailjetClient')->get(Resources::$Contact, [
|
||||
'id' => $contactId
|
||||
]);
|
||||
|
||||
if ($response->success()) {
|
||||
return $response->getData();
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
34
app/Providers/MailjetServiceProvider.php
Normal file
34
app/Providers/MailjetServiceProvider.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class MailjetServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
// Mailjet client to call the API.
|
||||
App::singleton('mailjetClient', function () {
|
||||
$mailjetClient = new \Mailjet\Client(
|
||||
config('mailjet.public'),
|
||||
config('mailjet.secret'),
|
||||
true,
|
||||
[
|
||||
'version' => 'v3',
|
||||
]
|
||||
);
|
||||
|
||||
$mailjetClient->setConnectionTimeout(200);
|
||||
$mailjetClient->setTimeout(200);
|
||||
|
||||
return $mailjetClient;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@
|
|||
"laravel/tinker": "^2.8",
|
||||
"laravel/ui": "^4.2",
|
||||
"league/csv": "^9.7",
|
||||
"mailjet/mailjet-apiv3-php": "^1.6",
|
||||
"mews/purifier": "^3.4",
|
||||
"propaganistas/laravel-phone": "^5.0",
|
||||
"sentry/sentry-laravel": "^4.10",
|
||||
|
|
|
|||
167
composer.lock
generated
167
composer.lock
generated
|
|
@ -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": "1417f675b37867e72bdd34d5b0fcf629",
|
||||
"content-hash": "70aad663cba0f4125a01bc8e53999abc",
|
||||
"packages": [
|
||||
{
|
||||
"name": "24slides/laravel-saml2",
|
||||
|
|
@ -3562,6 +3562,67 @@
|
|||
],
|
||||
"time": "2024-12-08T08:18:47+00:00"
|
||||
},
|
||||
{
|
||||
"name": "mailjet/mailjet-apiv3-php",
|
||||
"version": "v1.6.6",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mailjet/mailjet-apiv3-php.git",
|
||||
"reference": "9d5cea25f347719d7df7909fb4a43a72bd4e5011"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/mailjet/mailjet-apiv3-php/zipball/9d5cea25f347719d7df7909fb4a43a72bd4e5011",
|
||||
"reference": "9d5cea25f347719d7df7909fb4a43a72bd4e5011",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"guzzlehttp/guzzle": "^7.4.4",
|
||||
"php": "^8.1",
|
||||
"psr/http-client": "^1.0",
|
||||
"symfony/validator": "^6.3|^7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"mockery/mockery": "^1.4",
|
||||
"php-coveralls/php-coveralls": "^2.0",
|
||||
"phpcompatibility/php-compatibility": "*",
|
||||
"phpstan/phpstan": "^2.0",
|
||||
"phpunit/phpunit": "^11.4.0",
|
||||
"squizlabs/php_codesniffer": "*"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Mailjet": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Mailjet",
|
||||
"email": "dev@mailjet.com",
|
||||
"homepage": "https://dev.mailjet.com"
|
||||
}
|
||||
],
|
||||
"description": "PHP wrapper for the Mailjet API",
|
||||
"homepage": "https://github.com/mailjet/mailjet-apiv3-php/",
|
||||
"keywords": [
|
||||
"Mailjet",
|
||||
"api",
|
||||
"email",
|
||||
"php",
|
||||
"v3"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/mailjet/mailjet-apiv3-php/issues",
|
||||
"source": "https://github.com/mailjet/mailjet-apiv3-php/tree/v1.6.6"
|
||||
},
|
||||
"time": "2025-12-24T15:55:48+00:00"
|
||||
},
|
||||
{
|
||||
"name": "masterminds/html5",
|
||||
"version": "2.10.0",
|
||||
|
|
@ -7953,6 +8014,110 @@
|
|||
],
|
||||
"time": "2024-09-25T14:21:43+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/validator",
|
||||
"version": "v7.4.8",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/validator.git",
|
||||
"reference": "8f73cbddae916756f319b3e195088da216f0f12f"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/validator/zipball/8f73cbddae916756f319b3e195088da216f0f12f",
|
||||
"reference": "8f73cbddae916756f319b3e195088da216f0f12f",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.2",
|
||||
"symfony/deprecation-contracts": "^2.5|^3",
|
||||
"symfony/polyfill-ctype": "~1.8",
|
||||
"symfony/polyfill-mbstring": "~1.0",
|
||||
"symfony/polyfill-php83": "^1.27",
|
||||
"symfony/translation-contracts": "^2.5|^3"
|
||||
},
|
||||
"conflict": {
|
||||
"doctrine/lexer": "<1.1",
|
||||
"symfony/dependency-injection": "<6.4",
|
||||
"symfony/doctrine-bridge": "<7.0",
|
||||
"symfony/expression-language": "<6.4",
|
||||
"symfony/http-kernel": "<6.4",
|
||||
"symfony/intl": "<6.4",
|
||||
"symfony/property-info": "<6.4",
|
||||
"symfony/translation": "<6.4.3|>=7.0,<7.0.3",
|
||||
"symfony/var-exporter": "<6.4.25|>=7.0,<7.3.3",
|
||||
"symfony/yaml": "<6.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"egulias/email-validator": "^2.1.10|^3|^4",
|
||||
"symfony/cache": "^6.4|^7.0|^8.0",
|
||||
"symfony/config": "^6.4|^7.0|^8.0",
|
||||
"symfony/console": "^6.4|^7.0|^8.0",
|
||||
"symfony/dependency-injection": "^6.4|^7.0|^8.0",
|
||||
"symfony/expression-language": "^6.4|^7.0|^8.0",
|
||||
"symfony/finder": "^6.4|^7.0|^8.0",
|
||||
"symfony/http-client": "^6.4|^7.0|^8.0",
|
||||
"symfony/http-foundation": "^6.4|^7.0|^8.0",
|
||||
"symfony/http-kernel": "^6.4|^7.0|^8.0",
|
||||
"symfony/intl": "^6.4|^7.0|^8.0",
|
||||
"symfony/mime": "^6.4|^7.0|^8.0",
|
||||
"symfony/process": "^6.4|^7.0|^8.0",
|
||||
"symfony/property-access": "^6.4|^7.0|^8.0",
|
||||
"symfony/property-info": "^6.4|^7.0|^8.0",
|
||||
"symfony/string": "^6.4|^7.0|^8.0",
|
||||
"symfony/translation": "^6.4.3|^7.0.3|^8.0",
|
||||
"symfony/type-info": "^7.1.8",
|
||||
"symfony/yaml": "^6.4|^7.0|^8.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Symfony\\Component\\Validator\\": ""
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/",
|
||||
"/Resources/bin/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Provides tools to validate values",
|
||||
"homepage": "https://symfony.com",
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/validator/tree/v7.4.8"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/nicolas-grekas",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2026-03-30T12:55:43+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/var-dumper",
|
||||
"version": "v7.2.0",
|
||||
|
|
|
|||
|
|
@ -196,6 +196,7 @@ return [
|
|||
App\Providers\RouteServiceProvider::class,
|
||||
App\Providers\TranslationServiceProvider::class,
|
||||
App\Providers\ViewServiceProvider::class,
|
||||
App\Providers\MailjetServiceProvider::class,
|
||||
],
|
||||
|
||||
/*
|
||||
|
|
|
|||
6
config/mailjet.php
Normal file
6
config/mailjet.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'public' => env('MJ_APIKEY_PUBLIC'),
|
||||
'secret' => env('MJ_APIKEY_PRIVATE'),
|
||||
];
|
||||
|
|
@ -32,6 +32,8 @@ data:
|
|||
SAML2_CONTACT_TECHNICAL_EMAIL: "${APP_SAML2_CONTACT_TECHNICAL_EMAIL}"
|
||||
SAML2_CONTACT_SUPPORT_EMAIL: "${APP_SAML2_CONTACT_SUPPORT_EMAIL}"
|
||||
UWIL_ADMIN_SECURE_CIDR_LIST: "${APP_GLOBAL_UWIL_ADMIN_SECURE_CIDR_LIST}"
|
||||
MJ_APIKEY_PUBLIC: "${APP_MJ_APIKEY_PUBLIC}"
|
||||
MJ_APIKEY_PRIVATE: "${APP_MJ_APIKEY_PRIVATE}"
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
|
|
@ -82,7 +84,12 @@ spec:
|
|||
lifecycle:
|
||||
preStop:
|
||||
exec:
|
||||
command: ["/bin/sh","-c","kill -s TERM 1; while test $(ps ax | grep -v grep | grep bash | wc -l) -gt 0; do sleep 2s; done"]
|
||||
command:
|
||||
[
|
||||
"/bin/sh",
|
||||
"-c",
|
||||
"kill -s TERM 1; while test $(ps ax | grep -v grep | grep bash | wc -l) -gt 0; do sleep 2s; done",
|
||||
]
|
||||
resources:
|
||||
limits:
|
||||
cpu: "${APP_CI_CPU_LIMIT}"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue