From 8a80cfc0e6ba7e51ad91041c5bd5b93cb0005d1f Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Thu, 22 Oct 2020 12:04:22 +0530 Subject: [PATCH 01/38] Implemented: Added the basic directory structure of the headless commerce plugin. --- headless-commerce/README.adoc | 3 + headless-commerce/build.gradle | 37 +++++++ .../config/HeadlessCommerceUiLabels.xml | 42 ++++++++ .../data/HeadlessCommerceDemoData.xml | 23 +++++ .../HeadlessCommerceSecurityGroupDemoData.xml | 29 ++++++ ...lessCommerceSecurityPermissionSeedData.xml | 30 ++++++ .../data/HeadlessCommerceTypeData.xml | 23 +++++ headless-commerce/entitydef/entitymodel.xml | 31 ++++++ headless-commerce/ofbiz-component.xml | 53 ++++++++++ headless-commerce/servicedef/services.xml | 33 +++++++ .../api/customer/CustomerServices.java | 7 ++ .../testdef/HeadlessCommerceTests.xml | 26 +++++ .../headless-commerce/WEB-INF/controller.xml | 47 +++++++++ .../webapp/headless-commerce/WEB-INF/web.xml | 96 +++++++++++++++++++ .../webapp/headless-commerce/index.jsp | 20 ++++ headless-commerce/widget/CommonScreens.xml | 67 +++++++++++++ .../widget/HeadlessCommerceScreens.xml | 38 ++++++++ 17 files changed, 605 insertions(+) create mode 100644 headless-commerce/README.adoc create mode 100644 headless-commerce/build.gradle create mode 100644 headless-commerce/config/HeadlessCommerceUiLabels.xml create mode 100644 headless-commerce/data/HeadlessCommerceDemoData.xml create mode 100644 headless-commerce/data/HeadlessCommerceSecurityGroupDemoData.xml create mode 100644 headless-commerce/data/HeadlessCommerceSecurityPermissionSeedData.xml create mode 100644 headless-commerce/data/HeadlessCommerceTypeData.xml create mode 100644 headless-commerce/entitydef/entitymodel.xml create mode 100644 headless-commerce/ofbiz-component.xml create mode 100644 headless-commerce/servicedef/services.xml create mode 100644 headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java create mode 100644 headless-commerce/testdef/HeadlessCommerceTests.xml create mode 100644 headless-commerce/webapp/headless-commerce/WEB-INF/controller.xml create mode 100644 headless-commerce/webapp/headless-commerce/WEB-INF/web.xml create mode 100644 headless-commerce/webapp/headless-commerce/index.jsp create mode 100644 headless-commerce/widget/CommonScreens.xml create mode 100644 headless-commerce/widget/HeadlessCommerceScreens.xml diff --git a/headless-commerce/README.adoc b/headless-commerce/README.adoc new file mode 100644 index 000000000..1fef6b4d0 --- /dev/null +++ b/headless-commerce/README.adoc @@ -0,0 +1,3 @@ + +The files in this directory are used as templates for the ant create-component target, + +please do not modify them without understanding the effect that it will have on that build target. \ No newline at end of file diff --git a/headless-commerce/build.gradle b/headless-commerce/build.gradle new file mode 100644 index 000000000..0056e3fb9 --- /dev/null +++ b/headless-commerce/build.gradle @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +dependencies { + //Examples of compile-time and runtime dependencies + + //pluginLibsCompile 'junit:junit-dep:4.10' + //pluginLibsRuntime 'junit:junit-dep:4.10' +} + +task install { + doLast { + // Install logic for this plugin + } +} + +task uninstall { + doLast { + // uninstall logic for this plugin + } +} diff --git a/headless-commerce/config/HeadlessCommerceUiLabels.xml b/headless-commerce/config/HeadlessCommerceUiLabels.xml new file mode 100644 index 000000000..c303f7159 --- /dev/null +++ b/headless-commerce/config/HeadlessCommerceUiLabels.xml @@ -0,0 +1,42 @@ + + + + + + HeadlessCommerce Application + HeadlessCommerce应用程序 + HeadlessCommerce應用程式 + + + OFBiz: HeadlessCommerce + OFBiz: HeadlessCommerce + + + Part of the Apache OFBiz Family of Open Source Software + Un modulo della famiglia di software open source Apache OFBiz + 开源软件OFBiz的组成部分 + 開源軟體OFBiz的組成部分 + + + You are not allowed to view this page. + 不允许你浏览这个页面。 + 不允許您檢視這個頁面. + + diff --git a/headless-commerce/data/HeadlessCommerceDemoData.xml b/headless-commerce/data/HeadlessCommerceDemoData.xml new file mode 100644 index 000000000..b76e67e05 --- /dev/null +++ b/headless-commerce/data/HeadlessCommerceDemoData.xml @@ -0,0 +1,23 @@ + + + + + + \ No newline at end of file diff --git a/headless-commerce/data/HeadlessCommerceSecurityGroupDemoData.xml b/headless-commerce/data/HeadlessCommerceSecurityGroupDemoData.xml new file mode 100644 index 000000000..2ac611fa3 --- /dev/null +++ b/headless-commerce/data/HeadlessCommerceSecurityGroupDemoData.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + diff --git a/headless-commerce/data/HeadlessCommerceSecurityPermissionSeedData.xml b/headless-commerce/data/HeadlessCommerceSecurityPermissionSeedData.xml new file mode 100644 index 000000000..fef790a8d --- /dev/null +++ b/headless-commerce/data/HeadlessCommerceSecurityPermissionSeedData.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + diff --git a/headless-commerce/data/HeadlessCommerceTypeData.xml b/headless-commerce/data/HeadlessCommerceTypeData.xml new file mode 100644 index 000000000..b76e67e05 --- /dev/null +++ b/headless-commerce/data/HeadlessCommerceTypeData.xml @@ -0,0 +1,23 @@ + + + + + + \ No newline at end of file diff --git a/headless-commerce/entitydef/entitymodel.xml b/headless-commerce/entitydef/entitymodel.xml new file mode 100644 index 000000000..adba0b411 --- /dev/null +++ b/headless-commerce/entitydef/entitymodel.xml @@ -0,0 +1,31 @@ + + + + + + + + Entity of HeadlessCommerce Component + None + + + + \ No newline at end of file diff --git a/headless-commerce/ofbiz-component.xml b/headless-commerce/ofbiz-component.xml new file mode 100644 index 000000000..4ef2d97f9 --- /dev/null +++ b/headless-commerce/ofbiz-component.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml new file mode 100644 index 000000000..b7a2b4c92 --- /dev/null +++ b/headless-commerce/servicedef/services.xml @@ -0,0 +1,33 @@ + + + + + HeadlessCommerce Services + + 1.0 + + + + Dummy service to prevent empty files and syntax error - Remove when the 1st real service will be added here + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java new file mode 100644 index 000000000..1a452e8de --- /dev/null +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -0,0 +1,7 @@ +package org.apache.ofbiz.hc.api.customer; + +public class CustomerServices { + + public static final String MODULE = CustomerServices.class.getName(); + public static final String RESOURCE = "PartyUiLabels"; +} diff --git a/headless-commerce/testdef/HeadlessCommerceTests.xml b/headless-commerce/testdef/HeadlessCommerceTests.xml new file mode 100644 index 000000000..de385c4b4 --- /dev/null +++ b/headless-commerce/testdef/HeadlessCommerceTests.xml @@ -0,0 +1,26 @@ + + + + + + + \ No newline at end of file diff --git a/headless-commerce/webapp/headless-commerce/WEB-INF/controller.xml b/headless-commerce/webapp/headless-commerce/WEB-INF/controller.xml new file mode 100644 index 000000000..385c4067c --- /dev/null +++ b/headless-commerce/webapp/headless-commerce/WEB-INF/controller.xml @@ -0,0 +1,47 @@ + + + + + + + + HeadlessCommerce Component Site Configuration File + + + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/webapp/headless-commerce/WEB-INF/web.xml b/headless-commerce/webapp/headless-commerce/WEB-INF/web.xml new file mode 100644 index 000000000..d2796fca9 --- /dev/null +++ b/headless-commerce/webapp/headless-commerce/WEB-INF/web.xml @@ -0,0 +1,96 @@ + + + + + Apache OFBiz - HeadlessCommerce Component + HeadlessCommerce Component of the Apache OFBiz Project + + + + A unique name used to identify/recognize the local dispatcher for the Service Engine + localDispatcherNameheadless-commerce + + + The Name of the Entity Delegator to use, defined in entityengine.xml + entityDelegatorNamedefault + + + The location of the main-decorator screen to use for this webapp; referred to as a context variable in screen def XML files. + mainDecoratorLocation + component://headless-commerce/widget/CommonScreens.xml + + + Remove unnecessary whitespace from HTML output. + compressHTML + false + + + + ControlFilter + ControlFilter + org.apache.ofbiz.webapp.control.ControlFilter + + allowedPaths + /error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images + + redirectPath/control/main + + + ContextFilter + ContextFilter + org.apache.ofbiz.webapp.control.ContextFilter + + + SameSiteFilter + SameSiteFilter + org.apache.ofbiz.webapp.control.SameSiteFilter + + ControlFilter/* + ContextFilter/* + SameSiteFilter/* + + org.apache.ofbiz.webapp.control.ControlEventListener + org.apache.ofbiz.webapp.control.LoginEventListener + + + + + Main Control Servlet + ControlServlet + ControlServlet + org.apache.ofbiz.webapp.control.ControlServlet + 1 + + ControlServlet/control/* + + + 60 + + + + index.jsp + index.html + index.htm + + diff --git a/headless-commerce/webapp/headless-commerce/index.jsp b/headless-commerce/webapp/headless-commerce/index.jsp new file mode 100644 index 000000000..4ea7d0c8b --- /dev/null +++ b/headless-commerce/webapp/headless-commerce/index.jsp @@ -0,0 +1,20 @@ +<%-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--%> + +<%response.sendRedirect("control/main");%> \ No newline at end of file diff --git a/headless-commerce/widget/CommonScreens.xml b/headless-commerce/widget/CommonScreens.xml new file mode 100644 index 000000000..45c4a78ec --- /dev/null +++ b/headless-commerce/widget/CommonScreens.xml @@ -0,0 +1,67 @@ + + + + + + +
+ + + + + + + + + + + + + + + +
+
+ + +
+ + + + + +
+ + + + + + + + + +
+
+
+
+
+
+
\ No newline at end of file diff --git a/headless-commerce/widget/HeadlessCommerceScreens.xml b/headless-commerce/widget/HeadlessCommerceScreens.xml new file mode 100644 index 000000000..ebd3940d5 --- /dev/null +++ b/headless-commerce/widget/HeadlessCommerceScreens.xml @@ -0,0 +1,38 @@ + + + + + + +
+ + + + + + + + + +
+
+ +
\ No newline at end of file From bc64894a335ed5e1f39fe9f4514e7236b626c6e7 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Thu, 22 Oct 2020 16:51:32 +0530 Subject: [PATCH 02/38] Implemented: Added password-hint api. --- headless-commerce/servicedef/services.xml | 11 ++--- .../api/customer/CustomerServices.java | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index b7a2b4c92..82202df98 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -24,10 +24,11 @@ under the License. 1.0 - - - Dummy service to prevent empty files and syntax error - Remove when the 1st real service will be added here - - + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 1a452e8de..bc4363ed0 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1,7 +1,51 @@ package org.apache.ofbiz.hc.api.customer; +import org.apache.ofbiz.base.util.*; +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.EntityUtilProperties; +import org.apache.ofbiz.service.DispatchContext; +import org.apache.ofbiz.service.ServiceUtil; + +import java.util.Locale; +import java.util.Map; + public class CustomerServices { public static final String MODULE = CustomerServices.class.getName(); public static final String RESOURCE = "PartyUiLabels"; + + public static Map getPasswordHint(DispatchContext ctx, Map context) { + Delegator delegator = ctx.getDelegator(); + Locale locale = (Locale) context.get("locale"); + String userLoginId = (String) context.get("username"); + Map result = ServiceUtil.returnSuccess(); + String passwordHint = null; + + try { + String usernameLowercase = EntityUtilProperties.getPropertyValue("security", "username.lowercase", delegator); + if (UtilValidate.isNotEmpty(userLoginId) && "true".equals(usernameLowercase)) { + userLoginId = userLoginId.toLowerCase(locale); + } + + GenericValue supposedUserLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginId).queryOne(); + if (supposedUserLogin != null) { + passwordHint = supposedUserLogin.getString("passwordHint"); + result.put("username", userLoginId); + result.put("passwordHint", passwordHint); + } + if (supposedUserLogin == null || UtilValidate.isEmpty(passwordHint)) { + // the Username was not found or there was no hint for the Username + String errMsg = UtilProperties.getMessage("SecurityextUiLabels", "loginevents.no_password_hint_specified_try_password_emailed", locale); + Debug.logError(errMsg, MODULE); + return ServiceUtil.returnError(errMsg); + } + } catch (GenericEntityException gee) { + Debug.logWarning(gee, "", MODULE); + return ServiceUtil.returnError(gee.getMessage()); + } + return result; + } } From 7bd7d0f31177631b741166a1cbf198f41abb75d9 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Thu, 22 Oct 2020 17:08:20 +0530 Subject: [PATCH 03/38] Implemented: Adding the xml based REST DSL file for headless commerce plugin to bind the ofbiz services. --- headless-commerce/api/headless-commerce.rest.xml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 headless-commerce/api/headless-commerce.rest.xml diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml new file mode 100644 index 000000000..f8ae7fa55 --- /dev/null +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file From 12bdf69f845caa1eed48d782d2f56e65ac3ec7ab Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Fri, 23 Oct 2020 15:45:19 +0530 Subject: [PATCH 04/38] Implemented: Added rest api for sending reset password mail. --- .../api/headless-commerce.rest.xml | 3 + headless-commerce/servicedef/services.xml | 7 +- .../api/customer/CustomerServices.java | 118 +++++++++++++++++- 3 files changed, 125 insertions(+), 3 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index f8ae7fa55..d414f6a0c 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -3,5 +3,8 @@ + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 82202df98..aa3389b07 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -29,6 +29,9 @@ under the License. - - + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index bc4363ed0..ef67de008 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1,21 +1,29 @@ package org.apache.ofbiz.hc.api.customer; import org.apache.ofbiz.base.util.*; +import org.apache.ofbiz.base.util.string.FlexibleStringExpander; 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.EntityUtilProperties; +import org.apache.ofbiz.party.contact.ContactHelper; +import org.apache.ofbiz.security.SecurityUtil; import org.apache.ofbiz.service.DispatchContext; +import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.service.ServiceUtil; +import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.stream.Collectors; public class CustomerServices { public static final String MODULE = CustomerServices.class.getName(); public static final String RESOURCE = "PartyUiLabels"; + public static final String SECRESOURCE = "SecurityextUiLabels"; public static Map getPasswordHint(DispatchContext ctx, Map context) { Delegator delegator = ctx.getDelegator(); @@ -38,7 +46,7 @@ public static Map getPasswordHint(DispatchContext ctx, Map getPasswordHint(DispatchContext ctx, Map sendResetPasswordEmail(DispatchContext ctx, Map context) { + Delegator delegator = ctx.getDelegator(); + LocalDispatcher dispatcher = ctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + String userLoginId = (String) context.get("username"); + String productStoreId = null; + String webSiteId = (String) context.get("webSiteId"); + String defaultScreenLocation = "component://securityext/widget/EmailSecurityScreens.xml#PasswordEmail"; + + try { + if (UtilValidate.isNotEmpty(webSiteId)) { + GenericValue webSite = EntityQuery.use(delegator).from("WebSite").where("webSiteId", webSiteId).queryOne(); + if (webSite != null) { + productStoreId = webSite.getString("productStoreId"); + } + } + // test if user exist and is active + GenericValue userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginId).queryOne(); + if (userLogin == null || "N".equals(userLogin.getString("enabled"))) { + Debug.logError("userlogin unknown or disabled " + userLogin, SECRESOURCE); + //giving a "sent email to associated email-address" response, to suppress feedback on in-/valid usernames + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.new_password_sent_check_email", locale)); + } + + // check login is associated to a party + GenericValue userParty = userLogin.getRelatedOne("Party", false); + if (userParty == null) { + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.username_not_found_reenter", locale)); + } + + // check there is an email to send to + List contactMechs = (List) ContactHelper.getContactMechByPurpose(userParty, "PRIMARY_EMAIL", false); + if (UtilValidate.isEmpty(contactMechs)) { + // the email was not found + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.no_primary_email_address_set_contact_customer_service", locale)); + } + String emails = contactMechs.stream() + .map(email -> email.getString("infoString")) + .collect(Collectors.joining(",")); + + //Generate a JWT with default retention time + String jwtToken = SecurityUtil.generateJwtToAuthenticateUserLogin(delegator, userLoginId); + + // get the ProductStore email settings + GenericValue productStoreEmail = null; + try { + productStoreEmail = EntityQuery.use(delegator).from("ProductStoreEmailSetting").where("productStoreId", + productStoreId, "emailType", "PRDS_PWD_RETRIEVE").queryOne(); + } catch (GenericEntityException e) { + Debug.logError(e, "Problem getting ProductStoreEmailSetting", MODULE); + } + + String bodyScreenLocation = null; + if (productStoreEmail != null) { + bodyScreenLocation = productStoreEmail.getString("bodyScreenLocation"); + } + if (UtilValidate.isEmpty(bodyScreenLocation)) { + bodyScreenLocation = defaultScreenLocation; + } + + // set the needed variables in new context + Map bodyParameters = new HashMap<>(); + bodyParameters.put("token", jwtToken); + bodyParameters.put("locale", locale); + bodyParameters.put("userLogin", userLogin); + bodyParameters.put("productStoreId", productStoreId); + + Map serviceContext = new HashMap<>(); + serviceContext.put("bodyScreenUri", bodyScreenLocation); + serviceContext.put("bodyParameters", bodyParameters); + serviceContext.put("webSiteId", webSiteId); + if (productStoreEmail != null) { + serviceContext.put("subject", productStoreEmail.getString("subject")); + serviceContext.put("sendFrom", productStoreEmail.get("fromAddress")); + serviceContext.put("sendCc", productStoreEmail.get("ccAddress")); + serviceContext.put("sendBcc", productStoreEmail.get("bccAddress")); + serviceContext.put("contentType", productStoreEmail.get("contentType")); + } else { + GenericValue emailTemplateSetting = EntityQuery.use(delegator).from("EmailTemplateSetting").where("emailTemplateSettingId", + "EMAIL_PASSWORD").cache().queryOne(); + if (emailTemplateSetting != null) { + String subject = emailTemplateSetting.getString("subject"); + subject = FlexibleStringExpander.expandString(subject, UtilMisc.toMap("userLoginId", userLoginId)); + serviceContext.put("subject", subject); + serviceContext.put("sendFrom", emailTemplateSetting.get("fromAddress")); + } else { + serviceContext.put("subject", UtilProperties.getMessage(SECRESOURCE, "loginservices.password_reminder_subject", + UtilMisc.toMap("userLoginId", userLoginId), locale)); + serviceContext.put("sendFrom", EntityUtilProperties.getPropertyValue("general", "defaultFromEmailAddress", delegator)); + } + } + serviceContext.put("sendTo", emails); + serviceContext.put("partyId", userParty.getString("partyId")); + + Map result = dispatcher.runSync("sendMailHiddenInLogFromScreen", serviceContext); + if (!ServiceUtil.isSuccess(result)) { + String errorMessage = ServiceUtil.getErrorMessage(result); + Map messageMap = UtilMisc.toMap("errorMessage", errorMessage); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.error_unable_email_password_contact_customer_service_errorwas", + messageMap, locale)); + } + } catch (GeneralException e) { + Debug.logWarning(e, "", MODULE); + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.error_unable_email_password_contact_customer_service", locale)); + } + return ServiceUtil.returnSuccess(UtilProperties.getMessage(SECRESOURCE, "loginevents.new_password_sent_check_email", locale)); + } } From 52803dbcfec8bd46dd9f689d1d9962a2b2772867 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Sat, 24 Oct 2020 19:16:44 +0530 Subject: [PATCH 05/38] Implemented: Added rest api for the user registration. Also did some minor improvements. --- .../api/headless-commerce.rest.xml | 3 + .../config/HeadlessCommerceUiLabels.xml | 7 + headless-commerce/servicedef/services.xml | 55 +++ .../api/common/CommonUtil.java | 27 ++ .../api/customer/CustomerServices.java | 433 +++++++++++++++++- 5 files changed, 511 insertions(+), 14 deletions(-) create mode 100644 headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index d414f6a0c..dcf7af0da 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -6,5 +6,8 @@ + + + \ No newline at end of file diff --git a/headless-commerce/config/HeadlessCommerceUiLabels.xml b/headless-commerce/config/HeadlessCommerceUiLabels.xml index c303f7159..d62247e1c 100644 --- a/headless-commerce/config/HeadlessCommerceUiLabels.xml +++ b/headless-commerce/config/HeadlessCommerceUiLabels.xml @@ -39,4 +39,11 @@ under the License. 不允许你浏览这个页面。 不允許您檢視這個頁面. + + State [${stateCode}] does not in country [${countryCode}] + + + Invalid country [${countryCode}] + + diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index aa3389b07..017af5e63 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -34,4 +34,59 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java new file mode 100644 index 000000000..08109fed4 --- /dev/null +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java @@ -0,0 +1,27 @@ +package org.apache.ofbiz.hc.api.common; + +import org.apache.ofbiz.base.util.UtilValidate; +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.hc.api.customer.CustomerServices; + +public class CommonUtil { + public static final String MODULE = CommonUtil.class.getName(); + + public static GenericValue getProductStore (Delegator delegator, String webSiteId) throws GenericEntityException { + if (UtilValidate.isNotEmpty(webSiteId)) { + GenericValue webSite = EntityQuery.use(delegator).from("WebSite").where("webSiteId", webSiteId).queryOne(); + if (webSite != null) { + String productStoreId = webSite.getString("productStoreId"); + if (UtilValidate.isNotEmpty(productStoreId)) { + return EntityQuery.use(delegator).from("ProductStore").where("productStoreId", productStoreId).queryOne(); + } + } + } + return null; + } + + +} diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index ef67de008..32e5c6f30 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -2,21 +2,18 @@ import org.apache.ofbiz.base.util.*; import org.apache.ofbiz.base.util.string.FlexibleStringExpander; +import org.apache.ofbiz.common.login.LoginServices; 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.EntityUtilProperties; +import org.apache.ofbiz.hc.api.common.CommonUtil; import org.apache.ofbiz.party.contact.ContactHelper; import org.apache.ofbiz.security.SecurityUtil; -import org.apache.ofbiz.service.DispatchContext; -import org.apache.ofbiz.service.LocalDispatcher; -import org.apache.ofbiz.service.ServiceUtil; - -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import org.apache.ofbiz.service.*; + +import java.util.*; import java.util.stream.Collectors; public class CustomerServices { @@ -66,11 +63,9 @@ public static Map sendResetPasswordEmail(DispatchContext ctx, Ma String defaultScreenLocation = "component://securityext/widget/EmailSecurityScreens.xml#PasswordEmail"; try { - if (UtilValidate.isNotEmpty(webSiteId)) { - GenericValue webSite = EntityQuery.use(delegator).from("WebSite").where("webSiteId", webSiteId).queryOne(); - if (webSite != null) { - productStoreId = webSite.getString("productStoreId"); - } + GenericValue productStore = CommonUtil.getProductStore(delegator, webSiteId); + if (UtilValidate.isNotEmpty(productStore)) { + productStoreId = productStore.getString("productStoreId"); } // test if user exist and is active GenericValue userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginId).queryOne(); @@ -164,4 +159,414 @@ public static Map sendResetPasswordEmail(DispatchContext ctx, Ma } return ServiceUtil.returnSuccess(UtilProperties.getMessage(SECRESOURCE, "loginevents.new_password_sent_check_email", locale)); } -} + + public static Map createCustomer(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Map result; + Map serviceCtx; + String webSiteId = (String) context.get("webSiteId"); + String firstName = (String) context.get("firstName"); + String middleName = (String) context.get("middleName"); + String lastName = (String) context.get("lastName"); + String emailAddress = (String) context.get("emailAddress"); + String username = (String) context.get("username"); + String password = (String) context.get("password"); + String confirmPassword = (String) context.get("confirmPassword"); + String passwordHint = (String) context.get("passwordHint"); + Map shippingAddress = UtilGenerics.cast(context.get("shippingAddress")); + Map homePhone = UtilGenerics.cast(context.get("homePhone")); + Map workPhone = UtilGenerics.cast(context.get("workPhone")); + Map faxNumber = UtilGenerics.cast(context.get("faxNumber")); + Map mobilePhone = UtilGenerics.cast(context.get("mobilePhone")); + List errorList; + GenericValue productStore = null; + + try { + result = dispatcher.runSync("validateCreateCustomer", context); + if (!ServiceUtil.isSuccess(result)) { + errorList = UtilGenerics.cast(result.get("errorMessageList")); + Debug.logError("parameter validation exception", errorList.toString()); + return ServiceUtil.returnError(errorList); + } + GenericValue system = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "system").queryOne(); + + //fetching product store + if (UtilValidate.isNotEmpty(webSiteId)) { + productStore = CommonUtil.getProductStore(delegator, webSiteId); + } + + //checking lowercase settings + boolean usernameLowercase = Boolean.parseBoolean(EntityUtilProperties.getPropertyValue("security", "username.lowercase", "false", delegator)); + boolean passwordLowercase = Boolean.parseBoolean(EntityUtilProperties.getPropertyValue("security", "password.lowercase", "false", delegator)); + username = usernameLowercase ? username.toLowerCase(): username; + password = passwordLowercase ? password.toLowerCase(): password; + confirmPassword = passwordLowercase ? confirmPassword.toLowerCase(): confirmPassword; + + //create userlogin + serviceCtx = dctx.getModelService("createPersonAndUserLogin").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("userLoginId", username); + serviceCtx.put("currentPassword", password); + serviceCtx.put("currentPasswordVerify", confirmPassword); + serviceCtx.put("passwordHint", passwordHint); + result = dispatcher.runSync("createPersonAndUserLogin", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + GenericValue createdUserLogin = (GenericValue) result.get("newUserLogin"); + String userPartyId = (String) result.get("partyId"); + + //Assigning customer role + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("roleTypeId", "CUSTOMER"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyRole", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + + //shipping address + if (UtilValidate.isNotEmpty(shippingAddress)) { + String countryGeoId; + String stateProvinceGeoId = null; + GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoTypeId", "COUNTRY", "abbreviation", shippingAddress.get("country")).queryFirst(); + countryGeoId = UtilValidate.isNotEmpty(countryGeo) ? countryGeo.getString("geoId") : null; + + if (UtilValidate.isNotEmpty(shippingAddress.get("state"))) { + GenericValue regionGeo = EntityQuery.use(delegator).from("GeoAssocAndGeoToWithState"). + where("geoIdFrom", countryGeoId, "geoAssocTypeId", "REGIONS", "geoCode", shippingAddress.get("state")).queryFirst(); + stateProvinceGeoId = UtilValidate.isEmpty(regionGeo) ? regionGeo.getString("geoId") : null; + } + + serviceCtx.clear(); + serviceCtx = dctx.getModelService("createPartyPostalAddress").makeValid(shippingAddress, ModelService.IN_PARAM); + serviceCtx.put("toName", firstName + " " + middleName + " " + lastName); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); + serviceCtx.put("countryGeoId", countryGeoId); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + String contactMechId = (String) result.get("contactMechId"); + + //create the shipping location + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", "SHIPPING_LOCATION"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + + //also consider this address the general correspondence address + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", "GENERAL_LOCATION"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + //home phone + if (UtilValidate.isNotEmpty(homePhone)) { + serviceCtx.clear(); + serviceCtx = dctx.getModelService("createPartyTelecomNumber").makeValid(homePhone, ModelService.IN_PARAM); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyTelecomNumber", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + String contactMechId = (String) result.get("contactMechId"); + + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", "PHONE_HOME"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + //also consider as primary phone + serviceCtx.put("contactMechPurposeTypeId", "PRIMARY_PHONE"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + //work phone + if (UtilValidate.isNotEmpty(workPhone)) { + serviceCtx.clear(); + serviceCtx = dctx.getModelService("createPartyTelecomNumber").makeValid(workPhone, ModelService.IN_PARAM); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyTelecomNumber", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + String contactMechId = (String) result.get("contactMechId"); + + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", "PHONE_WORK"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + //fax number + if (UtilValidate.isNotEmpty(faxNumber)) { + serviceCtx.clear(); + serviceCtx = dctx.getModelService("createPartyTelecomNumber").makeValid(faxNumber, ModelService.IN_PARAM); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyTelecomNumber", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + String contactMechId = (String) result.get("contactMechId"); + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", "FAX_NUMBER"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + + //mobile phone + if (UtilValidate.isNotEmpty(mobilePhone)) { + serviceCtx.clear(); + serviceCtx = dctx.getModelService("createPartyTelecomNumber").makeValid(mobilePhone, ModelService.IN_PARAM); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyTelecomNumber", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + String contactMechId = (String) result.get("contactMechId"); + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", "PHONE_MOBILE"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + //email address + if (UtilValidate.isNotEmpty(emailAddress)) { + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("emailAddress", emailAddress); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyEmailAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + String contactMechId = (String) result.get("contactMechId"); + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", "PRIMARY_EMAIL"); + serviceCtx.put("userLogin", createdUserLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + //club number + GenericValue person = EntityQuery.use(delegator).from("Person").where("partyId", userPartyId).queryOne(); + if (UtilValidate.isNotEmpty(context.get("requireClub"))) { + if (person != null && UtilValidate.isEmpty(context.get("clubNumber")) ) { + String clubId = org.apache.ofbiz.party.party.PartyWorker.createClubId(delegator, "999", 13); + person.set("memberId", clubId); + person.store(); + } + } + + if (productStore != null) { + //Associate to Product Store + String productStoreId = productStore.getString("productStoreId"); + serviceCtx.clear(); + serviceCtx.put("partyId", userPartyId); + serviceCtx.put("roleTypeId", "CUSTOMER"); + serviceCtx.put("productStoreId", productStoreId); + serviceCtx.put("userLogin", system); + result = dispatcher.runSync("createProductStoreRole", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + + //send off the registration email + if (UtilValidate.isNotEmpty(emailAddress)) { + GenericValue productStoreEmailSetting = EntityQuery.use(delegator).from("ProductStoreEmailSetting").where("productStoreId", productStoreId, "emailType", "PRDS_CUST_REGISTER").queryFirst(); + if (productStoreEmailSetting != null) { + serviceCtx.clear(); + Map bodyParameters = new HashMap<>(); + bodyParameters.put("person", person); + serviceCtx.put("bodyParameters", bodyParameters); + serviceCtx.put("sendTo", emailAddress); + serviceCtx.put("subject", productStoreEmailSetting.getString("subject")); + serviceCtx.put("sendFrom", productStoreEmailSetting.getString("fromAddress")); + serviceCtx.put("sendCc", productStoreEmailSetting.getString("ccAddress")); + serviceCtx.put("sendBcc", productStoreEmailSetting.getString("bccAddress")); + serviceCtx.put("contentType", productStoreEmailSetting.getString("contentType")); + serviceCtx.put("bodyScreenUri", productStoreEmailSetting.getString("bodyScreenLocation")); + serviceCtx.put("emailType", "PRDS_CUST_REGISTER"); + dispatcher.runAsync("sendMailFromScreen", serviceCtx); + } + } + } + + //assign security group to manage profile + if (createdUserLogin != null) { + serviceCtx.clear(); + serviceCtx.put("userLoginId", createdUserLogin.getString("userLoginId")); + serviceCtx.put("groupId", "ECOMMERCE_CUSTOMER"); + serviceCtx.put("userLogin", system); + result = dispatcher.runSync("addUserLoginToSecurityGroup", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + } catch (GenericServiceException | GenericEntityException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } + + public static Map validateCreateCustomer(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + Locale locale = (Locale) context.get("locale"); + String emailAddress = (String) context.get("emailAddress"); + String username = (String) context.get("username"); + String password = (String) context.get("password"); + String passwordHint = (String) context.get("passwordHint"); + String confirmPassword = (String) context.get("confirmPassword"); + Map shippingAddress = UtilGenerics.cast(context.get("shippingAddress")); + Map homePhone = UtilGenerics.cast(context.get("homePhone")); + Map workPhone = UtilGenerics.cast(context.get("workPhone")); + Map faxNumber = UtilGenerics.cast(context.get("faxNumber")); + Map mobilePhone = UtilGenerics.cast(context.get("mobilePhone")); + List errorList = new ArrayList<>(); + + try { + if (!UtilValidate.isEmail(emailAddress)) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyEmailAddressNotFormattedCorrectly", locale)); + } + if (UtilValidate.isNotEmpty(shippingAddress)) { + if (UtilValidate.isEmpty(shippingAddress.get("address1"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyAddressLine1MissingError", locale)); + } + if (UtilValidate.isEmpty(shippingAddress.get("city"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyCityMissing", locale)); + } + if (UtilValidate.isEmpty(shippingAddress.get("postalCode"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyZipCodeMissing", locale)); + } + if (UtilValidate.isEmpty(shippingAddress.get("country"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyCountryMissing", locale)); + } + String country = shippingAddress.get("country"); + String state = shippingAddress.get("state"); + if ("USA".equals(country) && UtilValidate.isEmpty(state)) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyStateInUsMissing", locale)); + } + if ("CAN".equals(country) && UtilValidate.isEmpty(state)) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyStateInCanadaMissing", locale)); + } + GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoTypeId", "COUNTRY", "abbreviation", country).queryFirst(); + if (UtilValidate.isNotEmpty(country) && countryGeo == null) { + errorList.add(UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCInvalidCountryCode", + UtilMisc.toMap("countryCode", country), locale)); + } + if (countryGeo != null && UtilValidate.isNotEmpty(state)) { + GenericValue regionGeo = EntityQuery.use(delegator).from("GeoAssocAndGeoToWithState"). + where("geoIdFrom", countryGeo.getString("geoId"), "geoAssocTypeId", "REGIONS", "geoCode", state).queryFirst(); + if (UtilValidate.isEmpty(regionGeo)) { + // if given state is not associated with country then return error. + errorList.add(UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCCountrysStateIsNotValid", + UtilMisc.toMap("stateCode", state, "countryCode", country), locale)); + } + } + + } + if (UtilValidate.isNotEmpty(homePhone) && UtilValidate.isEmpty(homePhone.get("contactNumber"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); + } + if (UtilValidate.isNotEmpty(workPhone) && UtilValidate.isEmpty(workPhone.get("contactNumber"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); + } + if (UtilValidate.isNotEmpty(faxNumber) && UtilValidate.isEmpty(faxNumber.get("contactNumber"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); + } + if (UtilValidate.isNotEmpty(mobilePhone) && UtilValidate.isEmpty(mobilePhone.get("contactNumber"))) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); + } + if (!password.equals(confirmPassword)) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyPasswordMatchError", locale)); + } + + GenericValue existingUser = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", username).queryOne(); + if (existingUser != null) { + errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyUserNameInUse", locale)); + } + + //Check the password, etc for validity + GenericValue newUserLogin = delegator.makeValue("UserLogin"); + newUserLogin.set("userLoginId", username); + newUserLogin.set("currentPassword", password); + newUserLogin.set("passwordHint", passwordHint); + LoginServices.checkNewPassword(newUserLogin, null, password, confirmPassword, passwordHint, errorList, true, locale); + + } catch (GenericEntityException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + Debug.log("===========errorList="+errorList+"============"); + if (UtilValidate.isNotEmpty(errorList)) { + return ServiceUtil.returnError(errorList); + } + return ServiceUtil.returnSuccess(); + } +} \ No newline at end of file From aec4fbbc614ee2d4ce5c41910c4e8d6d1b6957cd Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 26 Oct 2020 09:55:14 +0530 Subject: [PATCH 06/38] Improved: Removed log statement committed accidentally. --- .../java/org.apache.ofbiz.hc/api/customer/CustomerServices.java | 1 - 1 file changed, 1 deletion(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 32e5c6f30..f2a42ab21 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -563,7 +563,6 @@ public static Map validateCreateCustomer(DispatchContext dctx, M Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); } - Debug.log("===========errorList="+errorList+"============"); if (UtilValidate.isNotEmpty(errorList)) { return ServiceUtil.returnError(errorList); } From 285aa4af1f0826823aef4c268d5b64884f5c9ff9 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 26 Oct 2020 18:20:48 +0530 Subject: [PATCH 07/38] Implemented: Added api to change password. --- .../api/headless-commerce.rest.xml | 3 ++ .../config/HeadlessCommerceUiLabels.xml | 3 ++ headless-commerce/servicedef/services.xml | 9 ++++++ .../api/common/CommonUtil.java | 11 +++++-- .../api/customer/CustomerServices.java | 30 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index dcf7af0da..e7c49d5b5 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -9,5 +9,8 @@ + + + \ No newline at end of file diff --git a/headless-commerce/config/HeadlessCommerceUiLabels.xml b/headless-commerce/config/HeadlessCommerceUiLabels.xml index d62247e1c..6b127d138 100644 --- a/headless-commerce/config/HeadlessCommerceUiLabels.xml +++ b/headless-commerce/config/HeadlessCommerceUiLabels.xml @@ -45,5 +45,8 @@ under the License. Invalid country [${countryCode}] + + Access denied. Invalid user. + diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 017af5e63..a12e40cfe 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -34,6 +34,15 @@ under the License. + + + + + + + + diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java index 08109fed4..90dc79c52 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/common/CommonUtil.java @@ -22,6 +22,13 @@ public static GenericValue getProductStore (Delegator delegator, String webSiteI } return null; } - - + public static boolean isValidCutomer (Delegator delegator, GenericValue userLogin, String customerPartyId) throws GenericEntityException { + if (UtilValidate.isNotEmpty(customerPartyId) && userLogin != null && customerPartyId.equalsIgnoreCase(userLogin.getString("partyId"))) { + GenericValue partyRole = EntityQuery.use(delegator).from("PartyRole").where("partyId", customerPartyId, "roleTypeId", "CUSTOMER").queryOne(); + if (partyRole != null) { + return true; + } + } + return false; + } } diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index f2a42ab21..6b011c74c 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -159,6 +159,36 @@ public static Map sendResetPasswordEmail(DispatchContext ctx, Ma } return ServiceUtil.returnSuccess(UtilProperties.getMessage(SECRESOURCE, "loginevents.new_password_sent_check_email", locale)); } + public static Map changePassword(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + GenericValue customerUserLogin = EntityQuery.use(delegator).from("UserLogin").where("partyId", customerPartyId).queryFirst(); + if (customerUserLogin != null) { + Map serviceContext = dctx.getModelService("updatePassword").makeValid(context, ModelService.IN_PARAM); + serviceContext.put("userLoginId", customerUserLogin.getString("userLoginId")); + serviceContext.put("userLogin", customerUserLogin); + Map result = dispatcher.runSync("updatePassword", serviceContext); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } public static Map createCustomer(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); From 11ee733f7b64e82b7c0a4fbaf52da921d45252e0 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Tue, 27 Oct 2020 14:16:31 +0530 Subject: [PATCH 08/38] Improved: Corrected field types in create customer api.. --- headless-commerce/servicedef/services.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index a12e40cfe..489926f22 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -58,12 +58,12 @@ under the License. - - - - - - + + + + + + From 38c72d2d21ebd0dd9f3d9d6cc41969113c96dabc Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Tue, 27 Oct 2020 19:25:57 +0530 Subject: [PATCH 09/38] Implemented: Added service to get the customer profile data. --- .../api/headless-commerce.rest.xml | 3 + headless-commerce/servicedef/services.xml | 74 ++++++++++++- .../api/customer/CustomerHelper.java | 52 +++++++++ .../api/customer/CustomerServices.java | 103 ++++++++++++++++++ 4 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index e7c49d5b5..2c135d055 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -12,5 +12,8 @@ + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 489926f22..ee6909b3d 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -97,5 +97,77 @@ under the License. location="org.apache.ofbiz.hc.api.customer.CustomerServices" invoke="validateCreateCustomer"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java new file mode 100644 index 000000000..66e5db7c5 --- /dev/null +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java @@ -0,0 +1,52 @@ +package org.apache.ofbiz.hc.api.customer; + +import jdk.nashorn.internal.runtime.logging.DebugLogger; +import org.apache.ofbiz.base.util.Debug; +import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.base.util.UtilValidate; +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.service.GenericServiceException; +import org.apache.ofbiz.service.LocalDispatcher; +import org.apache.ofbiz.service.ServiceUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CustomerHelper { + public static final String MODULE = CustomerServices.class.getName(); + + public static Map getLoyaltyPoints(LocalDispatcher dispatcher, String customerPartyId, GenericValue userLogin) throws GenericServiceException { + Map loyaltyPointMap = new HashMap<>(); + if (UtilValidate.isNotEmpty(customerPartyId)) { + //TODO: Need to make it configurable + int monthsToInclude = 12; + Map result = dispatcher.runSync("getOrderedSummaryInformation", + UtilMisc.toMap("partyId", customerPartyId, "roleTypeId", "PLACING_CUSTOMER", + "orderTypeId", "SALES_ORDER", "statusId", "ORDER_COMPLETED", "monthsToInclude", monthsToInclude, "userLogin", userLogin)); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return null; + } + loyaltyPointMap.put("points", result.get("totalSubRemainingAmount")); + loyaltyPointMap.put("totalOrders", result.get("totalOrders")); + loyaltyPointMap.put("monthsToInclude", monthsToInclude); + } + return loyaltyPointMap; + } + public static List> prepareContactMechPurposeList(Delegator delegator, List partyContactMechPurposes) throws GenericEntityException { + List> contactMechPurposes = new ArrayList<>(); + for (GenericValue partyContactMechPurpose : partyContactMechPurposes) { + Map purposeMap = new HashMap<>(); + GenericValue contactMechPurposeType = EntityQuery.use(delegator).from("ContactMechPurposeType").where("contactMechPurposeTypeId", partyContactMechPurpose.getString("contactMechPurposeTypeId")).queryOne(); + purposeMap.put("contactMechPurposeTypeId", contactMechPurposeType.getString("contactMechPurposeTypeId")); + purposeMap.put("description", contactMechPurposeType.getString("description")); + contactMechPurposes.add(purposeMap); + } + return contactMechPurposes; + } +} diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 6b011c74c..47d709734 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -10,6 +10,7 @@ import org.apache.ofbiz.entity.util.EntityUtilProperties; import org.apache.ofbiz.hc.api.common.CommonUtil; import org.apache.ofbiz.party.contact.ContactHelper; +import org.apache.ofbiz.party.contact.ContactMechWorker; import org.apache.ofbiz.security.SecurityUtil; import org.apache.ofbiz.service.*; @@ -598,4 +599,106 @@ public static Map validateCreateCustomer(DispatchContext dctx, M } return ServiceUtil.returnSuccess(); } + public static Map getCustomerProfile(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String showOldStr = (String) context.get("showOld"); + Map response = ServiceUtil.returnSuccess(); + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + boolean showOld = "true".equals(showOldStr); + GenericValue person = EntityQuery.use(delegator).from("Person").where("partyId", customerPartyId).queryFirst(); + if (person != null) { + response.put("customerPartyId", person.getString("partyId")); + response.put("personalTitle", person.getString("personalTitle")); + response.put("firstName", person.getString("firstName")); + response.put("middleName", person.getString("middleName")); + response.put("lastName", person.getString("lastName")); + response.put("suffix", person.getString("suffix")); + + Map contactInformation = new HashMap<>(); + //postal addresses + List> addresses = new ArrayList<>(); + List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld, "POSTAL_ADDRESS"); + for (Map partyContactMechValueMap : partyContactMechValueMaps) { + Map infoMap = new HashMap<>(); + GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); + if (postalAddress != null) { + infoMap.put("contactMechId", postalAddress.getString("contactMechId")); + infoMap.put("toName", postalAddress.getString("toName")); + infoMap.put("address1", postalAddress.getString("address1")); + infoMap.put("address2", postalAddress.getString("address2")); + infoMap.put("city", postalAddress.getString("city")); + infoMap.put("postalCode", postalAddress.getString("postalCode")); + + Map geoMap = new HashMap<>(); + GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); + if (countryGeo != null) { + geoMap.put("geoId", countryGeo.getString("geoId")); + geoMap.put("geoName", countryGeo.getString("geoName")); + geoMap.put("geoCode", countryGeo.getString("abbreviation")); + infoMap.put("country", geoMap); + } + geoMap = new HashMap<>(); + GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); + if (stateGeo != null) { + geoMap.put("geoId", stateGeo.getString("geoId")); + geoMap.put("geoName", stateGeo.getString("geoName")); + geoMap.put("geoCode", stateGeo.getString("abbreviation")); + infoMap.put("state", geoMap); + } + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + addresses.add(infoMap); + } + } + //phone numbers + List> phoneNumbers = new ArrayList<>(); + partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld, "TELECOM_NUMBER"); + for (Map partyContactMechValueMap : partyContactMechValueMaps) { + Map infoMap = new HashMap<>(); + GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); + if (telecomNumber != null) { + infoMap.put("contactMechId", telecomNumber.getString("contactMechId")); + infoMap.put("countryCode", telecomNumber.getString("countryCode")); + infoMap.put("areaCode", telecomNumber.getString("areaCode")); + infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + phoneNumbers.add(infoMap); + } + } + //emails + List> emails = new ArrayList<>(); + partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld, "EMAIL_ADDRESS"); + for (Map partyContactMechValueMap : partyContactMechValueMaps) { + Map infoMap = new HashMap<>(); + GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); + if (contactMech != null) { + infoMap.put("emailAddress", contactMech.getString("infoString")); + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + emails.add(infoMap); + } + } + contactInformation.put("addresses", addresses); + contactInformation.put("phoneNumbers", phoneNumbers); + contactInformation.put("emails", emails); + response.put("contactInformation", contactInformation); + response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); + } + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return response; + } } \ No newline at end of file From a8f8776070293c23e3fa8905433b7a19047d5b14 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Tue, 27 Oct 2020 19:32:40 +0530 Subject: [PATCH 10/38] Improved: Remove unnecessary code. --- .../org.apache.ofbiz.hc/api/customer/CustomerServices.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 47d709734..0b27e771d 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -173,17 +173,14 @@ public static Map changePassword(DispatchContext dctx, Map serviceContext = dctx.getModelService("updatePassword").makeValid(context, ModelService.IN_PARAM); - serviceContext.put("userLoginId", customerUserLogin.getString("userLoginId")); - serviceContext.put("userLogin", customerUserLogin); + serviceContext.put("userLoginId", userLogin.getString("userLoginId")); + serviceContext.put("userLogin", userLogin); Map result = dispatcher.runSync("updatePassword", serviceContext); if (!ServiceUtil.isSuccess(result)) { Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } - } } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); From f000c7cf3ebad4026e9926a09fabb8b017893c14 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Tue, 27 Oct 2020 19:33:20 +0530 Subject: [PATCH 11/38] Improved: Formatting changes. --- .../api/customer/CustomerServices.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 0b27e771d..30de2eaa4 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -173,14 +173,14 @@ public static Map changePassword(DispatchContext dctx, Map serviceContext = dctx.getModelService("updatePassword").makeValid(context, ModelService.IN_PARAM); - serviceContext.put("userLoginId", userLogin.getString("userLoginId")); - serviceContext.put("userLogin", userLogin); - Map result = dispatcher.runSync("updatePassword", serviceContext); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } + Map serviceContext = dctx.getModelService("updatePassword").makeValid(context, ModelService.IN_PARAM); + serviceContext.put("userLoginId", userLogin.getString("userLoginId")); + serviceContext.put("userLogin", userLogin); + Map result = dispatcher.runSync("updatePassword", serviceContext); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); From 15f2b4e51cb50e00df81ffab83fa5820d7a9cda2 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Wed, 28 Oct 2020 12:37:30 +0530 Subject: [PATCH 12/38] Implementred: Restructured information to return the contact information. --- headless-commerce/servicedef/services.xml | 84 +++++-------- .../api/customer/CustomerServices.java | 118 +++++++++--------- 2 files changed, 89 insertions(+), 113 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index ee6909b3d..62ac5262b 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -106,63 +106,41 @@ under the License. - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + - - - - - - - - - - - + + + + - + diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 30de2eaa4..31d769733 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -621,75 +621,73 @@ public static Map getCustomerProfile(DispatchContext dctx, Map contactInformation = new HashMap<>(); - //postal addresses - List> addresses = new ArrayList<>(); - List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld, "POSTAL_ADDRESS"); + List> contactMechs = new ArrayList<>(); + List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); for (Map partyContactMechValueMap : partyContactMechValueMaps) { Map infoMap = new HashMap<>(); - GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); - if (postalAddress != null) { - infoMap.put("contactMechId", postalAddress.getString("contactMechId")); - infoMap.put("toName", postalAddress.getString("toName")); - infoMap.put("address1", postalAddress.getString("address1")); - infoMap.put("address2", postalAddress.getString("address2")); - infoMap.put("city", postalAddress.getString("city")); - infoMap.put("postalCode", postalAddress.getString("postalCode")); - - Map geoMap = new HashMap<>(); - GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); - if (countryGeo != null) { - geoMap.put("geoId", countryGeo.getString("geoId")); - geoMap.put("geoName", countryGeo.getString("geoName")); - geoMap.put("geoCode", countryGeo.getString("abbreviation")); - infoMap.put("country", geoMap); + GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); + GenericValue contactMechType = (GenericValue) partyContactMechValueMap.get("contactMechType"); + infoMap.put("contactMechId", contactMech.getString("contactMechId")); + if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("postalAddress"))) { + //postal address + GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); + if (postalAddress != null) { + infoMap.put("toName", postalAddress.getString("toName")); + infoMap.put("address1", postalAddress.getString("address1")); + infoMap.put("address2", postalAddress.getString("address2")); + infoMap.put("city", postalAddress.getString("city")); + infoMap.put("postalCode", postalAddress.getString("postalCode")); + + Map geoMap = new HashMap<>(); + GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); + if (countryGeo != null) { + geoMap.put("geoId", countryGeo.getString("geoId")); + geoMap.put("geoName", countryGeo.getString("geoName")); + geoMap.put("geoCode", countryGeo.getString("abbreviation")); + infoMap.put("country", geoMap); + } + geoMap = new HashMap<>(); + GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); + if (stateGeo != null) { + geoMap.put("geoId", stateGeo.getString("geoId")); + geoMap.put("geoName", stateGeo.getString("geoName")); + geoMap.put("geoCode", stateGeo.getString("abbreviation")); + infoMap.put("state", geoMap); + } + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + contactMechs.add(infoMap); } - geoMap = new HashMap<>(); - GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); - if (stateGeo != null) { - geoMap.put("geoId", stateGeo.getString("geoId")); - geoMap.put("geoName", stateGeo.getString("geoName")); - geoMap.put("geoCode", stateGeo.getString("abbreviation")); - infoMap.put("state", geoMap); + } else if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("telecomNumber"))) { + //phone number + GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); + if (telecomNumber != null) { + infoMap.put("countryCode", telecomNumber.getString("countryCode")); + infoMap.put("areaCode", telecomNumber.getString("areaCode")); + infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + contactMechs.add(infoMap); } - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - addresses.add(infoMap); + } else { + //email, web address, electronic address, etc. + infoMap.put("infoString", contactMech.getString("infoString")); } - } - //phone numbers - List> phoneNumbers = new ArrayList<>(); - partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld, "TELECOM_NUMBER"); - for (Map partyContactMechValueMap : partyContactMechValueMaps) { - Map infoMap = new HashMap<>(); - GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); - if (telecomNumber != null) { - infoMap.put("contactMechId", telecomNumber.getString("contactMechId")); - infoMap.put("countryCode", telecomNumber.getString("countryCode")); - infoMap.put("areaCode", telecomNumber.getString("areaCode")); - infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - phoneNumbers.add(infoMap); - } - } - //emails - List> emails = new ArrayList<>(); - partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld, "EMAIL_ADDRESS"); - for (Map partyContactMechValueMap : partyContactMechValueMaps) { - Map infoMap = new HashMap<>(); - GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); - if (contactMech != null) { - infoMap.put("emailAddress", contactMech.getString("infoString")); + + //contact mech type + Map contactMechTypeMap = new HashMap<>(); + contactMechTypeMap.put("contactMechTypeId", contactMechType.getString("contactMechTypeId")); + contactMechTypeMap.put("description", contactMechType.getString("description")); + infoMap.put("contactMechType", contactMechTypeMap); + + //contact mech purpose + if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("partyContactMechPurposes"))) { infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - emails.add(infoMap); } + contactMechs.add(infoMap); } - contactInformation.put("addresses", addresses); - contactInformation.put("phoneNumbers", phoneNumbers); - contactInformation.put("emails", emails); - response.put("contactInformation", contactInformation); + response.put("contactMechs", contactMechs); response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); } } catch (GenericEntityException | GenericServiceException e) { From 6e1ae2678a6d12a5cd0a740980c96f40d669926d Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Wed, 28 Oct 2020 18:04:13 +0530 Subject: [PATCH 13/38] Implemented: Added logic to fetch the payment methods for the customer profile. --- headless-commerce/servicedef/services.xml | 35 +++++++++ .../api/customer/CustomerServices.java | 78 +++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 62ac5262b..1327cd283 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -106,6 +106,7 @@ under the License. + @@ -147,5 +148,39 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 31d769733..3eddeb80a 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1,5 +1,6 @@ package org.apache.ofbiz.hc.api.customer; +import org.apache.ofbiz.accounting.payment.PaymentWorker; import org.apache.ofbiz.base.util.*; import org.apache.ofbiz.base.util.string.FlexibleStringExpander; import org.apache.ofbiz.common.login.LoginServices; @@ -620,6 +621,7 @@ public static Map getCustomerProfile(DispatchContext dctx, Map> contactMechs = new ArrayList<>(); List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); @@ -687,6 +689,16 @@ public static Map getCustomerProfile(DispatchContext dctx, Map serviceContext = new HashMap<>(); + serviceContext.put("customerPartyId", customerPartyId); + serviceContext.put("showOld", showOld); + serviceContext.put("userLogin", userLogin); + Map result = dispatcher.runSync("getPartyPaymentMethods", serviceContext); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + } + response.put("paymentMethods", UtilGenerics.cast(result.get("paymentMethods"))); response.put("contactMechs", contactMechs); response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); } @@ -696,4 +708,70 @@ public static Map getCustomerProfile(DispatchContext dctx, Map getPartyPaymentMethods(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String showOldStr = (String) context.get("showOld"); + Map result = ServiceUtil.returnSuccess(); + List> paymentMethods = new ArrayList<>(); + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + boolean showOld = "true".equals(showOldStr); + List> paymentMethodValueMaps = PaymentWorker.getPartyPaymentMethodValueMaps(delegator, customerPartyId, showOld); + for (Map paymentMethodValueMap : paymentMethodValueMaps) { + Map infoMap = new HashMap<>(); + GenericValue paymentMethod = (GenericValue) paymentMethodValueMap.get("paymentMethod"); + infoMap.put("paymentMethodId", paymentMethod.getString("paymentMethodId")); + infoMap.put("description", paymentMethod.getString("description")); + infoMap.put("fromDate", paymentMethod.getString("fromDate")); + infoMap.put("thruDate", paymentMethod.getString("thruDate")); + + //payment method type + Map paymentMethodTypeMap = new HashMap<>(); + GenericValue paymentMethodType = EntityQuery.use(delegator).from("PaymentMethodType").where("paymentMethodTypeId", paymentMethod.getString("paymentMethodTypeId")).queryOne(); + paymentMethodTypeMap.put("paymentMethodTypeId", paymentMethodType.getString("paymentMethodTypeId")); + paymentMethodTypeMap.put("description", paymentMethodType.getString("description")); + infoMap.put("paymentMethodType", paymentMethodTypeMap); + + if (UtilValidate.isNotEmpty(paymentMethodValueMap.get("creditCard"))) { + GenericValue creditCard = (GenericValue) paymentMethodValueMap.get("creditCard"); + infoMap.put("companyNameOnCard", creditCard.getString("companyNameOnCard")); + infoMap.put("titleOnCard", creditCard.getString("titleOnCard")); + infoMap.put("firstNameOnCard", creditCard.getString("firstNameOnCard")); + infoMap.put("middleNameOnCard", creditCard.getString("middleNameOnCard")); + infoMap.put("lastNameOnCard", creditCard.getString("lastNameOnCard")); + infoMap.put("suffixOnCard", creditCard.getString("suffixOnCard")); + infoMap.put("cardNumber", creditCard.getString("cardNumber")); + infoMap.put("expireDate", creditCard.getString("expireDate")); + } else if (UtilValidate.isNotEmpty(paymentMethodValueMap.get("giftCard"))) { + GenericValue giftCard = (GenericValue) paymentMethodValueMap.get("giftCard"); + infoMap.put("cardNumber", giftCard.getString("cardNumber")); + infoMap.put("expireDate", giftCard.getString("expireDate")); + + } else if (UtilValidate.isNotEmpty(paymentMethodValueMap.get("eftAccount"))) { + GenericValue eftAccount = (GenericValue) paymentMethodValueMap.get("eftAccount"); + infoMap.put("bankName", eftAccount.getString("bankName")); + infoMap.put("routingNumber", eftAccount.getString("routingNumber")); + infoMap.put("accountType", eftAccount.getString("accountType")); + infoMap.put("accountNumber", eftAccount.getString("accountNumber")); + infoMap.put("nameOnAccount", eftAccount.getString("nameOnAccount")); + infoMap.put("companyNameOnAccount", eftAccount.getString("companyNameOnAccount")); + } + paymentMethods.add(infoMap); + } + } catch (GenericEntityException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + result.put("paymentMethods", paymentMethods); + return result; + } + } \ No newline at end of file From fd4a766bdfe0785a56388e3949a9243dcf8d2650 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Thu, 29 Oct 2020 12:15:16 +0530 Subject: [PATCH 14/38] Implemented: Added user's contact lists and communications to the customer profile api. --- headless-commerce/servicedef/services.xml | 25 +++++++++ .../api/customer/CustomerHelper.java | 53 +++++++++++++++++++ .../api/customer/CustomerServices.java | 6 ++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 1327cd283..854a9a7b6 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -176,6 +176,31 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java index 66e5db7c5..87efb08a8 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java @@ -7,7 +7,10 @@ import org.apache.ofbiz.entity.Delegator; import org.apache.ofbiz.entity.GenericEntityException; import org.apache.ofbiz.entity.GenericValue; +import org.apache.ofbiz.entity.condition.EntityCondition; +import org.apache.ofbiz.entity.condition.EntityOperator; import org.apache.ofbiz.entity.util.EntityQuery; +import org.apache.ofbiz.party.party.PartyHelper; import org.apache.ofbiz.service.GenericServiceException; import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.service.ServiceUtil; @@ -49,4 +52,54 @@ public static List> prepareContactMechPurposeList(Delegator } return contactMechPurposes; } + public static List> getPartyContactLists(Delegator delegator, String customerPartyId) throws GenericEntityException { + List> subscriptionList = new ArrayList<>(); + List contactListPartyList = EntityQuery.use(delegator).from("ContactListParty").where("partyId", customerPartyId).orderBy("-fromDate").queryList(); + for (GenericValue contactListParty : contactListPartyList) { + Map infoMap = new HashMap<>(); + GenericValue contactList = EntityQuery.use(delegator).from("ContactList").where("contactListId", contactListParty.getString("contactListId")).queryOne(); + GenericValue statusItem = EntityQuery.use(delegator).from("StatusItem").where("statusId", contactListParty.getString("statusId")).queryOne(); + GenericValue emailContactMech = EntityQuery.use(delegator).select("infoString").from("ContactMech").where("contactMechId", contactListParty.getString("preferredContactMechId"), "contactMechTypeId", "EMAIL_ADDRESS").queryOne(); + infoMap.put("contactListId", contactList.getString("contactListId")); + infoMap.put("email", emailContactMech.getString("infoString")); + infoMap.put("contactListName", contactList.getString("contactListName")); + infoMap.put("description", contactList.getString("description")); + infoMap.put("fromDate", contactListParty.getString("fromDate")); + infoMap.put("thruDate", contactListParty.getString("thruDate")); + infoMap.put("status", UtilMisc.toMap("statusId", statusItem.getString("statusId"), + "description", statusItem.getString("description"), "statusCode", statusItem.getString("statusCode"))); + subscriptionList.add(infoMap); + } + return subscriptionList; + } + public static List> getPartyCommunications(Delegator delegator, String customerPartyId, boolean showReceived, boolean showSent) throws GenericEntityException { + List> communications = new ArrayList<>(); + EntityCondition condition = null; + if (showReceived && showSent) { + condition = EntityCondition.makeCondition( + EntityCondition.makeCondition("partyIdTo", customerPartyId), + EntityOperator.OR, + EntityCondition.makeCondition("partyIdFrom", customerPartyId)); + } else if (showReceived) { + condition = EntityCondition.makeCondition("partyIdTo", customerPartyId); + } else if (showSent) { + condition = EntityCondition.makeCondition("partyIdFrom", customerPartyId); + } + + List communicationEvents = EntityQuery.use(delegator).from("CommunicationEvent").where(condition).orderBy("-entryDate").queryList(); + for (GenericValue communicationEvent : communicationEvents) { + Map infoMap = new HashMap<>(); + String fromPartyName = PartyHelper.getPartyName(delegator, communicationEvent.getString("partyIdFrom"), true) ; + String toPartyName = PartyHelper.getPartyName(delegator, communicationEvent.getString("partyIdTo"), true) ; + infoMap.put("communicationEventId", communicationEvent.getString("communicationEventId")); + infoMap.put("fromPartyName", fromPartyName); + infoMap.put("toPartyName", toPartyName); + infoMap.put("subject", communicationEvent.getString("subject")); + infoMap.put("entryDate", communicationEvent.getString("entryDate")); + infoMap.put("content", communicationEvent.getString("content")); + communications.add(infoMap); + } + return communications; + } } + diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 3eddeb80a..235bb1874 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -690,6 +690,7 @@ public static Map getCustomerProfile(DispatchContext dctx, Map serviceContext = new HashMap<>(); serviceContext.put("customerPartyId", customerPartyId); serviceContext.put("showOld", showOld); @@ -698,9 +699,12 @@ public static Map getCustomerProfile(DispatchContext dctx, Map Date: Thu, 29 Oct 2020 13:26:09 +0530 Subject: [PATCH 15/38] Implemented: Added support to filter out the customer profile detail. Also done some improvement in structuring the personal information. --- headless-commerce/servicedef/services.xml | 17 ++++--- .../api/customer/CustomerServices.java | 48 +++++++++++++------ 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 854a9a7b6..40586ba54 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -99,14 +99,17 @@ under the License. - + - - - - - - + + + + + + + + + diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 235bb1874..bada91813 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -604,9 +604,14 @@ public static Map getCustomerProfile(DispatchContext dctx, Map filters = UtilGenerics.cast(context.get("filters")); Map response = ServiceUtil.returnSuccess(); try { + if (UtilValidate.isEmpty(filters)) { + filters = UtilMisc.toMap("showPersonalInformation", "Y", "showLoyaltyPoints", "Y", "showContactMechs", "N", "showPaymentMethods", "N", "showContactLists", "N", "showCommunications", "N"); + } + response.put("filters", filters); if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); Debug.logError(errorMessage, MODULE); @@ -615,14 +620,21 @@ public static Map getCustomerProfile(DispatchContext dctx, Map personalInformationMap = new HashMap<>(); + personalInformationMap.put("personalTitle", person.getString("personalTitle")); + personalInformationMap.put("firstName", person.getString("firstName")); + personalInformationMap.put("middleName", person.getString("middleName")); + personalInformationMap.put("lastName", person.getString("lastName")); + personalInformationMap.put("suffix", person.getString("suffix")); + personalInformationMap.put("userName", userLogin.getString("userLoginId")); + response.put("personalInformation", personalInformationMap); + } + if ("Y".equals(filters.get("showLoyaltyPoints"))) { + response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); + } + if ("Y".equals(filters.get("showContactMechs"))) { List> contactMechs = new ArrayList<>(); List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); for (Map partyContactMechValueMap : partyContactMechValueMaps) { @@ -689,7 +701,10 @@ public static Map getCustomerProfile(DispatchContext dctx, Map serviceContext = new HashMap<>(); serviceContext.put("customerPartyId", customerPartyId); @@ -699,12 +714,15 @@ public static Map getCustomerProfile(DispatchContext dctx, Map getPartyPaymentMethods(DispatchContext dctx, M List> paymentMethodValueMaps = PaymentWorker.getPartyPaymentMethodValueMaps(delegator, customerPartyId, showOld); for (Map paymentMethodValueMap : paymentMethodValueMaps) { Map infoMap = new HashMap<>(); - GenericValue paymentMethod = (GenericValue) paymentMethodValueMap.get("paymentMethod"); + GenericValue paymentMethod = UtilGenerics.cast(paymentMethodValueMap.get("paymentMethod")); infoMap.put("paymentMethodId", paymentMethod.getString("paymentMethodId")); infoMap.put("description", paymentMethod.getString("description")); infoMap.put("fromDate", paymentMethod.getString("fromDate")); @@ -745,7 +763,7 @@ public static Map getPartyPaymentMethods(DispatchContext dctx, M infoMap.put("paymentMethodType", paymentMethodTypeMap); if (UtilValidate.isNotEmpty(paymentMethodValueMap.get("creditCard"))) { - GenericValue creditCard = (GenericValue) paymentMethodValueMap.get("creditCard"); + GenericValue creditCard = UtilGenerics.cast(paymentMethodValueMap.get("creditCard")); infoMap.put("companyNameOnCard", creditCard.getString("companyNameOnCard")); infoMap.put("titleOnCard", creditCard.getString("titleOnCard")); infoMap.put("firstNameOnCard", creditCard.getString("firstNameOnCard")); @@ -755,12 +773,12 @@ public static Map getPartyPaymentMethods(DispatchContext dctx, M infoMap.put("cardNumber", creditCard.getString("cardNumber")); infoMap.put("expireDate", creditCard.getString("expireDate")); } else if (UtilValidate.isNotEmpty(paymentMethodValueMap.get("giftCard"))) { - GenericValue giftCard = (GenericValue) paymentMethodValueMap.get("giftCard"); + GenericValue giftCard = UtilGenerics.cast(paymentMethodValueMap.get("giftCard")); infoMap.put("cardNumber", giftCard.getString("cardNumber")); infoMap.put("expireDate", giftCard.getString("expireDate")); } else if (UtilValidate.isNotEmpty(paymentMethodValueMap.get("eftAccount"))) { - GenericValue eftAccount = (GenericValue) paymentMethodValueMap.get("eftAccount"); + GenericValue eftAccount = UtilGenerics.cast(paymentMethodValueMap.get("eftAccount")); infoMap.put("bankName", eftAccount.getString("bankName")); infoMap.put("routingNumber", eftAccount.getString("routingNumber")); infoMap.put("accountType", eftAccount.getString("accountType")); From 70d15a5e6950aed2d52d3e791fd7170bac6f9342 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Thu, 29 Oct 2020 13:27:20 +0530 Subject: [PATCH 16/38] Improved: Formatting changes only. --- .../api/customer/CustomerServices.java | 161 +++++++++--------- 1 file changed, 80 insertions(+), 81 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index bada91813..4212f6a02 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -623,105 +623,104 @@ public static Map getCustomerProfile(DispatchContext dctx, Map personalInformationMap = new HashMap<>(); - personalInformationMap.put("personalTitle", person.getString("personalTitle")); - personalInformationMap.put("firstName", person.getString("firstName")); - personalInformationMap.put("middleName", person.getString("middleName")); - personalInformationMap.put("lastName", person.getString("lastName")); - personalInformationMap.put("suffix", person.getString("suffix")); - personalInformationMap.put("userName", userLogin.getString("userLoginId")); - response.put("personalInformation", personalInformationMap); + personalInformationMap.put("personalTitle", person.getString("personalTitle")); + personalInformationMap.put("firstName", person.getString("firstName")); + personalInformationMap.put("middleName", person.getString("middleName")); + personalInformationMap.put("lastName", person.getString("lastName")); + personalInformationMap.put("suffix", person.getString("suffix")); + personalInformationMap.put("userName", userLogin.getString("userLoginId")); + response.put("personalInformation", personalInformationMap); } if ("Y".equals(filters.get("showLoyaltyPoints"))) { - response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); + response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); } if ("Y".equals(filters.get("showContactMechs"))) { - List> contactMechs = new ArrayList<>(); - List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); - for (Map partyContactMechValueMap : partyContactMechValueMaps) { - Map infoMap = new HashMap<>(); - GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); - GenericValue contactMechType = (GenericValue) partyContactMechValueMap.get("contactMechType"); - infoMap.put("contactMechId", contactMech.getString("contactMechId")); - if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("postalAddress"))) { - //postal address - GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); - if (postalAddress != null) { - infoMap.put("toName", postalAddress.getString("toName")); - infoMap.put("address1", postalAddress.getString("address1")); - infoMap.put("address2", postalAddress.getString("address2")); - infoMap.put("city", postalAddress.getString("city")); - infoMap.put("postalCode", postalAddress.getString("postalCode")); - - Map geoMap = new HashMap<>(); - GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); - if (countryGeo != null) { - geoMap.put("geoId", countryGeo.getString("geoId")); - geoMap.put("geoName", countryGeo.getString("geoName")); - geoMap.put("geoCode", countryGeo.getString("abbreviation")); - infoMap.put("country", geoMap); + List> contactMechs = new ArrayList<>(); + List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); + for (Map partyContactMechValueMap : partyContactMechValueMaps) { + Map infoMap = new HashMap<>(); + GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); + GenericValue contactMechType = (GenericValue) partyContactMechValueMap.get("contactMechType"); + infoMap.put("contactMechId", contactMech.getString("contactMechId")); + if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("postalAddress"))) { + //postal address + GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); + if (postalAddress != null) { + infoMap.put("toName", postalAddress.getString("toName")); + infoMap.put("address1", postalAddress.getString("address1")); + infoMap.put("address2", postalAddress.getString("address2")); + infoMap.put("city", postalAddress.getString("city")); + infoMap.put("postalCode", postalAddress.getString("postalCode")); + + Map geoMap = new HashMap<>(); + GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); + if (countryGeo != null) { + geoMap.put("geoId", countryGeo.getString("geoId")); + geoMap.put("geoName", countryGeo.getString("geoName")); + geoMap.put("geoCode", countryGeo.getString("abbreviation")); + infoMap.put("country", geoMap); + } + geoMap = new HashMap<>(); + GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); + if (stateGeo != null) { + geoMap.put("geoId", stateGeo.getString("geoId")); + geoMap.put("geoName", stateGeo.getString("geoName")); + geoMap.put("geoCode", stateGeo.getString("abbreviation")); + infoMap.put("state", geoMap); + } + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + contactMechs.add(infoMap); } - geoMap = new HashMap<>(); - GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); - if (stateGeo != null) { - geoMap.put("geoId", stateGeo.getString("geoId")); - geoMap.put("geoName", stateGeo.getString("geoName")); - geoMap.put("geoCode", stateGeo.getString("abbreviation")); - infoMap.put("state", geoMap); + } else if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("telecomNumber"))) { + //phone number + GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); + if (telecomNumber != null) { + infoMap.put("countryCode", telecomNumber.getString("countryCode")); + infoMap.put("areaCode", telecomNumber.getString("areaCode")); + infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + contactMechs.add(infoMap); } - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - contactMechs.add(infoMap); + } else { + //email, web address, electronic address, etc. + infoMap.put("infoString", contactMech.getString("infoString")); } - } else if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("telecomNumber"))) { - //phone number - GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); - if (telecomNumber != null) { - infoMap.put("countryCode", telecomNumber.getString("countryCode")); - infoMap.put("areaCode", telecomNumber.getString("areaCode")); - infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); + + //contact mech type + Map contactMechTypeMap = new HashMap<>(); + contactMechTypeMap.put("contactMechTypeId", contactMechType.getString("contactMechTypeId")); + contactMechTypeMap.put("description", contactMechType.getString("description")); + infoMap.put("contactMechType", contactMechTypeMap); + + //contact mech purpose + if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("partyContactMechPurposes"))) { infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - contactMechs.add(infoMap); } - } else { - //email, web address, electronic address, etc. - infoMap.put("infoString", contactMech.getString("infoString")); - } - - //contact mech type - Map contactMechTypeMap = new HashMap<>(); - contactMechTypeMap.put("contactMechTypeId", contactMechType.getString("contactMechTypeId")); - contactMechTypeMap.put("description", contactMechType.getString("description")); - infoMap.put("contactMechType", contactMechTypeMap); - - //contact mech purpose - if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("partyContactMechPurposes"))) { - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + contactMechs.add(infoMap); } - contactMechs.add(infoMap); - } - response.put("contactMechs", contactMechs); + response.put("contactMechs", contactMechs); } if ("Y".equals(filters.get("showPaymentMethods"))) { - //payment information - Map serviceContext = new HashMap<>(); - serviceContext.put("customerPartyId", customerPartyId); - serviceContext.put("showOld", showOld); - serviceContext.put("userLogin", userLogin); - Map result = dispatcher.runSync("getPartyPaymentMethods", serviceContext); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - } - response.put("paymentMethods", UtilGenerics.cast(result.get("paymentMethods"))); + //payment information + Map serviceContext = new HashMap<>(); + serviceContext.put("customerPartyId", customerPartyId); + serviceContext.put("showOld", showOld); + serviceContext.put("userLogin", userLogin); + Map result = dispatcher.runSync("getPartyPaymentMethods", serviceContext); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + } + response.put("paymentMethods", UtilGenerics.cast(result.get("paymentMethods"))); } - if ("Y".equals(filters.get("showContactLists"))) { - response.put("contactLists", CustomerHelper.getPartyContactLists(delegator, customerPartyId)); + response.put("contactLists", CustomerHelper.getPartyContactLists(delegator, customerPartyId)); } if ("Y".equals(filters.get("showCommunications"))) { - response.put("communications", CustomerHelper.getPartyCommunications(delegator, customerPartyId, true, false)); + response.put("communications", CustomerHelper.getPartyCommunications(delegator, customerPartyId, true, false)); } } } catch (GenericEntityException | GenericServiceException e) { From 8caed70971f578f40ae6f9b0be7ec28105af9036 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Fri, 30 Oct 2020 10:34:10 +0530 Subject: [PATCH 17/38] Implemented: Added endpoint to update the customer personal information. --- headless-commerce/api/headless-commerce.rest.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index 2c135d055..354d5c9f1 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -9,6 +9,9 @@ + + + From ae87d9b9cc562714ba96be240612c9dc02a9d0c2 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Fri, 30 Oct 2020 14:51:41 +0530 Subject: [PATCH 18/38] Implemented: Added endPoints to create, update and delete postal address of a customer. --- .../api/headless-commerce.rest.xml | 11 +- headless-commerce/servicedef/services.xml | 38 ++++ .../api/customer/CustomerHelper.java | 20 +++ .../api/customer/CustomerServices.java | 167 ++++++++++++++++++ 4 files changed, 235 insertions(+), 1 deletion(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index 354d5c9f1..adfd25d8e 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -9,7 +9,7 @@ - + @@ -18,5 +18,14 @@ + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 40586ba54..13ae5c740 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -211,4 +211,42 @@ under the License. + + To create the postal address with purpose + + + + + + + + + + + + + + To update the postal address and to add a purpose + + + + + + + + + + + + + + + To remove the party contact mech purpose or the the party contact mech + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java index 87efb08a8..fb44cb843 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerHelper.java @@ -3,6 +3,7 @@ import jdk.nashorn.internal.runtime.logging.DebugLogger; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.base.util.UtilProperties; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.entity.Delegator; import org.apache.ofbiz.entity.GenericEntityException; @@ -101,5 +102,24 @@ public static List> getPartyCommunications(Delegator delegat } return communications; } + public static String getGeoId(Delegator delegator, String geoCode, String geoTypeId) throws GenericEntityException { + if (UtilValidate.isNotEmpty(geoCode)) { + GenericValue geo = EntityQuery.use(delegator).from("Geo").where("abbreviation", geoCode, "geoTypeId", geoTypeId).queryOne(); + if (geo != null) { + return geo.getString("geoId"); + } + } + return null; + } + public static boolean isValidGeoAssoc(Delegator delegator, String countryGeoId, String stateGeoId) throws GenericEntityException { + if (UtilValidate.isNotEmpty(countryGeoId) && UtilValidate.isNotEmpty(stateGeoId)) { + GenericValue regionGeo = EntityQuery.use(delegator).from("GeoAssocAndGeoToWithState"). + where("geoIdFrom", countryGeoId, "geoAssocTypeId", "REGIONS", "geoId", stateGeoId).queryFirst(); + if (regionGeo != null) { + return true; + } + } + return false; + } } diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 4212f6a02..ecec10a60 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -794,5 +794,172 @@ public static Map getPartyPaymentMethods(DispatchContext dctx, M result.put("paymentMethods", paymentMethods); return result; } + public static Map createCustomerPostalAddress(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String stateCode = (String) context.get("stateCode"); + String countryCode = (String) context.get("countryCode"); + String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + String countryGeoId = CustomerHelper.getGeoId(delegator, stateCode, "COUNTRY"); + String stateProvinceGeoId = null; + if (UtilValidate.isNotEmpty(stateCode)) { + stateProvinceGeoId = CustomerHelper.getGeoId(delegator, stateCode, "STATE"); + if (!CustomerHelper.isValidGeoAssoc(delegator, countryGeoId, stateProvinceGeoId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCCountrysStateIsNotValid", + UtilMisc.toMap("stateCode", stateCode, "countryCode", countryCode), locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + } + + Map serviceCtx = dctx.getModelService("createPartyPostalAddress").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); + serviceCtx.put("countryGeoId", countryGeoId); + serviceCtx.put("userLogin", userLogin); + Map result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + String contactMechId = (String) result.get("contactMechId"); + + //assign purpose + if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { + serviceCtx.clear(); + result.clear(); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } + public static Map updateCustomerPostalAddress(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + String address1 = (String) context.get("address1"); + String stateCode = (String) context.get("stateCode"); + String countryCode = (String) context.get("countryCode"); + Map serviceCtx = new HashMap<>(); + Map result = new HashMap<>(); + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + if (UtilValidate.isNotEmpty(address1)) { + String countryGeoId = CustomerHelper.getGeoId(delegator, stateCode, "COUNTRY"); + String stateProvinceGeoId = null; + if (UtilValidate.isNotEmpty(stateCode)) { + stateProvinceGeoId = CustomerHelper.getGeoId(delegator, stateCode, "STATE"); + if (!CustomerHelper.isValidGeoAssoc(delegator, countryGeoId, stateProvinceGeoId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCCountrysStateIsNotValid", + UtilMisc.toMap("stateCode", stateCode, "countryCode", countryCode), locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + } + serviceCtx = dctx.getModelService("updatePartyPostalAddress").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); + serviceCtx.put("countryGeoId", countryGeoId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("updatePartyPostalAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { + GenericValue partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, + "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); + if (partyContactMechPurpose == null) { + serviceCtx.clear(); + result.clear(); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } + public static Map removeCustomerPostalAddress(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + //expiring the contact mech purpose only + if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { + GenericValue partyContactMechPurpse = EntityQuery.use(delegator).from("PartyContactMechPurpse").where("partyId", customerPartyId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); + if (partyContactMechPurpse != null) { + Map serviceCtx = dctx.getModelService("deletePartyContactMech").makeValid(partyContactMechPurpse, ModelService.IN_PARAM); + serviceCtx.put("userLogin", userLogin); + Map result = dispatcher.runSync("expirePartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } else { + //expiring party contact mech only + Map serviceCtx = dctx.getModelService("deletePartyContactMech").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + Map result = dispatcher.runSync("deletePartyContactMech", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } } \ No newline at end of file From 491e3e723cf0aded45f61ddcc6a20620f9dcd724 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 2 Nov 2020 16:47:32 +0530 Subject: [PATCH 19/38] Implemented: Added endPoints to create/update telecom number. Also modified delete customer postal address endpoints to make it more generic. --- .../api/headless-commerce.rest.xml | 11 ++- headless-commerce/servicedef/services.xml | 30 +++++- .../api/customer/CustomerServices.java | 94 +++++++++++++------ 3 files changed, 101 insertions(+), 34 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index adfd25d8e..3b4c5c8c0 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -25,7 +25,16 @@ - + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 13ae5c740..40ee9cb12 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -242,11 +242,35 @@ under the License. - - To remove the party contact mech purpose or the the party contact mech + + To remove the the party contact mech + + + To create the telecom number with purpose + + + + + + + + + + + To update the telecom number and to add a purpose + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index ecec10a60..601d22d6c 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -832,22 +832,6 @@ public static Map createCustomerPostalAddress(DispatchContext dc Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } - String contactMechId = (String) result.get("contactMechId"); - - //assign purpose - if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { - serviceCtx.clear(); - result.clear(); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("contactMechId", contactMechId); - serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); @@ -861,6 +845,7 @@ public static Map updateCustomerPostalAddress(DispatchContext dc GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + String contactMechId = (String) context.get("contactMechId"); String address1 = (String) context.get("address1"); String stateCode = (String) context.get("stateCode"); String countryCode = (String) context.get("countryCode"); @@ -898,11 +883,12 @@ public static Map updateCustomerPostalAddress(DispatchContext dc } if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { GenericValue partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, - "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); + "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); if (partyContactMechPurpose == null) { serviceCtx.clear(); result.clear(); serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("contactMechId", contactMechId); serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); serviceCtx.put("userLogin", userLogin); result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); @@ -918,13 +904,46 @@ public static Map updateCustomerPostalAddress(DispatchContext dc } return ServiceUtil.returnSuccess(); } - public static Map removeCustomerPostalAddress(DispatchContext dctx, Map context) { + public static Map removeCustomerContactMech(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + //expiring party contact mech only + Map serviceCtx = dctx.getModelService("deletePartyContactMech").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + Map result = dispatcher.runSync("deletePartyContactMech", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } + public static Map createUpdateCustomerTelecomNumber(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + String contactMechId = (String) context.get("contactMechId"); + String contactNumber = (String) context.get("contactNumber"); + Map serviceCtx = new HashMap<>(); + Map result = new HashMap<>(); + try { if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { @@ -932,34 +951,49 @@ public static Map removeCustomerPostalAddress(DispatchContext dc Debug.logError(errorMessage, MODULE); return ServiceUtil.returnError(errorMessage); } - //expiring the contact mech purpose only - if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { - GenericValue partyContactMechPurpse = EntityQuery.use(delegator).from("PartyContactMechPurpse").where("partyId", customerPartyId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); - if (partyContactMechPurpse != null) { - Map serviceCtx = dctx.getModelService("deletePartyContactMech").makeValid(partyContactMechPurpse, ModelService.IN_PARAM); - serviceCtx.put("userLogin", userLogin); - Map result = dispatcher.runSync("expirePartyContactMechPurpose", serviceCtx); + + if (UtilValidate.isNotEmpty(contactMechId)) { + //upate telecom number and purpose + if (UtilValidate.isNotEmpty(contactNumber)) { + serviceCtx = dctx.getModelService("updatePartyTelecomNumber").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + result = dispatcher.runSync("updatePartyTelecomNumber", serviceCtx); if (!ServiceUtil.isSuccess(result)) { Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } } + if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { + GenericValue partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, + "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); + if (partyContactMechPurpose == null) { + serviceCtx.clear(); + result.clear(); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } } else { - //expiring party contact mech only - Map serviceCtx = dctx.getModelService("deletePartyContactMech").makeValid(context, ModelService.IN_PARAM); + //create telecom number with purpose + serviceCtx = dctx.getModelService("createPartyTelecomNumber").makeValid(context, ModelService.IN_PARAM); serviceCtx.put("partyId", customerPartyId); - Map result = dispatcher.runSync("deletePartyContactMech", serviceCtx); + result = dispatcher.runSync("createPartyTelecomNumber", serviceCtx); if (!ServiceUtil.isSuccess(result)) { Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } } - } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); } return ServiceUtil.returnSuccess(); } - } \ No newline at end of file From 5eaba8c3b68a72973afb04e3b5ac3cf8b6fb6727 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 2 Nov 2020 17:12:18 +0530 Subject: [PATCH 20/38] Implemented: Modified code to combine create/update postal address logic in a single service. --- headless-commerce/servicedef/services.xml | 4 +- .../api/customer/CustomerServices.java | 112 +++++++----------- 2 files changed, 44 insertions(+), 72 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 40ee9cb12..e9e4fcc91 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -212,7 +212,7 @@ under the License. + location="org.apache.ofbiz.hc.api.customer.CustomerServices" invoke="createUpdateCustomerPostalAddress"> To create the postal address with purpose @@ -227,7 +227,7 @@ under the License. + location="org.apache.ofbiz.hc.api.customer.CustomerServices" invoke="createUpdateCustomerPostalAddress"> To update the postal address and to add a purpose diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 601d22d6c..19695fd38 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -794,7 +794,7 @@ public static Map getPartyPaymentMethods(DispatchContext dctx, M result.put("paymentMethods", paymentMethods); return result; } - public static Map createCustomerPostalAddress(DispatchContext dctx, Map context) { + public static Map createUpdateCustomerPostalAddress(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Locale locale = (Locale) context.get("locale"); @@ -803,6 +803,10 @@ public static Map createCustomerPostalAddress(DispatchContext dc String stateCode = (String) context.get("stateCode"); String countryCode = (String) context.get("countryCode"); String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + String contactMechId = (String) context.get("contactMechId"); + String address1 = (String) context.get("address1"); + Map serviceCtx = new HashMap<>(); + Map result = new HashMap<>(); try { if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { @@ -810,7 +814,8 @@ public static Map createCustomerPostalAddress(DispatchContext dc Debug.logError(errorMessage, MODULE); return ServiceUtil.returnError(errorMessage); } - String countryGeoId = CustomerHelper.getGeoId(delegator, stateCode, "COUNTRY"); + + String countryGeoId = CustomerHelper.getGeoId(delegator, countryCode, "COUNTRY"); String stateProvinceGeoId = null; if (UtilValidate.isNotEmpty(stateCode)) { stateProvinceGeoId = CustomerHelper.getGeoId(delegator, stateCode, "STATE"); @@ -821,82 +826,49 @@ public static Map createCustomerPostalAddress(DispatchContext dc return ServiceUtil.returnError(errorMessage); } } - - Map serviceCtx = dctx.getModelService("createPartyPostalAddress").makeValid(context, ModelService.IN_PARAM); + if (UtilValidate.isNotEmpty(contactMechId)) { + //update postal address and purpose + if (UtilValidate.isNotEmpty(address1)) { + serviceCtx = dctx.getModelService("updatePartyPostalAddress").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); + serviceCtx.put("countryGeoId", countryGeoId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("updatePartyPostalAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { + GenericValue partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, + "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); + if (partyContactMechPurpose == null) { + serviceCtx.clear(); + result.clear(); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } + } else { + //create postal address with purpose + serviceCtx = dctx.getModelService("createPartyPostalAddress").makeValid(context, ModelService.IN_PARAM); serviceCtx.put("partyId", customerPartyId); serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); serviceCtx.put("countryGeoId", countryGeoId); serviceCtx.put("userLogin", userLogin); - Map result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); + result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); if (!ServiceUtil.isSuccess(result)) { Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } - } catch (GenericEntityException | GenericServiceException e) { - Debug.logError(e, MODULE); - return ServiceUtil.returnError(e.getMessage()); - } - return ServiceUtil.returnSuccess(); - } - public static Map updateCustomerPostalAddress(DispatchContext dctx, Map context) { - Delegator delegator = dctx.getDelegator(); - LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); - GenericValue userLogin = (GenericValue) context.get("userLogin"); - String customerPartyId = (String) context.get("customerPartyId"); - String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); - String contactMechId = (String) context.get("contactMechId"); - String address1 = (String) context.get("address1"); - String stateCode = (String) context.get("stateCode"); - String countryCode = (String) context.get("countryCode"); - Map serviceCtx = new HashMap<>(); - Map result = new HashMap<>(); - - try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } - if (UtilValidate.isNotEmpty(address1)) { - String countryGeoId = CustomerHelper.getGeoId(delegator, stateCode, "COUNTRY"); - String stateProvinceGeoId = null; - if (UtilValidate.isNotEmpty(stateCode)) { - stateProvinceGeoId = CustomerHelper.getGeoId(delegator, stateCode, "STATE"); - if (!CustomerHelper.isValidGeoAssoc(delegator, countryGeoId, stateProvinceGeoId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCCountrysStateIsNotValid", - UtilMisc.toMap("stateCode", stateCode, "countryCode", countryCode), locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } - } - serviceCtx = dctx.getModelService("updatePartyPostalAddress").makeValid(context, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); - serviceCtx.put("countryGeoId", countryGeoId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("updatePartyPostalAddress", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } - if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { - GenericValue partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, - "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); - if (partyContactMechPurpose == null) { - serviceCtx.clear(); - result.clear(); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("contactMechId", contactMechId); - serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } } } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); From a5b3ec03f0d6fba47ea04112f62b036f181d32d8 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 2 Nov 2020 17:13:35 +0530 Subject: [PATCH 21/38] Improved: Formatting changes only. --- .../api/customer/CustomerServices.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 19695fd38..d05de8ae0 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -859,16 +859,16 @@ public static Map createUpdateCustomerPostalAddress(DispatchCont } } else { //create postal address with purpose - serviceCtx = dctx.getModelService("createPartyPostalAddress").makeValid(context, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); - serviceCtx.put("countryGeoId", countryGeoId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } + serviceCtx = dctx.getModelService("createPartyPostalAddress").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); + serviceCtx.put("countryGeoId", countryGeoId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } } } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); From 1c9debe5bd110cf3ef56d789ab445cb7662afb27 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 2 Nov 2020 17:28:58 +0530 Subject: [PATCH 22/38] Implemented: Added endpoints to remove a contact purpose. --- .../api/headless-commerce.rest.xml | 8 ++--- headless-commerce/servicedef/services.xml | 9 ++++- .../api/customer/CustomerServices.java | 35 ++++++++++++++----- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index 3b4c5c8c0..ab0d12a8f 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -24,17 +24,17 @@ - + + + + - - - \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index e9e4fcc91..dfeb2a5c5 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -243,11 +243,18 @@ under the License. + location="org.apache.ofbiz.hc.api.customer.CustomerServices" invoke="removeCustomerContactMechAndPurpose"> To remove the the party contact mech + + To remove the the party contact mech purpose + + + + To create the telecom number with purpose diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index d05de8ae0..93cdac807 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -876,12 +876,14 @@ public static Map createUpdateCustomerPostalAddress(DispatchCont } return ServiceUtil.returnSuccess(); } - public static Map removeCustomerContactMech(DispatchContext dctx, Map context) { + public static Map removeCustomerContactMechAndPurpose(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); + String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + String contactMechId = (String) context.get("contactMechId"); try { if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { @@ -889,15 +891,30 @@ public static Map removeCustomerContactMech(DispatchContext dctx Debug.logError(errorMessage, MODULE); return ServiceUtil.returnError(errorMessage); } - //expiring party contact mech only - Map serviceCtx = dctx.getModelService("deletePartyContactMech").makeValid(context, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - Map result = dispatcher.runSync("deletePartyContactMech", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { + //expiring party contact mech purpose only + GenericValue partyContactMech = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, "contactMechId", contactMechId, + "contactMechPurposeTypeId" contactMechPurposeTypeId).filterByDate().queryFirst(); + if (partyContactMech != null) { + Map serviceCtx = dctx.getModelService("expirePartyContactMechPurpose").makeValid(partyContactMech, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + Map result = dispatcher.runSync("expirePartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } else { + //expiring party contact mech only + Map serviceCtx = dctx.getModelService("deletePartyContactMech").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + Map result = dispatcher.runSync("deletePartyContactMech", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } } - } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); From 92e583b6000422694f226fd358af14ac3d43e3cd Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 2 Nov 2020 17:32:23 +0530 Subject: [PATCH 23/38] Fixed: corrected syntax. --- .../java/org.apache.ofbiz.hc/api/customer/CustomerServices.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 93cdac807..90faf17f8 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -894,7 +894,7 @@ public static Map removeCustomerContactMechAndPurpose(DispatchCo if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { //expiring party contact mech purpose only GenericValue partyContactMech = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, "contactMechId", contactMechId, - "contactMechPurposeTypeId" contactMechPurposeTypeId).filterByDate().queryFirst(); + "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); if (partyContactMech != null) { Map serviceCtx = dctx.getModelService("expirePartyContactMechPurpose").makeValid(partyContactMech, ModelService.IN_PARAM); serviceCtx.put("partyId", customerPartyId); From 54a8ce6b1b85e29a2e4ec4d996d0eee54f9e27b0 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Tue, 3 Nov 2020 11:05:11 +0530 Subject: [PATCH 24/38] Implemented: Added endPoints to create/update email address. --- .../api/headless-commerce.rest.xml | 8 ++- headless-commerce/servicedef/services.xml | 17 +++++ .../api/customer/CustomerServices.java | 64 +++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index ab0d12a8f..b63fb3597 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -27,7 +27,7 @@ - + @@ -36,5 +36,11 @@ + + + + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index dfeb2a5c5..92096a65e 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -280,4 +280,21 @@ under the License. + + To create the telecom number + + + + + + + To update the email address + + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 90faf17f8..e731ed3d0 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -985,4 +985,68 @@ public static Map createUpdateCustomerTelecomNumber(DispatchCont } return ServiceUtil.returnSuccess(); } + public static Map createUpdateCustomerEmailAddress(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + String contactMechId = (String) context.get("contactMechId"); + String emailAddress = (String) context.get("emailAddress"); + Map serviceCtx = new HashMap<>(); + Map result = new HashMap<>(); + + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + + if (UtilValidate.isNotEmpty(contactMechId)) { + //update email address + if (UtilValidate.isNotEmpty(emailAddress)) { + serviceCtx = dctx.getModelService("updatePartyEmailAddress").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + result = dispatcher.runSync("updatePartyEmailAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { + GenericValue partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, + "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).filterByDate().queryFirst(); + if (partyContactMechPurpose == null) { + serviceCtx.clear(); + result.clear(); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("contactMechId", contactMechId); + serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createPartyContactMechPurpose", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } + } else { + //create email address + serviceCtx = dctx.getModelService("createPartyEmailAddress").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + result = dispatcher.runSync("createPartyEmailAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } } \ No newline at end of file From 33387e92bd70f44c82e4e53d8fb572d3c74e52ab Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Tue, 3 Nov 2020 11:48:30 +0530 Subject: [PATCH 25/38] Implemented: Added endpoints to create/update customer contact information other than postal address, phone, email. --- .../api/headless-commerce.rest.xml | 18 ++++--- headless-commerce/servicedef/services.xml | 17 +++++++ .../api/customer/CustomerServices.java | 47 +++++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index b63fb3597..b1f0ae9a6 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -24,12 +24,6 @@ - - - - - - @@ -42,5 +36,17 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 92096a65e..3aa63dce6 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -297,4 +297,21 @@ under the License. + + To create contact mech + + + + + + + To update contact mech + + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index e731ed3d0..e0031c24e 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1049,4 +1049,51 @@ public static Map createUpdateCustomerEmailAddress(DispatchConte } return ServiceUtil.returnSuccess(); } + public static Map createUpdateCustomerContactMech(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String contactMechId = (String) context.get("contactMechId"); + String infoString = (String) context.get("infoString"); + Map serviceCtx = new HashMap<>(); + Map result = new HashMap<>(); + + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + + if (UtilValidate.isNotEmpty(contactMechId)) { + //update contact mech + if (UtilValidate.isNotEmpty(infoString)) { + serviceCtx = dctx.getModelService("updatePartyContactMech").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + result = dispatcher.runSync("updatePartyContactMech", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } else { + //create email address + serviceCtx = dctx.getModelService("createPartyContactMech").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + result = dispatcher.runSync("createPartyContactMech", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } + } \ No newline at end of file From e99ae43d56612bae97d7ea140c19a4d1cb193584 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Wed, 4 Nov 2020 10:48:04 +0530 Subject: [PATCH 26/38] Implemented: Added endpoints to create/update credit card, gift card and eft account. --- .../api/headless-commerce.rest.xml | 18 +++ headless-commerce/servicedef/services.xml | 97 ++++++++++++++++ .../api/customer/CustomerServices.java | 108 ++++++++++++++++++ 3 files changed, 223 insertions(+) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index b1f0ae9a6..8c5d0a657 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -48,5 +48,23 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 3aa63dce6..1006bb621 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -314,4 +314,101 @@ under the License. + + creates credit card + + + + + + + + + + + + + + + + + + + + + + + + updates credit card + + + + + + + + + + + + + + + + + + + + + + + + + creates gift card + + + + + + + + updates gift card + + + + + + + + + creates EFT account + + + + + + + + + + + + updates EFT account + + + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index e0031c24e..cd42c5b44 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1095,5 +1095,113 @@ public static Map createUpdateCustomerContactMech(DispatchContex } return ServiceUtil.returnSuccess(); } + public static Map createUpdateCustomerPaymentMethod(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + Map creditCard = UtilGenerics.cast(context.get("creditCard")); + Map giftCard = UtilGenerics.cast(context.get("giftCard")); + Map eftAccount = UtilGenerics.cast(context.get("eftAccount")); + Map address = UtilGenerics.cast(context.get("address")); + Map serviceCtx = new HashMap<>(); + Map result = new HashMap<>(); + String paymentMethodId = null; + + + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + Debug.logError(errorMessage, MODULE); + return ServiceUtil.returnError(errorMessage); + } + + if (UtilValidate.isNotEmpty(creditCard)) { + paymentMethodId = (String) creditCard.get("paymentMethodId"); + if (UtilValidate.isNotEmpty(paymentMethodId)) { + serviceCtx = dctx.getModelService("updateCreditCard").makeValid(creditCard, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("updateCreditCard", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } else { + serviceCtx = dctx.getModelService("createCreditCard").makeValid(creditCard, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createCreditCard", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + paymentMethodId = (String) result.get("paymentMethodId"); + } + if (UtilValidate.isNotEmpty(address) && UtilValidate.isEmpty(address.get("contactMechId"))) { + serviceCtx.clear(); + result.clear(); + serviceCtx = dctx.getModelService("createPartyPostalAddress").makeValid(address, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("paymentMethodId", paymentMethodId); + serviceCtx.put("contactMechPurposeTypeId", "BILLING_LOCATION"); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } else if (UtilValidate.isNotEmpty(giftCard)) { + paymentMethodId = (String) giftCard.get("paymentMethodId"); + if (UtilValidate.isNotEmpty(paymentMethodId)) { + serviceCtx = dctx.getModelService("updateGiftCard").makeValid(giftCard, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("updateGiftCard", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } else { + serviceCtx = dctx.getModelService("createGiftCard").makeValid(giftCard, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createGiftCard", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } else if (UtilValidate.isNotEmpty(eftAccount)) { + paymentMethodId = (String) eftAccount.get("paymentMethodId"); + if (UtilValidate.isNotEmpty(paymentMethodId)) { + serviceCtx = dctx.getModelService("updateEftAccount").makeValid(eftAccount, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("updateEftAccount", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } else { + serviceCtx = dctx.getModelService("createEftAccount").makeValid(eftAccount, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + result = dispatcher.runSync("createEftAccount", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } + } + } catch (GenericEntityException | GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } + } \ No newline at end of file From 99bd192761ef70f38e17931b64f1d2eadcc90f86 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Wed, 4 Nov 2020 11:26:12 +0530 Subject: [PATCH 27/38] Improved: Added permisson service to check if the requesting user is a valid customer. --- headless-commerce/servicedef/services.xml | 26 ++++++ .../api/customer/CustomerServices.java | 80 ++++++------------- 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 1006bb621..aee842a2d 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -24,6 +24,13 @@ under the License. 1.0 + + + + Contains true if the if the requesting user is valid. + + @@ -36,6 +43,7 @@ under the License. + @@ -99,6 +107,7 @@ under the License. + @@ -207,6 +216,7 @@ under the License. + @@ -214,6 +224,7 @@ under the License. To create the postal address with purpose + @@ -229,6 +240,7 @@ under the License. To update the postal address and to add a purpose + @@ -245,12 +257,14 @@ under the License. To remove the the party contact mech + To remove the the party contact mech purpose + @@ -258,6 +272,7 @@ under the License. To create the telecom number with purpose + @@ -270,6 +285,7 @@ under the License. To update the telecom number and to add a purpose + @@ -283,6 +299,7 @@ under the License. To create the telecom number + @@ -291,6 +308,7 @@ under the License. To update the email address + @@ -300,6 +318,7 @@ under the License. To create contact mech + @@ -308,6 +327,7 @@ under the License. To update contact mech + @@ -317,6 +337,7 @@ under the License. creates credit card + @@ -342,6 +363,7 @@ under the License. updates credit card + @@ -368,6 +390,7 @@ under the License. creates gift card + @@ -377,6 +400,7 @@ under the License. updates gift card + @@ -387,6 +411,7 @@ under the License. creates EFT account + @@ -400,6 +425,7 @@ under the License. updates EFT account + diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index cd42c5b44..a5008d10a 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -169,11 +169,6 @@ public static Map changePassword(DispatchContext dctx, Map serviceContext = dctx.getModelService("updatePassword").makeValid(context, ModelService.IN_PARAM); serviceContext.put("userLoginId", userLogin.getString("userLoginId")); serviceContext.put("userLogin", userLogin); @@ -182,7 +177,7 @@ public static Map changePassword(DispatchContext dctx, Map getCustomerProfile(DispatchContext dctx, Map getPartyPaymentMethods(DispatchContext dctx, M List> paymentMethods = new ArrayList<>(); try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } boolean showOld = "true".equals(showOldStr); List> paymentMethodValueMaps = PaymentWorker.getPartyPaymentMethodValueMaps(delegator, customerPartyId, showOld); for (Map paymentMethodValueMap : paymentMethodValueMaps) { @@ -809,12 +794,6 @@ public static Map createUpdateCustomerPostalAddress(DispatchCont Map result = new HashMap<>(); try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } - String countryGeoId = CustomerHelper.getGeoId(delegator, countryCode, "COUNTRY"); String stateProvinceGeoId = null; if (UtilValidate.isNotEmpty(stateCode)) { @@ -886,11 +865,6 @@ public static Map removeCustomerContactMechAndPurpose(DispatchCo String contactMechId = (String) context.get("contactMechId"); try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } if (UtilValidate.isNotEmpty(contactMechPurposeTypeId)) { //expiring party contact mech purpose only GenericValue partyContactMech = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", customerPartyId, "contactMechId", contactMechId, @@ -935,12 +909,6 @@ public static Map createUpdateCustomerTelecomNumber(DispatchCont try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } - if (UtilValidate.isNotEmpty(contactMechId)) { //upate telecom number and purpose if (UtilValidate.isNotEmpty(contactNumber)) { @@ -999,12 +967,6 @@ public static Map createUpdateCustomerEmailAddress(DispatchConte try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } - if (UtilValidate.isNotEmpty(contactMechId)) { //update email address if (UtilValidate.isNotEmpty(emailAddress)) { @@ -1062,12 +1024,6 @@ public static Map createUpdateCustomerContactMech(DispatchContex try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } - if (UtilValidate.isNotEmpty(contactMechId)) { //update contact mech if (UtilValidate.isNotEmpty(infoString)) { @@ -1089,7 +1045,7 @@ public static Map createUpdateCustomerContactMech(DispatchContex return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } } - } catch (GenericEntityException | GenericServiceException e) { + } catch (GenericServiceException e) { Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); } @@ -1111,12 +1067,6 @@ public static Map createUpdateCustomerPaymentMethod(DispatchCont try { - if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); - Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(errorMessage); - } - if (UtilValidate.isNotEmpty(creditCard)) { paymentMethodId = (String) creditCard.get("paymentMethodId"); if (UtilValidate.isNotEmpty(paymentMethodId)) { @@ -1196,12 +1146,34 @@ public static Map createUpdateCustomerPaymentMethod(DispatchCont } } } - } catch (GenericEntityException | GenericServiceException e) { + } catch (GenericServiceException e) { Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); } return ServiceUtil.returnSuccess(); } + public static Map validateCustomer(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String errorMessage = ""; + Map result = ServiceUtil.returnSuccess(); + try { + if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { + errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + result = ServiceUtil.returnError(errorMessage); + result.put("hasPermission", false); + Debug.logError(errorMessage, MODULE); + return result; + } + } catch (GenericEntityException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } - + result.put("hasPermission", true); + return result; + } } \ No newline at end of file From e9e63d773cbc34f9b0567c65b87f88d481126848 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Fri, 6 Nov 2020 10:40:07 +0530 Subject: [PATCH 28/38] Improved: Removed unused variables and did some minor improvement. --- .../api/customer/CustomerServices.java | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index a5008d10a..089081970 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -162,11 +162,8 @@ public static Map sendResetPasswordEmail(DispatchContext ctx, Ma return ServiceUtil.returnSuccess(UtilProperties.getMessage(SECRESOURCE, "loginevents.new_password_sent_check_email", locale)); } public static Map changePassword(DispatchContext dctx, Map context) { - Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); - String customerPartyId = (String) context.get("customerPartyId"); try { Map serviceContext = dctx.getModelService("updatePassword").makeValid(context, ModelService.IN_PARAM); @@ -595,7 +592,6 @@ public static Map validateCreateCustomer(DispatchContext dctx, M public static Map getCustomerProfile(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String showOldStr = (String) context.get("showOld"); @@ -721,8 +717,6 @@ public static Map getCustomerProfile(DispatchContext dctx, Map getPartyPaymentMethods(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); - Locale locale = (Locale) context.get("locale"); - GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String showOldStr = (String) context.get("showOld"); Map result = ServiceUtil.returnSuccess(); @@ -858,7 +852,6 @@ public static Map createUpdateCustomerPostalAddress(DispatchCont public static Map removeCustomerContactMechAndPurpose(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); @@ -898,7 +891,6 @@ public static Map removeCustomerContactMechAndPurpose(DispatchCo public static Map createUpdateCustomerTelecomNumber(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); @@ -956,7 +948,6 @@ public static Map createUpdateCustomerTelecomNumber(DispatchCont public static Map createUpdateCustomerEmailAddress(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); @@ -1012,24 +1003,18 @@ public static Map createUpdateCustomerEmailAddress(DispatchConte return ServiceUtil.returnSuccess(); } public static Map createUpdateCustomerContactMech(DispatchContext dctx, Map context) { - Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); - GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); String contactMechId = (String) context.get("contactMechId"); String infoString = (String) context.get("infoString"); - Map serviceCtx = new HashMap<>(); - Map result = new HashMap<>(); - try { if (UtilValidate.isNotEmpty(contactMechId)) { //update contact mech if (UtilValidate.isNotEmpty(infoString)) { - serviceCtx = dctx.getModelService("updatePartyContactMech").makeValid(context, ModelService.IN_PARAM); + Map serviceCtx = dctx.getModelService("updatePartyContactMech").makeValid(context, ModelService.IN_PARAM); serviceCtx.put("partyId", customerPartyId); - result = dispatcher.runSync("updatePartyContactMech", serviceCtx); + Map result = dispatcher.runSync("updatePartyContactMech", serviceCtx); if (!ServiceUtil.isSuccess(result)) { Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); @@ -1037,9 +1022,9 @@ public static Map createUpdateCustomerContactMech(DispatchContex } } else { //create email address - serviceCtx = dctx.getModelService("createPartyContactMech").makeValid(context, ModelService.IN_PARAM); + Map serviceCtx = dctx.getModelService("createPartyContactMech").makeValid(context, ModelService.IN_PARAM); serviceCtx.put("partyId", customerPartyId); - result = dispatcher.runSync("createPartyContactMech", serviceCtx); + Map result = dispatcher.runSync("createPartyContactMech", serviceCtx); if (!ServiceUtil.isSuccess(result)) { Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); @@ -1052,9 +1037,7 @@ public static Map createUpdateCustomerContactMech(DispatchContex return ServiceUtil.returnSuccess(); } public static Map createUpdateCustomerPaymentMethod(DispatchContext dctx, Map context) { - Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); - Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); Map creditCard = UtilGenerics.cast(context.get("creditCard")); @@ -1154,15 +1137,13 @@ public static Map createUpdateCustomerPaymentMethod(DispatchCont } public static Map validateCustomer(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); - LocalDispatcher dispatcher = dctx.getDispatcher(); Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); - String errorMessage = ""; Map result = ServiceUtil.returnSuccess(); try { if (!CommonUtil.isValidCutomer(delegator, userLogin, customerPartyId)) { - errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); + String errorMessage = UtilProperties.getMessage("HeadlessCommerceUiLabels", "HCAccessDeniedInvalidUser", locale); result = ServiceUtil.returnError(errorMessage); result.put("hasPermission", false); Debug.logError(errorMessage, MODULE); From 11c18bf11e0491ca9008fca20fa8d326bb58179c Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Fri, 6 Nov 2020 11:08:59 +0530 Subject: [PATCH 29/38] Improved: Done some code optimization. --- .../api/customer/CustomerServices.java | 90 ++++++------------- 1 file changed, 28 insertions(+), 62 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 089081970..4c2da5af2 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1044,34 +1044,20 @@ public static Map createUpdateCustomerPaymentMethod(DispatchCont Map giftCard = UtilGenerics.cast(context.get("giftCard")); Map eftAccount = UtilGenerics.cast(context.get("eftAccount")); Map address = UtilGenerics.cast(context.get("address")); - Map serviceCtx = new HashMap<>(); - Map result = new HashMap<>(); - String paymentMethodId = null; - try { if (UtilValidate.isNotEmpty(creditCard)) { - paymentMethodId = (String) creditCard.get("paymentMethodId"); - if (UtilValidate.isNotEmpty(paymentMethodId)) { - serviceCtx = dctx.getModelService("updateCreditCard").makeValid(creditCard, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("updateCreditCard", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } else { - serviceCtx = dctx.getModelService("createCreditCard").makeValid(creditCard, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("createCreditCard", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - paymentMethodId = (String) result.get("paymentMethodId"); + String paymentMethodId = (String) creditCard.get("paymentMethodId"); + String serviceName = UtilValidate.isNotEmpty(paymentMethodId) ? "updateCreditCard" : "createCreditCard"; + Map serviceCtx = dctx.getModelService(serviceName).makeValid(creditCard, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + Map result = dispatcher.runSync(serviceName, serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } + paymentMethodId = (String) result.get("paymentMethodId"); if (UtilValidate.isNotEmpty(address) && UtilValidate.isEmpty(address.get("contactMechId"))) { serviceCtx.clear(); result.clear(); @@ -1087,46 +1073,26 @@ public static Map createUpdateCustomerPaymentMethod(DispatchCont } } } else if (UtilValidate.isNotEmpty(giftCard)) { - paymentMethodId = (String) giftCard.get("paymentMethodId"); - if (UtilValidate.isNotEmpty(paymentMethodId)) { - serviceCtx = dctx.getModelService("updateGiftCard").makeValid(giftCard, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("updateGiftCard", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } else { - serviceCtx = dctx.getModelService("createGiftCard").makeValid(giftCard, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("createGiftCard", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } + String paymentMethodId = (String) giftCard.get("paymentMethodId"); + String serviceName = UtilValidate.isNotEmpty(paymentMethodId) ? "updateGiftCard" : "createGiftCard"; + Map serviceCtx = dctx.getModelService(serviceName).makeValid(giftCard, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + Map result = dispatcher.runSync(serviceName, serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } } else if (UtilValidate.isNotEmpty(eftAccount)) { - paymentMethodId = (String) eftAccount.get("paymentMethodId"); - if (UtilValidate.isNotEmpty(paymentMethodId)) { - serviceCtx = dctx.getModelService("updateEftAccount").makeValid(eftAccount, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("updateEftAccount", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } else { - serviceCtx = dctx.getModelService("createEftAccount").makeValid(eftAccount, ModelService.IN_PARAM); - serviceCtx.put("partyId", customerPartyId); - serviceCtx.put("userLogin", userLogin); - result = dispatcher.runSync("createEftAccount", serviceCtx); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } + String paymentMethodId = (String) eftAccount.get("paymentMethodId"); + String serviceName = UtilValidate.isNotEmpty(paymentMethodId) ? "updateEftAccount" : "createEftAccount"; + Map serviceCtx = dctx.getModelService(serviceName).makeValid(eftAccount, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + Map result = dispatcher.runSync(serviceName, serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } } } catch (GenericServiceException e) { From f0643d8dff1525cc5ff9b27f88e38d27d6ef029b Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Fri, 6 Nov 2020 11:44:24 +0530 Subject: [PATCH 30/38] Implemented: Added endpoints to delete payment methods. --- .../api/headless-commerce.rest.xml | 9 ++++++++ headless-commerce/servicedef/services.xml | 8 +++++++ .../api/customer/CustomerServices.java | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index 8c5d0a657..a83f98b09 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -54,17 +54,26 @@ + + + + + + + + + \ No newline at end of file diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index aee842a2d..7222e3e57 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -437,4 +437,12 @@ under the License. + + delete payment method(credit card, giftt card, EFT account) + + + + + \ No newline at end of file diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 4c2da5af2..61e80f4c6 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1101,6 +1101,27 @@ public static Map createUpdateCustomerPaymentMethod(DispatchCont } return ServiceUtil.returnSuccess(); } + public static Map deleteCustomerPaymentMethod(DispatchContext dctx, Map context) { + LocalDispatcher dispatcher = dctx.getDispatcher(); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String customerPartyId = (String) context.get("customerPartyId"); + String paymentMethodId = (String) context.get("paymentMethodId"); + + try { + Map serviceCtx = dctx.getModelService("deletePaymentMethod").makeValid(context, ModelService.IN_PARAM); + serviceCtx.put("partyId", customerPartyId); + serviceCtx.put("userLogin", userLogin); + Map result = dispatcher.runSync("deletePaymentMethod", serviceCtx); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } catch (GenericServiceException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + return ServiceUtil.returnSuccess(); + } public static Map validateCustomer(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); Locale locale = (Locale) context.get("locale"); From ae178d0e68f12a095773e511a3de97b7dcd8627c Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 23 Nov 2020 11:18:31 +0530 Subject: [PATCH 31/38] Implemented: Removed home phone, work phone, fax number from create customer api to keep the payload specific with only shipping address and mobile phone. --- headless-commerce/servicedef/services.xml | 20 ---- .../api/customer/CustomerServices.java | 106 ------------------ 2 files changed, 126 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 7222e3e57..973700591 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -74,26 +74,6 @@ under the License. - - - - - - - - - - - - - - - - - - - - diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 61e80f4c6..5c4cb306c 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -196,9 +196,6 @@ public static Map createCustomer(DispatchContext dctx, Map shippingAddress = UtilGenerics.cast(context.get("shippingAddress")); - Map homePhone = UtilGenerics.cast(context.get("homePhone")); - Map workPhone = UtilGenerics.cast(context.get("workPhone")); - Map faxNumber = UtilGenerics.cast(context.get("faxNumber")); Map mobilePhone = UtilGenerics.cast(context.get("mobilePhone")); List errorList; GenericValue productStore = null; @@ -300,88 +297,6 @@ public static Map createCustomer(DispatchContext dctx, Map createCustomer(DispatchContext dctx, Map validateCreateCustomer(DispatchContext dctx, M String passwordHint = (String) context.get("passwordHint"); String confirmPassword = (String) context.get("confirmPassword"); Map shippingAddress = UtilGenerics.cast(context.get("shippingAddress")); - Map homePhone = UtilGenerics.cast(context.get("homePhone")); - Map workPhone = UtilGenerics.cast(context.get("workPhone")); - Map faxNumber = UtilGenerics.cast(context.get("faxNumber")); Map mobilePhone = UtilGenerics.cast(context.get("mobilePhone")); List errorList = new ArrayList<>(); @@ -552,15 +455,6 @@ public static Map validateCreateCustomer(DispatchContext dctx, M } } - if (UtilValidate.isNotEmpty(homePhone) && UtilValidate.isEmpty(homePhone.get("contactNumber"))) { - errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); - } - if (UtilValidate.isNotEmpty(workPhone) && UtilValidate.isEmpty(workPhone.get("contactNumber"))) { - errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); - } - if (UtilValidate.isNotEmpty(faxNumber) && UtilValidate.isEmpty(faxNumber.get("contactNumber"))) { - errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); - } if (UtilValidate.isNotEmpty(mobilePhone) && UtilValidate.isEmpty(mobilePhone.get("contactNumber"))) { errorList.add(UtilProperties.getMessage("PartyUiLabels", "PartyContactNumberMissing", locale)); } From 76b898e46c935a219d73c55f36b815cc47bfdfba Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 23 Nov 2020 11:29:02 +0530 Subject: [PATCH 32/38] Improved: Removed filter parameter from get customer profile endpoint and converted the api to use get method instead of post. --- .../api/headless-commerce.rest.xml | 4 ++-- headless-commerce/servicedef/services.xml | 6 ++---- .../api/customer/CustomerServices.java | 19 ++----------------- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index a83f98b09..9674ec130 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -15,8 +15,8 @@ - - + + diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 973700591..3384dc474 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -85,12 +85,10 @@ under the License. location="org.apache.ofbiz.hc.api.customer.CustomerServices" invoke="validateCreateCustomer"> - + - - diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 5c4cb306c..684ad6a40 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -483,7 +483,7 @@ public static Map validateCreateCustomer(DispatchContext dctx, M } return ServiceUtil.returnSuccess(); } - public static Map getCustomerProfile(DispatchContext dctx, Map context) { + public static Map getCustomer(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); @@ -493,15 +493,10 @@ public static Map getCustomerProfile(DispatchContext dctx, Map response = ServiceUtil.returnSuccess(); try { - if (UtilValidate.isEmpty(filters)) { - filters = UtilMisc.toMap("showPersonalInformation", "Y", "showLoyaltyPoints", "Y", "showContactMechs", "N", "showPaymentMethods", "N", "showContactLists", "N", "showCommunications", "N"); - } - response.put("filters", filters); boolean showOld = "true".equals(showOldStr); GenericValue person = EntityQuery.use(delegator).from("Person").where("partyId", customerPartyId).queryFirst(); if (person != null) { response.put("customerPartyId", customerPartyId); - if ("Y".equals(filters.get("showPersonalInformation"))) { Map personalInformationMap = new HashMap<>(); personalInformationMap.put("personalTitle", person.getString("personalTitle")); personalInformationMap.put("firstName", person.getString("firstName")); @@ -510,11 +505,8 @@ public static Map getCustomerProfile(DispatchContext dctx, Map> contactMechs = new ArrayList<>(); List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); for (Map partyContactMechValueMap : partyContactMechValueMaps) { @@ -582,9 +574,7 @@ public static Map getCustomerProfile(DispatchContext dctx, Map serviceContext = new HashMap<>(); serviceContext.put("customerPartyId", customerPartyId); @@ -595,13 +585,8 @@ public static Map getCustomerProfile(DispatchContext dctx, Map Date: Mon, 23 Nov 2020 11:29:53 +0530 Subject: [PATCH 33/38] Improved: Formatting changes only --- .../api/customer/CustomerServices.java | 158 +++++++++--------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 684ad6a40..90713aefd 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -497,96 +497,96 @@ public static Map getCustomer(DispatchContext dctx, Map personalInformationMap = new HashMap<>(); - personalInformationMap.put("personalTitle", person.getString("personalTitle")); - personalInformationMap.put("firstName", person.getString("firstName")); - personalInformationMap.put("middleName", person.getString("middleName")); - personalInformationMap.put("lastName", person.getString("lastName")); - personalInformationMap.put("suffix", person.getString("suffix")); - personalInformationMap.put("userName", userLogin.getString("userLoginId")); - response.put("personalInformation", personalInformationMap); - response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); + Map personalInformationMap = new HashMap<>(); + personalInformationMap.put("personalTitle", person.getString("personalTitle")); + personalInformationMap.put("firstName", person.getString("firstName")); + personalInformationMap.put("middleName", person.getString("middleName")); + personalInformationMap.put("lastName", person.getString("lastName")); + personalInformationMap.put("suffix", person.getString("suffix")); + personalInformationMap.put("userName", userLogin.getString("userLoginId")); + response.put("personalInformation", personalInformationMap); + response.put("loyaltyPoints", CustomerHelper.getLoyaltyPoints(dispatcher, customerPartyId, userLogin)); - List> contactMechs = new ArrayList<>(); - List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); - for (Map partyContactMechValueMap : partyContactMechValueMaps) { - Map infoMap = new HashMap<>(); - GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); - GenericValue contactMechType = (GenericValue) partyContactMechValueMap.get("contactMechType"); - infoMap.put("contactMechId", contactMech.getString("contactMechId")); - if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("postalAddress"))) { - //postal address - GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); - if (postalAddress != null) { - infoMap.put("toName", postalAddress.getString("toName")); - infoMap.put("address1", postalAddress.getString("address1")); - infoMap.put("address2", postalAddress.getString("address2")); - infoMap.put("city", postalAddress.getString("city")); - infoMap.put("postalCode", postalAddress.getString("postalCode")); + List> contactMechs = new ArrayList<>(); + List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); + for (Map partyContactMechValueMap : partyContactMechValueMaps) { + Map infoMap = new HashMap<>(); + GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); + GenericValue contactMechType = (GenericValue) partyContactMechValueMap.get("contactMechType"); + infoMap.put("contactMechId", contactMech.getString("contactMechId")); + if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("postalAddress"))) { + //postal address + GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); + if (postalAddress != null) { + infoMap.put("toName", postalAddress.getString("toName")); + infoMap.put("address1", postalAddress.getString("address1")); + infoMap.put("address2", postalAddress.getString("address2")); + infoMap.put("city", postalAddress.getString("city")); + infoMap.put("postalCode", postalAddress.getString("postalCode")); - Map geoMap = new HashMap<>(); - GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); - if (countryGeo != null) { - geoMap.put("geoId", countryGeo.getString("geoId")); - geoMap.put("geoName", countryGeo.getString("geoName")); - geoMap.put("geoCode", countryGeo.getString("abbreviation")); - infoMap.put("country", geoMap); - } - geoMap = new HashMap<>(); - GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); - if (stateGeo != null) { - geoMap.put("geoId", stateGeo.getString("geoId")); - geoMap.put("geoName", stateGeo.getString("geoName")); - geoMap.put("geoCode", stateGeo.getString("abbreviation")); - infoMap.put("state", geoMap); - } - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - contactMechs.add(infoMap); + Map geoMap = new HashMap<>(); + GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); + if (countryGeo != null) { + geoMap.put("geoId", countryGeo.getString("geoId")); + geoMap.put("geoName", countryGeo.getString("geoName")); + geoMap.put("geoCode", countryGeo.getString("abbreviation")); + infoMap.put("country", geoMap); } - } else if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("telecomNumber"))) { - //phone number - GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); - if (telecomNumber != null) { - infoMap.put("countryCode", telecomNumber.getString("countryCode")); - infoMap.put("areaCode", telecomNumber.getString("areaCode")); - infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - contactMechs.add(infoMap); + geoMap = new HashMap<>(); + GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); + if (stateGeo != null) { + geoMap.put("geoId", stateGeo.getString("geoId")); + geoMap.put("geoName", stateGeo.getString("geoName")); + geoMap.put("geoCode", stateGeo.getString("abbreviation")); + infoMap.put("state", geoMap); } - } else { - //email, web address, electronic address, etc. - infoMap.put("infoString", contactMech.getString("infoString")); + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + contactMechs.add(infoMap); } - - //contact mech type - Map contactMechTypeMap = new HashMap<>(); - contactMechTypeMap.put("contactMechTypeId", contactMechType.getString("contactMechTypeId")); - contactMechTypeMap.put("description", contactMechType.getString("description")); - infoMap.put("contactMechType", contactMechTypeMap); - - //contact mech purpose - if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("partyContactMechPurposes"))) { + } else if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("telecomNumber"))) { + //phone number + GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); + if (telecomNumber != null) { + infoMap.put("countryCode", telecomNumber.getString("countryCode")); + infoMap.put("areaCode", telecomNumber.getString("areaCode")); + infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); + contactMechs.add(infoMap); } - contactMechs.add(infoMap); + } else { + //email, web address, electronic address, etc. + infoMap.put("infoString", contactMech.getString("infoString")); } - response.put("contactMechs", contactMechs); - //payment information - Map serviceContext = new HashMap<>(); - serviceContext.put("customerPartyId", customerPartyId); - serviceContext.put("showOld", showOld); - serviceContext.put("userLogin", userLogin); - Map result = dispatcher.runSync("getPartyPaymentMethods", serviceContext); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + //contact mech type + Map contactMechTypeMap = new HashMap<>(); + contactMechTypeMap.put("contactMechTypeId", contactMechType.getString("contactMechTypeId")); + contactMechTypeMap.put("description", contactMechType.getString("description")); + infoMap.put("contactMechType", contactMechTypeMap); + + //contact mech purpose + if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("partyContactMechPurposes"))) { + infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( + delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); } - response.put("paymentMethods", UtilGenerics.cast(result.get("paymentMethods"))); - response.put("contactLists", CustomerHelper.getPartyContactLists(delegator, customerPartyId)); - response.put("communications", CustomerHelper.getPartyCommunications(delegator, customerPartyId, true, false)); + contactMechs.add(infoMap); + } + response.put("contactMechs", contactMechs); + + //payment information + Map serviceContext = new HashMap<>(); + serviceContext.put("customerPartyId", customerPartyId); + serviceContext.put("showOld", showOld); + serviceContext.put("userLogin", userLogin); + Map result = dispatcher.runSync("getPartyPaymentMethods", serviceContext); + if (!ServiceUtil.isSuccess(result)) { + Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); + } + response.put("paymentMethods", UtilGenerics.cast(result.get("paymentMethods"))); + response.put("contactLists", CustomerHelper.getPartyContactLists(delegator, customerPartyId)); + response.put("communications", CustomerHelper.getPartyCommunications(delegator, customerPartyId, true, false)); } } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); From 6ef36490faef018267910e0a0f2049697b935b75 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Mon, 23 Nov 2020 12:51:06 +0530 Subject: [PATCH 34/38] Improved: Combined credit card, gift card and eft account creation through a single create payment method api. --- .../api/headless-commerce.rest.xml | 74 +++-------- headless-commerce/servicedef/services.xml | 119 +----------------- 2 files changed, 23 insertions(+), 170 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index 9674ec130..7438ad6ee 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -1,78 +1,42 @@ - - - - - - + + + - - - - - + + - + - + - - - - - - - - - - - - - - + + - - + + - + - - - - - - - - - - - - - - - - - - - - + + - - + + - - + + - + diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 3384dc474..f974f047e 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -239,82 +239,9 @@ under the License. - - To remove the the party contact mech purpose - - - - - - - To create the telecom number with purpose - - - - - - - - - - - - To update the telecom number and to add a purpose - - - - - - - - - - - - - To create the telecom number - - - - - - - - To update the email address - - - - - - - - - To create contact mech - - - - - - - - To update contact mech - - - - - - - - - creates credit card + creates credit card, gift card, eft account @@ -338,9 +265,9 @@ under the License. - - updates credit card + updates credit card, gift card, eft account @@ -364,48 +291,10 @@ under the License. - - - creates gift card - - - - - - - - - updates gift card - - - - - - creates EFT account - - - - - - - - - - - - - updates EFT account - - - From 7a99ba6dcf79c1fe1d9982fc748d0e1407c5ad86 Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Sat, 28 Nov 2020 10:07:44 +0530 Subject: [PATCH 35/38] Improved: Retruned only personal information and loyalty points information in the response of getCustomer endpoint. --- headless-commerce/servicedef/services.xml | 89 ------------------- .../api/customer/CustomerServices.java | 84 ----------------- 2 files changed, 173 deletions(-) diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index f974f047e..812dbeefb 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -97,100 +97,11 @@ under the License. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 90713aefd..588cd6c6c 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -488,12 +488,9 @@ public static Map getCustomer(DispatchContext dctx, Map filters = UtilGenerics.cast(context.get("filters")); Map response = ServiceUtil.returnSuccess(); try { - boolean showOld = "true".equals(showOldStr); GenericValue person = EntityQuery.use(delegator).from("Person").where("partyId", customerPartyId).queryFirst(); if (person != null) { response.put("customerPartyId", customerPartyId); @@ -506,87 +503,6 @@ public static Map getCustomer(DispatchContext dctx, Map> contactMechs = new ArrayList<>(); - List> partyContactMechValueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, customerPartyId, showOld); - for (Map partyContactMechValueMap : partyContactMechValueMaps) { - Map infoMap = new HashMap<>(); - GenericValue contactMech = (GenericValue) partyContactMechValueMap.get("contactMech"); - GenericValue contactMechType = (GenericValue) partyContactMechValueMap.get("contactMechType"); - infoMap.put("contactMechId", contactMech.getString("contactMechId")); - if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("postalAddress"))) { - //postal address - GenericValue postalAddress = (GenericValue) partyContactMechValueMap.get("postalAddress"); - if (postalAddress != null) { - infoMap.put("toName", postalAddress.getString("toName")); - infoMap.put("address1", postalAddress.getString("address1")); - infoMap.put("address2", postalAddress.getString("address2")); - infoMap.put("city", postalAddress.getString("city")); - infoMap.put("postalCode", postalAddress.getString("postalCode")); - - Map geoMap = new HashMap<>(); - GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("countryGeoId")).queryOne(); - if (countryGeo != null) { - geoMap.put("geoId", countryGeo.getString("geoId")); - geoMap.put("geoName", countryGeo.getString("geoName")); - geoMap.put("geoCode", countryGeo.getString("abbreviation")); - infoMap.put("country", geoMap); - } - geoMap = new HashMap<>(); - GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", postalAddress.getString("stateProvinceGeoId")).queryOne(); - if (stateGeo != null) { - geoMap.put("geoId", stateGeo.getString("geoId")); - geoMap.put("geoName", stateGeo.getString("geoName")); - geoMap.put("geoCode", stateGeo.getString("abbreviation")); - infoMap.put("state", geoMap); - } - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - contactMechs.add(infoMap); - } - } else if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("telecomNumber"))) { - //phone number - GenericValue telecomNumber = (GenericValue) partyContactMechValueMap.get("telecomNumber"); - if (telecomNumber != null) { - infoMap.put("countryCode", telecomNumber.getString("countryCode")); - infoMap.put("areaCode", telecomNumber.getString("areaCode")); - infoMap.put("contactNumber", telecomNumber.getString("contactNumber")); - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - contactMechs.add(infoMap); - } - } else { - //email, web address, electronic address, etc. - infoMap.put("infoString", contactMech.getString("infoString")); - } - - //contact mech type - Map contactMechTypeMap = new HashMap<>(); - contactMechTypeMap.put("contactMechTypeId", contactMechType.getString("contactMechTypeId")); - contactMechTypeMap.put("description", contactMechType.getString("description")); - infoMap.put("contactMechType", contactMechTypeMap); - - //contact mech purpose - if (UtilValidate.isNotEmpty(partyContactMechValueMap.get("partyContactMechPurposes"))) { - infoMap.put("contactMechPurposes", CustomerHelper.prepareContactMechPurposeList( - delegator, UtilGenerics.cast(partyContactMechValueMap.get("partyContactMechPurposes")))); - } - contactMechs.add(infoMap); - } - response.put("contactMechs", contactMechs); - - //payment information - Map serviceContext = new HashMap<>(); - serviceContext.put("customerPartyId", customerPartyId); - serviceContext.put("showOld", showOld); - serviceContext.put("userLogin", userLogin); - Map result = dispatcher.runSync("getPartyPaymentMethods", serviceContext); - if (!ServiceUtil.isSuccess(result)) { - Debug.logError(ServiceUtil.getErrorMessage(result), MODULE); - } - response.put("paymentMethods", UtilGenerics.cast(result.get("paymentMethods"))); - response.put("contactLists", CustomerHelper.getPartyContactLists(delegator, customerPartyId)); - response.put("communications", CustomerHelper.getPartyCommunications(delegator, customerPartyId, true, false)); } } catch (GenericEntityException | GenericServiceException e) { Debug.logError(e, MODULE); From eeb4045af75f130dfb88f48efc4939d6a6ce1f5e Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Sat, 28 Nov 2020 11:36:18 +0530 Subject: [PATCH 36/38] Implemented: Added endpoint to get the shipping addresses. --- .../api/headless-commerce.rest.xml | 2 +- headless-commerce/servicedef/services.xml | 31 ++++++++++-- .../api/customer/CustomerServices.java | 48 +++++++++++++++++-- 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/headless-commerce/api/headless-commerce.rest.xml b/headless-commerce/api/headless-commerce.rest.xml index 7438ad6ee..f791b9cec 100644 --- a/headless-commerce/api/headless-commerce.rest.xml +++ b/headless-commerce/api/headless-commerce.rest.xml @@ -10,7 +10,7 @@ - + diff --git a/headless-commerce/servicedef/services.xml b/headless-commerce/servicedef/services.xml index 812dbeefb..e2c85bd51 100644 --- a/headless-commerce/servicedef/services.xml +++ b/headless-commerce/servicedef/services.xml @@ -110,6 +110,33 @@ under the License. + + To get the customer's shipping addresses + + + + + + + + + + + + + + + + + + + + + + + + To create the postal address with purpose @@ -123,8 +150,6 @@ under the License. - - @@ -140,8 +165,6 @@ under the License. - - diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 588cd6c6c..ab577fc2a 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -11,7 +11,6 @@ import org.apache.ofbiz.entity.util.EntityUtilProperties; import org.apache.ofbiz.hc.api.common.CommonUtil; import org.apache.ofbiz.party.contact.ContactHelper; -import org.apache.ofbiz.party.contact.ContactMechWorker; import org.apache.ofbiz.security.SecurityUtil; import org.apache.ofbiz.service.*; @@ -568,6 +567,49 @@ public static Map getPartyPaymentMethods(DispatchContext dctx, M result.put("paymentMethods", paymentMethods); return result; } + public static Map getCustomerPostalAddress(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + String customerPartyId = (String) context.get("customerPartyId"); + List> addresses = new ArrayList<>(); + + try { + List partyContactDetails = EntityQuery.use(delegator).from("PartyContactDetailByPurpose") + .where("partyId", customerPartyId, "contactMechTypeId", "POSTAL_ADDRESS", "contactMechPurposeTypeId", "SHIPPING_LOCATION") + .filterByDate() + .queryList(); + for (GenericValue partyContactDetail : partyContactDetails) { + Map addressMap = new HashMap<>(); + addressMap.put("address1", partyContactDetail.getString("address1")); + addressMap.put("address2", partyContactDetail.getString("address2")); + addressMap.put("city", partyContactDetail.getString("city")); + addressMap.put("postalCode", partyContactDetail.getString("postalCode")); + Map stateInfoMap = new HashMap<>(); + Map countryInfoMap = new HashMap<>(); + GenericValue state = EntityQuery.use(delegator).from("Geo").where("geoId", partyContactDetail.getString("stateProvinceGeoId")).queryOne(); + if (state != null) { + stateInfoMap.put("stateProvinceGeoId", state.getString("geoId")); + stateInfoMap.put("stateName", state.getString("geoName")); + stateInfoMap.put("stateCode", state.getString("abbreviation")); + addressMap.put("state", stateInfoMap); + } + GenericValue country = EntityQuery.use(delegator).from("Geo").where("geoId", partyContactDetail.getString("countryGeoId")).queryOne(); + if (country != null) { + countryInfoMap.put("countryGeoId", country.getString("geoId")); + countryInfoMap.put("countryName", country.getString("geoName")); + countryInfoMap.put("countryCode", country.getString("abbreviation")); + addressMap.put("country", countryInfoMap); + } + addresses.add(addressMap); + } + + } catch (GenericEntityException e) { + Debug.logError(e, MODULE); + return ServiceUtil.returnError(e.getMessage()); + } + Map result = ServiceUtil.returnSuccess(); + result.put("addresses", addresses); + return result; + } public static Map createUpdateCustomerPostalAddress(DispatchContext dctx, Map context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); @@ -576,7 +618,7 @@ public static Map createUpdateCustomerPostalAddress(DispatchCont String customerPartyId = (String) context.get("customerPartyId"); String stateCode = (String) context.get("stateCode"); String countryCode = (String) context.get("countryCode"); - String contactMechPurposeTypeId = (String) context.get("contactMechPurposeTypeId"); + String contactMechPurposeTypeId = "SHIPPING_LOCATION"; String contactMechId = (String) context.get("contactMechId"); String address1 = (String) context.get("address1"); Map serviceCtx = new HashMap<>(); @@ -631,6 +673,7 @@ public static Map createUpdateCustomerPostalAddress(DispatchCont serviceCtx.put("partyId", customerPartyId); serviceCtx.put("stateProvinceGeoId", stateProvinceGeoId); serviceCtx.put("countryGeoId", countryGeoId); + serviceCtx.put("contactMechPurposeTypeId", contactMechPurposeTypeId); serviceCtx.put("userLogin", userLogin); result = dispatcher.runSync("createPartyPostalAddress", serviceCtx); if (!ServiceUtil.isSuccess(result)) { @@ -900,7 +943,6 @@ public static Map deleteCustomerPaymentMethod(DispatchContext dc LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String customerPartyId = (String) context.get("customerPartyId"); - String paymentMethodId = (String) context.get("paymentMethodId"); try { Map serviceCtx = dctx.getModelService("deletePaymentMethod").makeValid(context, ModelService.IN_PARAM); From 8366df5c11a6895ce38f4a51d49c4e7e2b45f1bc Mon Sep 17 00:00:00 2001 From: Ravi Lodhi Date: Sat, 10 Apr 2021 11:50:55 +0530 Subject: [PATCH 37/38] Improved: Imported specific classes only. (HEADLESS-COMMERCE) --- .../api/customer/CustomerServices.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index ab577fc2a..2cae3c398 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -1,7 +1,12 @@ package org.apache.ofbiz.hc.api.customer; import org.apache.ofbiz.accounting.payment.PaymentWorker; -import org.apache.ofbiz.base.util.*; +import org.apache.ofbiz.base.util.Debug; +import org.apache.ofbiz.base.util.GeneralException; +import org.apache.ofbiz.base.util.UtilGenerics; +import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.base.util.UtilProperties; +import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.base.util.string.FlexibleStringExpander; import org.apache.ofbiz.common.login.LoginServices; import org.apache.ofbiz.entity.Delegator; @@ -12,9 +17,17 @@ import org.apache.ofbiz.hc.api.common.CommonUtil; import org.apache.ofbiz.party.contact.ContactHelper; import org.apache.ofbiz.security.SecurityUtil; -import org.apache.ofbiz.service.*; - -import java.util.*; +import org.apache.ofbiz.service.DispatchContext; +import org.apache.ofbiz.service.GenericServiceException; +import org.apache.ofbiz.service.LocalDispatcher; +import org.apache.ofbiz.service.ModelService; +import org.apache.ofbiz.service.ServiceUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; import java.util.stream.Collectors; public class CustomerServices { @@ -981,4 +994,4 @@ public static Map validateCustomer(DispatchContext dctx, Map Date: Sat, 10 Apr 2021 11:56:38 +0530 Subject: [PATCH 38/38] Improved: Fixed some checkstyle warnings/errors.(HEADLESS-COMMERCE) --- .../api/customer/CustomerServices.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java index 2cae3c398..bebd07ace 100644 --- a/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java +++ b/headless-commerce/src/main/java/org.apache.ofbiz.hc/api/customer/CustomerServices.java @@ -22,7 +22,6 @@ import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.service.ModelService; import org.apache.ofbiz.service.ServiceUtil; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -31,7 +30,6 @@ import java.util.stream.Collectors; public class CustomerServices { - public static final String MODULE = CustomerServices.class.getName(); public static final String RESOURCE = "PartyUiLabels"; public static final String SECRESOURCE = "SecurityextUiLabels"; @@ -86,20 +84,23 @@ public static Map sendResetPasswordEmail(DispatchContext ctx, Ma if (userLogin == null || "N".equals(userLogin.getString("enabled"))) { Debug.logError("userlogin unknown or disabled " + userLogin, SECRESOURCE); //giving a "sent email to associated email-address" response, to suppress feedback on in-/valid usernames - return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.new_password_sent_check_email", locale)); + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, + "loginevents.new_password_sent_check_email", locale)); } // check login is associated to a party GenericValue userParty = userLogin.getRelatedOne("Party", false); if (userParty == null) { - return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.username_not_found_reenter", locale)); + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, + "loginevents.username_not_found_reenter", locale)); } // check there is an email to send to - List contactMechs = (List) ContactHelper.getContactMechByPurpose(userParty, "PRIMARY_EMAIL", false); + List contactMechs = UtilGenerics.cast(ContactHelper.getContactMechByPurpose(userParty, "PRIMARY_EMAIL", false)); if (UtilValidate.isEmpty(contactMechs)) { // the email was not found - return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.no_primary_email_address_set_contact_customer_service", locale)); + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, + "loginevents.no_primary_email_address_set_contact_customer_service", locale)); } String emails = contactMechs.stream() .map(email -> email.getString("infoString")) @@ -153,7 +154,8 @@ public static Map sendResetPasswordEmail(DispatchContext ctx, Ma } else { serviceContext.put("subject", UtilProperties.getMessage(SECRESOURCE, "loginservices.password_reminder_subject", UtilMisc.toMap("userLoginId", userLoginId), locale)); - serviceContext.put("sendFrom", EntityUtilProperties.getPropertyValue("general", "defaultFromEmailAddress", delegator)); + serviceContext.put("sendFrom", EntityUtilProperties.getPropertyValue("general", + "defaultFromEmailAddress", delegator)); } } serviceContext.put("sendTo", emails); @@ -164,14 +166,17 @@ public static Map sendResetPasswordEmail(DispatchContext ctx, Ma String errorMessage = ServiceUtil.getErrorMessage(result); Map messageMap = UtilMisc.toMap("errorMessage", errorMessage); Debug.logError(errorMessage, MODULE); - return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.error_unable_email_password_contact_customer_service_errorwas", + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, + "loginevents.error_unable_email_password_contact_customer_service_errorwas", messageMap, locale)); } } catch (GeneralException e) { Debug.logWarning(e, "", MODULE); - return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, "loginevents.error_unable_email_password_contact_customer_service", locale)); + return ServiceUtil.returnError(UtilProperties.getMessage(SECRESOURCE, + "loginevents.error_unable_email_password_contact_customer_service", locale)); } - return ServiceUtil.returnSuccess(UtilProperties.getMessage(SECRESOURCE, "loginevents.new_password_sent_check_email", locale)); + return ServiceUtil.returnSuccess(UtilProperties.getMessage(SECRESOURCE, + "loginevents.new_password_sent_check_email", locale)); } public static Map changePassword(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher();