Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: use validated data for workflow step updates and creation

Marc Seitzd8e73fd8c164
diff --git a/app/(ee)/api/workflows/[workflowId]/steps/[stepId]/route.ts b/app/(ee)/api/workflows/[workflowId]/steps/[stepId]/route.ts
index deb7a1e9..1887067f 100644
--- a/app/(ee)/api/workflows/[workflowId]/steps/[stepId]/route.ts
+++ b/app/(ee)/api/workflows/[workflowId]/steps/[stepId]/route.ts
@@ -51,6 +51,7 @@ export async function PATCH(
}
// Validate conditions if provided
+ let validatedConditions: any | undefined;
if (validation.data.conditions) {
const conditionsValidation = validateConditions(
validation.data.conditions,
@@ -61,9 +62,11 @@ export async function PATCH(
{ status: 400 },
);
}
+ validatedConditions = conditionsValidation.data;
}
// Validate actions if provided
+ let validatedActions: any[] | undefined;
if (validation.data.actions) {
const actionsValidation = validateActions(validation.data.actions);
if (!actionsValidation.valid) {
@@ -73,8 +76,11 @@ export async function PATCH(
);
}
+ // Use actionsValidation.data so enrichment persists
+ validatedActions = actionsValidation.data;
+
// Validate target link exists and belongs to the team
- const routeAction = validation.data.actions.find((a) => a.type === "route");
+ const routeAction = validatedActions.find((a) => a.type === "route");
if (routeAction && routeAction.targetLinkId) {
// First get the workflow to get teamId
const workflow = await prisma.workflow.findUnique({
@@ -97,7 +103,7 @@ export async function PATCH(
);
}
- // Update action with target details
+ // Update action with target details (mutates validatedActions)
if (targetLink.linkType === "DOCUMENT_LINK" && targetLink.documentId) {
routeAction.targetDocumentId = targetLink.documentId;
} else if (
@@ -149,11 +155,10 @@ export async function PATCH(
// Extract emails and domains from conditions to sync with link allowList (if conditions updated)
let allowListItems: string[] | undefined;
- if (validation.data.conditions) {
+ if (validatedConditions) {
allowListItems = [];
- const conditionsData = validation.data.conditions as any;
- if (conditionsData.items) {
- conditionsData.items.forEach((condition: any) => {
+ if (validatedConditions.items) {
+ validatedConditions.items.forEach((condition: any) => {
const values = Array.isArray(condition.value)
? condition.value
: [condition.value];
@@ -169,8 +174,8 @@ export async function PATCH(
// Get target link ID (either from update or existing step)
let targetLinkId: string | undefined;
- if (validation.data.actions) {
- const routeAction = validation.data.actions.find((a) => a.type === "route");
+ if (validatedActions) {
+ const routeAction = validatedActions.find((a) => a.type === "route");
targetLinkId = routeAction?.targetLinkId;
} else {
// Get from existing step
@@ -183,11 +188,20 @@ export async function PATCH(
targetLinkId = existingRouteAction?.targetLinkId;
}
+ // Build update data with validated conditions and actions (if provided)
+ const updateData: any = { ...validation.data };
+ if (validatedConditions) {
+ updateData.conditions = validatedConditions;
+ }
+ if (validatedActions) {
+ updateData.actions = validatedActions;
+ }
+
// Update step and optionally update link allowList in transaction
const updates: any[] = [
prisma.workflowStep.update({
where: { id: stepId },
- data: validation.data as any,
+ data: updateData as any,
}),
];
diff --git a/app/(ee)/api/workflows/[workflowId]/steps/route.ts b/app/(ee)/api/workflows/[workflowId]/steps/route.ts
index df2e89a1..ed1669a9 100644
--- a/app/(ee)/api/workflows/[workflowId]/steps/route.ts
+++ b/app/(ee)/api/workflows/[workflowId]/steps/route.ts
@@ -203,7 +203,8 @@ export async function POST(
}
// Validate target link exists and belongs to the team
- const routeAction = actions.find((a) => a.type === "route");
+ // Use actionsValidation.data so enrichment persists
+ const routeAction = actionsValidation.data.find((a) => a.type === "route");
if (routeAction && routeAction.targetLinkId) {
const targetLink = await prisma.link.findUnique({
where: {
@@ -219,7 +220,7 @@ export async function POST(
);
}
- // Update action with target details
+ // Update action with target details (mutates actionsValidation.data)
if (targetLink.linkType === "DOCUMENT_LINK" && targetLink.documentId) {
routeAction.targetDocumentId = targetLink.documentId;
} else if (
@@ -250,8 +251,8 @@ export async function POST(
});
}
- // Create step and update target link's allowList in transaction
- const [newStep] = await prisma.$transaction([
+ // Create step and conditionally update target link's allowList in transaction
+ const transactionSteps: any[] = [
prisma.workflowStep.create({
data: {
workflowId,
@@ -262,14 +263,21 @@ export async function POST(
actions: actionsValidation.data as any,
},
}),
- // Update target link's allowList
- prisma.link.update({
- where: { id: routeAction.targetLinkId },
- data: {
- allowList: allowListItems,
- },
- }),
- ]);
+ ];
+
+ // Only update link allowList if we have a route action with a target link
+ if (routeAction && routeAction.targetLinkId) {
+ transactionSteps.push(
+ prisma.link.update({
+ where: { id: routeAction.targetLinkId },
+ data: {
+ allowList: allowListItems,
+ },
+ }),
+ );
+ }
+
+ const [newStep] = await prisma.$transaction(transactionSteps);
return NextResponse.json(newStep, { status: 201 });
} catch (error) {
diff --git a/ee/features/workflows/lib/validation.ts b/ee/features/workflows/lib/validation.ts
index 13926cc3..1b675fd4 100644
--- a/ee/features/workflows/lib/validation.ts
+++ b/ee/features/workflows/lib/validation.ts
@@ -1,16 +1,17 @@
import { ZodError } from "zod";
+
import {
- ConditionsConfigSchema,
+ type Action,
ActionSchema,
- WorkflowStepDefinitionSchema,
+ type ConditionsConfig,
+ ConditionsConfigSchema,
CreateWorkflowRequestSchema,
- UpdateWorkflowRequestSchema,
CreateWorkflowStepRequestSchema,
- UpdateWorkflowStepRequestSchema,
ReorderStepsRequestSchema,
- type ConditionsConfig,
- type Action,
+ UpdateWorkflowRequestSchema,
+ UpdateWorkflowStepRequestSchema,
type WorkflowStepDefinition,
+ WorkflowStepDefinitionSchema,
} from "./types";
export {
@@ -34,7 +35,9 @@ export function validateConditions(
if (error instanceof ZodError) {
return {
valid: false,
- error: error.errors.map((e) => `${e.path.join(".")}: ${e.message}`).join(", "),
+ error: error.errors
+ .map((e) => `${e.path.join(".")}: ${e.message}`)
+ .join(", "),
};
}
return { valid: false, error: "Invalid conditions format" };
@@ -57,7 +60,9 @@ export function validateActions(
if (error instanceof ZodError) {
return {
valid: false,
- error: error.errors.map((e) => `${e.path.join(".")}: ${e.message}`).join(", "),
+ error: error.errors
+ .map((e) => `${e.path.join(".")}: ${e.message}`)
+ .join(", "),
};
}
return { valid: false, error: "Invalid actions format" };
@@ -79,7 +84,9 @@ export function validateWorkflowStep(
if (error instanceof ZodError) {
return {
valid: false,
- error: error.errors.map((e) => `${e.path.join(".")}: ${e.message}`).join(", "),
+ error: error.errors
+ .map((e) => `${e.path.join(".")}: ${e.message}`)
+ .join(", "),
};
}
return { valid: false, error: "Invalid workflow step format" };
@@ -90,6 +97,7 @@ export function validateWorkflowStep(
* Helper to format Zod validation errors for API responses
*/
export function formatZodError(error: ZodError): string {
- return error.errors.map((e) => `${e.path.join(".")}: ${e.message}`).join(", ");
+ return error.errors
+ .map((e) => `${e.path.join(".")}: ${e.message}`)
+ .join(", ");
}
-