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

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


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

commit a8d2138448e3ec544df20f574fe0da92a016433d
Author: Giulio Speri <giulio.sp...@mpstyle.it>
AuthorDate: Sat Nov 26 12:58: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     |  8 +++
 .../marketing/marketing/MarketingServices.java     | 78 +++++++++++++++++++++-
 3 files changed, 102 insertions(+), 2 deletions(-)

diff --git a/applications/marketing/config/MarketingUiLabels.xml 
b/applications/marketing/config/MarketingUiLabels.xml
index 85be546987..51793e1f75 100644
--- a/applications/marketing/config/MarketingUiLabels.xml
+++ b/applications/marketing/config/MarketingUiLabels.xml
@@ -1045,7 +1045,23 @@
         <value xml:lang="vi">Chào mừng bạn đến với Ứng dụng quản trị Tiếp thị 
!</value>
         <value xml:lang="zh">欢迎使用市场管理程序!</value>
         <value xml:lang="zh-TW">歡迎使用行銷管理程式!</value>
-    </property>
+    </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 01465b6efa..e07ca3cdcb 100644
--- a/applications/marketing/servicedef/services.xml
+++ b/applications/marketing/servicedef/services.xml
@@ -217,10 +217,18 @@ 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>
+    -->
+    <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">
         <description>Create ContactListParty Status</description>
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..ba0dafc716 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,6 +100,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(RESOURCE, 
"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(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 accepted
             input = UtilMisc.toMap("userLogin", userLogin, "contactListId", 
contactList.get("contactListId"),
                     "partyId", partyId, "fromDate", fromDate, "statusId", 
"CLPT_PENDING", "preferredContactMechId", contactMechId, "baseLocation",
@@ -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