Skip to content

Commit

Permalink
Merge pull request #6855 from SalesforceFoundation/feature/238__rd-pa…
Browse files Browse the repository at this point in the history
…yment-state

RD2: Consolidate Payment Widget State into App State object
  • Loading branch information
daniel-fuller authored Feb 21, 2022
2 parents 492a4c6 + af4ea36 commit 220fca5
Show file tree
Hide file tree
Showing 33 changed files with 1,657 additions and 1,131 deletions.
111 changes: 76 additions & 35 deletions force-app/main/default/classes/RD2_EntryFormController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ public with sharing class RD2_EntryFormController {
set;
}

/***
* @description Permissions class used to determine if the running user has permissions
*/
@TestVisible
private static UTIL_Permissions permissions {
get {
if (permissions == null) {
permissions = new UTIL_Permissions();
}
return permissions;
}
set;
}

/***
* @description Retrieves the record type id for the household account record type
*/
Expand All @@ -87,6 +101,13 @@ public with sharing class RD2_EntryFormController {
@AuraEnabled
public static RD2AppView getInitialView(Id parentId, Id recordId) {
RD2AppView view = new RD2AppView();

view.hasRequiredPermissions = hasRequiredFieldPermissions();
if(!view.hasRequiredPermissions) {
return view;
}


view.isAutoNamingEnabled = RD2_NamingService.isAutomaticNamingEnabled;
view.isMultiCurrencyEnabled = UserInfo.isMultiCurrencyOrganization();
view.parentSObjectType = (parentId == null) ? null : UTIL_Describe.objectNameFromId(parentId);
Expand Down Expand Up @@ -133,35 +154,50 @@ public with sharing class RD2_EntryFormController {
return UTIL_Describe.getLightningSelectOptions('npe03__Recurring_Donation__c','npe03__Installment_Period__c');
}


/**
* @description Gather the org, object and environment setting for Recurring Donation Entry/Edit Form
* @param parentId The parentId of the new RD
* @return Map<String, Object>
* @description Retrieve the donor address given an Account or Contact Id
* @param donorId Id of donor, Account or Contact
* @return Address__c of the Account's BillingAddress or Contact's MailingAddress
*/
@AuraEnabled(Cacheable=true)
public static Map<String, Object> getRecurringSettings(Id parentId) {
String parentSObjectType = (parentId == null)
? null
: parentId.getSobjectType().getDescribe().getName();

String defaultRecurringType = UTIL_Describe.getDefaultSelectOption(
'npe03__Recurring_Donation__c', String.valueOf(npe03__Recurring_Donation__c.RecurringType__c)
);
@AuraEnabled
public static Address__c getDonorAddress(Id donorId) {
if (donorId == null) {
return null;
}

return new Map<String, Object> {
'isAutoNamingEnabled' => RD2_NamingService.isAutomaticNamingEnabled,
'isMultiCurrencyEnabled' => UserInfo.isMultiCurrencyOrganization(),
'parentSObjectType' => parentSObjectType,
'InstallmentPeriodPermissions' => getFieldPermissionsMap('npe03__Installment_Period__c'),
'InstallmentFrequencyPermissions' => getFieldPermissionsMap(UTIL_Namespace.StrTokenNSPrefix('InstallmentFrequency__c')),
'customFieldSets' => getCustomFieldSectionFields(),
'isElevateCustomer' => RD2_ElevateIntegrationService.isIntegrationEnabled(),
'isChangeLogEnabled' => RD2_ChangeLogService.isChangeLogEnabled,
'periodToYearlyFrequencyMap' => RD2_Constants.PERIOD_TO_YEARLY_FREQUENCY,
'closedStatusValues' => RD2_StatusMapper.getInstance().getClosedStatusValues(),
'defaultRecurringType' => defaultRecurringType
};
SObjectType donorObjectType = donorId.getSobjectType();
if (!hasDonorAddressPermissions(donorObjectType)) {
AuraHandledException ex = new AuraHandledException(Label.commonPermissionErrorMessage);
ex.setMessage(Label.commonPermissionErrorMessage);
throw ex;
}

NPSP_Address donorAddress = new NPSP_Address(new Address__c());
if(donorObjectType == Account.SObjectType) {
Account donorAccount;
Map<Id, Account> accounts = Addresses.getAccountsByIds(new Set<Id>{donorId});
if (accounts != null) {
donorAddress.copyFromSObject(accounts.get(donorId), 'Billing', null);
}
} else if(donorObjectType == Contact.SObjectType) {
List<Contact> contacts = ContactSelector.getContactAddressFieldsForContactsIn(new Set<Id>{donorId});
if (contacts != null) {
donorAddress.copyFromSObject(contacts[0], 'Mailing', null);
}
}

Address__c address = donorAddress.getRecord();
return address;
}

private static Boolean hasDonorAddressPermissions(SObjectType donorObjectType) {
Boolean hasAccess = false;
if (donorObjectType == Account.SObjectType) {
hasAccess = permissions.canRead(Account.SObjectType, new Set<SObjectField>{Account.BillingAddress});
} else if(donorObjectType == Contact.SObjectType) {
hasAccess = permissions.canRead(Contact.SObjectType, new Set<SObjectField>{Contact.MailingAddress});
}
return hasAccess;
}

private static String getDonorType(npe03__Recurring_Donation__c rd) {
Expand All @@ -188,16 +224,16 @@ public with sharing class RD2_EntryFormController {
* false, the custom UI will display a warning message at the top of the page.
* @return True if the User has Read access to all required fields in the UI
*/
@AuraEnabled(Cacheable=true)
public static Boolean hasRequiredFieldPermissions() {
@TestVisible
private static Boolean hasRequiredFieldPermissions() {
Set<String> requiredFields = new Set<String>{
'npe03__Amount__c',
'npe03__Contact__c',
'npe03__Installment_Period__c',
'npe03__Date_Established__c',
UTIL_Namespace.StrTokenNSPrefix('InstallmentFrequency__c'),
UTIL_Namespace.StrTokenNSPrefix('Day_of_Month__c'),
UTIL_Namespace.StrTokenNSPrefix('StartDate__c')
String.valueOf(npe03__Recurring_Donation__c.npe03__Amount__c),
String.valueOf(npe03__Recurring_Donation__c.npe03__Contact__c),
String.valueOf(npe03__Recurring_Donation__c.npe03__Installment_Period__c),
String.valueOf(npe03__Recurring_Donation__c.npe03__Date_Established__c),
String.valueOf(npe03__Recurring_Donation__c.InstallmentFrequency__c),
String.valueOf(npe03__Recurring_Donation__c.Day_of_Month__c),
String.valueOf(npe03__Recurring_Donation__c.StartDate__c)
};

Boolean hasPermissions = true;
Expand Down Expand Up @@ -510,6 +546,7 @@ public with sharing class RD2_EntryFormController {

public class RD2RecordView {
@AuraEnabled public Id recordId;
@AuraEnabled public String recordName;
@AuraEnabled public String recurringStatus;
@AuraEnabled public Id contactId;
@AuraEnabled public Id accountId;
Expand All @@ -531,6 +568,7 @@ public with sharing class RD2_EntryFormController {
@AuraEnabled public String cardLastFour;
@AuraEnabled public String cardExpirationMonth;
@AuraEnabled public String cardExpirationYear;
@AuraEnabled public String paymentMethod;
@AuraEnabled public Map<String, Object> customFieldValues;

public RD2RecordView() {
Expand All @@ -546,6 +584,7 @@ public with sharing class RD2_EntryFormController {
this.contactId = record.npe03__Contact__c;
this.accountId = record.npe03__Organization__c;
this.recurringStatus = record.Status__c;
this.recordName = record.Name;
this.donorType = getDonorType(record);
this.dateEstablished = record.npe03__Date_Established__c;
this.donationValue = record.npe03__Amount__c;
Expand All @@ -556,6 +595,7 @@ public with sharing class RD2_EntryFormController {
this.dayOfMonth = record.Day_of_Month__c;
this.plannedInstallments = record.npe03__Installments__c?.intValue();
this.paidInstallments = record.npe03__Total_Paid_Installments__c?.intValue();
this.paymentMethod = record.PaymentMethod__c;
this.recurringType = record.RecurringType__c;
this.commitmentId = record.CommitmentId__c;
this.achLastFour = record.ACH_Last_4__c;
Expand Down Expand Up @@ -584,6 +624,7 @@ public with sharing class RD2_EntryFormController {
@AuraEnabled public String defaultInstallmentPeriod;
@AuraEnabled public List<Map<String, String>> installmentPeriodOptions;
@AuraEnabled public Id parentId;
@AuraEnabled public Boolean hasRequiredPermissions;

@AuraEnabled public RD2RecordView record;

Expand Down
Loading

0 comments on commit 220fca5

Please sign in to comment.