Skip to content

Commit

Permalink
dynamic parameter passing in document & folder
Browse files Browse the repository at this point in the history
  • Loading branch information
swarnadipa-dev committed Oct 4, 2024
1 parent 220d0cb commit 83eae15
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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],
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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:
Expand All @@ -61,9 +61,8 @@ export const featureMap = () => ({
}

return {
requestParams,
labels,
data
data,
};
}
},
});
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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<ActionInfo> {
return this.networkService.makeHttpRequest<ActionInfo>(
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<ActionInfo> {
return this.networkService.makeHttpRequest<ActionInfo>(
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 } }
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ export class GenericMultiFeatureUtilitiesService {
}

insertParamInQuery(requestQuery: string, param: string) {
return requestQuery.replaceAll("{queryParam}", param);
return requestQuery.replaceAll("{query}", param);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>)["query"] === "object" && // Check if 'query' is an object
Object.keys((data["bodyParam"] as Record<string, any>)["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<T>(url, bodyParam["query"] || {});
return this.http.post<T>(url, data?.["bodyParam"] || {});
break;
case "PUT":
return this.http.put<T>(url, data || {});
Expand Down

0 comments on commit 83eae15

Please sign in to comment.