Skip to content

Commit

Permalink
21367: Added Controllers part
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Aug 29, 2024
1 parent bd70d79 commit df7f3ee
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,76 @@

package com.impactupgrade.nucleus.controller;

import com.impactupgrade.nucleus.entity.JobStatus;
import com.impactupgrade.nucleus.entity.JobType;
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.environment.EnvironmentFactory;
import com.impactupgrade.nucleus.service.segment.SynchronizationService;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Calendar;

@Path("/accounting")
public class AccountingController {

private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_FORMAT = "yyyy-MM-dd";

protected final EnvironmentFactory environmentFactory;
protected final EnvironmentFactory environmentFactory;

public AccountingController(EnvironmentFactory environmentFactory) {
this.environmentFactory = environmentFactory;
public AccountingController(EnvironmentFactory environmentFactory) {
this.environmentFactory = environmentFactory;
}

@GET
@Path("/sync/contacts/daily")
public Response syncDaily(@QueryParam("syncDays") Integer syncDays, @Context HttpServletRequest request) throws Exception {
Environment env = environmentFactory.init(request);

Calendar lastSync = Calendar.getInstance();
// run daily, but setting this high to catch previous misses
if (syncDays == null || syncDays <= 0) {
syncDays = 3;
}
lastSync.add(Calendar.DATE, -syncDays);

Runnable thread = () -> {
env.startJobLog(JobType.EVENT, null, "Contacts Sync: Daily", "Nucleus Portal");

SynchronizationService synchronizationService = env.synchronizationService("xero");
try {
synchronizationService.syncContacts(lastSync);
env.logJobInfo("{}: sync unsubscribes done", synchronizationService.name());
env.endJobLog(JobStatus.DONE);
} catch (Exception e) {
env.logJobError("contact sync failed for {}", synchronizationService.name(), e);
env.logJobError(e.getMessage());
env.endJobLog(JobStatus.FAILED);
}
};
new Thread(thread).start();

@Path("/sync-transactions")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response syncTransactions(
@FormParam("start_date") String start,
@FormParam("end_date") String end,
@Context HttpServletRequest request
) throws Exception {
return Response.ok().build();
}

@Path("/sync-transactions")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response syncTransactions(
@FormParam("start_date") String start,
@FormParam("end_date") String end,
@Context HttpServletRequest request
) throws Exception {
// Environment environment = environmentFactory.init(request);
// SecurityUtil.verifyApiKey(environment);
//
Expand Down Expand Up @@ -83,7 +121,6 @@ public Response syncTransactions(
// // Away from the main thread
// new Thread(runnable).start();

return Response.ok().build();
}

return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.impactupgrade.nucleus.model.PaymentGatewayTransaction;
import com.impactupgrade.nucleus.security.SecurityUtil;
import com.impactupgrade.nucleus.service.segment.PaymentGatewayService;
import com.impactupgrade.nucleus.service.segment.SynchronizationService;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -27,6 +28,7 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -353,4 +355,34 @@ public Response verifyAndReplayDeposits(

return Response.status(200).build();
}

@GET
@Path("/sync/contacts/daily")
public Response syncDaily(@QueryParam("syncDays") Integer syncDays, @Context HttpServletRequest request) throws Exception {
Environment env = envFactory.init(request);

Calendar lastSync = Calendar.getInstance();
if (syncDays == null || syncDays <= 0) {
syncDays = 3;
}
lastSync.add(Calendar.DATE, -syncDays);

Runnable thread = () -> {
env.startJobLog(JobType.EVENT, null, "Contacts Sync: Daily", "Nucleus Portal");

SynchronizationService synchronizationService = env.synchronizationService("stripe");
try {
synchronizationService.syncContacts(lastSync);
env.logJobInfo("{}: sync unsubscribes done", synchronizationService.name());
env.endJobLog(JobStatus.DONE);
} catch (Exception e) {
env.logJobError("contact sync failed for {}", synchronizationService.name(), e);
env.logJobError(e.getMessage());
env.endJobLog(JobStatus.FAILED);
}
};
new Thread(thread).start();

return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.impactupgrade.nucleus.service.segment.BareCrmService;
import com.impactupgrade.nucleus.service.segment.PaymentGatewayService;
import com.impactupgrade.nucleus.service.segment.SegmentService;
import com.impactupgrade.nucleus.service.segment.SynchronizationService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.map.CaseInsensitiveMap;
import org.apache.http.client.utils.URLEncodedUtils;
Expand Down Expand Up @@ -205,6 +206,10 @@ public CommunicationService communicationService(String name) {
return segmentService(name, CommunicationService.class);
}

public SynchronizationService synchronizationService(String name) {
return segmentService(name, SynchronizationService.class);
}

public EnrichmentService enrichmentService(String name) {
return segmentService(name, EnrichmentService.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.impactupgrade.nucleus.service.segment;

import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.model.CrmContact;
import com.impactupgrade.nucleus.model.PagedResults;

import java.util.Calendar;

public class StripeSynchronizationService implements SynchronizationService {

protected Environment env;

@Override
public String name() {
return "stripeSync";
}

@Override
public boolean isConfigured(Environment env) {
return true;
}

@Override
public void init(Environment env) {
}

@Override
public void syncContacts(Calendar updatedAfter) throws Exception {
PagedResults<CrmContact> contactPagedResults = env.primaryCrmService().getDonatingContacts(updatedAfter);
for (PagedResults.ResultSet<CrmContact> resultSet : contactPagedResults.getResultSets()) {
//TODO: bulk update?
for (CrmContact crmContact : resultSet.getRecords()) {
//TODO: update contact in Xero
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ public boolean isConfigured(Environment env) {
public void init(Environment env) {
}

// The method can/should be moved to an abstract class
// However, it depends on whether downstream platform support bulk contact processing or not -
// may be bulk vs 1 by 1 processing
//TODO: move to abstract class?
@Override
public void syncContacts(Calendar updatedAfter) throws Exception {
PagedResults<CrmContact> contactPagedResults = env.primaryCrmService().getDonatingContacts(updatedAfter);

for (PagedResults.ResultSet<CrmContact> resultSet : contactPagedResults.getResultSets()) {
//TODO: bulk update?
for (CrmContact crmContact : resultSet.getRecords()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.impactupgrade.nucleus.service.segment.StripeSynchronizationService
com.impactupgrade.nucleus.service.segment.XeroSynchronizationService

0 comments on commit df7f3ee

Please sign in to comment.