Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbranham committed Jul 16, 2024
1 parent 6eb6194 commit 10cf208
Show file tree
Hide file tree
Showing 56 changed files with 1,023 additions and 167 deletions.
8 changes: 8 additions & 0 deletions module-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<graalvm.version>24.0.1</graalvm.version>
<javet.version>3.1.3</javet.version>
<jsonschema-generator.version>4.35.0</jsonschema-generator.version>
<handlebars-version>4.4.0</handlebars-version>
</properties>

<build>
Expand Down Expand Up @@ -104,6 +105,13 @@
<version>${jsonschema-generator.version}</version>
</dependency>


<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>${handlebars-version}</version>
</dependency>

<!-- Spring -->

<!-- CAUSEWAY API -->
Expand Down
51 changes: 35 additions & 16 deletions module-flow/src/main/java/net/savantly/nexus/flow/FlowModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.savantly.nexus.flow;

import org.apache.causeway.applib.services.email.EmailService;
import org.apache.causeway.applib.services.repository.RepositoryService;
import org.apache.causeway.extensions.fullcalendar.applib.CausewayModuleExtFullCalendarApplib;
import org.apache.causeway.extensions.pdfjs.applib.CausewayModuleExtPdfjsApplib;
Expand All @@ -20,6 +21,7 @@
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.caoccao.javet.interop.engine.IJavetEnginePool;
Expand All @@ -34,14 +36,15 @@
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.destinations.DestinationHookFactory;
import net.savantly.nexus.flow.dom.destination.DestinationHookFactory;
import net.savantly.nexus.flow.dom.emailTarget.EmailDestinationHookFactory;
import net.savantly.nexus.flow.dom.emailTarget.EmailTargets;
import net.savantly.nexus.flow.dom.flowContext.FlowContextFactory;
import net.savantly.nexus.flow.dom.flowDefinition.FlowDefinitionExecutionProxy;
import net.savantly.nexus.flow.dom.flowDefinition.FlowDefinitionRepository;
import net.savantly.nexus.flow.dom.flowDefinition.FlowDefinitions;
import net.savantly.nexus.flow.dom.flowNode.FlowNodeDiscoveryService;
import net.savantly.nexus.flow.dom.flowNodeSchema.FlowNodeSchemaGenerator;
import net.savantly.nexus.flow.dom.flowSecret.FlowSecretRepository;
import net.savantly.nexus.flow.dom.flowSecret.FlowSecrets;
import net.savantly.nexus.flow.dom.form.FormRepository;
import net.savantly.nexus.flow.dom.form.FormSubmissionProxy;
import net.savantly.nexus.flow.dom.form.Forms;
Expand All @@ -52,6 +55,8 @@
import net.savantly.nexus.flow.executor.FlowExecutorFactory;
import net.savantly.nexus.flow.executor.FlowNodeFactory;
import net.savantly.nexus.flow.executor.javascript.JavascriptExecutor;
import net.savantly.nexus.organizations.dom.organizationSecret.OrganizationSecretRepository;
import net.savantly.nexus.organizations.dom.organizationSecret.OrganizationSecrets;
import net.savantly.nexus.webhooks.dom.webhook.Webhooks;

