refactor: Consolidate WorkflowService.getMany() (no-changelog) (#6892)
In scope: - Consolidate `WorkflowService.getMany()`. - Support non-entity field `ownedBy` for `select`. - Support `tags` for `filter`. - Move `addOwnerId` to `OwnershipService`. - Remove unneeded check for `filter.id`. - Simplify DTO validation for `filter` and `select`. - Expand tests for `GET /workflows`. Workflow list query DTOs: ``` filter → name, active, tags select → id, name, active, tags, createdAt, updatedAt, versionId, ownedBy ``` Out of scope: - Migrate `shared_workflow.roleId` and `shared_credential.roleId` to string IDs. - Refactor `WorkflowHelpers.getSharedWorkflowIds()`.
This commit is contained in:
@@ -1,46 +1,34 @@
|
||||
import { handleListQueryError } from './error';
|
||||
import { jsonParse } from 'n8n-workflow';
|
||||
import { WorkflowSchema } from './workflow.schema';
|
||||
import * as utils from '@/utils';
|
||||
import type { ListQueryRequest } from '@/requests';
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
|
||||
import { WorkflowSelect } from './dtos/workflow.select.dto';
|
||||
import * as ResponseHelper from '@/ResponseHelper';
|
||||
import { toError } from '@/utils';
|
||||
|
||||
import type { RequestHandler } from 'express';
|
||||
import type { Schema } from '@/middlewares/listQuery/schema';
|
||||
import type { ListQuery } from '@/requests';
|
||||
|
||||
function toQuerySelect(rawSelect: string, schema: typeof Schema) {
|
||||
const asArr = jsonParse(rawSelect, { errorMessage: 'Failed to parse select JSON' });
|
||||
|
||||
if (!utils.isStringArray(asArr)) {
|
||||
throw new Error('Parsed select is not a string array');
|
||||
}
|
||||
|
||||
return asArr.reduce<Record<string, true>>((acc, field) => {
|
||||
if (!schema.fieldNames.includes(field)) return acc;
|
||||
return (acc[field] = true), acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
export const selectListQueryMiddleware: RequestHandler = (req: ListQueryRequest, res, next) => {
|
||||
export const selectListQueryMiddleware: RequestHandler = (req: ListQuery.Request, res, next) => {
|
||||
const { select: rawSelect } = req.query;
|
||||
|
||||
if (!rawSelect) return next();
|
||||
|
||||
let schema;
|
||||
let Select;
|
||||
|
||||
if (req.baseUrl.endsWith('workflows')) {
|
||||
schema = WorkflowSchema;
|
||||
Select = WorkflowSelect;
|
||||
} else {
|
||||
return next();
|
||||
}
|
||||
|
||||
try {
|
||||
const select = toQuerySelect(rawSelect, schema);
|
||||
const select = Select.fromString(rawSelect);
|
||||
|
||||
if (Object.keys(select).length === 0) return next();
|
||||
|
||||
req.listQueryOptions = { ...req.listQueryOptions, select };
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
handleListQueryError('select', rawSelect, error);
|
||||
} catch (maybeError) {
|
||||
ResponseHelper.sendErrorResponse(res, toError(maybeError));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user