Skip to content

Commit

Permalink
adds ability to transform payload in destination
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbranham committed Aug 17, 2024
1 parent 1383b8f commit 0270991
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 29 deletions.
27 changes: 21 additions & 6 deletions module-flow/src/main/java/net/savantly/nexus/flow/FlowModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.savantly.nexus.flow.dom.connections.datasource.DatasourceFactory;
import net.savantly.nexus.flow.dom.connections.flowHook.FlowDestinationHookFactory;
import net.savantly.nexus.flow.dom.connections.jdbcConnection.JdbcConnections;
import net.savantly.nexus.flow.dom.connections.webhook.WebhookDestinationHookFactory;
import net.savantly.nexus.flow.dom.destination.DestinationHookFactory;
import net.savantly.nexus.flow.dom.emailTarget.EmailDestinationHookFactory;
import net.savantly.nexus.flow.dom.emailTarget.EmailTargets;
Expand Down Expand Up @@ -116,13 +117,24 @@ public FlowDestinationHookFactory flow_flowDestinationHookFactory(FlowDefinition
return new FlowDestinationHookFactory(flowDefinitions, flowDefinitionExecutionProxy);
}

@Bean
public WebhookDestinationHookFactory flow_webhookDestinationHookFactory(Webhooks webhooks,
FlowContextFactory flowContextFactory, JavascriptExecutor javascriptExecutor,
RepositoryService repositoryService, RestTemplateBuilder restTemplateBuilder,
ObjectMapper objectMapper) {
return new WebhookDestinationHookFactory(flowContextFactory, javascriptExecutor, repositoryService, webhooks,
restTemplateBuilder, objectMapper);
}

@Bean
public DestinationHookFactory flow_destinationHookFactory(ObjectMapper objectMapper,
DatasourceFactory datasourceFactory, JdbcConnections jdbcConnections, Webhooks webhooks,
RestTemplateBuilder restTemplateBuilder, FlowDestinationHookFactory flowDestinationHookFactory,
EmailDestinationHookFactory emailDestinationHookFactory) {
return new DestinationHookFactory(objectMapper, datasourceFactory, jdbcConnections, webhooks,
restTemplateBuilder, flowDestinationHookFactory, emailDestinationHookFactory);
EmailDestinationHookFactory emailDestinationHookFactory,
WebhookDestinationHookFactory webhookDestinationHookFactory) {
return new DestinationHookFactory(objectMapper, datasourceFactory, jdbcConnections,
restTemplateBuilder, flowDestinationHookFactory, emailDestinationHookFactory,
webhookDestinationHookFactory);
}

