From 83eae15c6fdcea8a7cbf6f2adba32ff9f56504f1 Mon Sep 17 00:00:00 2001 From: swarnadipa-dev Date: Fri, 4 Oct 2024 18:03:18 +0530 Subject: [PATCH] dynamic parameter passing in document & folder --- .../document-tab/document-tab.component.ts | 50 ++++++++++------- .../folder-tab/folder-tab.component.ts | 53 ++++++++++--------- .../generic-multi-feature-layout.interface.ts | 6 +-- .../generic-multi-feature-layout.mapping.ts | 39 +++++++------- ...generic-multi-feature-endpoints.service.ts | 28 ++++++---- ...generic-multi-feature-utilities.service.ts | 2 +- .../store/actions.ts | 4 +- .../store/effects.ts | 4 +- .../app/shared/services/network.service.ts | 36 +++---------- 9 files changed, 110 insertions(+), 112 deletions(-) diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/document-tab/document-tab.component.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/document-tab/document-tab.component.ts index 6b2356f7..d98f619f 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/document-tab/document-tab.component.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/document-tab/document-tab.component.ts @@ -66,6 +66,7 @@ export class DocumentTabComponent implements OnInit, OnDestroy { actionsImportFn: ActionsImportFunction | null = null; activeFeature: FeaturesKey = {} as FeaturesKey; endpointData = {} as unknown; + requestQuery = ''; constructor( public dialogService: MatDialog, private fb: FormBuilder, @@ -220,37 +221,46 @@ export class DocumentTabComponent implements OnInit, OnDestroy { decodeURIComponent(doc.path) ) : doc.path; - const featureEndpointData = this.templateConfigData?.data; - let requestQueryParam = ""; - let requestBodyParam = ""; - if ("queryParam" in featureEndpointData) { - requestQueryParam = - this.genericMultiFeatureUtilitiesService.getRequestQuery( - this.templateConfigData.requestParams as string, - decodedPath - ); - } - - if ("bodyParam" in featureEndpointData) { - requestBodyParam = - this.genericMultiFeatureUtilitiesService.getRequestQuery( - this.templateConfigData.requestParams as string, - decodedPath - ); - } + /* const requestQuery = this.genericMultiFeatureUtilitiesService.getRequestQuery( this.templateConfigData.requestParams as string, decodedPath ); */ + this.requestQuery = + this.genericMultiFeatureUtilitiesService.getRequestQuery( + (this.templateConfigData?.data["queryParam"]) + ? this.templateConfigData?.data["queryParam"]["query"] as string + : this.templateConfigData?.data["bodyParam"]["query"] as string, + decodedPath + ); const featureKey = getFeatureKeyByValue( this.activeFeature ) as FeaturesKey; if (featureKey in FEATURES) { + let requestUrl = ""; + let requestParams = this.templateConfigData?.data["bodyParam"]; + // Prepare body params object with dynamic parameters & their values entered as input + if (requestParams) { + // Since, it is bodyParam, the query would be part of body params object & not the url + requestParams["query"] = this.requestQuery; + Object.keys(requestParams).forEach((key) => { + if (key in this) { + const paramValue = this[key as keyof DocumentTabComponent]; + /* Only add the param to body params object list if user has enetered a value for it */ + if (paramValue) { + requestParams[key] = paramValue; + } + } + }); + } else { + // since it is queryParam, the query would be appended to the url + requestUrl = this.requestQuery; + } this.store.dispatch( FeatureActions.performDocumentAction({ - requestQueryParam, - requestBodyParam, + requestUrl, + requestParams, featureEndpoint: REST_END_POINTS[featureKey], }) ); diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/folder-tab/folder-tab.component.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/folder-tab/folder-tab.component.ts index a6780e4b..af7fa34c 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/folder-tab/folder-tab.component.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/components/folder-tab/folder-tab.component.ts @@ -73,8 +73,6 @@ export class FolderTabComponent implements OnInit, OnDestroy { templateLabels: labelsList = {} as labelsList; requestQuery = ""; activeFeature: FeaturesKey = {} as FeaturesKey; - requestQueryParam = ""; - requestBodyParam = ""; constructor( public dialogService: MatDialog, @@ -211,29 +209,14 @@ export class FolderTabComponent implements OnInit, OnDestroy { decodeURIComponent(this.userInput) ); - const featureEndpointData = this.templateConfigData?.data; - if ("queryParam" in featureEndpointData) { - this.requestQueryParam = - this.genericMultiFeatureUtilitiesService.getRequestQuery( - this.templateConfigData.requestParams as string, - this.decodedUserInput - ); - } - - if ("bodyParam" in featureEndpointData) { - this.requestBodyParam = - this.genericMultiFeatureUtilitiesService.getRequestQuery( - this.templateConfigData.requestParams as string, - this.decodedUserInput - ); - } - - /* this.requestQuery = + this.requestQuery = this.genericMultiFeatureUtilitiesService.getRequestQuery( - this.templateConfigData.requestParams as string, + (this.templateConfigData?.data["queryParam"]) + ? this.templateConfigData?.data["queryParam"]["query"] as string + : this.templateConfigData?.data["bodyParam"]["query"] as string, this.decodedUserInput - ); */ - this.fetchNoOfDocuments(this.requestQuery as string); + ); + this.fetchNoOfDocuments(this.requestQuery); } catch (error) { this.showActionErrorModal({ type: ERROR_TYPES.INVALID_DOC_ID, @@ -323,10 +306,30 @@ export class FolderTabComponent implements OnInit, OnDestroy { this.activeFeature ) as FeaturesKey; if (featureKey in FEATURES) { + let requestUrl = ""; + let requestParams = this.templateConfigData?.data["bodyParam"]; + // Prepare body params object with dynamic parameters & their values entered as input + if (requestParams) { + // Since, it is bodyParam, the query would be part of body params object & not the url + requestParams["query"] = this.requestQuery; + Object.keys(requestParams).forEach((key) => { + if (key in this) { + const paramValue = this[key as keyof FolderTabComponent]; + /* Only add the param to body params object list if user has enetered a value for it */ + if (paramValue) { + requestParams[key] = paramValue; + } + } + }); + } else { + // since it is queryParam, the query would be appended to the url + requestUrl = this.requestQuery; + } + this.store.dispatch( FeatureActions.performFolderAction({ - requestQueryParam: this.requestQueryParam, - requestBodyParam: this.requestBodyParam, + requestUrl, + requestParams, featureEndpoint: REST_END_POINTS[featureKey as FeaturesKey], }) ); diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.interface.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.interface.ts index f1416190..887c2f60 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.interface.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.interface.ts @@ -48,7 +48,7 @@ export interface FeatureData { } export interface RequestParamType { - queryParam: { [key: string]: any }; - bodyParam: { [key: string]: any }; - urlParam: { [key: string]: any }; + queryParam: { [key: string]: unknown }; + bodyParam: { [key: string]: unknown }; + urlParam: { [key: string]: unknown }; } diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.mapping.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.mapping.ts index d791b747..ef6c3660 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.mapping.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/generic-multi-feature-layout.mapping.ts @@ -3,7 +3,7 @@ import { GENERIC_LABELS } from "./generic-multi-feature-layout.constants"; import { labelsList } from "./generic-multi-feature-layout.interface"; export const FEATURES = { - ELASTIC_SEARCH_REINDEX: "elasticsearch-reindex" + ELASTIC_SEARCH_REINDEX: "elasticsearch-reindex", // Add other features here. Value MUST match route name } as const; @@ -27,25 +27,25 @@ export const featureMap = () => ({ switch (tabType) { case GENERIC_LABELS.DOCUMENT: - (requestParams = `ecm:path='{queryParam}'`), - (labels = { - pageTitle: ELASTIC_SEARCH_LABELS.DOCUMENT_REINDEX_TITLE, - submitBtnLabel: ELASTIC_SEARCH_LABELS.REINDEX_BUTTON_LABEL, - }); - (data = { - queryParam: { query: requestParams} - }) + labels = { + pageTitle: ELASTIC_SEARCH_LABELS.DOCUMENT_REINDEX_TITLE, + submitBtnLabel: ELASTIC_SEARCH_LABELS.REINDEX_BUTTON_LABEL, + }; + data = { + queryParam: { query: `ecm:path='{query}'` }, + }; break; case GENERIC_LABELS.FOLDER: - (requestParams = `ecm:uuid='{queryParam}' OR ecm:ancestorId='{queryParam}' ${GENERIC_LABELS.AND} ${GENERIC_LABELS.SELECT_QUERY_CONDITIONS}`), - (labels = { - pageTitle: ELASTIC_SEARCH_LABELS.FOLDER_REINDEX_TITLE, - submitBtnLabel: ELASTIC_SEARCH_LABELS.REINDEX_BUTTON_LABEL, - }); - (data = { - bodyParam: { query: requestParams} - }) + labels = { + pageTitle: ELASTIC_SEARCH_LABELS.FOLDER_REINDEX_TITLE, + submitBtnLabel: ELASTIC_SEARCH_LABELS.REINDEX_BUTTON_LABEL, + }; + data = { + queryParam: { + query: `ecm:uuid='{query}' OR ecm:ancestorId='{query}' ${GENERIC_LABELS.AND} ${GENERIC_LABELS.SELECT_QUERY_CONDITIONS}`, + }, + }; break; case GENERIC_LABELS.NXQL: @@ -61,9 +61,8 @@ export const featureMap = () => ({ } return { - requestParams, labels, - data + data, }; - } + }, }); diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-endpoints.service.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-endpoints.service.ts index 41fdf71e..537a7416 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-endpoints.service.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-endpoints.service.ts @@ -1,7 +1,9 @@ import { NetworkService } from "./../../../../shared/services/network.service"; import { Injectable } from "@angular/core"; import { Observable } from "rxjs"; -import { ActionInfo } from "../generic-multi-feature-layout.interface"; +import { + ActionInfo, +} from "../generic-multi-feature-layout.interface"; import { REST_END_POINTS } from "../../../../shared/constants/rest-end-ponts.constants"; @Injectable({ @@ -10,24 +12,32 @@ import { REST_END_POINTS } from "../../../../shared/constants/rest-end-ponts.con export class GenericMultiFeatureEndpointsService { constructor(private networkService: NetworkService) {} performDocumentAction( - requestQueryParam: string | null, - requestBodyParam: string | null, - featureEndpoint: string, + requestUrl: string | null, + requestParams: unknown, + featureEndpoint: string ): Observable { return this.networkService.makeHttpRequest( REST_END_POINTS[featureEndpoint as keyof typeof REST_END_POINTS], - {queryParam: {query: requestQueryParam}, bodyParam: {query: requestBodyParam}} + /* + No params for queryParam since, the only param, i.e. 'query' is appended to the url + & no request url for bodyParam, since endpoint is already sent as the 1st parameter here, and query is part of body params + */ + { queryParam: { requestUrl }, bodyParam: { requestParams } } ); } performFolderAction( - requestQueryParam: string | null, - requestBodyParam: string | null, - featureEndpoint: string, + requestUrl: string | null, + requestParams: unknown, + featureEndpoint: string ): Observable { return this.networkService.makeHttpRequest( REST_END_POINTS[featureEndpoint as keyof typeof REST_END_POINTS], - {queryParam: {query: requestQueryParam}, bodyParam: {query: requestBodyParam}} + /* + No params for queryParam since, the only param, i.e. 'query' is appended to the url + & no request url for bodyParam, since endpoint is already sent as the 1st parameter here, and query is part of body params + */ + { queryParam: { requestUrl }, bodyParam: { requestParams } } ); } diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-utilities.service.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-utilities.service.ts index e60d73d5..b68c460a 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-utilities.service.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/services/generic-multi-feature-utilities.service.ts @@ -82,6 +82,6 @@ export class GenericMultiFeatureUtilitiesService { } insertParamInQuery(requestQuery: string, param: string) { - return requestQuery.replaceAll("{queryParam}", param); + return requestQuery.replaceAll("{query}", param); } } diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/actions.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/actions.ts index 8daeaafd..bb21fba7 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/actions.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/actions.ts @@ -4,7 +4,7 @@ import { ActionInfo } from "../generic-multi-feature-layout.interface"; export const performDocumentAction = createAction( "[Admin] Perform Action", - props<{ requestQueryParam: string | null; requestBodyParam: string | null; featureEndpoint: string }>() + props<{ requestUrl: string | null; requestParams: unknown; featureEndpoint: string }>() ); export const onDocumentActionLaunch = createAction( "[Admin] On Action Launch", @@ -19,7 +19,7 @@ export const resetDocumentActionState = createAction( ); export const performFolderAction = createAction( "[Admin] Perform Folder Action", - props<{ requestQueryParam: string | null; requestBodyParam: string | null; featureEndpoint: string }>() + props<{ requestUrl: string | null; requestParams: unknown; featureEndpoint: string }>() ); export const onFolderActionLaunch = createAction( "[Admin] On Folder Action Launch", diff --git a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/effects.ts b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/effects.ts index 35b1f43e..cd385057 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/effects.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/features/sub-features/generic-multi-feature-layout/store/effects.ts @@ -14,7 +14,7 @@ export const loadPerformDocumentActionEffect = createEffect( ofType(FeatureActions.performDocumentAction), switchMap((action) => { return genericMultiFeatureEndpointsService - .performDocumentAction(action?.requestQueryParam, action?.requestBodyParam, action?.featureEndpoint) + .performDocumentAction(action?.requestUrl, action?.requestParams, action?.featureEndpoint) .pipe( map((data) => { return FeatureActions.onDocumentActionLaunch({ @@ -41,7 +41,7 @@ export const loadPerformFolderActionEffect = createEffect( ofType(FeatureActions.performFolderAction), switchMap((action) => { return genericMultiFeatureEndpointsService - .performFolderAction(action?.requestQueryParam, action?.requestBodyParam, action?.featureEndpoint) + .performFolderAction(action?.requestUrl, action?.requestParams, action?.featureEndpoint) .pipe( map((data) => { return FeatureActions.onFolderActionLaunch({ diff --git a/nuxeo-admin-console-web/angular-app/src/app/shared/services/network.service.ts b/nuxeo-admin-console-web/angular-app/src/app/shared/services/network.service.ts index 5dd14d54..0dd4ccb6 100644 --- a/nuxeo-admin-console-web/angular-app/src/app/shared/services/network.service.ts +++ b/nuxeo-admin-console-web/angular-app/src/app/shared/services/network.service.ts @@ -44,41 +44,17 @@ export class NetworkService { delete data["urlParam"]; } - // iterate & add if (data?.["queryParam"]) { - const queryParam = data["queryParam"] as { query: string }; - url += `?query=${queryParam["query"]}`; - delete data["query"]; - } - - /* if (data?.["bodyParam"]) { - const renamedObj = Object.fromEntries( - Object.entries(data).map(([key, value]) => - [`query`, value] - ) - ) - data = renamedObj; - } */ - let bodyParam = {} as { query: string }; - if (data?.["bodyParam"]) { - bodyParam = data["bodyParam"] as { query: string }; - } - - if ( - data?.["bodyParam"] && - typeof data["bodyParam"] === "object" && // Ensure it's an object - "query" in data["bodyParam"] && // Check if "query" exists - typeof (data["bodyParam"] as Record)["query"] === "object" && // Check if 'query' is an object - Object.keys((data["bodyParam"] as Record)["query"]).length > - 0 - ) { - // 'query' is an object and it is not empty - bodyParam = data["bodyParam"] as { query: string }; + const queryParam = data["queryParam"] as { requestUrl: string }; + if (queryParam["requestUrl"] !== "") { + url += `?query=${queryParam["requestUrl"]}`; + delete data["queryParam"]; + } } switch (method) { case "POST": - return this.http.post(url, bodyParam["query"] || {}); + return this.http.post(url, data?.["bodyParam"] || {}); break; case "PUT": return this.http.put(url, data || {});