@Configuration
Expand All @@ -74,11 +79,9 @@ public class FlowModule implements ModuleWithFixtures {
public static final String NAMESPACE = "nexus.flow";
public static final String SCHEMA = "flow";


@Setter
private String recaptchEndpoint = "https://www.google.com/recaptcha/api/siteverify";


@Override
public FixtureScript getTeardownFixture() {
return new TeardownFixtureJpaAbstract() {
Expand All @@ -88,7 +91,6 @@ protected void execute(ExecutionContext executionContext) {
}
};
}


@Bean
@Primary
Expand Down Expand Up @@ -117,19 +119,21 @@ public FlowDestinationHookFactory flow_flowDestinationHookFactory(FlowDefinition
@Bean
public DestinationHookFactory flow_destinationHookFactory(ObjectMapper objectMapper,
DatasourceFactory datasourceFactory, JdbcConnections jdbcConnections, Webhooks webhooks,
RestTemplateBuilder restTemplateBuilder, FlowDestinationHookFactory flowDestinationHookFactory) {
RestTemplateBuilder restTemplateBuilder, FlowDestinationHookFactory flowDestinationHookFactory,
EmailDestinationHookFactory emailDestinationHookFactory) {
return new DestinationHookFactory(objectMapper, datasourceFactory, jdbcConnections, webhooks,
restTemplateBuilder, flowDestinationHookFactory);
restTemplateBuilder, flowDestinationHookFactory, emailDestinationHookFactory);
}

@Bean
public DatasourceFactory flow_datasourceFactory(FlowSecrets flowSecrets) {
public DatasourceFactory flow_datasourceFactory(OrganizationSecrets flowSecrets) {
return new DatasourceFactory(flowSecrets);
}

@Bean
@Scope(scopeName = "prototype")
public JavascriptExecutor flow_javascriptExecutor(IJavetEnginePool javetEnginePool) {
return new JavascriptExecutor(javetEnginePool);
return new JavascriptExecutor(() -> javetEnginePool);
}

@Bean
Expand All @@ -147,25 +151,34 @@ public FlowExecutorFactory flow_flowExecutorFactory(FlowNodeFactory nodeFactory)
return new FlowExecutorFactory(nodeFactory);
}

@Bean
public FlowContextFactory flow_flowContextFactory(OrganizationSecrets flowSecrets) {
return new FlowContextFactory(flowSecrets);
}

@Bean
public FlowDefinitionExecutionProxy flow_flowDefinitionExecutionProxy(FlowExecutorFactory flowExecutorFactory,
FlowDefinitionRepository flowDefinitionRepository, ObjectMapper objectMapper,
RepositoryService repositoryService, FlowSecretRepository secretRepository) {
RepositoryService repositoryService, OrganizationSecretRepository secretRepository, FlowContextFactory flowContextFactory) {
return new FlowDefinitionExecutionProxy(flowExecutorFactory, flowDefinitionRepository, objectMapper,
repositoryService, secretRepository);
repositoryService, flowContextFactory);
}

