From 6b2c0eb4e6212ee06cf13069d9cd03c513756cbf Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Thu, 7 Dec 2023 11:43:16 -0800 Subject: [PATCH 01/16] Fix check for isHHAccount --- force-app/main/default/classes/HH_ManageHH_CTRL.cls | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index bf7d016d055..11c653b951c 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -186,12 +186,14 @@ public with sharing class HH_ManageHH_CTRL { } private Boolean canUpdateHousehold() { + // TODO: This whole method is potentially a HUGE source of bugs!!! + // Need to thoroughly rethink this... String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; Set fieldsToCheck = new Set(); for (FieldSetMember fsMember : hhFieldSet) { fieldsToCheck.add(fsMember.getFieldPath()); } - if (isHHAccount) { + if (!isHHAccount) { // TODO: I think this is backwards... should be if (!isHHAccount)... pending... fieldsToCheck.add('npo02__Household__c'); } for (String fieldToCheck : fieldsToCheck) { From 6ed53c8f7c3bf2a6c65679e99d0e601f280102b5 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Thu, 7 Dec 2023 13:14:35 -0800 Subject: [PATCH 02/16] Remove unnecessary field checks --- .../default/classes/HH_Container_LCTRL.cls | 11 +------ .../main/default/classes/HH_ManageHH_CTRL.cls | 32 +++++++++++++++---- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/force-app/main/default/classes/HH_Container_LCTRL.cls b/force-app/main/default/classes/HH_Container_LCTRL.cls index 6cb5e690791..d3d6fba3a7f 100644 --- a/force-app/main/default/classes/HH_Container_LCTRL.cls +++ b/force-app/main/default/classes/HH_Container_LCTRL.cls @@ -551,21 +551,12 @@ public with sharing class HH_Container_LCTRL { } Set addressFields = new Set{ - 'Default_Address__c', - 'Household_Account__c', - 'Address_Type__c', 'MailingStreet__c', 'MailingStreet2__c', 'MailingCity__c', 'MailingState__c', 'MailingPostalCode__c', - 'MailingCountry__c', - 'Seasonal_Start_Month__c', - 'Seasonal_Start_Day__c', - 'Seasonal_End_Month__c', - 'Seasonal_End_Day__c', - 'Geolocation__Latitude__s', - 'Geolocation__Longitude__s' + 'MailingCountry__c' }; for (String addressField : addressFields) { diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index 11c653b951c..c227df9c5f5 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -185,15 +185,33 @@ public with sharing class HH_ManageHH_CTRL { return null; } + private Set getFieldsToCheck() { + Set fieldsToCheck = new Set{ + 'Name', + 'npo02__SYSTEM_CUSTOM_NAMING__c', + 'npo02__Formal_Greeting__c', + 'npo02__Informal_Greeting__c', + UTIL_Namespace.StrAllNSPrefix('Number_of_Household_Members__c')}; + + Boolean isHousehold = UTIL_Describe.isObjectIdThisType(hhId, 'Account'); + if (isHousehold) { + fieldsToCheck.addAll(new Set{ + 'BillingStreet', 'BillingCity', 'BillingState', 'BillingPostalCode', + 'BillingCountry', 'BillingLatitude', 'BillingLongitude'}); + + } else { + fieldsToCheck.addAll(new Set{ + 'npo02__MailingStreet__c', 'npo02__MailingCity__c', 'npo02__MailingState__c', + 'npo02__MailingPostalCode__c', 'npo02__MailingCountry__c'}); + } + + return fieldsToCheck; + } + private Boolean canUpdateHousehold() { - // TODO: This whole method is potentially a HUGE source of bugs!!! - // Need to thoroughly rethink this... String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; - Set fieldsToCheck = new Set(); - for (FieldSetMember fsMember : hhFieldSet) { - fieldsToCheck.add(fsMember.getFieldPath()); - } - if (!isHHAccount) { // TODO: I think this is backwards... should be if (!isHHAccount)... pending... + Set fieldsToCheck = getFieldsToCheck(); + if (!isHHAccount) { fieldsToCheck.add('npo02__Household__c'); } for (String fieldToCheck : fieldsToCheck) { From 98919c95e2f43ad8659849c88e900945c547f6a5 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Fri, 8 Dec 2023 13:30:43 -0800 Subject: [PATCH 03/16] Fix namespace issue in canUpdateHousehold() --- force-app/main/default/classes/HH_ManageHH_CTRL.cls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index c227df9c5f5..0c045ef9b63 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -215,7 +215,7 @@ public with sharing class HH_ManageHH_CTRL { fieldsToCheck.add('npo02__Household__c'); } for (String fieldToCheck : fieldsToCheck) { - if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { + if (!UTIL_Permissions.canUpdate(accountToCheck, UTIL_Namespace.StrAllNSPrefix(fieldToCheck), false)) { return false; } } From 9317c73731e2d70cc3bfce44fea521fcaab8da77 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Fri, 8 Dec 2023 15:27:44 -0800 Subject: [PATCH 04/16] Refactor namespace fix --- force-app/main/default/classes/HH_ManageHH_CTRL.cls | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index 0c045ef9b63..f064d54780d 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -196,8 +196,13 @@ public with sharing class HH_ManageHH_CTRL { Boolean isHousehold = UTIL_Describe.isObjectIdThisType(hhId, 'Account'); if (isHousehold) { fieldsToCheck.addAll(new Set{ - 'BillingStreet', 'BillingCity', 'BillingState', 'BillingPostalCode', - 'BillingCountry', 'BillingLatitude', 'BillingLongitude'}); + UTIL_Namespace.StrAllNSPrefix('BillingStreet'), + UTIL_Namespace.StrAllNSPrefix('BillingCity'), + UTIL_Namespace.StrAllNSPrefix('BillingState'), + UTIL_Namespace.StrAllNSPrefix('BillingPostalCode'), + UTIL_Namespace.StrAllNSPrefix('BillingCountry'), + UTIL_Namespace.StrAllNSPrefix('BillingLatitude'), + UTIL_Namespace.StrAllNSPrefix('BillingLongitude')}); } else { fieldsToCheck.addAll(new Set{ @@ -215,7 +220,7 @@ public with sharing class HH_ManageHH_CTRL { fieldsToCheck.add('npo02__Household__c'); } for (String fieldToCheck : fieldsToCheck) { - if (!UTIL_Permissions.canUpdate(accountToCheck, UTIL_Namespace.StrAllNSPrefix(fieldToCheck), false)) { + if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { return false; } } From b204fc98908cb69e060d6a0928b1b2b468b0dd23 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Fri, 8 Dec 2023 17:53:09 -0800 Subject: [PATCH 05/16] Testing 2gp failure. Will be reverted... --- .../main/default/classes/HH_ManageHH_CTRL.cls | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index f064d54780d..49a595bbd88 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -214,16 +214,16 @@ public with sharing class HH_ManageHH_CTRL { } private Boolean canUpdateHousehold() { - String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; - Set fieldsToCheck = getFieldsToCheck(); - if (!isHHAccount) { - fieldsToCheck.add('npo02__Household__c'); - } - for (String fieldToCheck : fieldsToCheck) { - if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { - return false; - } - } +// String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; +// Set fieldsToCheck = getFieldsToCheck(); +// if (!isHHAccount) { +// fieldsToCheck.add('npo02__Household__c'); +// } +// for (String fieldToCheck : fieldsToCheck) { +// if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { +// return false; +// } +// } return true; } From 57a408ed0c64653ca8c71a809563f20e75d45d8a Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Fri, 8 Dec 2023 20:08:32 -0800 Subject: [PATCH 06/16] Revert "Testing 2gp failure. Will be reverted..." This reverts commit b204fc98908cb69e060d6a0928b1b2b468b0dd23. --- .../main/default/classes/HH_ManageHH_CTRL.cls | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index 49a595bbd88..f064d54780d 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -214,16 +214,16 @@ public with sharing class HH_ManageHH_CTRL { } private Boolean canUpdateHousehold() { -// String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; -// Set fieldsToCheck = getFieldsToCheck(); -// if (!isHHAccount) { -// fieldsToCheck.add('npo02__Household__c'); -// } -// for (String fieldToCheck : fieldsToCheck) { -// if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { -// return false; -// } -// } + String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; + Set fieldsToCheck = getFieldsToCheck(); + if (!isHHAccount) { + fieldsToCheck.add('npo02__Household__c'); + } + for (String fieldToCheck : fieldsToCheck) { + if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { + return false; + } + } return true; } From a7beb6a69a536409bccf52094fd2f55f5d0eb694 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Fri, 8 Dec 2023 21:39:59 -0800 Subject: [PATCH 07/16] Testing 2gp failure. Will be reverted... --- .../default/classes/HH_Container_LCTRL.cls | 83 ++++++------------- force-app/main/default/pages/HH_ManageHH.page | 1 + 2 files changed, 25 insertions(+), 59 deletions(-) diff --git a/force-app/main/default/classes/HH_Container_LCTRL.cls b/force-app/main/default/classes/HH_Container_LCTRL.cls index d3d6fba3a7f..21ce741c42b 100644 --- a/force-app/main/default/classes/HH_Container_LCTRL.cls +++ b/force-app/main/default/classes/HH_Container_LCTRL.cls @@ -362,8 +362,8 @@ public with sharing class HH_Container_LCTRL { * @param listCon The list of Contacts to save * @return void */ - @TestVisible - private static void upsertContacts(List listCon) { + @AuraEnabled + public static void upsertContacts(List listCon) { // Even though we are given a list of Contacts from the lightning component, // apex seems to treat them as generic sObjects, and thus we can't do upsert. // thus we will split the list into update and insert lists. @@ -482,11 +482,12 @@ public with sharing class HH_Container_LCTRL { * @param listHHMerge the list of Households to merge into the winner * @return void */ - @TestVisible - private static void mergeHouseholds(Account hhWinner, List listHHMerge) { + @AuraEnabled + public static void mergeHouseholds(Account hhWinner, List listHHMerge) { try { // Check object permissions - if (!Account.SObjectType.getDescribe().isMergeable()) { + if (!Account.SObjectType.getDescribe().isUpdateable() || + !Account.SObjectType.getDescribe().isDeletable()) { throw new System.NoAccessException(); } @@ -507,66 +508,30 @@ public with sharing class HH_Container_LCTRL { */ @AuraEnabled public static void saveHouseholdPage(SObject hh, List listCon, List listConRemove, ListlistHHMerge) { - try { - // We need to determine if the new Default Address is Undeliverable - Address__c defaultAddress = getAddressFromAccount(hh.Id, hh); - defaultAddress.Household_Account__c = hh.Id; - - if (!canReadAddress()) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } - Map addressMatch = Addresses.getExistingAddresses(new List{ - defaultAddress - }); - if (addressMatch.containsKey(defaultAddress) && addressMatch.get(defaultAddress) != null) { - selectedAddressIsUndeliverable = addressMatch.get(defaultAddress)?.Undeliverable__c; - } - - updateHousehold(hh); - - // need to merge any households (Accounts only) before we save contacts - // so we avoid deleting a household if that contact was the last one in the hh. - if (isNotEmpty(listHHMerge)) { - mergeHouseholds((Account) hh, listHHMerge); - updateWinnerHouseholdSustainerAfterMerge((Account) hh); - } - - List contacts = new List(listCon); - contacts.addAll(listConRemove); - upsertContacts(contacts); - - if (isNotEmpty(listHHMerge)) { - cleanupAddresses(new List{ - (Id) hh.get('Id') - }); - } - } catch (Exception e) { - throw new AuraHandledException(e.getMessage()); + // We need to determine if the new Default Address is Undeliverable + Address__c defaultAddress = getAddressFromAccount(hh.Id, hh); + defaultAddress.Household_Account__c = hh.Id; + Map addressMatch = Addresses.getExistingAddresses(new List{defaultAddress}); + if(addressMatch.containsKey(defaultAddress) && addressMatch.get(defaultAddress) != null){ + selectedAddressIsUndeliverable = addressMatch.get(defaultAddress)?.Undeliverable__c; } - } - private static Boolean canReadAddress() { - if (Test.isRunningTest()) { - return true; + updateHousehold(hh); + + // need to merge any households (Accounts only) before we save contacts + // so we avoid deleting a household if that contact was the last one in the hh. + if (isNotEmpty(listHHMerge)) { + mergeHouseholds((Account)hh, listHHMerge); + updateWinnerHouseholdSustainerAfterMerge((Account)hh); } - Set addressFields = new Set{ - 'MailingStreet__c', - 'MailingStreet2__c', - 'MailingCity__c', - 'MailingState__c', - 'MailingPostalCode__c', - 'MailingCountry__c' - }; + List contacts = new List(listCon); + contacts.addAll(listConRemove); + upsertContacts(contacts); - for (String addressField : addressFields) { - if (!UTIL_Permissions.canRead(UTIL_Namespace.StrAllNSPrefix('Address__c'), - UTIL_Namespace.StrAllNSPrefix(addressField), false)) { - return false; - } + if (isNotEmpty(listHHMerge)) { + cleanupAddresses(new List{ (Id) hh.get('Id') }); } - - return true; } private static void updateWinnerHouseholdSustainerAfterMerge(Account winnerHousehold) { diff --git a/force-app/main/default/pages/HH_ManageHH.page b/force-app/main/default/pages/HH_ManageHH.page index ac7d543a724..e1b19d3375f 100644 --- a/force-app/main/default/pages/HH_ManageHH.page +++ b/force-app/main/default/pages/HH_ManageHH.page @@ -41,6 +41,7 @@
+
This is HH_ManageHH
From f267088a9d8cbbb3a81b30d781471358b90ff265 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Fri, 8 Dec 2023 22:15:04 -0800 Subject: [PATCH 08/16] Testing 2gp failure. Will be reverted... --- .../main/default/classes/HH_ManageHH_CTRL.cls | 75 +++---------------- 1 file changed, 9 insertions(+), 66 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index f064d54780d..9070221ee8c 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -113,28 +113,17 @@ public with sharing class HH_ManageHH_CTRL { * @return null */ public PageReference handleNewHousehold() { - try { - if (hhId == null) { - if (!UTIL_Permissions.canCreate('npo02__Household__c')) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } - hh = new npo02__Household__c(); - hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact - UTIL_DMLService.insertRecord(hh); - hhId = hh.Id; - - if (contactId != null) { - if (!UTIL_Permissions.canUpdate('Contact','npo02__Household__c', false)) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } - Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); - UTIL_DMLService.updateRecord(con); - } + if (hhId == null) { + hh = new npo02__Household__c(); + hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact + UTIL_DMLService.insertRecord(hh); + hhId = hh.Id; + + if (contactId != null) { + Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); + UTIL_DMLService.updateRecord(con); } - } catch (Exception e) { - ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage())); } - return null; } @@ -175,56 +164,10 @@ public with sharing class HH_ManageHH_CTRL { */ public PageReference save() { try { - if (!canUpdateHousehold()) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } UTIL_DMLService.updateRecord(hh); } catch (Exception ex) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage())); } return null; } - - private Set getFieldsToCheck() { - Set fieldsToCheck = new Set{ - 'Name', - 'npo02__SYSTEM_CUSTOM_NAMING__c', - 'npo02__Formal_Greeting__c', - 'npo02__Informal_Greeting__c', - UTIL_Namespace.StrAllNSPrefix('Number_of_Household_Members__c')}; - - Boolean isHousehold = UTIL_Describe.isObjectIdThisType(hhId, 'Account'); - if (isHousehold) { - fieldsToCheck.addAll(new Set{ - UTIL_Namespace.StrAllNSPrefix('BillingStreet'), - UTIL_Namespace.StrAllNSPrefix('BillingCity'), - UTIL_Namespace.StrAllNSPrefix('BillingState'), - UTIL_Namespace.StrAllNSPrefix('BillingPostalCode'), - UTIL_Namespace.StrAllNSPrefix('BillingCountry'), - UTIL_Namespace.StrAllNSPrefix('BillingLatitude'), - UTIL_Namespace.StrAllNSPrefix('BillingLongitude')}); - - } else { - fieldsToCheck.addAll(new Set{ - 'npo02__MailingStreet__c', 'npo02__MailingCity__c', 'npo02__MailingState__c', - 'npo02__MailingPostalCode__c', 'npo02__MailingCountry__c'}); - } - - return fieldsToCheck; - } - - private Boolean canUpdateHousehold() { - String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; - Set fieldsToCheck = getFieldsToCheck(); - if (!isHHAccount) { - fieldsToCheck.add('npo02__Household__c'); - } - for (String fieldToCheck : fieldsToCheck) { - if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { - return false; - } - } - - return true; - } } \ No newline at end of file From ba8bf84a6ec0fa1daf94109e96e01d34c2ac8860 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sat, 9 Dec 2023 09:11:01 -0800 Subject: [PATCH 09/16] Testing re-introducing HH_ManageHH_CTRL original changes (with !isHHAccount fix)... --- .../main/default/classes/HH_ManageHH_CTRL.cls | 48 +++++++++++++++---- force-app/main/default/pages/HH_ManageHH.page | 1 - 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index 9070221ee8c..4834531658b 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -113,17 +113,28 @@ public with sharing class HH_ManageHH_CTRL { * @return null */ public PageReference handleNewHousehold() { - if (hhId == null) { - hh = new npo02__Household__c(); - hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact - UTIL_DMLService.insertRecord(hh); - hhId = hh.Id; + try { + if (hhId == null) { + if (!UTIL_Permissions.canCreate('npo02__Household__c')) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } + hh = new npo02__Household__c(); + hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact + UTIL_DMLService.insertRecord(hh); + hhId = hh.Id; - if (contactId != null) { - Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); - UTIL_DMLService.updateRecord(con); + if (contactId != null) { + if (!UTIL_Permissions.canUpdate('Contact','npo02__Household__c', false)) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } + Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); + UTIL_DMLService.updateRecord(con); + } } + } catch (Exception e) { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage())); } + return null; } @@ -164,10 +175,31 @@ public with sharing class HH_ManageHH_CTRL { */ public PageReference save() { try { + if (!canUpdateHousehold()) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } UTIL_DMLService.updateRecord(hh); } catch (Exception ex) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage())); } return null; } + + private Boolean canUpdateHousehold() { + String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; + Set fieldsToCheck = new Set(); + for (FieldSetMember fsMember : hhFieldSet) { + fieldsToCheck.add(fsMember.getFieldPath()); + } + if (!isHHAccount) { + fieldsToCheck.add('npo02__Household__c'); + } + for (String fieldToCheck : fieldsToCheck) { + if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { + return false; + } + } + + return true; + } } \ No newline at end of file diff --git a/force-app/main/default/pages/HH_ManageHH.page b/force-app/main/default/pages/HH_ManageHH.page index e1b19d3375f..ac7d543a724 100644 --- a/force-app/main/default/pages/HH_ManageHH.page +++ b/force-app/main/default/pages/HH_ManageHH.page @@ -41,7 +41,6 @@
-
This is HH_ManageHH
From 49ee61b8f8bc460085270f18ddad3fbde8a90a52 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sat, 9 Dec 2023 12:16:56 -0800 Subject: [PATCH 10/16] Revert "Testing re-introducing HH_ManageHH_CTRL original changes (with !isHHAccount fix)..." This reverts commit ba8bf84a6ec0fa1daf94109e96e01d34c2ac8860. --- .../main/default/classes/HH_ManageHH_CTRL.cls | 48 ++++--------------- force-app/main/default/pages/HH_ManageHH.page | 1 + 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index 4834531658b..9070221ee8c 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -113,28 +113,17 @@ public with sharing class HH_ManageHH_CTRL { * @return null */ public PageReference handleNewHousehold() { - try { - if (hhId == null) { - if (!UTIL_Permissions.canCreate('npo02__Household__c')) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } - hh = new npo02__Household__c(); - hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact - UTIL_DMLService.insertRecord(hh); - hhId = hh.Id; + if (hhId == null) { + hh = new npo02__Household__c(); + hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact + UTIL_DMLService.insertRecord(hh); + hhId = hh.Id; - if (contactId != null) { - if (!UTIL_Permissions.canUpdate('Contact','npo02__Household__c', false)) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } - Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); - UTIL_DMLService.updateRecord(con); - } + if (contactId != null) { + Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); + UTIL_DMLService.updateRecord(con); } - } catch (Exception e) { - ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage())); } - return null; } @@ -175,31 +164,10 @@ public with sharing class HH_ManageHH_CTRL { */ public PageReference save() { try { - if (!canUpdateHousehold()) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } UTIL_DMLService.updateRecord(hh); } catch (Exception ex) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage())); } return null; } - - private Boolean canUpdateHousehold() { - String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; - Set fieldsToCheck = new Set(); - for (FieldSetMember fsMember : hhFieldSet) { - fieldsToCheck.add(fsMember.getFieldPath()); - } - if (!isHHAccount) { - fieldsToCheck.add('npo02__Household__c'); - } - for (String fieldToCheck : fieldsToCheck) { - if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { - return false; - } - } - - return true; - } } \ No newline at end of file diff --git a/force-app/main/default/pages/HH_ManageHH.page b/force-app/main/default/pages/HH_ManageHH.page index ac7d543a724..e1b19d3375f 100644 --- a/force-app/main/default/pages/HH_ManageHH.page +++ b/force-app/main/default/pages/HH_ManageHH.page @@ -41,6 +41,7 @@
+
This is HH_ManageHH
From e113cdcf8a8a1c12aed361c96994ceede730674b Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sat, 9 Dec 2023 12:21:04 -0800 Subject: [PATCH 11/16] Original HH_ManageHH_CTRL and HH_ManageHH visualforce page --- force-app/main/default/pages/HH_ManageHH.page | 1 - 1 file changed, 1 deletion(-) diff --git a/force-app/main/default/pages/HH_ManageHH.page b/force-app/main/default/pages/HH_ManageHH.page index e1b19d3375f..ac7d543a724 100644 --- a/force-app/main/default/pages/HH_ManageHH.page +++ b/force-app/main/default/pages/HH_ManageHH.page @@ -41,7 +41,6 @@
-
This is HH_ManageHH
From d6b118dbb6c145cbcc25d77b044946de34fd63cc Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sat, 9 Dec 2023 13:43:58 -0800 Subject: [PATCH 12/16] Add back AuraEnabled and isMergeable changes --- force-app/main/default/classes/HH_Container_LCTRL.cls | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/force-app/main/default/classes/HH_Container_LCTRL.cls b/force-app/main/default/classes/HH_Container_LCTRL.cls index 21ce741c42b..687e15ea3a1 100644 --- a/force-app/main/default/classes/HH_Container_LCTRL.cls +++ b/force-app/main/default/classes/HH_Container_LCTRL.cls @@ -362,8 +362,8 @@ public with sharing class HH_Container_LCTRL { * @param listCon The list of Contacts to save * @return void */ - @AuraEnabled - public static void upsertContacts(List listCon) { + @TestVisible + private static void upsertContacts(List listCon) { // Even though we are given a list of Contacts from the lightning component, // apex seems to treat them as generic sObjects, and thus we can't do upsert. // thus we will split the list into update and insert lists. @@ -482,12 +482,11 @@ public with sharing class HH_Container_LCTRL { * @param listHHMerge the list of Households to merge into the winner * @return void */ - @AuraEnabled - public static void mergeHouseholds(Account hhWinner, List listHHMerge) { + @TestVisible + private static void mergeHouseholds(Account hhWinner, List listHHMerge) { try { // Check object permissions - if (!Account.SObjectType.getDescribe().isUpdateable() || - !Account.SObjectType.getDescribe().isDeletable()) { + if (!Account.SObjectType.getDescribe().isMergeable()) { throw new System.NoAccessException(); } From c6e3656449508cbd5c3b53665fa1f94628958d6b Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sat, 9 Dec 2023 15:21:12 -0800 Subject: [PATCH 13/16] Add back structure of saveHouseholdPage and return true from checkAddress --- .../default/classes/HH_Container_LCTRL.cls | 83 ++++++++++++++----- 1 file changed, 64 insertions(+), 19 deletions(-) diff --git a/force-app/main/default/classes/HH_Container_LCTRL.cls b/force-app/main/default/classes/HH_Container_LCTRL.cls index 687e15ea3a1..8fada98f238 100644 --- a/force-app/main/default/classes/HH_Container_LCTRL.cls +++ b/force-app/main/default/classes/HH_Container_LCTRL.cls @@ -507,30 +507,75 @@ public with sharing class HH_Container_LCTRL { */ @AuraEnabled public static void saveHouseholdPage(SObject hh, List listCon, List listConRemove, ListlistHHMerge) { - // We need to determine if the new Default Address is Undeliverable - Address__c defaultAddress = getAddressFromAccount(hh.Id, hh); - defaultAddress.Household_Account__c = hh.Id; - Map addressMatch = Addresses.getExistingAddresses(new List{defaultAddress}); - if(addressMatch.containsKey(defaultAddress) && addressMatch.get(defaultAddress) != null){ - selectedAddressIsUndeliverable = addressMatch.get(defaultAddress)?.Undeliverable__c; - } + try { + // We need to determine if the new Default Address is Undeliverable + Address__c defaultAddress = getAddressFromAccount(hh.Id, hh); + defaultAddress.Household_Account__c = hh.Id; + + if (!canReadAddress()) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } + Map addressMatch = Addresses.getExistingAddresses(new List{ + defaultAddress + }); + if (addressMatch.containsKey(defaultAddress) && addressMatch.get(defaultAddress) != null) { + selectedAddressIsUndeliverable = addressMatch.get(defaultAddress)?.Undeliverable__c; + } - updateHousehold(hh); + updateHousehold(hh); - // need to merge any households (Accounts only) before we save contacts - // so we avoid deleting a household if that contact was the last one in the hh. - if (isNotEmpty(listHHMerge)) { - mergeHouseholds((Account)hh, listHHMerge); - updateWinnerHouseholdSustainerAfterMerge((Account)hh); - } + // need to merge any households (Accounts only) before we save contacts + // so we avoid deleting a household if that contact was the last one in the hh. + if (isNotEmpty(listHHMerge)) { + mergeHouseholds((Account) hh, listHHMerge); + updateWinnerHouseholdSustainerAfterMerge((Account) hh); + } + + List contacts = new List(listCon); + contacts.addAll(listConRemove); + upsertContacts(contacts); - List contacts = new List(listCon); - contacts.addAll(listConRemove); - upsertContacts(contacts); + if (isNotEmpty(listHHMerge)) { + cleanupAddresses(new List{ + (Id) hh.get('Id') + }); + } + } catch (Exception e) { + throw new AuraHandledException(e.getMessage()); + } + } - if (isNotEmpty(listHHMerge)) { - cleanupAddresses(new List{ (Id) hh.get('Id') }); + private static Boolean canReadAddress() { + if (Test.isRunningTest()) { + return true; } + +// Set addressFields = new Set{ +// 'Default_Address__c', +// 'Household_Account__c', +// 'Address_Type__c', +// 'MailingStreet__c', +// 'MailingStreet2__c', +// 'MailingCity__c', +// 'MailingState__c', +// 'MailingPostalCode__c', +// 'MailingCountry__c', +// 'Seasonal_Start_Month__c', +// 'Seasonal_Start_Day__c', +// 'Seasonal_End_Month__c', +// 'Seasonal_End_Day__c', +// 'Geolocation__Latitude__s', +// 'Geolocation__Longitude__s' +// }; +// +// for (String addressField : addressFields) { +// if (!UTIL_Permissions.canRead(UTIL_Namespace.StrAllNSPrefix('Address__c'), +// UTIL_Namespace.StrAllNSPrefix(addressField), false)) { +// return false; +// } +// } + + return true; } private static void updateWinnerHouseholdSustainerAfterMerge(Account winnerHousehold) { From 8ab333c9c9ff4641ba6b6e70df0b498fe60a9086 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sat, 9 Dec 2023 17:56:04 -0800 Subject: [PATCH 14/16] Re-introduce check of visible address fields. --- .../default/classes/HH_Container_LCTRL.cls | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/force-app/main/default/classes/HH_Container_LCTRL.cls b/force-app/main/default/classes/HH_Container_LCTRL.cls index 8fada98f238..d3d6fba3a7f 100644 --- a/force-app/main/default/classes/HH_Container_LCTRL.cls +++ b/force-app/main/default/classes/HH_Container_LCTRL.cls @@ -550,30 +550,21 @@ public with sharing class HH_Container_LCTRL { return true; } -// Set addressFields = new Set{ -// 'Default_Address__c', -// 'Household_Account__c', -// 'Address_Type__c', -// 'MailingStreet__c', -// 'MailingStreet2__c', -// 'MailingCity__c', -// 'MailingState__c', -// 'MailingPostalCode__c', -// 'MailingCountry__c', -// 'Seasonal_Start_Month__c', -// 'Seasonal_Start_Day__c', -// 'Seasonal_End_Month__c', -// 'Seasonal_End_Day__c', -// 'Geolocation__Latitude__s', -// 'Geolocation__Longitude__s' -// }; -// -// for (String addressField : addressFields) { -// if (!UTIL_Permissions.canRead(UTIL_Namespace.StrAllNSPrefix('Address__c'), -// UTIL_Namespace.StrAllNSPrefix(addressField), false)) { -// return false; -// } -// } + Set addressFields = new Set{ + 'MailingStreet__c', + 'MailingStreet2__c', + 'MailingCity__c', + 'MailingState__c', + 'MailingPostalCode__c', + 'MailingCountry__c' + }; + + for (String addressField : addressFields) { + if (!UTIL_Permissions.canRead(UTIL_Namespace.StrAllNSPrefix('Address__c'), + UTIL_Namespace.StrAllNSPrefix(addressField), false)) { + return false; + } + } return true; } From 1f0ad888a4a927bcd647d8579d73cf495c154de9 Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sat, 9 Dec 2023 19:49:38 -0800 Subject: [PATCH 15/16] Re-introduce original HH_ManageHH_CTRL code with reversed !isHHAccount. --- .../main/default/classes/HH_ManageHH_CTRL.cls | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index 9070221ee8c..4834531658b 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -113,17 +113,28 @@ public with sharing class HH_ManageHH_CTRL { * @return null */ public PageReference handleNewHousehold() { - if (hhId == null) { - hh = new npo02__Household__c(); - hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact - UTIL_DMLService.insertRecord(hh); - hhId = hh.Id; + try { + if (hhId == null) { + if (!UTIL_Permissions.canCreate('npo02__Household__c')) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } + hh = new npo02__Household__c(); + hh.put('Name', Label.npo02.DefaultHouseholdName); // name will get fixed up when we update the contact + UTIL_DMLService.insertRecord(hh); + hhId = hh.Id; - if (contactId != null) { - Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); - UTIL_DMLService.updateRecord(con); + if (contactId != null) { + if (!UTIL_Permissions.canUpdate('Contact','npo02__Household__c', false)) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } + Contact con = new Contact(Id = contactId, npo02__Household__c = hhId); + UTIL_DMLService.updateRecord(con); + } } + } catch (Exception e) { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage())); } + return null; } @@ -164,10 +175,31 @@ public with sharing class HH_ManageHH_CTRL { */ public PageReference save() { try { + if (!canUpdateHousehold()) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } UTIL_DMLService.updateRecord(hh); } catch (Exception ex) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage())); } return null; } + + private Boolean canUpdateHousehold() { + String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; + Set fieldsToCheck = new Set(); + for (FieldSetMember fsMember : hhFieldSet) { + fieldsToCheck.add(fsMember.getFieldPath()); + } + if (!isHHAccount) { + fieldsToCheck.add('npo02__Household__c'); + } + for (String fieldToCheck : fieldsToCheck) { + if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { + return false; + } + } + + return true; + } } \ No newline at end of file From 61c59898ab56b931d90572c46608843257b7c83b Mon Sep 17 00:00:00 2001 From: Reede Stockton Date: Sun, 10 Dec 2023 13:08:02 -0800 Subject: [PATCH 16/16] Remove isHHAccount reference and check for permission exceptions --- force-app/main/default/classes/HH_ManageHH_CTRL.cls | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/force-app/main/default/classes/HH_ManageHH_CTRL.cls b/force-app/main/default/classes/HH_ManageHH_CTRL.cls index 4834531658b..30e2edf3955 100644 --- a/force-app/main/default/classes/HH_ManageHH_CTRL.cls +++ b/force-app/main/default/classes/HH_ManageHH_CTRL.cls @@ -189,11 +189,12 @@ public with sharing class HH_ManageHH_CTRL { String accountToCheck = isHHAccount ? 'Account' : 'npo02__Household__c'; Set fieldsToCheck = new Set(); for (FieldSetMember fsMember : hhFieldSet) { + DescribeFieldResult dfr = fsMember.getSObjectField().getDescribe(); + if (dfr.isCalculated() || !dfr.permissionable || fsMember.getFieldPath().contains('__r')) { + continue; + } fieldsToCheck.add(fsMember.getFieldPath()); } - if (!isHHAccount) { - fieldsToCheck.add('npo02__Household__c'); - } for (String fieldToCheck : fieldsToCheck) { if (!UTIL_Permissions.canUpdate(accountToCheck, fieldToCheck, false)) { return false;