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

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


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

commit bfdd996bd5a33591294f665a1a8d3929b180bb8d
Author: Giulio Speri <[email protected]>
AuthorDate: Thu Nov 24 23:38:15 2022 +0100

    Fixed: newsletter multiple subscript. w same email address (OFBIZ-9361)
    
    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         | 18 ++++-
 applications/marketing/servicedef/services.xml     |  7 +-
 .../marketing/marketing/MarketingServices.java     | 84 ++++++++++++++++++++--
 3 files changed, 101 insertions(+), 8 deletions(-)

diff --git a/applications/marketing/config/MarketingUiLabels.xml 
b/applications/marketing/config/MarketingUiLabels.xml
index 85be546987..c7a2eb1e91 100644
--- a/applications/marketing/config/MarketingUiLabels.xml
+++ b/applications/marketing/config/MarketingUiLabels.xml
@@ -1046,6 +1046,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>
@@ -3865,4 +3881,4 @@
         <value xml:lang="en">vCard</value>
         <value xml:lang="es">vCard</value>
     </property>
-</resource>
+</resource>
\ No newline at end of file
diff --git a/applications/marketing/servicedef/services.xml 
b/applications/marketing/servicedef/services.xml
index 01465b6efa..957be55c4b 100644
--- a/applications/marketing/servicedef/services.xml
+++ b/applications/marketing/servicedef/services.xml
@@ -217,9 +217,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 b3a9cc968b..bd0d3f8592 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(RESOURCE, 
"MarketingNewsletterSubscriptionRequestSuccessMessage", locale);
 
         if (!UtilValidate.isEmail(email)) {
             String error = UtilProperties.getMessage(RESOURCE, 
"MarketingCampaignInvalidEmailInput", locale);
@@ -97,10 +100,45 @@ public class MarketingServices {
                 throw new 
GenericServiceException(ServiceUtil.getErrorMessage(serviceResults));
             }
             String contactMechId = (String) 
serviceResults.get("contactMechId");
-            // create a new association at this fromDate to the anonymous 
party with status accepted
+
+            //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(RESOURCE, 
"MarketingNewsletterSubscriptionAlreadyExistsMsg", locale);
+                Debug.logError(error, MODULE);
+                return ServiceUtil.returnError(error);
+            }
+            /* checks 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(RESOURCE, 
"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 pending
             input = UtilMisc.toMap("userLogin", userLogin, "contactListId", 
contactList.get("contactListId"),
-                    "partyId", partyId, "fromDate", fromDate, "statusId", 
"CLPT_PENDING", "preferredContactMechId", contactMechId, "baseLocation",
-                    context.get("baseLocation"));
+                "partyId", partyId, "fromDate", fromDate, "statusId", 
"CLPT_PENDING", "preferredContactMechId", contactMechId, "baseLocation",
+                context.get("baseLocation"));
             serviceResults = dispatcher.runSync("createContactListParty", 
input);
             if (ServiceUtil.isError(serviceResults)) {
                 throw new 
GenericServiceException(ServiceUtil.getErrorMessage(serviceResults));
@@ -114,6 +152,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(RESOURCE, 
"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(RES_ORDER, 
"checkhelper.problems_reading_database", locale);
+            Debug.logError(e, error + e.getMessage(), MODULE);
+            return ServiceUtil.returnError(error);
+        }
+        return ServiceUtil.returnSuccess(successMessage);
     }
 }

Reply via email to