Skip to content

Commit ce0aa8f

Browse files
committed
refactor(api, web): clean up code formatting and improve readability in various files, including email handling and dashboard components
1 parent c228355 commit ce0aa8f

13 files changed

Lines changed: 161 additions & 74 deletions

File tree

apps/api/src/db/queries.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,12 @@ export async function getDeploymentsGroupedByWorkflow(
689689
db
690690
.select({
691691
workflowId: deployments.workflowId,
692-
maxVersion: sql<number>`MAX(${deployments.version})`.mapWith(Number).as("max_version"),
693-
deploymentCount: sql<number>`COUNT(${deployments.id})`.mapWith(Number).as("deployment_count"),
692+
maxVersion: sql<number>`MAX(${deployments.version})`
693+
.mapWith(Number)
694+
.as("max_version"),
695+
deploymentCount: sql<number>`COUNT(${deployments.id})`
696+
.mapWith(Number)
697+
.as("deployment_count"),
694698
})
695699
.from(deployments)
696700
.innerJoin(
@@ -727,11 +731,18 @@ export async function getDeploymentsGroupedByWorkflow(
727731
.innerJoin(
728732
actualLatestDeployment,
729733
and(
730-
eq(actualLatestDeployment.workflowId, workflowDeploymentAggregates.workflowId),
731-
eq(actualLatestDeployment.version, workflowDeploymentAggregates.maxVersion)
734+
eq(
735+
actualLatestDeployment.workflowId,
736+
workflowDeploymentAggregates.workflowId
737+
),
738+
eq(
739+
actualLatestDeployment.version,
740+
workflowDeploymentAggregates.maxVersion
741+
)
732742
)
733743
)
734-
.innerJoin( // Ensure workflows themselves are filtered by the organization
744+
.innerJoin(
745+
// Ensure workflows themselves are filtered by the organization
735746
organizations,
736747
and(
737748
eq(workflows.organizationId, organizations.id),
@@ -1064,4 +1075,3 @@ export async function getExecutionWithVisibility(
10641075

10651076
return execution;
10661077
}
1067-

apps/api/src/email.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ export async function handleIncomingEmail(
5454
return;
5555
}
5656

57-
let [triggerType, organizationIdOrHandle, workflowIdOrHandle, version] =
58-
parts;
57+
const [
58+
triggerType,
59+
organizationIdOrHandle,
60+
workflowIdOrHandle,
61+
deploymentVersion,
62+
] = parts;
63+
const version = deploymentVersion || "latest";
5964

6065
if (triggerType !== "workflow") {
61-
console.error(
62-
`Invalid trigger type: ${triggerType}. Expected "workflow".`
63-
);
66+
console.error(`Invalid trigger type: ${triggerType}. Expected "workflow".`);
6467
return;
6568
}
6669

67-
if (!version) {
68-
version = "latest";
69-
}
70+
// TODO: Use triggerType if needed in the future. For now, it's parsed but not used.
71+
console.log(`Parsed trigger type: ${triggerType}`);
7072

7173
const db = createDatabase(env.DB);
7274

apps/api/src/routes/dashboard.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,23 @@ dashboard.get("/", async (c) => {
3333
const workflowsCount = workflows.length;
3434

3535
// Deployments count
36-
const deployments = await getDeploymentsGroupedByWorkflow(db, organizationId);
36+
const deployments = await getDeploymentsGroupedByWorkflow(
37+
db,
38+
organizationId
39+
);
3740
const deploymentsCount = deployments.reduce(
3841
(acc: number, w: { deploymentCount: number }) => acc + w.deploymentCount,
3942
0
4043
);
4144

4245
// Executions stats
43-
const executions: ExecutionRow[] = await listExecutions(db, organizationId, {
44-
limit: 10,
45-
}); // limit for perf
46+
const executions: ExecutionRow[] = await listExecutions(
47+
db,
48+
organizationId,
49+
{
50+
limit: 10,
51+
}
52+
); // limit for perf
4653
const totalExecutions = executions.length;
4754
const runningExecutions = executions.filter(
4855
(e: ExecutionRow) => e.status === ExecutionStatus.EXECUTING

apps/api/src/routes/deployments.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ deploymentRoutes.get("/", jwtMiddleware, async (c) => {
4444
const organizationId = c.get("organizationId")!;
4545
const db = createDatabase(c.env.DB);
4646

47-
const groupedDeployments = await getDeploymentsGroupedByWorkflow(db, organizationId);
47+
const groupedDeployments = await getDeploymentsGroupedByWorkflow(
48+
db,
49+
organizationId
50+
);
4851

4952
// Transform to match WorkflowDeployment type
5053
const typedDeployments: WorkflowDeployment[] = groupedDeployments;
@@ -100,7 +103,11 @@ deploymentRoutes.get("/:workflowIdOrHandle", jwtMiddleware, async (c) => {
100103
}
101104

102105
// Get the latest deployment
103-
const deployment = await getLatestDeployment(db, workflowIdOrHandle, organizationId);
106+
const deployment = await getLatestDeployment(
107+
db,
108+
workflowIdOrHandle,
109+
organizationId
110+
);
104111

105112
if (!deployment) {
106113
return c.json({ error: "No deployments found for this workflow" }, 404);
@@ -142,8 +149,11 @@ deploymentRoutes.post("/:workflowIdOrHandle", jwtMiddleware, async (c) => {
142149

143150
// Get the latest version number and increment
144151
const latestVersion =
145-
(await getLatestDeploymentsVersionNumbers(db, workflowIdOrHandle, organizationId)) ||
146-
0;
152+
(await getLatestDeploymentsVersionNumbers(
153+
db,
154+
workflowIdOrHandle,
155+
organizationId
156+
)) || 0;
147157
const newVersion = latestVersion + 1;
148158

149159
// Create new deployment
@@ -191,7 +201,11 @@ deploymentRoutes.get(
191201
}
192202

193203
// Get all deployments for this workflow
194-
const deploymentsList = await getDeployments(db, workflowIdOrHandle, organizationId);
204+
const deploymentsList = await getDeployments(
205+
db,
206+
workflowIdOrHandle,
207+
organizationId
208+
);
195209

196210
// Transform to match WorkflowDeploymentVersion type
197211
const deploymentVersions = deploymentsList.map((deployment) => {

apps/api/src/routes/executions.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
createDatabase,
1515
getExecution,
1616
getExecutionWithVisibility,
17-
getOrganization,
1817
getWorkflowName,
1918
getWorkflowNames,
2019
listExecutions,
@@ -39,7 +38,11 @@ executionRoutes.get("/:id", apiKeyOrJwtMiddleware, async (c) => {
3938
}
4039

4140
// Get workflow name
42-
const workflowName = await getWorkflowName(db, execution.workflowId, organizationId);
41+
const workflowName = await getWorkflowName(
42+
db,
43+
execution.workflowId,
44+
organizationId
45+
);
4346

4447
const executionData = execution.data as WorkflowExecution;
4548
const workflowExecution: WorkflowExecution = {
@@ -81,11 +84,7 @@ executionRoutes.get("/", jwtMiddleware, async (c) => {
8184
offset: parsedOffset,
8285
};
8386

84-
const executions = await listExecutions(
85-
db,
86-
organizationId,
87-
queryParams
88-
);
87+
const executions = await listExecutions(db, organizationId, queryParams);
8988

9089
// Get workflow names for all executions
9190
const workflowIds = [...new Set(executions.map((e) => e.workflowId))];
@@ -119,7 +118,11 @@ executionRoutes.patch("/:id/share/public", jwtMiddleware, async (c) => {
119118
const organizationId = c.get("organizationId")!;
120119

121120
try {
122-
const execution = await getExecutionWithVisibility(db, executionId, organizationId);
121+
const execution = await getExecutionWithVisibility(
122+
db,
123+
executionId,
124+
organizationId
125+
);
123126

124127
if (!execution) {
125128
return c.json({ error: "Execution not found" }, 404);

apps/api/src/routes/workflows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ workflowRoutes.get("/", jwtMiddleware, async (c) => {
5959
// Convert DB workflow objects to WorkflowWithMetadata objects
6060
const workflows: WorkflowWithMetadata[] = allWorkflows.map((workflow) => {
6161
return {
62-
id: workflow.id,
62+
id: workflow.id,
6363
name: workflow.name,
6464
handle: workflow.handle,
6565
type: workflow.data.type,

apps/web/src/components/deployments/email-integration-card.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ export function EmailIntegrationCard({
7272
</CardContent>
7373
</Card>
7474
);
75-
}
75+
}

apps/web/src/components/workflow/execution-email-dialog.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import { Textarea } from "@/components/ui/textarea"; // For body field
1919

2020
// Define the Zod schema as the primary source of truth for the form's shape
2121
const emailDialogZodSchema = z.object({
22-
from: z.string().email({ message: "Invalid email address for From field." }).min(1, { message: "From is required." }),
22+
from: z
23+
.string()
24+
.email({ message: "Invalid email address for From field." })
25+
.min(1, { message: "From is required." }),
2326
subject: z.string().min(1, { message: "Subject is required." }),
2427
body: z.string().min(1, { message: "Body is required." }),
2528
});
@@ -41,7 +44,6 @@ type Assert<T, U extends T> = U;
4144
type _SchemaMatchesData = Assert<EmailData, EmailDialogFormShape>;
4245
type _DataMatchesSchema = Assert<EmailDialogFormShape, EmailData>;
4346

44-
4547
type EmailDialogProps = {
4648
isOpen: boolean;
4749
onClose: () => void;
@@ -61,7 +63,8 @@ export function EmailDialog({
6163
handleSubmit,
6264
reset,
6365
formState: { errors, isDirty, isValid },
64-
} = useForm<EmailDialogFormShape>({ // Use the inferred type from the Zod schema
66+
} = useForm<EmailDialogFormShape>({
67+
// Use the inferred type from the Zod schema
6568
resolver: zodResolver(emailDialogZodSchema), // Use the Zod schema directly
6669
mode: "onChange",
6770
defaultValues: {
@@ -97,18 +100,25 @@ export function EmailDialog({
97100
<AlertDialogHeader>
98101
<AlertDialogTitle>Simulate Email Trigger</AlertDialogTitle>
99102
<AlertDialogDescription>
100-
Fill in the details below to simulate sending an email. This will trigger the workflow.
103+
Fill in the details below to simulate sending an email. This will
104+
trigger the workflow.
101105
</AlertDialogDescription>
102106
</AlertDialogHeader>
103107
<form onSubmit={handleSubmit(processSubmit)} id="emailTriggerForm">
104108
<div className="space-y-4 py-4 max-h-[60vh] overflow-y-auto pr-2">
105109
<div>
106-
<Label htmlFor="from">From <span className="text-destructive font-medium">*</span></Label>
110+
<Label htmlFor="from">
111+
From <span className="text-destructive font-medium">*</span>
112+
</Label>
107113
<Controller
108114
name="from"
109115
control={control}
110116
render={({ field }) => (
111-
<Input id="from" {...field} placeholder="sender@example.com" />
117+
<Input
118+
id="from"
119+
{...field}
120+
placeholder="sender@example.com"
121+
/>
112122
)}
113123
/>
114124
{errors.from && (
@@ -118,7 +128,9 @@ export function EmailDialog({
118128
)}
119129
</div>
120130
<div>
121-
<Label htmlFor="subject">Subject <span className="text-destructive font-medium">*</span></Label>
131+
<Label htmlFor="subject">
132+
Subject <span className="text-destructive font-medium">*</span>
133+
</Label>
122134
<Controller
123135
name="subject"
124136
control={control}
@@ -133,12 +145,19 @@ export function EmailDialog({
133145
)}
134146
</div>
135147
<div>
136-
<Label htmlFor="body">Body <span className="text-destructive font-medium">*</span></Label>
148+
<Label htmlFor="body">
149+
Body <span className="text-destructive font-medium">*</span>
150+
</Label>
137151
<Controller
138152
name="body"
139153
control={control}
140154
render={({ field }) => (
141-
<Textarea id="body" {...field} placeholder="Email body content..." className="min-h-[100px]" />
155+
<Textarea
156+
id="body"
157+
{...field}
158+
placeholder="Email body content..."
159+
className="min-h-[100px]"
160+
/>
142161
)}
143162
/>
144163
{errors.body && (
@@ -164,4 +183,4 @@ export function EmailDialog({
164183
</AlertDialogContent>
165184
</AlertDialog>
166185
);
167-
}
186+
}

apps/web/src/pages/dashboard-page.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { CreateWorkflowRequest, WorkflowType } from "@dafthunk/types";
22
import { format } from "date-fns";
3-
import { AlertCircle, Clock, Logs, Plus, Target, Workflow, MoreHorizontal, Eye } from "lucide-react";
3+
import {
4+
AlertCircle,
5+
Clock,
6+
Logs,
7+
MoreHorizontal,
8+
Plus,
9+
Target,
10+
Workflow,
11+
} from "lucide-react";
412
import { useState } from "react";
513
import { useNavigate } from "react-router";
614
import { Link } from "react-router";
@@ -12,14 +20,14 @@ import { InsetLoading } from "@/components/inset-loading";
1220
import { Button } from "@/components/ui/button";
1321
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
1422
import { DataTableCard } from "@/components/ui/data-table-card";
15-
import { CreateWorkflowDialog } from "@/components/workflow/create-workflow-dialog";
16-
import type { WorkflowExecutionStatus } from "@/components/workflow/workflow-types";
1723
import {
1824
DropdownMenu,
1925
DropdownMenuContent,
2026
DropdownMenuItem,
2127
DropdownMenuTrigger,
2228
} from "@/components/ui/dropdown-menu";
29+
import { CreateWorkflowDialog } from "@/components/workflow/create-workflow-dialog";
30+
import type { WorkflowExecutionStatus } from "@/components/workflow/workflow-types";
2331
import { useDashboard } from "@/services/dashboard-service";
2432
import { createWorkflow } from "@/services/workflow-service";
2533

@@ -204,7 +212,9 @@ export function DashboardPage() {
204212
const execution = row.original as RecentExecutionItem;
205213
return (
206214
<span className="text-xs text-muted-foreground">
207-
{execution.startedAt ? format(new Date(execution.startedAt), "PPpp") : "-"}
215+
{execution.startedAt
216+
? format(new Date(execution.startedAt), "PPpp")
217+
: "-"}
208218
</span>
209219
);
210220
},
@@ -242,13 +252,19 @@ export function DashboardPage() {
242252
formattedDuration += `${minutes}m `;
243253
}
244254
formattedDuration += `${seconds}s`;
245-
if (formattedDuration.trim() === "0s" && durationMs > 0 && durationMs < 1000) {
255+
if (
256+
formattedDuration.trim() === "0s" &&
257+
durationMs > 0 &&
258+
durationMs < 1000
259+
) {
246260
formattedDuration = "<1s";
247-
} else if (formattedDuration.trim() === "0s" && durationMs === 0) {
248-
formattedDuration = "0s";
261+
} else if (
262+
formattedDuration.trim() === "0s" &&
263+
durationMs === 0
264+
) {
265+
formattedDuration = "0s";
249266
}
250267

251-
252268
return <div>{formattedDuration.trim()}</div>;
253269
}
254270
return <div>-</div>;

apps/web/src/pages/workflows/deployment-detail-page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
Clock,
88
GitCommitHorizontal,
99
History,
10-
Mail,
1110
MoreHorizontal,
1211
Play,
1312
} from "lucide-react";

0 commit comments

Comments
 (0)