Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBT/PBT bulk endpoints #225

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.impactupgrade.nucleus.client;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.collect.Lists;
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.environment.EnvironmentConfig;
import com.impactupgrade.nucleus.model.CrmContact;
Expand All @@ -17,6 +18,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.impactupgrade.nucleus.util.HttpClient.get;
import static com.impactupgrade.nucleus.util.HttpClient.isOk;
Expand All @@ -25,11 +27,27 @@

public class MinistryByTextClient extends OAuthClient {

protected static String AUTH_ENDPOINT = "https://login.ministrybytext.com/connect/token";
protected static String API_ENDPOINT_BASE = "https://api.ministrybytext.com/";
protected static String AUTH_URL_PRODUCTION = "https://login.ministrybytext.com/connect/token";
protected static String AUTH_URL_SANDBOX = "https://login-qa.poweredbytext.com/connect/token";
protected static String API_BASE_URL_PRODUCTION = "https://api.ministrybytext.com";
protected static String API_BASE_URL_SANDBOX = "https://api-qa.poweredbytext.com";
protected static Integer BULK_API_LIMIT = 100;

protected final EnvironmentConfig.MBT mbtConfig;

protected static final String AUTH_URL;
protected static final String API_BASE_URL;
static {
String profile = System.getenv("PROFILE");
if ("production".equalsIgnoreCase(profile)) {
AUTH_URL = AUTH_URL_PRODUCTION;
API_BASE_URL = API_BASE_URL_PRODUCTION;
} else {
AUTH_URL = AUTH_URL_SANDBOX;
API_BASE_URL = API_BASE_URL_SANDBOX;
}
}

public MinistryByTextClient(EnvironmentConfig.MBT mbtConfig, Environment env) {
super("ministrybytext", env);
this.mbtConfig = mbtConfig;
Expand All @@ -49,25 +67,63 @@ protected JSONObject getClientConfigJson(JSONObject envJson) {

@Override
protected OAuthContext oAuthContext() {
return new ClientCredentialsOAuthContext(mbtConfig, AUTH_ENDPOINT, false);
return new ClientCredentialsOAuthContext(mbtConfig, AUTH_URL, false);
}

public List<Group> getGroups(EnvironmentConfig.MBT mbtConfig) {
return get(API_ENDPOINT_BASE + "campuses/" + mbtConfig.campusId + "/groups", headers(), new GenericType<>() {});
return get(API_BASE_URL + "campuses/" + mbtConfig.campusId + "/groups", headers(), new GenericType<>() {});
}

public Subscriber upsertSubscriber(CrmContact crmContact, EnvironmentConfig.MBT mbtConfig, EnvironmentConfig.CommunicationList communicationList) {
Subscriber subscriber = toMBTSubscriber(crmContact);
return post(API_ENDPOINT_BASE + "campuses/" + mbtConfig.campusId + "/groups/" + communicationList.id + "/subscribers", subscriber, APPLICATION_JSON, headers(), Subscriber.class);
return post(API_BASE_URL + "campuses/" + mbtConfig.campusId + "/groups/" + communicationList.id + "/subscribers", subscriber, APPLICATION_JSON, headers(), Subscriber.class);
}

public List<Subscriber> upsertSubscribersBulk(List<CrmContact> crmContacts, EnvironmentConfig.MBT mbtConfig, EnvironmentConfig.CommunicationList communicationList) {
List<Subscriber> subscribers = crmContacts.stream()
.map(this::toMBTSubscriber)
.collect(Collectors.toList());
List<List<Subscriber>> subscribersBatches = Lists.partition(subscribers, BULK_API_LIMIT);
int i = 0;
for (List<Subscriber> subscribersBatch: subscribersBatches) {
env.logJobInfo("Processing subscribers batch {} of total {}...", i++, subscribersBatches.size());
BulkOperationResponse bulkOperationResponse = post(API_BASE_URL + "/campuses/" + mbtConfig.campusId + "/groups/" + communicationList.id + "/new-subscribers/bulk",
subscribersBatch, APPLICATION_JSON, headers(), BulkOperationResponse.class);
if (bulkOperationResponse != null && !bulkOperationResponse.isError) {
env.logJobInfo("Submitted subscribers batch. Batch id={}", bulkOperationResponse.data.batchId);
} else {
env.logJobWarn("Failed to process subscribers batch {} of total {}! Error message={}", i, subscribersBatches.size(), bulkOperationResponse.message);
}
}
return subscribers;
}

public void upsertNotificationSetting(NotificationSetting notificationSetting,
EnvironmentConfig.MBT mbtConfig, EnvironmentConfig.CommunicationList communicationList) throws IOException, InterruptedException {
patch(API_ENDPOINT_BASE + "campuses/" + mbtConfig.campusId + "/groups/" + communicationList.id + "/notification-url", notificationSetting, APPLICATION_JSON, headers());
patch(API_BASE_URL + "campuses/" + mbtConfig.campusId + "/groups/" + communicationList.id + "/notification-url", notificationSetting, APPLICATION_JSON, headers());
}

public NotificationSettingResponse getNotificationSetting(EnvironmentConfig.MBT mbtConfig, EnvironmentConfig.CommunicationList communicationList) throws IOException, InterruptedException {
return get(API_ENDPOINT_BASE + "campuses/" + mbtConfig.campusId + "/groups/" + communicationList.id + "/notification-url", headers(), new GenericType<>() {});
return get(API_BASE_URL + "campuses/" + mbtConfig.campusId + "/groups/" + communicationList.id + "/notification-url", headers(), new GenericType<>() {});
}

public List<Contact> upsertContactsBulk(List<CrmContact> crmContacts, EnvironmentConfig.MBT mbtConfig) {
List<Contact> contacts = crmContacts.stream()
.map(this::toMBTContact)
.collect(Collectors.toList());
List<List<Contact>> contactsBatches = Lists.partition(contacts, BULK_API_LIMIT);
int i = 0;
for (List<Contact> contactsBatch: contactsBatches) {
env.logJobInfo("Processing contacts batch {} of total {}...", i++, contactsBatches.size());
BulkOperationResponse bulkOperationResponse = post(API_BASE_URL + "/campuses/" + mbtConfig.campusId + "/contacts/bulk",
contactsBatch, APPLICATION_JSON, headers(), BulkOperationResponse.class);
if (bulkOperationResponse != null && !bulkOperationResponse.isError) {
env.logJobInfo("Submitted contacts batch. Batch id={}", bulkOperationResponse.data.batchId);
} else {
env.logJobWarn("Failed to process contacts batch {} of total {}! Error message={}", i, contactsBatches.size(), bulkOperationResponse.message);
}
}
return contacts;
}

// Having to modify this due to MBT's limited API. There's no upsert concept, and we want to avoid having to retrieve
Expand Down Expand Up @@ -99,6 +155,17 @@ protected Subscriber toMBTSubscriber(CrmContact crmContact) {
return subscriber;
}

protected Contact toMBTContact(CrmContact crmContact) {
Contact contact = new Contact();
contact.firstName = crmContact.firstName;
contact.lastName = crmContact.lastName;
//contact.gender = crmContact ?
contact.mobileNo = crmContact.mobilePhone;
// TODO: address, using mailing or billing
//contact.region = crmContact ?
return contact;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Group {
public String id;
Expand Down Expand Up @@ -190,4 +257,43 @@ public String toString() {
'}';
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Contact {
public String firstName;
public String lastName;
public String gender;
public String mobileNo;
public SubscriberAddress address;
public String region;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class BulkOperationResponse {
public String message;
public boolean isError;
public String appCode;
public Data data;

@Override
public String toString() {
return "BulkOperationResponse{" +
"message='" + message + '\'' +
", isError=" + isError +
", appCode='" + appCode + '\'' +
", data=" + data +
'}';
}
}

public static class Data {
public String batchId;

@Override
public String toString() {
return "Data{" +
"batchId='" + batchId + '\'' +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import com.impactupgrade.nucleus.model.CrmContact;
import com.impactupgrade.nucleus.model.PagedResults;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -45,14 +47,18 @@ public void syncContacts(Calendar lastSync) throws Exception {

PagedResults<CrmContact> pagedResults = env.primaryCrmService().getSmsContacts(lastSync, communicationList);
for (PagedResults.ResultSet<CrmContact> resultSet : pagedResults.getResultSets()) {

List<CrmContact> crmContacts = new ArrayList<>();
for (CrmContact crmContact : resultSet.getRecords()) {
String smsPn = crmContact.phoneNumberForSMS();
if (!Strings.isNullOrEmpty(smsPn) && !seenPhones.contains(smsPn)) {
env.logJobInfo("upserting contact {} {} on list {}", crmContact.id, crmContact.phoneNumberForSMS(), communicationList.id);
mbtClient.upsertSubscriber(crmContact, mbtConfig, communicationList);
env.logJobInfo("upserting contact {} {} on list {}", crmContact.id, smsPn, communicationList.id);
crmContacts.add(crmContact);
seenPhones.add(smsPn);
}
}

mbtClient.upsertSubscribersBulk(crmContacts, mbtConfig, communicationList);
}
}
}
Expand Down
Loading