Skip to content

Commit

Permalink
Code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor authored and brmeyer committed Jun 28, 2024
1 parent 1925047 commit 7dcb0f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void processDonation(CrmDonation crmDonation) throws Exception {
}

//TODO: need to decide if we need to keep "account >> faux contact" concept here of move it to downstream
String contactId = _accountingPlatformService.get().updateOrCreateContact(Optional.of(crmAccount), crmContact);
String contactId = _accountingPlatformService.get().updateOrCreateContact(crmAccount, Optional.of(crmContact));
if (!Strings.isNullOrEmpty(contactId)) {
env.logJobInfo("Upserted contact: {}", contactId);

Expand Down Expand Up @@ -92,6 +92,21 @@ private CrmContact getDonationContact(CrmDonation crmDonation) throws Exception
return env.donationsCrmService().getContactById(crmDonation.contact.id).orElse(null);
}

private CrmContact asCrmContact(CrmAccount crmAccount) {
if (crmAccount != null && crmAccount.recordType == EnvironmentConfig.AccountType.ORGANIZATION) {
CrmContact crmContact = new CrmContact();
crmContact.id = crmAccount.id;
String[] firstLastName = Utils.fullNameToFirstLast(crmAccount.name);
crmContact.firstName = firstLastName[0];
crmContact.lastName = firstLastName[1];
crmContact.mailingAddress = crmAccount.billingAddress;
crmContact.crmRawObject = crmAccount.crmRawObject;
return crmContact;
} else {
return null;
}
}

// public void addDeposits(List<PaymentGatewayDeposit> paymentGatewayDeposits) {
// if (CollectionUtils.isEmpty(accountingPlatformServices)) {
// env.logJobInfo("Accounting Platform Services not defined for environment! Returning...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface AccountingPlatformService extends SegmentService {

Optional<AccountingTransaction> getTransaction(CrmDonation crmDonation) throws Exception;

String updateOrCreateContact(Optional<CrmAccount> crmAccount, CrmContact crmContact) throws Exception;
String updateOrCreateContact(CrmAccount crmAccount, Optional<CrmContact> crmContact) throws Exception;

String createTransaction(AccountingTransaction accountingTransaction) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.xero.api.client.AccountingApi;
import com.xero.models.accounting.Address;
import com.xero.models.accounting.Contact;
import com.xero.models.accounting.ContactPerson;
import com.xero.models.accounting.Contacts;
import com.xero.models.accounting.Element;
import com.xero.models.accounting.Invoice;
Expand Down Expand Up @@ -155,19 +154,11 @@ protected String getReference(CrmDonation crmDonation) {
}

@Override
public String updateOrCreateContact(Optional<CrmAccount> crmAccount, CrmContact crmContact) throws Exception {
Contact contact;
if (crmAccount.isPresent()) {
// Organization Contact with Contact Persons
contact = toOrgContact(crmAccount.get(), crmContact);
} else {
// Simple contact
contact = toContact(crmContact);
}

public String updateOrCreateContact(CrmAccount crmAccount, Optional<CrmContact> crmContact) throws Exception {
// Organization Contact with Contact Persons
Contact contact = toOrgContact(crmAccount, crmContact);
Contacts contacts = new Contacts();
contacts.setContacts(List.of(contact));

try {
// This method works very similar to POST Contacts (xeroApi.updateOrCreateContacts)
// but if an existing contact matches our ContactName or ContactNumber then we will receive an error
Expand All @@ -180,14 +171,16 @@ public String updateOrCreateContact(Optional<CrmAccount> crmAccount, CrmContact
for (Element element : e.getElements()) {
if (element.getValidationErrors().stream().anyMatch(error -> error.getMessage().contains("Account Number already exists"))) {
// TODO: Same as toContact -- DR specific, SFDC specific, etc.
if (crmContact.crmRawObject instanceof SObject sObject) {
// TODO: decide if the field should be taken from contact or account
if (crmContact.isPresent() && crmContact.get().crmRawObject instanceof SObject sObject) {
String supporterId = (String) sObject.getField(SUPPORTER_ID_FIELD_NAME);
return getContactForAccountNumber(supporterId).map(c -> c.getContactID().toString()).orElse(null);
}
}
if (element.getValidationErrors().stream().anyMatch(error -> error.getMessage().contains("contact name must be unique across all active contacts"))) {
// TODO: Same as toContact -- DR specific, SFDC specific, etc.
if (crmContact.crmRawObject instanceof SObject sObject) {
// TODO: decide if the field should be taken from contact or account
if (crmContact.isPresent() && crmContact.get().crmRawObject instanceof SObject sObject) {
String supporterId = (String) sObject.getField(SUPPORTER_ID_FIELD_NAME);
return getContactForName(contact.getName()).map(c -> {
// A few contacts have been entered manually without an Account Number being set.
Expand All @@ -197,10 +190,10 @@ public String updateOrCreateContact(Optional<CrmAccount> crmAccount, CrmContact
} else {
// Send notification if name already exists for different supporter id (account number)
try {
env.logJobInfo("Sending notification for duplicated contact name '{}'...", crmContact.getFullName());
env.logJobInfo("Sending notification for duplicated contact name '{}'...", contact.getName());
NotificationService.Notification notification = new NotificationService.Notification(
"Xero: Contact name already exists",
"Xero: Contact with name '" + crmContact.getFullName() + "' already exists. Supporter ID: " + supporterId + "."
"Xero: Contact with name '" + contact.getName() + "' already exists. Supporter ID: " + supporterId + "."
);
env.notificationService().sendNotification(notification, "xero:contact-name-exists");
} catch (Exception ex) {
Expand Down Expand Up @@ -436,7 +429,7 @@ protected String getExceptionDetails(Exception e) {
}

// Mappings
protected Contact toOrgContact(CrmAccount crmAccount, CrmContact crmContact) {
protected Contact toOrgContact(CrmAccount crmAccount, Optional<CrmContact> crmContact) {
if (crmAccount == null) {
return null;
}
Expand All @@ -456,17 +449,17 @@ protected Contact toOrgContact(CrmAccount crmAccount, CrmContact crmContact) {
// TODO: make this part not-sfdc specific?
// TODO: SUPPORTER_ID_FIELD_NAME is DR specific
// TODO: decide if the field should be taken from contact or account
if (crmContact.crmRawObject instanceof SObject sObject) {
if (crmContact.isPresent() && crmContact.get().crmRawObject instanceof SObject sObject) {
String supporterId = (String) sObject.getField(SUPPORTER_ID_FIELD_NAME);
contact.setAccountNumber(supporterId);
}

ContactPerson contactPerson = new ContactPerson();
contactPerson.setFirstName(crmContact.firstName);
contactPerson.setLastName(crmContact.lastName);
contactPerson.setEmailAddress(crmContact.email);

contact.setContactPersons(List.of(contactPerson));
// ContactPerson contactPerson = new ContactPerson();
// contactPerson.setFirstName(crmContact.firstName);
// contactPerson.setLastName(crmContact.lastName);
// contactPerson.setEmailAddress(crmContact.email);
//
// contact.setContactPersons(List.of(contactPerson));

// TODO: temp
env.logJobInfo("account {} {}; contact {} {} {} {} {}",
Expand Down

0 comments on commit 7dcb0f9

Please sign in to comment.