From 062aaeb6b520c1b5428e444422d1578315a51cc0 Mon Sep 17 00:00:00 2001 From: Anagh Hegde Date: Tue, 26 Mar 2024 19:51:04 +0530 Subject: [PATCH] fix: logic for fetching onPageLoadActions for blocks import (#32073) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Fetch the actions from database and populate the ids for onPageLoad actions for the building block. ## Automation /ok-to-test tags="tags.ImportExport" ### :mag: Cypress test results > [!IMPORTANT] > Workflow run: > Commit: `8b5e06e3b97ef2795f7664c026c5f67f1d2e8886` > Cypress dashboard url: Click here! > All cypress tests have passed 🎉🎉🎉 ## 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. --- .../partial/PartialImportServiceCEImpl.java | 90 ++++++++++++------- .../partial/PartialImportServiceImpl.java | 10 ++- 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java index dc3c93a0cf..7cf8077589 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java @@ -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 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 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 actionCollections = tuple.getT1(); + List 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); + }); }); }); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceImpl.java index 7f5683f848..6cb4bf4734 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceImpl.java @@ -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); } }