Skip to content

Commit

Permalink
743 - unified-api - Make instanceId optional, if there is only one in…
Browse files Browse the repository at this point in the history
…tegration instance per type, fetch connection via connected user
  • Loading branch information
ivicac committed Nov 22, 2024
1 parent b9edc7b commit d7cf09b
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public Optional<IntegrationInstance> fetchFirstIntegrationInstance(
throw new UnsupportedOperationException();
}

@Override
public IntegrationInstance getIntegrationInstance(
long connectedUserId, List<String> componentNames, Environment environment) {

throw new UnsupportedOperationException();
}

@Override
public List<IntegrationInstance> getConnectedUserIntegrationInstances(long connectedUserId) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public interface IntegrationInstanceService {
Optional<IntegrationInstance> fetchFirstIntegrationInstance(
long connectedUserId, String componentName, Environment environment);

IntegrationInstance getIntegrationInstance(
long connectedUserId, List<String> componentNames, Environment environment);

List<IntegrationInstance> getConnectedUserIntegrationInstances(long connectedUserId);

List<IntegrationInstance> getConnectedUserIntegrationInstances(List<Long> connectedUserIds);
Expand All @@ -42,4 +45,5 @@ Optional<IntegrationInstance> fetchFirstIntegrationInstance(
List<IntegrationInstance> getIntegrationInstances(long integrationInstanceConfigurationId);

void updateEnabled(long id, boolean enable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ Optional<IntegrationInstance> findByWorkflowIdAndEnvironment(
ORDER BY integration_instance.created_date DESC
LIMIT 1
""")
Optional<IntegrationInstance> findFirstByExternalIdAndComponentNameAndEnvironment(
Optional<IntegrationInstance> findFirstByConnectedUserIdIdAndComponentNameAndEnvironment(
@Param("connectedUserId") long connectedUserId, @Param("componentName") String componentName,
@Param("environment") int environment);

@Query("""
SELECT DISTINCT integration_instance.* FROM integration_instance
JOIN integration_instance_configuration on integration_instance_configuration_id = integration_instance_configuration.id
JOIN integration on integration_instance_configuration.integration_id = integration.id
WHERE integration.component_name in (:componentNames)
AND integration_instance_configuration.environment = :environment
AND integration_instance.connected_user_id = :connectedUserId
ORDER BY integration_instance.created_date DESC
LIMIT 1
""")
IntegrationInstance findFirstByConnectedUserIdIdAndComponentNamesAndEnvironment(
@Param("connectedUserId") long connectedUserId, @Param("componentNames") List<String> componentNames,
@Param("environment") int environment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,18 @@ public List<IntegrationInstance> getConnectedUserEnabledIntegrationInstances(lon
public Optional<IntegrationInstance> fetchFirstIntegrationInstance(
long connectedUserId, String componentName, Environment environment) {

return integrationInstanceRepository.findFirstByExternalIdAndComponentNameAndEnvironment(
return integrationInstanceRepository.findFirstByConnectedUserIdIdAndComponentNameAndEnvironment(
connectedUserId, componentName, environment.ordinal());
}

@Override
public IntegrationInstance getIntegrationInstance(
long connectedUserId, List<String> componentNames, Environment environment) {

return integrationInstanceRepository.findFirstByConnectedUserIdIdAndComponentNamesAndEnvironment(
connectedUserId, componentNames, environment.ordinal());
}

@Override
@Transactional(readOnly = true)
public IntegrationInstance getIntegrationInstance(long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,31 @@
import com.bytechef.component.definition.unified.base.model.UnifiedOutputModel;
import com.bytechef.embedded.unified.pagination.CursorPageSlice;
import com.bytechef.embedded.unified.pagination.CursorPageable;
import com.bytechef.platform.connection.domain.ConnectionEnvironment;
import com.bytechef.platform.constant.Environment;
import org.springframework.lang.NonNull;

/**
* @author Ivica Cardic
*/
public interface UnifiedApiFacade {

String create(
UnifiedInputModel unifiedInputModel, Category category, ModelType modelType, ConnectionEnvironment environment,
long instanceId);
@NonNull UnifiedInputModel unifiedInputModel, @NonNull Category category, @NonNull ModelType modelType,
@NonNull Environment environment, Long instanceId);

void delete(
String id, Category category, ModelType modelType, ConnectionEnvironment environment, long instanceId);
@NonNull String id, @NonNull Category category, @NonNull ModelType modelType, @NonNull Environment environment,
Long instanceId);

UnifiedOutputModel get(
String id, Category category, ModelType modelType, ConnectionEnvironment environment, long instanceId);
@NonNull String id, @NonNull Category category, @NonNull ModelType modelType, @NonNull Environment environment,
Long instanceId);

CursorPageSlice<? extends UnifiedOutputModel> getPage(
CursorPageable cursorPageable, Category category, ModelType modelType, ConnectionEnvironment environment,
long instanceId);
@NonNull CursorPageable cursorPageable, @NonNull Category category, @NonNull ModelType modelType,
@NonNull Environment environment, Long instanceId);

void update(
String id, UnifiedInputModel unifiedInputModel, Category category, ModelType modelType,
ConnectionEnvironment environment, long instanceId);
@NonNull String id, @NonNull UnifiedInputModel unifiedInputModel, @NonNull Category category,
@NonNull ModelType modelType, @NonNull Environment environment, Long instanceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ schema:
format: int64
example: 1234
description: The integration instance Id that uniquely identifies the connected user's specific integration instance
required: true
required: false
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.bytechef.embedded.unified.web.rest.accounting.model.CreateUpdateAccountModel;
import com.bytechef.embedded.unified.web.rest.accounting.model.CreatedModel;
import com.bytechef.embedded.unified.web.rest.accounting.model.ListAccountsPageableParameterModel;
import com.bytechef.platform.connection.domain.ConnectionEnvironment;
import com.bytechef.platform.constant.Environment;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.ConversionService;
Expand All @@ -54,49 +54,49 @@ public AccountingAccountApiController(ConversionService conversionService, Unifi

@Override
public ResponseEntity<CreatedModel> createAccount(
Long xInstanceId, CreateUpdateAccountModel createUpdateAccountModel, String environment) {
CreateUpdateAccountModel createUpdateAccountModel, Long xInstanceId, String environment) {

return ResponseEntity.ok(
new CreatedModel(
unifiedApiFacade.create(
conversionService.convert(createUpdateAccountModel, AccountUnifiedInputModel.class), Category.CRM,
AccountingModelType.ACCOUNT, ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)),
AccountingModelType.ACCOUNT, Environment.valueOf(StringUtils.upperCase(environment)),
xInstanceId)));
}

@Override
public ResponseEntity<AccountModel> getAccount(
Long xInstanceId, String accountId, String environment, Boolean includeRawData) {
String accountId, Long xInstanceId, String environment, Boolean includeRawData) {

return ResponseEntity.ok(
conversionService.convert(
unifiedApiFacade.get(
accountId, Category.CRM, AccountingModelType.ACCOUNT,
ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)), xInstanceId),
Environment.valueOf(StringUtils.upperCase(environment)), xInstanceId),
AccountModel.class));
}

@Override
public ResponseEntity<CursorPageSlice> listAccounts(
Long xConnectionId, String environment, Boolean includeRawData, ListAccountsPageableParameterModel pageable) {
Long xInstanceId, String environment, Boolean includeRawData, ListAccountsPageableParameterModel pageable) {

return ResponseEntity.ok(
unifiedApiFacade
.getPage(
conversionService.convert(pageable, CursorPageRequest.class), Category.CRM,
AccountingModelType.ACCOUNT, ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)),
xConnectionId)
AccountingModelType.ACCOUNT, Environment.valueOf(StringUtils.upperCase(environment)),
xInstanceId)
.map(unifiedOutputModel -> conversionService.convert(unifiedOutputModel, AccountModel.class)));
}

@Override
public ResponseEntity<Void> updateAccount(
Long xConnectionId, String accountId, CreateUpdateAccountModel createUpdateAccountModel, String environment) {
String accountId, CreateUpdateAccountModel createUpdateAccountModel, Long xInstanceId, String environment) {

unifiedApiFacade.update(
accountId, conversionService.convert(createUpdateAccountModel, AccountUnifiedInputModel.class),
Category.CRM, AccountingModelType.ACCOUNT,
ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)), xConnectionId);
Category.CRM, AccountingModelType.ACCOUNT, Environment.valueOf(StringUtils.upperCase(environment)),
xInstanceId);

return ResponseEntity.noContent()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.bytechef.embedded.unified.web.rest.crm.model.CreateUpdateAccountModel;
import com.bytechef.embedded.unified.web.rest.crm.model.CreatedModel;
import com.bytechef.embedded.unified.web.rest.crm.model.ListAccountsPageableParameterModel;
import com.bytechef.platform.connection.domain.ConnectionEnvironment;
import com.bytechef.platform.constant.Environment;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.ConversionService;
Expand All @@ -54,48 +54,48 @@ public CrmAccountApiController(ConversionService conversionService, UnifiedApiFa

@Override
public ResponseEntity<CreatedModel> createAccount(
Long xConnectionId, CreateUpdateAccountModel createUpdateAccountModel, String environment) {
CreateUpdateAccountModel createUpdateAccountModel, Long xInstanceId, String environment) {

return ResponseEntity.ok(
new CreatedModel(
unifiedApiFacade.create(
conversionService.convert(createUpdateAccountModel, AccountUnifiedInputModel.class), Category.CRM,
CrmModelType.ACCOUNT, ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)),
xConnectionId)));
CrmModelType.ACCOUNT, Environment.valueOf(StringUtils.upperCase(environment)),
xInstanceId)));
}

@Override
public ResponseEntity<AccountModel> getAccount(
Long xInstanceId, String accountId, String environment, Boolean includeRawData) {
String accountId, Long xInstanceId, String environment, Boolean includeRawData) {

return ResponseEntity.ok(
conversionService.convert(
unifiedApiFacade.get(
accountId, Category.CRM, CrmModelType.ACCOUNT,
ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)), xInstanceId),
Environment.valueOf(StringUtils.upperCase(environment)), xInstanceId),
AccountModel.class));
}

@Override
public ResponseEntity<CursorPageSlice> listAccounts(
Long xConnectionId, String environment, Boolean includeRawData, ListAccountsPageableParameterModel pageable) {
Long xInstanceId, String environment, Boolean includeRawData, ListAccountsPageableParameterModel pageable) {

return ResponseEntity.ok(
unifiedApiFacade
.getPage(
conversionService.convert(pageable, CursorPageRequest.class), Category.CRM, CrmModelType.ACCOUNT,
ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)), xConnectionId)
Environment.valueOf(StringUtils.upperCase(environment)), xInstanceId)
.map(unifiedOutputModel -> conversionService.convert(unifiedOutputModel, AccountModel.class)));
}

@Override
public ResponseEntity<Void> updateAccount(
Long xConnectionId, String accountId, CreateUpdateAccountModel createUpdateAccountModel, String environment) {
String accountId, CreateUpdateAccountModel createUpdateAccountModel, Long xInstanceId, String environment) {

unifiedApiFacade.update(
accountId, conversionService.convert(createUpdateAccountModel, AccountUnifiedInputModel.class),
Category.CRM, CrmModelType.ACCOUNT, ConnectionEnvironment.valueOf(StringUtils.upperCase(environment)),
xConnectionId);
Category.CRM, CrmModelType.ACCOUNT, Environment.valueOf(StringUtils.upperCase(environment)),
xInstanceId);

return ResponseEntity.noContent()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencies {
implementation("org.slf4j:slf4j-api")
implementation("org.springframework:spring-context")
implementation(project(":server:libs:embedded:embedded-configuration:embedded-configuration-api"))
implementation(project(":server:libs:embedded:embedded-connected-user:embedded-connected-user-api"))
implementation(project(":server:libs:embedded:embedded-unified:embedded-unified-api"))
implementation(project(":server:libs:platform:platform-component:platform-component-api"))
implementation(project(":server:libs:platform:platform-connection:platform-connection-api"))
Expand Down
Loading

0 comments on commit d7cf09b

Please sign in to comment.