add logging, optimize all customer sync, add presync

This commit is contained in:
O K
2025-09-02 19:30:44 +03:00
parent 918413d997
commit 02da623898
2 changed files with 28 additions and 6 deletions

View File

@@ -2,8 +2,8 @@
<module>
<name>mauticconnect</name>
<displayName><![CDATA[Mautic Connect]]></displayName>
<version><![CDATA[1.0.0]]></version>
<description><![CDATA[Integrate your PrestaShop store with Mautic for marketing automation.]]></description>
<version><![CDATA[1.2.0]]></version>
<description><![CDATA[A data-driven module to integrate PrestaShop with Mautic for marketing automation.]]></description>
<author><![CDATA[Your Name]]></author>
<tab><![CDATA[marketing]]></tab>
<confirmUninstall><![CDATA[Are you sure you want to uninstall this module? All Mautic connection data will be lost.]]></confirmUninstall>

View File

@@ -310,7 +310,7 @@ class MauticConnect extends Module
}
$orderId = (int)$params['id_order'];
$newStatusId = (int)$params['newOrderStatus']->id;
$this->log(['orderId' => $orderId, 'newStatusId' => $newStatusId]);
$eventHash = md5($newStatusId . '_' . $orderId);
if ($this->isAlreadyProcessed($eventHash)) {
return;
@@ -325,7 +325,9 @@ class MauticConnect extends Module
// ...call the processor method defined for this event.
if (method_exists($this, $event['processor_method'])) {
$this->{$event['processor_method']}($orderId, $event);
$this->markAsProcessed($eventHash);
// We break because an order status change should only trigger one event.
break;
}
@@ -492,6 +494,7 @@ class MauticConnect extends Module
*/
public function getOauth2RedirectUri()
{
return $this->context->link->getModuleLink($this->name, 'oauth2', [], true);
}
@@ -615,8 +618,10 @@ class MauticConnect extends Module
if (!$this->isConnected()) {
return false;
}
$this->log(['newCustomer' => $params['newCustomer']]);
if (isset($params['newCustomer']) && Validate::isLoadedObject($params['newCustomer'])) {
$this->syncCustomer($params['newCustomer']);
}
}
@@ -640,9 +645,12 @@ class MauticConnect extends Module
{
return (bool)Configuration::get(self::MAUTIC_ACCESS_TOKEN);
}
public function syncAllCustomers()
public function syncAllCustomers(bool $full = false)
{
$customers = new PrestaShopCollection('Customer');
if (!$full) {
$customers->where('date_add', '>', date("Y-m-d H:i:s", time() - 60 - 60 * 24 * 2));
}
foreach ($customers as $customer) {
$this->syncCustomer($customer);
}
@@ -819,13 +827,14 @@ class MauticConnect extends Module
if (!$mauticSegmentId || !$mauticTemplateId) {
return;
}
// 2. Get all necessary objects
$order = new Order($id_order);
$customer = new Customer((int)$order->id_customer);
$currency = new Currency((int)$order->id_currency);
$carrier = new Carrier((int)$order->id_carrier);
$link = new Link(); // Needed for generating image URLs
$this->syncCustomer($customer);
// 3. Gather primary data
$customer_email = $customer->email;
if (!$this->isContactInSegment($customer_email, $mauticSegmentId)) {
@@ -927,7 +936,7 @@ class MauticConnect extends Module
$currency = new Currency((int)$order->id_currency);
$carrier = new Carrier((int)$order->id_carrier);
$link = new Link(); // Needed for generating image URLs
$this->syncCustomer($customer);
// 3. Gather primary data
$customer_email = $customer->email;
if (!$this->isContactInSegment($customer_email, $mauticSegmentId)) {
@@ -1087,4 +1096,17 @@ class MauticConnect extends Module
'date_add' => date('Y-m-d H:i:s'),
], false, true, Db::INSERT_IGNORE); // INSERT IGNORE is a safe way to prevent errors on race conditions
}
public function log(array $data)
{
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$logdirectory = _PS_ROOT_DIR_ . '/var/modules/' . $this->name . '/logs/' . date("Y") . '/' . date("m") . '/' . date("d") . '/';
if (!is_dir($logdirectory)) {
mkdir($logdirectory, 0750, true);
}
$logger = new \FileLogger(0); //0 == debug level, logDebug() wont work without this.
$logger->setFilename($logdirectory . 'dayly.log');
$logger->logInfo($data);
}
}