Skip to content

Commit

Permalink
soft delete and save date when notification was sent
Browse files Browse the repository at this point in the history
  • Loading branch information
lmeyer1 committed Nov 4, 2022
1 parent f355ae8 commit ce7a202
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
15 changes: 9 additions & 6 deletions MailAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,20 @@ public static function customerHasNotification($id_customer, $id_product, $id_pr
SELECT *
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE ' . $where . '
AND deleted = 0
AND `id_product` = ' . (int) $id_product . '
AND `id_product_attribute` = ' . (int) $id_product_attribute . '
AND `id_shop` = ' . (int) $id_shop;

return count(Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($sql));
}

public static function deleteAlert($id_customer, $customer_email, $id_product, $id_product_attribute, $id_shop = null)
public static function deleteAlert($id_customer, $customer_email, $id_product, $id_product_attribute, $id_shop, $notification_sent)
{
$sql = '
DELETE FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE ' . (($id_customer > 0) ? '(`customer_email` = \'' . pSQL($customer_email) . '\' OR `id_customer` = ' . (int) $id_customer . ')' :
UPDATE `' . _DB_PREFIX_ . self::$definition['table'] . '` set `deleted` = 1 ' .
($notification_sent ? ', `notification_sent` = NOW()' : '') .
' WHERE ' . (($id_customer > 0) ? '(`customer_email` = \'' . pSQL($customer_email) . '\' OR `id_customer` = ' . (int) $id_customer . ')' :
'`customer_email` = \'' . pSQL($customer_email) . '\'') .
' AND `id_product` = ' . (int) $id_product . '
AND `id_product_attribute` = ' . (int) $id_product_attribute . '
Expand Down Expand Up @@ -258,7 +260,8 @@ public static function sendCustomerAlert($id_product, $id_product_attribute)
(string) $customer_email,
(int) $id_product,
(int) $id_product_attribute,
$id_shop
$id_shop,
true
);
}
}
Expand All @@ -281,7 +284,7 @@ public static function getProducts($customer, $id_lang)
$sql = '
SELECT ma.`id_product`, p.`quantity` AS product_quantity, pl.`name`, ma.`id_product_attribute`
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '` ma
JOIN `' . _DB_PREFIX_ . 'product` p ON (p.`id_product` = ma.`id_product`)
JOIN `' . _DB_PREFIX_ . 'product` p ON p.`id_product` = ma.`id_product` and ma.`deleted` = 0
' . Shop::addSqlAssociation('product', 'p') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (pl.`id_product` = p.`id_product` AND pl.id_shop IN (' . implode(', ', $list_shop_ids) . '))
WHERE product_shop.`active` = 1
Expand Down Expand Up @@ -318,7 +321,7 @@ public static function getCustomers($id_product, $id_product_attribute)
$sql = '
SELECT id_customer, customer_email, id_shop, id_lang
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE `id_product` = ' . (int) $id_product . ' AND `id_product_attribute` = ' . (int) $id_product_attribute;
WHERE `id_product` = ' . (int) $id_product . ' AND `id_product_attribute` = ' . (int) $id_product_attribute . ' AND `deleted` = 0';

return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($sql);
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/front/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function processRemove()
(string) $context->customer->email,
(int) $product->id,
(int) $this->id_product_attribute,
(int) $context->shop->id
(int) $context->shop->id,
false
)) {
exit('0');
}
Expand Down
18 changes: 12 additions & 6 deletions ps_emailalerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ public function install($delete_params = true)
`id_lang` int(10) unsigned NOT NULL,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
PRIMARY KEY (`id_customer`,`customer_email`,`id_product`,`id_product_attribute`,`id_shop`)
`deleted` TINYINT NOT NULL DEFAULT 0,
`notification_sent` DATETIME NULL DEFAULT NULL
PRIMARY KEY (`id_customer`,`customer_email`,`id_product`,`id_product_attribute`,`id_shop`),
INDEX (`deleted`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';

if (!Db::getInstance()->execute($sql)) {
Expand Down Expand Up @@ -690,7 +693,8 @@ public function hookDisplayMyAccountBlock($params)
public function hookActionProductDelete($params)
{
$sql = '
DELETE FROM `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
UPDATE `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
SET `deleted` = 1
WHERE `id_product` = ' . (int) $params['product']->id;

Db::getInstance()->execute($sql);
Expand All @@ -700,11 +704,13 @@ public function hookActionProductAttributeDelete($params)
{
if ($params['deleteAllAttributes']) {
$sql = '
DELETE FROM `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
UPDATE `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
SET `deleted` = 1
WHERE `id_product` = ' . (int) $params['id_product'];
} else {
$sql = '
DELETE FROM `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
UPDATE `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
SET `deleted` = 1
WHERE `id_product_attribute` = ' . (int) $params['id_product_attribute'] . '
AND `id_product` = ' . (int) $params['id_product'];
}
Expand Down Expand Up @@ -1203,7 +1209,7 @@ public function renderForm()
public function hookActionDeleteGDPRCustomer($customer)
{
if (!empty($customer['email']) && Validate::isEmail($customer['email'])) {
$sql = 'DELETE FROM ' . _DB_PREFIX_ . "mailalert_customer_oos WHERE customer_email = '" . pSQL($customer['email']) . "'";
$sql = 'UPDATE ' . _DB_PREFIX_ . "mailalert_customer_oos SET deleted = 1, customer_email = 'deleted' WHERE customer_email = '" . pSQL($customer['email']) . "'";
if (Db::getInstance()->execute($sql)) {
return json_encode(true);
}
Expand All @@ -1215,7 +1221,7 @@ public function hookActionDeleteGDPRCustomer($customer)
public function hookActionExportGDPRData($customer)
{
if (!Tools::isEmpty($customer['email']) && Validate::isEmail($customer['email'])) {
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . "mailalert_customer_oos WHERE customer_email = '" . pSQL($customer['email']) . "'";
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . "mailalert_customer_oos WHERE deleted = 0 and customer_email = '" . pSQL($customer['email']) . "'";
if ($res = Db::getInstance()->ExecuteS($sql)) {
return json_encode($res);
}
Expand Down
4 changes: 3 additions & 1 deletion upgrade/upgrade-2.3.4.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

function upgrade_module_2_3_4()
{
$result = Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . 'mailalert_customer_oos` ADD `date_add` DATETIME NOT NULL AFTER `id_lang`, ADD `date_upd` DATETIME NOT NULL AFTER `date_add`;');
$result = true;
$result = $result && Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . 'mailalert_customer_oos` ADD `date_add` DATETIME NOT NULL, ADD `date_upd` DATETIME NOT NULL;');
$result = $result && Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . 'mailalert_customer_oos` ADD `deleted` TINYINT NOT NULL DEFAULT 0, ADD `notification_sent` DATETIME NULL DEFAULT NULL, ADD INDEX (`deleted`)');

return $result;
}

0 comments on commit ce7a202

Please sign in to comment.