Skip to content

Commit a3c6415

Browse files
committed
Ensure the task list uses the dev environment of the logged in user
1 parent f4ef008 commit a3c6415

File tree

2 files changed

+35
-51
lines changed

2 files changed

+35
-51
lines changed

apps/webapp/app/presenters/v3/TaskListPresenter.server.ts

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1+
import { CURRENT_DEPLOYMENT_LABEL } from "@trigger.dev/core/v3/isomorphic";
12
import {
23
Prisma,
3-
type TaskTriggerSource,
4-
type TaskRunStatus as TaskRunStatusType,
5-
type RuntimeEnvironment,
64
type TaskRunStatus as DBTaskRunStatus,
5+
type TaskRunStatus as TaskRunStatusType,
6+
type TaskTriggerSource,
77
} from "@trigger.dev/database";
88
import { QUEUED_STATUSES } from "~/components/runs/v3/TaskRunStatus";
9+
import { TaskRunStatus } from "~/database-types";
910
import { sqlDatabaseSchema } from "~/db.server";
10-
import type { Organization } from "~/models/organization.server";
11-
import type { Project } from "~/models/project.server";
12-
import type { User } from "~/models/user.server";
1311
import { logger } from "~/services/logger.server";
1412
import { BasePresenter } from "./basePresenter.server";
15-
import { TaskRunStatus } from "~/database-types";
16-
import { CURRENT_DEPLOYMENT_LABEL } from "@trigger.dev/core/v3/isomorphic";
1713

1814
export type TaskListItem = {
1915
slug: string;
@@ -27,34 +23,7 @@ type Return = Awaited<ReturnType<TaskListPresenter["call"]>>;
2723
export type TaskActivity = Awaited<Return["activity"]>[string];
2824

2925
export class TaskListPresenter extends BasePresenter {
30-
public async call({
31-
userId,
32-
projectSlug,
33-
organizationSlug,
34-
environmentSlug,
35-
}: {
36-
userId: User["id"];
37-
projectSlug: Project["slug"];
38-
organizationSlug: Organization["slug"];
39-
environmentSlug: RuntimeEnvironment["slug"];
40-
}) {
41-
const environment = await this._replica.runtimeEnvironment.findFirstOrThrow({
42-
select: {
43-
id: true,
44-
type: true,
45-
projectId: true,
46-
},
47-
where: {
48-
slug: environmentSlug,
49-
project: {
50-
slug: projectSlug,
51-
},
52-
organization: {
53-
slug: organizationSlug,
54-
},
55-
},
56-
});
57-
26+
public async call({ environmentId, projectId }: { environmentId: string; projectId: string }) {
5827
const tasks = await this._replica.$queryRaw<
5928
{
6029
id: string;
@@ -69,13 +38,13 @@ export class TaskListPresenter extends BasePresenter {
6938
FROM ${sqlDatabaseSchema}."WorkerDeploymentPromotion" wdp
7039
INNER JOIN ${sqlDatabaseSchema}."WorkerDeployment" wd
7140
ON wd.id = wdp."deploymentId"
72-
WHERE wdp."environmentId" = ${environment.id}
41+
WHERE wdp."environmentId" = ${environmentId}
7342
AND wdp."label" = ${CURRENT_DEPLOYMENT_LABEL}
7443
),
7544
workers AS (
7645
SELECT DISTINCT ON ("runtimeEnvironmentId") id, "runtimeEnvironmentId", version
7746
FROM ${sqlDatabaseSchema}."BackgroundWorker"
78-
WHERE "runtimeEnvironmentId" = ${environment.id}
47+
WHERE "runtimeEnvironmentId" = ${environmentId}
7948
OR id IN (SELECT id FROM non_dev_workers)
8049
ORDER BY "runtimeEnvironmentId", "createdAt" DESC
8150
)
@@ -87,23 +56,23 @@ export class TaskListPresenter extends BasePresenter {
8756
//then get the activity for each task
8857
const activity = this.#getActivity(
8958
tasks.map((t) => t.slug),
90-
environment.projectId,
91-
environment.id
59+
projectId,
60+
environmentId
9261
);
9362

9463
const runningStats = this.#getRunningStats(
9564
tasks.map((t) => t.slug),
96-
environment.projectId,
97-
environment.id
65+
projectId,
66+
environmentId
9867
);
9968

10069
const durations = this.#getAverageDurations(
10170
tasks.map((t) => t.slug),
102-
environment.projectId,
103-
environment.id
71+
projectId,
72+
environmentId
10473
);
10574

106-
return { tasks, environment, activity, runningStats, durations };
75+
return { tasks, activity, runningStats, durations };
10776
}
10877

10978
async #getActivity(tasks: string[], projectId: string, environmentId: string) {

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam._index/route.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ import { useEventSource } from "~/hooks/useEventSource";
7070
import { useOrganization } from "~/hooks/useOrganizations";
7171
import { useProject } from "~/hooks/useProject";
7272
import { useTextFilter } from "~/hooks/useTextFilter";
73+
import { findProjectBySlug } from "~/models/project.server";
74+
import { findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
7375
import {
7476
type TaskActivity,
7577
type TaskListItem,
@@ -104,20 +106,33 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
104106
const userId = await requireUserId(request);
105107
const { organizationSlug, projectParam, envParam } = EnvironmentParamSchema.parse(params);
106108

109+
const project = await findProjectBySlug(organizationSlug, projectParam, userId);
110+
if (!project) {
111+
throw new Response(undefined, {
112+
status: 404,
113+
statusText: "Project not found",
114+
});
115+
}
116+
117+
const environment = await findEnvironmentBySlug(project.id, envParam, userId);
118+
if (!environment) {
119+
throw new Response(undefined, {
120+
status: 404,
121+
statusText: "Environment not found",
122+
});
123+
}
124+
107125
try {
108126
const presenter = new TaskListPresenter();
109-
const { tasks, environment, activity, runningStats, durations } = await presenter.call({
110-
userId,
111-
organizationSlug,
112-
projectSlug: projectParam,
113-
environmentSlug: envParam,
127+
const { tasks, activity, runningStats, durations } = await presenter.call({
128+
environmentId: environment.id,
129+
projectId: project.id,
114130
});
115131

116132
const usefulLinksPreference = await getUsefulLinksPreference(request);
117133

118134
return typeddefer({
119135
tasks,
120-
environment,
121136
activity,
122137
runningStats,
123138
durations,

0 commit comments

Comments
 (0)