@Bean
Expand Down Expand Up @@ -159,7 +171,8 @@ public FlowContextFactory flow_flowContextFactory(OrganizationSecrets flowSecret
@Bean
public FlowDefinitionExecutionProxy flow_flowDefinitionExecutionProxy(FlowExecutorFactory flowExecutorFactory,
FlowDefinitionRepository flowDefinitionRepository, ObjectMapper objectMapper,
RepositoryService repositoryService, OrganizationSecretRepository secretRepository, FlowContextFactory flowContextFactory) {
RepositoryService repositoryService, OrganizationSecretRepository secretRepository,
FlowContextFactory flowContextFactory) {
return new FlowDefinitionExecutionProxy(flowExecutorFactory, flowDefinitionRepository, objectMapper,
repositoryService, flowContextFactory);
}
Expand Down Expand Up @@ -190,8 +203,10 @@ public ReCaptchaService flow_reCaptchaService(ReCaptchaAttemptService reCaptchaA

@Bean
public EmailDestinationHookFactory flow_emailDestinationHookFactory(EmailService emailService,
EmailTargets emailTargets, JavascriptExecutor javascriptExecutor, FlowContextFactory flowContextFactory, RepositoryService repositoryService) {
return new EmailDestinationHookFactory(emailService, emailTargets, javascriptExecutor, flowContextFactory, repositoryService);
EmailTargets emailTargets, JavascriptExecutor javascriptExecutor, FlowContextFactory flowContextFactory,
RepositoryService repositoryService, ObjectMapper objectMapper) {
return new EmailDestinationHookFactory(emailService, emailTargets, javascriptExecutor, flowContextFactory,
repositoryService, objectMapper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,43 @@
import java.util.Map;
import java.util.Objects;

import org.apache.causeway.applib.services.repository.RepositoryService;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;

import lombok.RequiredArgsConstructor;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.destination.AbstractBaseDestinationHook;
import net.savantly.nexus.flow.dom.destination.Destination;
import net.savantly.nexus.flow.dom.destination.DestinationHook;
import net.savantly.nexus.flow.dom.destination.DestinationHookResponse;
import net.savantly.nexus.flow.dom.flowContext.FlowContextFactory;
import net.savantly.nexus.flow.dom.formMapping.Mapping;
import net.savantly.nexus.flow.executor.javascript.JavascriptExecutor;
import net.savantly.nexus.webhooks.dom.webhook.Webhooks;

@Log4j2
@RequiredArgsConstructor
public class WebhookDestinationHook implements DestinationHook {
public class WebhookDestinationHook extends AbstractBaseDestinationHook {

final private Webhooks webhooks;
final RestTemplateBuilder restTemplateBuilder;

public WebhookDestinationHook(FlowContextFactory flowContextFactory, JavascriptExecutor javascriptExecutor,
RepositoryService repositoryService, Webhooks webhooks, RestTemplateBuilder restTemplateBuilder,
ObjectMapper objectMapper) {
super(flowContextFactory, javascriptExecutor, repositoryService, objectMapper);
this.webhooks = webhooks;
this.restTemplateBuilder = restTemplateBuilder;
}

@Override
public void close() throws Exception {
}

@Override
public DestinationHookResponse execute(Destination destination, Map<String, Object> payload,
public DestinationHookResponse sendData(Destination destination, Map<String, Object> payload,
Collection<? extends Mapping> formMappings) {
var webhook = webhooks.findById(destination.getDestinationId()).orElseThrow();

Expand All @@ -40,6 +55,7 @@ public DestinationHookResponse execute(Destination destination, Map<String, Obje

var template = restTemplateBuilder.build();
var method = HttpMethod.valueOf(webhook.getMethod().name());

var request = RequestEntity.method(method, webhook.getUrl()).headers(headers).body(payload);

log.info("Sending webhook request: {}", request);
Expand All @@ -49,8 +65,4 @@ public DestinationHookResponse execute(Destination destination, Map<String, Obje
.setMessage(response.getBody());
}

@Override
public void close() throws Exception {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.savantly.nexus.flow.dom.connections.webhook;

import org.apache.causeway.applib.services.repository.RepositoryService;
import org.springframework.boot.web.client.RestTemplateBuilder;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;
import net.savantly.nexus.flow.dom.flowContext.FlowContextFactory;
import net.savantly.nexus.flow.executor.javascript.JavascriptExecutor;
import net.savantly.nexus.webhooks.dom.webhook.Webhooks;

@RequiredArgsConstructor
public class WebhookDestinationHookFactory {

private final FlowContextFactory flowContextFactory;
private final JavascriptExecutor javascriptExecutor;
private final RepositoryService repositoryService;
private final Webhooks webhooks;
private final RestTemplateBuilder restTemplateBuilder;
private final ObjectMapper objectMapper;

public WebhookDestinationHook createHook() {
return new WebhookDestinationHook(flowContextFactory, javascriptExecutor, repositoryService, webhooks,
restTemplateBuilder, objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

import org.apache.causeway.applib.services.repository.RepositoryService;

import com.caoccao.javet.enums.V8ValueReferenceType;
import com.caoccao.javet.values.reference.V8ValueObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.destinationExecution.DestinationExecution;
Expand All @@ -19,6 +24,7 @@ public abstract class AbstractBaseDestinationHook implements DestinationHook {
private final FlowContextFactory flowContextFactory;
private final JavascriptExecutor javascriptExecutor;
private final RepositoryService repositoryService;
private final ObjectMapper objectMapper;

public abstract DestinationHookResponse sendData(Destination destination, Map<String, Object> payload,
Collection<? extends Mapping> formMappings);
Expand All @@ -32,8 +38,17 @@ protected Map<String, Object> transformPayload(Destination destination, Map<Stri
try {
var result = javascriptExecutor.execute(destination.getTransformScript(), context);
log.info("Transform script result type: {}", result.getClass());
if (Map.class.isAssignableFrom(result.getClass())) {
return (Map<String, Object>) result;
if (V8ValueObject.class.isAssignableFrom(result.getClass())) {
try (var valueObject = ((V8ValueObject) result)) {
var objectType = valueObject.getType();
log.info("Object type: {}", objectType);
if (objectType == V8ValueReferenceType.Object || objectType == V8ValueReferenceType.Map) {

var json = valueObject.toJsonString();
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
});
}
}
} else {
log.warn("Transform script did not return a Map. Ignoring result.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import net.savantly.nexus.flow.dom.connections.flowHook.FlowDestinationHookFactory;
import net.savantly.nexus.flow.dom.connections.jdbcConnection.JdbcConnectionDestinationHook;
import net.savantly.nexus.flow.dom.connections.jdbcConnection.JdbcConnections;
import net.savantly.nexus.flow.dom.connections.webhook.WebhookDestinationHook;
import net.savantly.nexus.flow.dom.connections.webhook.WebhookDestinationHookFactory;
import net.savantly.nexus.flow.dom.emailTarget.EmailDestinationHookFactory;
import net.savantly.nexus.webhooks.dom.webhook.Webhooks;

@RequiredArgsConstructor
@Log4j2
Expand All @@ -21,10 +20,10 @@ public class DestinationHookFactory {
final ObjectMapper objectMapper;
final DatasourceFactory datasourceFactory;
final JdbcConnections jdbcConnections;
final Webhooks webhooks;
final RestTemplateBuilder restTemplateBuilder;
final FlowDestinationHookFactory flowDestinationHookFactory;
final EmailDestinationHookFactory emailDestinationHookFactory;
final EmailDestinationHookFactory emailDestinationHookFactory;
final WebhookDestinationHookFactory webhookDestinationHookFactory;

public DestinationHook createHook(Destination destination) {
log.info("Creating destination hook for: " + destination.getDestinationType());
Expand Down Expand Up @@ -55,11 +54,7 @@ private DestinationHook getJdbcHook(Destination destination) {
}

private DestinationHook getWebhookHook(Destination destination) {
var opt = webhooks.findById(destination.getDestinationId());
if (opt.isEmpty()) {
throw new RuntimeException("Webhook not found");
}
return new WebhookDestinationHook(webhooks, restTemplateBuilder);
return webhookDestinationHookFactory.createHook();
}

private DestinationHook getFlowDestinationHook(Destination destination) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.apache.causeway.applib.services.email.EmailService;
import org.apache.causeway.applib.services.repository.RepositoryService;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.destination.AbstractBaseDestinationHook;
import net.savantly.nexus.flow.dom.destination.Destination;
Expand All @@ -25,8 +27,8 @@ public class EmailDestinationHook extends AbstractBaseDestinationHook {

public EmailDestinationHook(EmailService emailService, EmailTargets emailTargets,
JavascriptExecutor javascriptExecutor, FlowContextFactory flowContextFactory,
RepositoryService repositoryService) {
super(flowContextFactory, javascriptExecutor, repositoryService);
RepositoryService repositoryService, ObjectMapper objectMapper) {
super(flowContextFactory, javascriptExecutor, repositoryService, objectMapper);
this.emailService = emailService;
this.emailTargets = emailTargets;
this.flowContextFactory = flowContextFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.apache.causeway.applib.services.email.EmailService;
import org.apache.causeway.applib.services.repository.RepositoryService;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.flowContext.FlowContextFactory;
Expand All @@ -17,9 +19,11 @@ public class EmailDestinationHookFactory {
private final JavascriptExecutor javascriptExecutor;
private final FlowContextFactory flowContextFactory;
private final RepositoryService repositoryService;
private final ObjectMapper objectMapper;

public EmailDestinationHook createHook() {
return new EmailDestinationHook(emailService, emailTargets, javascriptExecutor, flowContextFactory, repositoryService);
return new EmailDestinationHook(emailService, emailTargets, javascriptExecutor, flowContextFactory,
repositoryService, objectMapper);
}

}

0 comments on commit 0270991

Please sign in to comment.