diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_report_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_report_service.py index 92040b2be..ab180fee0 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_report_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_report_service.py @@ -412,11 +412,16 @@ class ProcessInstanceReportService: ) -> list[dict]: """Add_metadata_columns_to_process_instance.""" results = [] + non_metadata_columns = cls.non_metadata_columns() for process_instance_row in process_instance_sqlalchemy_rows: process_instance_mapping = process_instance_row._mapping process_instance_dict = process_instance_row[0].serialized + # import pdb; pdb.set_trace() for metadata_column in metadata_columns: + # if metadata_column["accessor"] not in non_metadata_columns: + # if metadata_column["accessor"] not in cls.process_instance_stock_columns(): if metadata_column["accessor"] not in process_instance_dict: + # import pdb; pdb.set_trace() process_instance_dict[metadata_column["accessor"]] = process_instance_mapping[ metadata_column["accessor"] ] @@ -477,6 +482,14 @@ class ProcessInstanceReportService: """Get_column_names_for_model.""" return [i.name for i in model.__table__.columns] + @classmethod + def process_instance_stock_columns(cls) -> list[str]: + return cls.get_column_names_for_model(ProcessInstanceModel) + + @classmethod + def non_metadata_columns(cls) -> list[str]: + return cls.process_instance_stock_columns() + ['process_initiator_username'] + @classmethod def builtin_column_options(cls) -> list[dict]: """Builtin_column_options.""" @@ -663,16 +676,15 @@ class ProcessInstanceReportService: process_instance_query = process_instance_query.filter(UserGroupAssignmentModel.user_id == user.id) instance_metadata_aliases = {} - report_column_list = cls.get_filter_value(filters, 'report_column_list') - report_filter_by_list = cls.get_filter_value(filters, 'report_filter_by_list') - stock_columns = cls.get_column_names_for_model(ProcessInstanceModel) - if isinstance(report_column_list, list): - process_instance_report.report_metadata["columns"] = report_column_list - if isinstance(report_filter_by_list, list): - process_instance_report.report_metadata["filter_by"] = report_filter_by_list + # print(f"report_metadata['columns']: {report_metadata['columns']}") + # import pdb; pdb.set_trace() + if report_metadata['columns'] and len(report_metadata['columns']) > 0: + process_instance_report.report_metadata["columns"] = report_metadata['columns'] + if report_metadata['filter_by'] and len(report_metadata['filter_by']) > 0: + process_instance_report.report_metadata["filter_by"] = report_metadata['filter_by'] for column in process_instance_report.report_metadata["columns"]: - if column["accessor"] in stock_columns: + if column["accessor"] in cls.non_metadata_columns(): continue instance_metadata_alias = aliased(ProcessInstanceMetadataModel) instance_metadata_aliases[column["accessor"]] = instance_metadata_alias @@ -699,32 +711,33 @@ class ProcessInstanceReportService: instance_metadata_alias, and_(*conditions), isouter=isouter ).add_columns(func.max(instance_metadata_alias.value).label(column["accessor"])) - order_by_query_array = [] - order_by_array = process_instance_report.report_metadata["order_by"] - if len(order_by_array) < 1: - order_by_array = ProcessInstanceReportModel.default_order_by() - for order_by_option in order_by_array: - attribute = re.sub("^-", "", order_by_option) - if attribute in stock_columns: - if order_by_option.startswith("-"): - order_by_query_array.append(getattr(ProcessInstanceModel, attribute).desc()) - else: - order_by_query_array.append(getattr(ProcessInstanceModel, attribute).asc()) - elif attribute in instance_metadata_aliases: - if order_by_option.startswith("-"): - order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).desc()) - else: - order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).asc()) + # order_by_query_array = [] + # order_by_array = process_instance_report.report_metadata["order_by"] + # if len(order_by_array) < 1: + # order_by_array = ProcessInstanceReportModel.default_order_by() + # for order_by_option in order_by_array: + # attribute = re.sub("^-", "", order_by_option) + # if attribute in cls.process_instance_stock_columns(): + # if order_by_option.startswith("-"): + # order_by_query_array.append(getattr(ProcessInstanceModel, attribute).desc()) + # else: + # order_by_query_array.append(getattr(ProcessInstanceModel, attribute).asc()) + # elif attribute in instance_metadata_aliases: + # if order_by_option.startswith("-"): + # order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).desc()) + # else: + # order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).asc()) process_instances = ( process_instance_query.group_by(ProcessInstanceModel.id) .add_columns(ProcessInstanceModel.id) - .order_by(*order_by_query_array) + # .order_by(*order_by_query_array) .paginate(page=page, per_page=per_page, error_out=False) ) results = cls.add_metadata_columns_to_process_instance( process_instances.items, process_instance_report.report_metadata["columns"] ) + # import pdb; pdb.set_trace() for value in cls.check_filter_value(filters, 'oldest_open_human_task_fields'): results = cls.add_human_task_fields(results, value) diff --git a/spiffworkflow-frontend/src/components/ProcessInstanceListTable.tsx b/spiffworkflow-frontend/src/components/ProcessInstanceListTable.tsx index 9b521e350..85de9ba9d 100644 --- a/spiffworkflow-frontend/src/components/ProcessInstanceListTable.tsx +++ b/spiffworkflow-frontend/src/components/ProcessInstanceListTable.tsx @@ -128,7 +128,9 @@ export default function ProcessInstanceListTable({ const permissionRequestData: PermissionsToCheck = { [targetUris.userSearch]: ['GET'], }; - const { ability } = usePermissionFetcher(permissionRequestData); + const { ability, permissionsLoaded } = usePermissionFetcher( + permissionRequestData + ); const canSearchUsers: boolean = ability.can('GET', targetUris.userSearch); const [processInstances, setProcessInstances] = useState([]); @@ -301,6 +303,10 @@ export default function ProcessInstanceListTable({ // eslint-disable-next-line sonarjs/cognitive-complexity useEffect(() => { + if (!permissionsLoaded) { + return; + } + // we apparently cannot use a state set in a useEffect from within that same useEffect // so use a variable instead let processModelSelectionItemsForUseEffect: ProcessModel[] = []; @@ -311,7 +317,6 @@ export default function ProcessInstanceListTable({ setProcessInstances(processInstancesFromApi); setPagination(result.pagination); setProcessInstanceFilters(result.filters); - setReportMetadata(result.report.report_metadata); if (result.report.id) { setProcessInstanceReportSelection(result.report); @@ -350,6 +355,10 @@ export default function ProcessInstanceListTable({ setProcessStatusSelection( (reportFilter.field_value || '').split(',') ); + setShowFilterOptions(true); + } else if (reportFilter.field_name === 'process_initiator_username') { + searchForProcessInitiator(reportFilter.field_value || ''); + setShowFilterOptions(true); } else if (reportFilter.field_name === 'process_model_identifier') { selectedProcessModelIdentifier = reportFilter.field_value || undefined; @@ -377,16 +386,11 @@ export default function ProcessInstanceListTable({ (processModel: ProcessModel) => { if (processModel.id === selectedProcessModelIdentifier) { setProcessModelSelection(processModel); + setShowFilterOptions(true); } } ); - // const postBody: ReportMetadata = { - // columns: [], - // filter_by: [], - // order_by: [], - // }; - // // if (searchParams.get('report_id')) { // queryParamString += `&report_id=${searchParams.get('report_id')}`; // } else if (reportIdentifier) { @@ -402,40 +406,6 @@ export default function ProcessInstanceListTable({ // postBody.columns = reportColumnsList; // } // } - // if (searchParams.get('report_filter_by')) { - // const reportFilterByBase64 = searchParams.get('report_filter_by'); - // if (reportFilterByBase64) { - // const reportFilterByList = JSON.parse( - // decodeBase64(reportFilterByBase64) - // ); - // postBody.filter_by = reportFilterByList; - // } - // } - // - // Object.keys(parametersToGetFromSearchParams).forEach( - // (paramName: string) => { - // if ( - // paramName === 'process_model_identifier' && - // processModelFullIdentifier - // ) { - // postBody.filter_by.push({ - // field_name: 'process_model_identifier', - // field_value: processModelFullIdentifier, - // }); - // } else if (searchParams.get(paramName)) { - // // @ts-expect-error TS(7053) FIXME: - // const functionToCall = parametersToGetFromSearchParams[paramName]; - // postBody.filter_by.push({ - // field_name: paramName, - // field_value: searchParams.get(paramName), - // }); - // if (functionToCall !== null) { - // functionToCall(searchParams.get(paramName) || ''); - // } - // setShowFilterOptions(true); - // } - // } - // ); // eslint-disable-next-line prefer-const let { page, perPage } = getPageInfoFromSearchParams( @@ -460,7 +430,7 @@ export default function ProcessInstanceListTable({ failureCallback: stopRefreshing, onUnauthorized: stopRefreshing, postBody: { - report_metadata: reportMetadataBody, + report_metadata: reportMetadataBodyToUse, }, }); } @@ -533,6 +503,7 @@ export default function ProcessInstanceListTable({ reportIdentifier, additionalParams, processInstanceApiSearchPath, + permissionsLoaded, ]); // // This sets the filter data using the saved reports returned from the initial instance_list query. @@ -682,14 +653,17 @@ export default function ProcessInstanceListTable({ const reportColumns = () => { if (reportMetadata) { - return (reportMetadata as any).columns; + return reportMetadata.columns; } - return null; + return []; }; - const reportFilterBy = () => { - return (reportMetadata as any).filter_by; - }; + // const reportFilterBy = () => { + // if (reportMetadata) { + // return reportMetadata.filter_by; + // } + // return null + // }; const navigateToNewReport = (queryParamString: string) => { removeError(); @@ -698,6 +672,16 @@ export default function ProcessInstanceListTable({ navigate(`${processInstanceListPathPrefix}?${queryParamString}`); }; + const removeFieldFromReportMetadata = ( + postBody: ReportMetadata, + fieldName: string + ) => { + const filtersToKeep = postBody.filter_by.filter( + (rf: ReportFilter) => rf.field_name !== fieldName + ); + postBody.filter_by = filtersToKeep; + }; + const addFieldValueToReportMetadata = ( postBody: ReportMetadata, fieldName: string, @@ -708,6 +692,8 @@ export default function ProcessInstanceListTable({ field_name: fieldName, field_value: fieldValue, }); + } else { + removeFieldFromReportMetadata(postBody, fieldName); } }; @@ -721,8 +707,6 @@ export default function ProcessInstanceListTable({ undefined, paginationQueryParamPrefix ); - // let queryParamString = `per_page=${perPage}&page=${page}&user_filter=true`; - const queryParamString = `per_page=${perPage}&page=${page}`; const { valid, startFromSeconds, @@ -735,11 +719,17 @@ export default function ProcessInstanceListTable({ return; } - const postBody: ReportMetadata = { - columns: [], - filter_by: [], - order_by: [], - }; + let postBody = null; + if (reportMetadata) { + postBody = { ...reportMetadata }; + } + if (!postBody) { + postBody = { + columns: [], + filter_by: [], + order_by: [], + }; + } addFieldValueToReportMetadata(postBody, 'start_from', startFromSeconds); addFieldValueToReportMetadata(postBody, 'start_to', startToSeconds); @@ -751,6 +741,8 @@ export default function ProcessInstanceListTable({ 'process_status', processStatusSelection.join(',') ); + } else { + removeFieldFromReportMetadata(postBody, 'process_status'); } if (processModelSelection) { @@ -759,17 +751,17 @@ export default function ProcessInstanceListTable({ 'process_model_identifier', processModelSelection.id ); + } else { + removeFieldFromReportMetadata(postBody, 'process_model_identifier'); } - if (processInstanceReportSelection) { - addFieldValueToReportMetadata( - postBody, - 'report_id', - processInstanceReportSelection.id.toString() - ); - } - - postBody.columns = reportColumns(); + // if (processInstanceReportSelection) { + // addFieldValueToReportMetadata( + // postBody, + // 'report_id', + // processInstanceReportSelection.id.toString() + // ); + // } if (processInitiatorSelection) { addFieldValueToReportMetadata( @@ -777,39 +769,17 @@ export default function ProcessInstanceListTable({ 'process_initiator_username', processInitiatorSelection.username ); - // navigateToNewReport(queryParamString); + } else { + removeFieldFromReportMetadata(postBody, 'process_initiator_username'); } - // } else if (processInitiatorText) { - // HttpService.makeCallToBackend({ - // path: targetUris.userExists, - // httpMethod: 'POST', - // postBody: { username: processInitiatorText }, - // successCallback: (result: any) => { - // if (result.user_found) { - // queryParamString += `&process_initiator_username=${processInitiatorText}`; - // // navigateToNewReport(queryParamString); - // } else { - // setProcessInitiatorNotFoundErrorText( - // `The provided username is invalid. Please type the exact username.` - // ); - // } - // }, - // }); - // } else { - // navigateToNewReport(queryParamString); - // } - - // http://localhost:7001/admin/process-instances/for-me?per_page=50&page=1&report_metadata_base64=eyJjb2x1bW5zIjpudWxsLCJmaWx0ZXJfYnkiOlt7ImZpZWxkX25hbWUiOiJwcm9jZXNzX3N0YXR1cyIsImZpZWxkX3ZhbHVlIjoiY29tcGxldGUifV0sIm9yZGVyX2J5IjpbXX0%3D - // const queryParamString = `per_page=${perPage}&page=${page}`; setListHasBeenFiltered(true); + setReportMetadata(postBody); searchParams.set('per_page', perPage.toString()); searchParams.set('page', page.toString()); - - // const reportMetadataBase64 = encodeBase64(JSON.stringify(postBody)); - // searchParams.set('report_metadata_base64', reportMetadataBase64); setSearchParams(searchParams); + const queryParamString = `per_page=${perPage}&page=${page}`; HttpService.makeCallToBackend({ path: `${processInstanceApiSearchPath}?${queryParamString}`, httpMethod: 'POST', @@ -1081,6 +1051,7 @@ export default function ProcessInstanceListTable({ reportColumn, { filter_field_value: '', filter_operator: '' } ); + if (reportColumn.filterable) { const reportFilter = getFilterByFromReportMetadata( reportColumnForEditing.accessor ); @@ -1090,6 +1061,7 @@ export default function ProcessInstanceListTable({ reportColumnForEditing.filter_operator = reportFilter.operator || 'equals'; } + } return reportColumnForEditing; }; @@ -1212,10 +1184,9 @@ export default function ProcessInstanceListTable({ }; const columnSelections = () => { - if (reportColumns()) { + if (reportColumns().length > 0) { const tags: any = []; - - (reportColumns() as any).forEach((reportColumn: ReportColumn) => { + reportColumns().forEach((reportColumn: ReportColumn) => { const reportColumnForEditing = reportColumnToReportColumnForEditing(reportColumn);