This is an automated email from the ASF dual-hosted git repository.

gsperi pushed a commit to branch release18.12
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release18.12 by this push:
     new 6ff007d003 Fixed: newsletter multiple subscript. w same email address 
(OFBIZ-9361)
6ff007d003 is described below

commit 6ff007d003873fa5f7141deccf54da8bf044987e
Author: Giulio Speri <giulio.sp...@mpstyle.it>
AuthorDate: Sun Nov 27 18:19:33 2022 +0100

    Fixed: newsletter multiple subscript. w same email address (OFBIZ-9361)
    
    (backport)
    It was possible to be subscribed to the newsletter with the same email
    address multiple time.
    
    Added a check for already ACCEPTED requests in order to refuse new
    incoming subscription with the same email address.
    For existing PENDING requests instead, has been added the deletion of
    all the existing records for the same email address, before adding a
    new one.
    
    Thanks Aditya Sharma for your feedback on this.
---
 .../marketing/config/MarketingUiLabels.xml         | 16 +++++
 applications/marketing/servicedef/services.xml     |  7 +-
 .../marketing/marketing/MarketingServices.java     | 78 +++++++++++++++++++++-
 3 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/applications/marketing/config/MarketingUiLabels.xml 
b/applications/marketing/config/MarketingUiLabels.xml
index a4a9af50c1..f00621a5ec 100644
--- a/applications/marketing/config/MarketingUiLabels.xml
+++ b/applications/marketing/config/MarketingUiLabels.xml
@@ -969,6 +969,22 @@
         <value xml:lang="zh">欢迎使用市场管理程序!</value>
         <value xml:lang="zh-TW">歡迎使用行銷管理程式!</value>
     </property>
+    <property key="MarketingNewsletterSubscriptionRequestSuccessMessage">
+        <value xml:lang="en">Newsletter subscription request successful. 
Please check your inbox for verification email.</value>
+        <value xml:lang="it">Richiesta di iscrizione alla Newsletter inviata 
con successo. Controlla la tua casella di posta per l'email di verifica.</value>
+    </property>
+    <property key="MarketingNewsletterSubscriptionReqstAlreadyExistsMsg">
+        <value xml:lang="en">You have already requested to subscribe for our 
newsletter. However, you have not verified your subscription. We have resent 
you the verification mail - please check your inbox for verification 
email.</value>
+        <value xml:lang="it">Hai già richiesto di iscriverti a questa 
newsletter, ma non hai verificato la sottoscrizione. Ti abbiamo nuovamente 
inviato l'email di verifica - controlla la tua casella di posta.</value>
+    </property>
+    <property key="MarketingNewsletterSubscriptionAlreadyExistsMsg">
+        <value xml:lang="en">You are already subscribed to our 
newsletter.</value>
+        <value xml:lang="it">Sei già iscritto alla nostra newsletter.</value>
+    </property>
+    <property 
key="MarketingNewsletterSubscriptionPendingRequestDeletedMessage">
+        <value xml:lang="en">Successfully removed Newsletter subscription 
request.</value>
+        <value xml:lang="it">Richiesta di iscrizione alla newseltter rimossa 
con sucesso.</value>
+    </property>
     <property key="MarketingNoOfEmployees">
         <value xml:lang="de">Anzahl Angestellte</value>
         <value xml:lang="en">No of Employees</value>
diff --git a/applications/marketing/servicedef/services.xml 
b/applications/marketing/servicedef/services.xml
index b22978c154..104c2dcf39 100644
--- a/applications/marketing/servicedef/services.xml
+++ b/applications/marketing/servicedef/services.xml
@@ -214,9 +214,10 @@ under the License.
         <attribute name="contactMechId" type="String" mode="IN" 
optional="false"/>
         <attribute name="oldContactMechId" type="String" mode="IN" 
optional="false"/>
     </service>
