fix: logic for fetching onPageLoadActions for blocks import (#32073)

## Description
Fetch the actions from database and populate the ids for onPageLoad
actions for the building block.


## Automation

/ok-to-test tags="tags.ImportExport"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!IMPORTANT]
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/8435259797>
> Commit: `8b5e06e3b97ef2795f7664c026c5f67f1d2e8886`
> Cypress dashboard url: <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=8435259797&attempt=1"
target="_blank">Click here!</a>
> All cypress tests have passed 🎉🎉🎉

<!-- end of auto-generated comment: Cypress test results  -->



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced the import functionality to correctly update onPageLoad
actions with their respective IDs for both action collections and new
actions, ensuring a smoother integration and setup process for users.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Anagh Hegde 2024-03-26 19:51:04 +05:30 committed by GitHub
parent 4a260b577b
commit 062aaeb6b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 32 deletions

View File

@ -4,6 +4,7 @@ import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.models.CreatorContextType;
import com.appsmith.external.models.Datasource;
import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.actioncollections.base.ActionCollectionService;
import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.constants.FieldName;
import com.appsmith.server.domains.ActionCollection;
@ -26,6 +27,7 @@ import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.helpers.ImportArtifactPermissionProvider;
import com.appsmith.server.imports.importable.ImportableService;
import com.appsmith.server.imports.internal.ImportService;
import com.appsmith.server.newactions.base.NewActionService;
import com.appsmith.server.newpages.base.NewPageService;
import com.appsmith.server.refactors.applications.RefactoringService;
import com.appsmith.server.repositories.PermissionGroupRepository;
@ -48,8 +50,10 @@ import org.springframework.transaction.reactive.TransactionalOperator;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@ -83,6 +87,8 @@ public class PartialImportServiceCEImpl implements PartialImportServiceCE {
private final ApplicationTemplateService applicationTemplateService;
private final WidgetRefactorUtil widgetRefactorUtil;
private final ApplicationPageService applicationPageService;
private final NewActionService newActionService;
private final ActionCollectionService actionCollectionService;
@Override
public Mono<Application> importResourceInPage(
@ -414,6 +420,10 @@ public class PartialImportServiceCEImpl implements PartialImportServiceCE {
// Fetch layout and get new onPageLoadActions
// This data is not present in a client, since these are created
// after importing the block
BuildingBlockResponseDTO buildingBlockResponseDTO = new BuildingBlockResponseDTO();
buildingBlockResponseDTO.setWidgetDsl(buildingBlockImportDTO.getWidgetDsl());
buildingBlockResponseDTO.setOnPageLoadActions(new ArrayList<>());
Set<String> newOnPageLoadActionNames = new HashSet<>();
applicationJson
.getPageList()
@ -426,41 +436,61 @@ public class PartialImportServiceCEImpl implements PartialImportServiceCE {
dslExecutableDTOS.forEach(dslExecutableDTO -> {
if (dslExecutableDTO.getName() != null) {
newOnPageLoadActionNames.add(dslExecutableDTO.getName());
buildingBlockResponseDTO
.getOnPageLoadActions()
.add(dslExecutableDTO);
}
});
});
BuildingBlockResponseDTO buildingBlockResponseDTO = new BuildingBlockResponseDTO();
buildingBlockResponseDTO.setWidgetDsl(buildingBlockImportDTO.getWidgetDsl());
return newPageService
.findBranchedPageId(
branchName, buildingBlockDTO.getPageId(), AclPermission.MANAGE_PAGES)
.flatMap(branchedPageId -> newPageService
.findById(branchedPageId, Optional.empty())
.map(newPage -> {
if (newPage.getUnpublishedPage()
.getLayouts()
.get(0)
.getLayoutOnLoadActions()
== null) {
return buildingBlockResponseDTO;
}
newPage.getUnpublishedPage()
.getLayouts()
.get(0)
.getLayoutOnLoadActions()
.forEach(dslExecutableDTOS -> {
// Filter the onPageLoadActions based on the json file
buildingBlockResponseDTO
.getOnPageLoadActions()
.addAll(dslExecutableDTOS.stream()
.filter(dslExecutableDTO ->
newOnPageLoadActionNames.contains(
dslExecutableDTO.getName()))
.toList());
// Fetch all actions and action collections and update the onPageLoadActions with correct ids
return actionCollectionService
.findByPageId(buildingBlockDTO.getPageId())
.collectList()
.zipWith(newActionService
.findByPageId(buildingBlockDTO.getPageId())
.collectList())
.flatMap(tuple -> {
List<ActionCollection> actionCollections = tuple.getT1();
List<NewAction> newActions = tuple.getT2();
actionCollections.forEach(actionCollection -> {
if (newOnPageLoadActionNames.contains(actionCollection
.getUnpublishedCollection()
.getName())) {
buildingBlockResponseDTO
.getOnPageLoadActions()
.forEach(dslExecutableDTO -> {
if (dslExecutableDTO
.getName()
.equals(actionCollection
.getUnpublishedCollection()
.getName())) {
dslExecutableDTO.setId(actionCollection.getId());
}
});
return buildingBlockResponseDTO;
}));
}
});
newActions.forEach(newAction -> {
if (newOnPageLoadActionNames.contains(
newAction.getUnpublishedAction().getName())) {
buildingBlockResponseDTO
.getOnPageLoadActions()
.forEach(dslExecutableDTO -> {
if (dslExecutableDTO
.getName()
.equals(newAction
.getUnpublishedAction()
.getName())) {
dslExecutableDTO.setId(newAction.getId());
}
});
}
});
return Mono.just(buildingBlockResponseDTO);
});
});
});
}

View File

@ -1,6 +1,7 @@
package com.appsmith.server.imports.internal.partial;
import com.appsmith.external.models.Datasource;
import com.appsmith.server.actioncollections.base.ActionCollectionService;
import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.domains.ActionCollection;
import com.appsmith.server.domains.CustomJSLib;
@ -9,6 +10,7 @@ import com.appsmith.server.domains.NewPage;
import com.appsmith.server.domains.Plugin;
import com.appsmith.server.imports.importable.ImportableService;
import com.appsmith.server.imports.internal.ImportService;
import com.appsmith.server.newactions.base.NewActionService;
import com.appsmith.server.newpages.base.NewPageService;
import com.appsmith.server.refactors.applications.RefactoringService;
import com.appsmith.server.repositories.PermissionGroupRepository;
@ -56,7 +58,9 @@ public class PartialImportServiceImpl extends PartialImportServiceCEImpl impleme
RefactoringService refactoringService,
ApplicationTemplateService applicationTemplateService,
WidgetRefactorUtil widgetRefactorUtil,
ApplicationPageService applicationPageService) {
ApplicationPageService applicationPageService,
NewActionService newActionService,
ActionCollectionService actionCollectionService) {
super(
importService,
workspaceService,
@ -80,6 +84,8 @@ public class PartialImportServiceImpl extends PartialImportServiceCEImpl impleme
refactoringService,
applicationTemplateService,
widgetRefactorUtil,
applicationPageService);
applicationPageService,
newActionService,
actionCollectionService);
}
}