@Bean
public FlowService flow_flowService(FlowDefinitions flowDefinitions, FlowNodeDiscoveryService flowNodeDiscoveryService, Forms forms) {
public FlowService flow_flowService(FlowDefinitions flowDefinitions,
FlowNodeDiscoveryService flowNodeDiscoveryService, Forms forms) {
return new FlowService(flowDefinitions, flowNodeDiscoveryService, forms);
}

@Bean
public FormSubmissionProxy flow_formSubmissionProxy(FormSubmissions formSubmissions, FormRepository formRepository, ObjectMapper objectMapper, DestinationHookFactory destinationHookFactory, ReCaptchaService recaptchaService) {
return new FormSubmissionProxy(formSubmissions, formRepository, objectMapper, destinationHookFactory, recaptchaService);
public FormSubmissionProxy flow_formSubmissionProxy(FormSubmissions formSubmissions, FormRepository formRepository,
ObjectMapper objectMapper, DestinationHookFactory destinationHookFactory,
ReCaptchaService recaptchaService) {
return new FormSubmissionProxy(formSubmissions, formRepository, objectMapper, destinationHookFactory,
recaptchaService);
}

@Bean
@Bean
public ReCaptchaAttemptService flow_reCaptchaAttemptService() {
return new ReCaptchaAttemptService();
}
Expand All @@ -175,4 +188,10 @@ public ReCaptchaService flow_reCaptchaService(ReCaptchaAttemptService reCaptchaA
return new ReCaptchaService(reCaptchaAttemptService, recaptchEndpoint);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.connections.jdbcConnection.JdbcConnection;
import net.savantly.nexus.flow.dom.flowSecret.FlowSecrets;
import net.savantly.nexus.organizations.dom.organizationSecret.OrganizationSecrets;

@Log4j2
@RequiredArgsConstructor
public class DatasourceFactory {

private final FlowSecrets flowSecrets;
private final OrganizationSecrets flowSecrets;

public DataSource createFromJdbcConnection(JdbcConnection jdbcConnection) {
log.info("creating datasource for url: {}", jdbcConnection.getJdbcUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.Map;

import lombok.RequiredArgsConstructor;
import net.savantly.nexus.flow.dom.destinations.Destination;
import net.savantly.nexus.flow.dom.destinations.DestinationHook;
import net.savantly.nexus.flow.dom.destinations.DestinationHookResponse;
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.flowDefinition.FlowDefinitionExecutionProxy;
import net.savantly.nexus.flow.dom.flowDefinition.FlowDefinitions;
import net.savantly.nexus.flow.dom.formMapping.Mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import lombok.val;
import net.savantly.nexus.common.types.Name;
import net.savantly.nexus.flow.FlowModule;
import net.savantly.nexus.flow.dom.flowSecret.FlowSecret;
import net.savantly.nexus.organizations.dom.organization.Organization;
import net.savantly.nexus.organizations.dom.organizationSecret.OrganizationSecret;

@Named(FlowModule.NAMESPACE + ".JdbcConnection")
@jakarta.persistence.Entity
Expand Down Expand Up @@ -117,7 +117,7 @@ public static JdbcConnection withName(Organization organization, String name) {
@PropertyLayout(fieldSetId = "identity", sequence = "1.7")
@Setter
@Getter
private FlowSecret password;
private OrganizationSecret password;


@Column(length = 255, nullable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.destinations.Destination;
import net.savantly.nexus.flow.dom.destinations.DestinationHook;
import net.savantly.nexus.flow.dom.destinations.DestinationHookResponse;
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.formData.FormDataRecord;
import net.savantly.nexus.flow.dom.formMapping.Mapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import jakarta.inject.Inject;
import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.flowSecret.FlowSecret;
import net.savantly.nexus.flow.dom.flowSecret.FlowSecrets;
import net.savantly.nexus.organizations.dom.organizationSecret.OrganizationSecret;
import net.savantly.nexus.organizations.dom.organizationSecret.OrganizationSecrets;

@Log4j2
@jakarta.annotation.Priority(PriorityPrecedence.EARLY)
Expand All @@ -36,10 +36,10 @@ public static class ActionEvent
@Inject
ObjectMapper objectMapper;
@Inject
FlowSecrets flowSecrets;
OrganizationSecrets flowSecrets;

@MemberSupport
public JdbcConnection act(final FlowSecret secret) {
public JdbcConnection act(final OrganizationSecret secret) {
try {
object.setPassword(secret);
messageService.informUser("Updated Secret Reference");
Expand All @@ -52,12 +52,12 @@ public JdbcConnection act(final FlowSecret secret) {
}

@MemberSupport
public FlowSecret default0Act() {
public OrganizationSecret default0Act() {
return object.getPassword();
}

@MemberSupport
public Set<FlowSecret> choices0Act() {
public Set<OrganizationSecret> choices0Act() {
return flowSecrets.findByOrganizationId(object.getOrganization().getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.destinations.Destination;
import net.savantly.nexus.flow.dom.destinations.DestinationHook;
import net.savantly.nexus.flow.dom.destinations.DestinationHookResponse;
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.formMapping.Mapping;
import net.savantly.nexus.webhooks.dom.webhook.Webhooks;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package net.savantly.nexus.flow.dom.destination;

import java.util.Collection;
import java.util.Map;

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

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.savantly.nexus.flow.dom.destinationExecution.DestinationExecution;
import net.savantly.nexus.flow.dom.flowContext.FlowContextFactory;
import net.savantly.nexus.flow.dom.formMapping.Mapping;
import net.savantly.nexus.flow.executor.javascript.JavascriptExecutor;

@Log4j2
@RequiredArgsConstructor
public abstract class AbstractBaseDestinationHook implements DestinationHook {

private final FlowContextFactory flowContextFactory;
private final JavascriptExecutor javascriptExecutor;
private final RepositoryService repositoryService;

public abstract DestinationHookResponse sendData(Destination destination, Map<String, Object> payload,
Collection<? extends Mapping> formMappings);

protected Map<String, Object> transformPayload(Destination destination, Map<String, Object> payload) {
log.info("Transforming payload for destination {}", destination.getName());
var context = flowContextFactory.create(destination.getOrganization().getId());
context.setVariable("payload", payload);

if (destination.getTransformScript() != null) {
try {
var result = javascriptExecutor.execute(destination.getTransformScript(), context);
log.info("Transform script result type: {}", result.getClass());
} catch (Exception e) {
log.error("Failed to execute transform script", e);
throw new IllegalArgumentException("Failed to execute transform script. " + e.getMessage());
}
}
return payload;
}

@Override
public DestinationHookResponse execute(Destination destination, Map<String, Object> payload,
Collection<? extends Mapping> formMappings) {

var transformedPayload = transformPayload(destination, payload);

var result = sendData(destination, transformedPayload, formMappings);
var executionAudit = DestinationExecution.withResult(destination, result.isSuccess(), result.getMessage());

repositoryService.persist(executionAudit);

return result;
}

}
Loading

0 comments on commit 10cf208

Please sign in to comment.