-    <service name="deleteContactListParty" 
default-entity-name="ContactListParty" engine="entity-auto" invoke="delete" 
auth="true">
-        <description>Remove Party from ContactList</description>
-        <auto-attributes include="pk" mode="IN" optional="false"/>
+    <service name="deleteContactListParty" 
default-entity-name="ContactListParty" engine="java"
+       location="org.apache.ofbiz.marketing.marketing.MarketingServices" 
invoke="deleteContactListParty" auth="true">
+       <description>Remove Party from ContactList</description>
+       <auto-attributes include="pk" mode="IN" optional="true"/>
     </service>
     <service name="createContactListPartyStatus" 
default-entity-name="ContactListPartyStatus" engine="simple"
         
location="component://marketing/minilang/marketing/contact/ContactListServices.xml"
 invoke="createContactListPartyStatus" auth="true">
diff --git 
a/applications/marketing/src/main/java/org/apache/ofbiz/marketing/marketing/MarketingServices.java
 
b/applications/marketing/src/main/java/org/apache/ofbiz/marketing/marketing/MarketingServices.java
index af4dd18f8c..0c3a2b045c 100644
--- 
a/applications/marketing/src/main/java/org/apache/ofbiz/marketing/marketing/MarketingServices.java
+++ 
b/applications/marketing/src/main/java/org/apache/ofbiz/marketing/marketing/MarketingServices.java
@@ -19,6 +19,7 @@
 package org.apache.ofbiz.marketing.marketing;
 
 import java.sql.Timestamp;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
@@ -31,6 +32,7 @@ import org.apache.ofbiz.entity.Delegator;
 import org.apache.ofbiz.entity.GenericEntityException;
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.entity.util.EntityQuery;
+import org.apache.ofbiz.entity.util.EntityUtil;
 import org.apache.ofbiz.service.DispatchContext;
 import org.apache.ofbiz.service.GenericServiceException;
 import org.apache.ofbiz.service.LocalDispatcher;
