Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions app/client/src/workers/Evaluation/JSObject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import JSObjectCollection from "./Collection";
import ExecutionMetaData from "../fns/utils/ExecutionMetaData";
import { jsPropertiesState } from "./jsPropertiesState";
import { getFixedTimeDifference } from "workers/common/DataTreeEvaluator/utils";
interface ParseJSAction extends ParsedJSSubAction {
parsedFunction: unknown;
Comment thread
sneha122 marked this conversation as resolved.
Outdated
}

/**
* Here we update our unEvalTree according to the change in JSObject's body
Expand Down Expand Up @@ -114,18 +117,17 @@ export function saveResolvedFunctionsAndJSUpdates(
JSObjectName: entityName,
JSObjectASTParseTime,
});
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const actions: any = [];
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const variables: any = [];

const actionsMap: Record<string, ParseJSAction> = {};
const variablesMap: Record<string, { name: string; value: unknown }> = {};

if (success) {
if (!!parsedObject) {
jsPropertiesState.update(entityName, parsedObject);
parsedObject.forEach((parsedElement) => {
if (isJSFunctionProperty(parsedElement)) {
if (actionsMap[parsedElement.key]) return;

try {
ExecutionMetaData.setExecutionMetaData({
enableJSVarUpdateTracking: false,
Expand Down Expand Up @@ -164,12 +166,12 @@ export function saveResolvedFunctionsAndJSUpdates(
`${entityName}.${parsedElement.key}`,
functionString,
);
actions.push({
actionsMap[parsedElement.key] = {
name: parsedElement.key,
body: functionString,
arguments: params,
parsedFunction: result,
});
};
}
} catch {
// in case we need to handle error state
Expand All @@ -184,10 +186,10 @@ export function saveResolvedFunctionsAndJSUpdates(
? parsedElement.key.slice(1, -1)
: parsedElement.key;

variables.push({
variablesMap[parsedKey] = {
name: parsedKey,
value: parsedElement.value,
});
};
JSObjectCollection.updateUnEvalState(
`${entityName}.${parsedElement.key}`,
parsedElement.value,
Expand All @@ -196,8 +198,8 @@ export function saveResolvedFunctionsAndJSUpdates(
});
const parsedBody = {
body: entity.body,
actions: actions,
variables,
actions: Object.values(actionsMap),
variables: Object.values(variablesMap),
};

set(jsUpdates, `${entityName}`, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public Mono<ActionCollectionDTO> updateUnpublishedActionCollection(

final Mono<Map<String, String>> newValidActionIdsMono = branchedActionCollectionMono.flatMap(
branchedActionCollection -> Flux.fromIterable(actionCollectionDTO.getActions())
.distinct(actionDTO -> actionCollectionDTO.getName() + "." + actionDTO.getName())
.flatMap(actionDTO -> {
actionDTO.setDeletedAt(null);
setContextId(branchedActionCollection, actionDTO);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify initialization of baseActionIds

You create a new HashSet called baseActionIds and add all elements from validBaseActionIds. Since validBaseActionIds is already a set, you can use it directly or instantiate baseActionIds with new HashSet<>(validBaseActionIds) to simplify the code.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,57 @@ public void testUpdateUnpublishedActionCollection_withInvalidId_throwsError() th
.verify();
}

@Test
public void testUpdateUnpublishedActionCollection_withDuplicateActions() throws IOException {
ActionCollectionDTO actionCollectionDTO = new ActionCollectionDTO();
actionCollectionDTO.setId("testId");
actionCollectionDTO.setPageId("testPageId");
actionCollectionDTO.setApplicationId("testApplicationId");
actionCollectionDTO.setWorkspaceId("testWorkspaceId");
actionCollectionDTO.setPluginId("testPluginId");
actionCollectionDTO.setPluginType(PluginType.JS);

ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonNode = objectMapper.readValue(mockObjects, JsonNode.class);
final NewPage newPage = objectMapper.convertValue(jsonNode.get("newPage"), NewPage.class);

Mockito.when(actionCollectionRepository.findById(Mockito.anyString(), Mockito.<AclPermission>any()))
.thenReturn(Mono.empty());

Mockito.when(newPageService.findByBranchNameAndBasePageId(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(Mono.just(newPage));

Mockito.when(newPageService.findById(Mockito.any(), Mockito.<AclPermission>any()))
.thenReturn(Mono.just(newPage));

Mockito.when(newActionService.findByCollectionIdAndViewMode(
Mockito.anyString(), Mockito.anyBoolean(), Mockito.any()))
.thenReturn(Flux.empty());

ActionDTO action = new ActionDTO();
action.setName("testAction");
action.setClientSideExecution(true);
actionCollectionDTO.setActions(List.of(action));
action.setName("testAction");
action.setClientSideExecution(true);
actionCollectionDTO.setActions(List.of(action));

final Mono<ActionCollectionDTO> actionCollectionDTOMono =
layoutCollectionService.updateUnpublishedActionCollection("testId", actionCollectionDTO);

// verify that actionCollectionDTOMono has only one action and the duplicate action was ignored
StepVerifier.create(actionCollectionDTOMono)
.assertNext(actionCollectionDTO1 -> {
assertEquals(1, actionCollectionDTO1.getActions().size());
final ActionDTO actionDTO =
actionCollectionDTO1.getActions().get(0);
assertEquals("testAction", actionDTO.getName());
assertTrue(actionDTO.getClientSideExecution());
})
.verifyComplete();
}

@Test
public void testDeleteUnpublishedActionCollection_withInvalidId_throwsError() {
Mockito.when(actionCollectionRepository.findById(Mockito.any(), Mockito.<AclPermission>any()))
Expand Down