Skip to content

Commit

Permalink
Revert "18623: Organisations not integrating to Xero"
Browse files Browse the repository at this point in the history
This reverts commit e77c455.
  • Loading branch information
brmeyer committed Jul 8, 2024
1 parent 08627e8 commit dcaa1b6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class AccountingTransaction {

// TODO: Rename? "Contact" in Xero, but something else in QB?
public String contactId;
public String crmAccountId;
public String crmContactId;

public Double amountInDollars;
Expand All @@ -29,7 +28,6 @@ public class AccountingTransaction {

public AccountingTransaction(
String contactId,
String crmAccountId,
String crmContactId,
Double amountInDollars,
ZonedDateTime date,
Expand All @@ -40,7 +38,6 @@ public AccountingTransaction(
Boolean recurring
) {
this.contactId = contactId;
this.crmAccountId = crmAccountId;
this.crmContactId = crmContactId;

this.amountInDollars = amountInDollars;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,49 +47,55 @@ public void processTransaction(PaymentGatewayEvent paymentGatewayEvent) throws E
return;
}

processDonation(paymentGatewayEvent.getCrmDonation());
processTransaction(paymentGatewayEvent.getCrmDonation());

for (CrmDonation child : paymentGatewayEvent.getCrmDonation().children) {
processDonation(child);
processTransaction(child);
}
}

protected void processDonation(CrmDonation crmDonation) throws Exception {
CrmAccount crmAccount = getDonationAccount(crmDonation);
protected void processTransaction(CrmDonation crmDonation) throws Exception {
CrmContact crmContact = getDonationContact(crmDonation);
if (crmContact == null) {
// Should be unreachable
env.logJobWarn("Failed to find crm contact for crm donation {}!", crmDonation.id);
return;
}

//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(crmContact);
if (!Strings.isNullOrEmpty(contactId)) {
env.logJobInfo("Upserted contact: {}", contactId);

AccountingTransaction accountingTransaction = toAccountingTransaction(crmDonation, contactId, crmAccount.id, crmContact.id);
AccountingTransaction accountingTransaction = toAccountingTransaction(crmDonation, contactId, crmContact.id);
String transactionId = _accountingPlatformService.get().createTransaction(accountingTransaction);
env.logJobInfo("Created transaction: {}", transactionId);
}
}

private CrmAccount getDonationAccount(CrmDonation crmDonation) throws Exception {
if (crmDonation.account == null || StringUtils.isEmpty(crmDonation.account.id)) {
return null;
}
// We retrieve the full objects from the CRM here,
// in case they have additional info over what was in the payment event.
return env.donationsCrmService().getAccountById(crmDonation.account.id).orElse(null);
}

private CrmContact getDonationContact(CrmDonation crmDonation) throws Exception {
if (crmDonation.contact == null || StringUtils.isEmpty(crmDonation.contact.id)) {
return null;
}
// We retrieve the full objects from the CRM here, in case they have additional info over what was in
// the payment event.
return env.donationsCrmService().getContactById(crmDonation.contact.id).orElse(null);
CrmContact crmContact = null;
if (!StringUtils.isEmpty(crmDonation.account.id)) {
CrmAccount crmAccount = env.donationsCrmService().getAccountById(crmDonation.account.id).orElse(null);
// create a faux contact for org type account
if (crmAccount != null && crmAccount.recordType == EnvironmentConfig.AccountType.ORGANIZATION) {
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;
}
}
if (crmContact == null) {
if (!StringUtils.isEmpty(crmDonation.contact.id)) {
// Get non-org contact
crmContact = env.donationsCrmService().getContactById(crmDonation.contact.id).orElse(null);
}
}
return crmContact;
}

// public void addDeposits(List<PaymentGatewayDeposit> paymentGatewayDeposits) {
Expand Down Expand Up @@ -231,10 +237,9 @@ private CrmContact getDonationContact(CrmDonation crmDonation) throws Exception
// }).collect(Collectors.toList());
// }

private AccountingTransaction toAccountingTransaction(CrmDonation crmDonation, String accountingContactId, String crmAccountId, String crmContactId) {
private AccountingTransaction toAccountingTransaction(CrmDonation crmDonation, String accountingContactId, String crmContactId) {
return new AccountingTransaction(
accountingContactId,
crmAccountId,
crmContactId,
crmDonation.amount,
crmDonation.closeDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package com.impactupgrade.nucleus.service.segment;

import com.impactupgrade.nucleus.model.AccountingTransaction;
import com.impactupgrade.nucleus.model.CrmAccount;
import com.impactupgrade.nucleus.model.CrmContact;
import com.impactupgrade.nucleus.model.CrmDonation;

Expand All @@ -15,7 +14,7 @@ public interface AccountingPlatformService extends SegmentService {

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

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

String createTransaction(AccountingTransaction accountingTransaction) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.environment.EnvironmentConfig;
import com.impactupgrade.nucleus.model.AccountingTransaction;
import com.impactupgrade.nucleus.model.CrmAccount;
import com.impactupgrade.nucleus.model.CrmContact;
import com.impactupgrade.nucleus.model.CrmDonation;
import com.impactupgrade.nucleus.service.logic.NotificationService;
Expand All @@ -30,7 +29,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 +153,10 @@ 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(CrmContact crmContact) throws Exception {
Contact contact = toContact(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 Down Expand Up @@ -436,46 +425,6 @@ protected String getExceptionDetails(Exception e) {
}

// Mappings
protected Contact toOrgContact(CrmAccount crmAccount, CrmContact crmContact) {
if (crmAccount == null) {
return null;
}
Contact contact = new Contact();
contact.setName(crmAccount.name);
contact.setEmailAddress(crmAccount.email);
if (!Strings.isNullOrEmpty(crmAccount.billingAddress.street)) {
Address address = new Address()
.addressLine1(crmAccount.billingAddress.street)
.city(crmAccount.billingAddress.city)
.region(crmAccount.billingAddress.state)
.postalCode(crmAccount.billingAddress.postalCode)
.country(crmAccount.billingAddress.country);
contact.setAddresses(List.of(address));
}

// 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) {
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));

// TODO: temp
env.logJobInfo("account {} {}; contact {} {} {} {} {}",
crmAccount.id, crmAccount.name,
contact.getFirstName(), contact.getLastName(), contact.getName(), contact.getEmailAddress(), contact.getAccountNumber());

return contact;
}

protected Contact toContact(CrmContact crmContact) {
if (crmContact == null) {
return null;
Expand Down Expand Up @@ -568,7 +517,6 @@ protected AccountingTransaction toAccountingTransaction(Invoice invoice) {
null,
null,
null,
null,
getPaymentGatewayTransactionId(invoice),
null
);
Expand Down

0 comments on commit dcaa1b6

Please sign in to comment.