@@ -56,6 +58,7 @@ public class MarketingServices {
         String contactListId = (String) context.get("contactListId");
         String email = (String) context.get("email");
         String partyId = (String) context.get("partyId");
+        String successMessage = UtilProperties.getMessage(resourceMarketing, 
"MarketingNewsletterSubscriptionRequestSuccessMessage", locale);
 
         if (!UtilValidate.isEmail(email)) {
             String error = UtilProperties.getMessage(resourceMarketing, 
"MarketingCampaignInvalidEmailInput", locale);
@@ -95,6 +98,41 @@ public class MarketingServices {
                 throw new 
GenericServiceException(ServiceUtil.getErrorMessage(serviceResults));
             }
             String contactMechId = (String) 
serviceResults.get("contactMechId");
+
+            //checks if user is already subscribed to newsletter
+            input = UtilMisc.toMap("contactListId", 
contactList.get("contactListId"), "partyId", partyId, "preferredContactMechId", 
contactMechId);
+            List<GenericValue> contactListPartyList = 
EntityQuery.use(delegator).from("ContactListParty").where(input).filterByDate().queryList();
+
+            List<GenericValue> acceptedContactListPartyList = 
EntityUtil.filterByAnd(contactListPartyList,
+                    UtilMisc.toMap("statusId", "CLPT_ACCEPTED"));
+            if (UtilValidate.isNotEmpty(acceptedContactListPartyList)) {
+                String error = UtilProperties.getMessage(resourceMarketing, 
"MarketingNewsletterSubscriptionAlreadyExistsMsg", locale);
+                Debug.logError(error, module);
+                return ServiceUtil.returnError(error);
+            }
+            /* check if user has already requested to sign up: if yes, delete 
all the existing
+             * pending records and then add a new one.
+             */
+            List<GenericValue> pendingContactListPartyList = 
EntityUtil.filterByAnd(contactListPartyList,
+                    UtilMisc.toMap("statusId", "CLPT_PENDING"));
+            if (UtilValidate.isNotEmpty(pendingContactListPartyList)) {
+                successMessage = UtilProperties.getMessage(resourceMarketing, 
"MarketingNewsletterSubscriptionReqstAlreadyExistsMsg", locale);
+                int count = 0;
+                for (GenericValue pendingCLP : pendingContactListPartyList) {
+                    Map<String, Object> deletePendingCLPInput = 
UtilMisc.toMap("userLogin", userLogin,
+                            "contactListId", pendingCLP.get("contactListId"), 
"fromDate", pendingCLP.get("fromDate"),
+                            "partyId", pendingCLP.get("partyId"));
+
+                    Map<String, Object> deletePendingCLPResults = 
dispatcher.runSync("deleteContactListParty", deletePendingCLPInput);
+                    if (ServiceUtil.isSuccess(deletePendingCLPResults)) {
+                        count++;
+                    } else {
+                        
Debug.logError(ServiceUtil.getErrorMessage(deletePendingCLPResults), module);
+                    }
+                }
+                Debug.logInfo("Successfully deleted " + count + " old Contact 
List PENDING requests.", module);
+            }
+
             // create a new association at this fromDate to the anonymous 
party with status accepted
             input = UtilMisc.toMap("userLogin", userLogin, "contactListId", 
contactList.get("contactListId"),
                     "partyId", partyId, "fromDate", fromDate, "statusId", 
"CLPT_PENDING", "preferredContactMechId", contactMechId, "baseLocation", 
context.get("baseLocation"));
@@ -111,6 +149,44 @@ public class MarketingServices {
             Debug.logInfo(e, error + e.getMessage(), module);
             return ServiceUtil.returnError(error);
         }
-        return ServiceUtil.returnSuccess();
+        return ServiceUtil.returnSuccess(successMessage);
+    }
+
+    public static Map<String, Object> deleteContactListParty(DispatchContext 
dctx, Map<String, ? extends Object> context) {
+        Delegator delegator = dctx.getDelegator();
+        Locale locale = (Locale) context.get("locale");
+
+        String contactListId = (String) context.get("contactListId");
+        String partyId = (String) context.get("partyId");
+        Timestamp fromDate = (Timestamp) context.get("fromDate");
+        String successMessage = UtilProperties.getMessage(resourceMarketing, 
"MarketingNewsletterSubscriptionPendingRequestDeletedMessage", locale);
+
+        Map<String, Object> input = UtilMisc.toMap("contactListId", 
contactListId, "partyId", partyId,
+                "fromDate", fromDate);
+        int cntListPartyRemoved = 0;
+        try {
+            GenericValue contactListParty = 
EntityQuery.use(delegator).from("ContactListParty").where(input).filterByDate().queryOne();
+            if (contactListParty != null) {
+                List<GenericValue> relContactListPartyStatusList = 
contactListParty.getRelated("ContactListPartyStatus", null, null, true);
+                int cntLstPrtStatusRemoved = 0;
+                if (relContactListPartyStatusList != null && 
relContactListPartyStatusList.size() > 0) {
+                    cntLstPrtStatusRemoved = 
delegator.removeAll(relContactListPartyStatusList);
+                }
+                if (cntLstPrtStatusRemoved > 0) {
+                    cntListPartyRemoved = 
delegator.removeValue(contactListParty);
+                }
+            }
+            if (cntListPartyRemoved > 0) {
+                successMessage = successMessage + "[contactListId: " + 
contactListId
+                        + ", partyId: " + partyId + ", fromDate: "
+                        + fromDate + ", Status: " + 
contactListParty.getString("statusId") + "]";
+                Debug.logInfo(successMessage, module);
+            }
+        } catch (GenericEntityException e) {
+            String error = UtilProperties.getMessage(resourceOrder, 
"checkhelper.problems_reading_database", locale);
+            Debug.logError(e, error + e.getMessage(), module);
+            return ServiceUtil.returnError(error);
+        }
+        return ServiceUtil.returnSuccess(successMessage);
     }
 }

Reply via email to