Skip to content

Commit cfc7cd0

Browse files
committed
Same fix for the upserting of schedules in the dashboard
1 parent 34db26d commit cfc7cd0

File tree

3 files changed

+31
-14
lines changed
  • apps/webapp/app

3 files changed

+31
-14
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { RuntimeEnvironmentType } from "@trigger.dev/database";
2-
import { PrismaClient, prisma } from "~/db.server";
3-
import { displayableEnvironment } from "~/models/runtimeEnvironment.server";
1+
import { type RuntimeEnvironmentType } from "@trigger.dev/database";
2+
import { type PrismaClient, prisma } from "~/db.server";
3+
import { displayableEnvironment, findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
44
import { logger } from "~/services/logger.server";
55
import { filterOrphanedEnvironments } from "~/utils/environmentSort";
66
import { getTimezones } from "~/utils/timezones.server";
7+
import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server";
8+
import { ServiceValidationError } from "~/v3/services/baseService.server";
79

810
type EditScheduleOptions = {
911
userId: string;
1012
projectSlug: string;
13+
environmentSlug: string;
1114
friendlyId?: string;
1215
};
1316

@@ -26,7 +29,7 @@ export class EditSchedulePresenter {
2629
this.#prismaClient = prismaClient;
2730
}
2831

29-
public async call({ userId, projectSlug, friendlyId }: EditScheduleOptions) {
32+
public async call({ userId, projectSlug, environmentSlug, friendlyId }: EditScheduleOptions) {
3033
// Find the project scoped to the organization
3134
const project = await this.#prismaClient.project.findFirstOrThrow({
3235
select: {
@@ -62,13 +65,25 @@ export class EditSchedulePresenter {
6265
},
6366
});
6467

65-
const possibleTasks = await this.#prismaClient.backgroundWorkerTask.findMany({
66-
distinct: ["slug"],
67-
where: {
68-
projectId: project.id,
69-
triggerSource: "SCHEDULED",
70-
},
71-
});
68+
const environment = await findEnvironmentBySlug(project.id, environmentSlug, userId);
69+
if (!environment) {
70+
throw new ServiceValidationError("No matching environment for project", 404);
71+
}
72+
73+
//get the latest BackgroundWorker
74+
const latestWorker = await findCurrentWorkerFromEnvironment(environment, this.#prismaClient);
75+
76+
//get all possible scheduled tasks
77+
const possibleTasks = latestWorker
78+
? await this.#prismaClient.backgroundWorkerTask.findMany({
79+
where: {
80+
workerId: latestWorker.id,
81+
projectId: project.id,
82+
runtimeEnvironmentId: environment.id,
83+
triggerSource: "SCHEDULED",
84+
},
85+
})
86+
: [];
7287

7388
const possibleEnvironments = filterOrphanedEnvironments(project.environments).map(
7489
(environment) => {
@@ -77,7 +92,7 @@ export class EditSchedulePresenter {
7792
);
7893

7994
return {
80-
possibleTasks: possibleTasks.map((task) => task.slug),
95+
possibleTasks: possibleTasks.map((task) => task.slug).sort(),
8196
possibleEnvironments,
8297
possibleTimezones: getTimezones(),
8398
schedule: await this.#getExistingSchedule(friendlyId, possibleEnvironments),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
1515
const result = await presenter.call({
1616
userId,
1717
projectSlug: projectParam,
18+
environmentSlug: envParam,
1819
friendlyId: scheduleParam,
1920
});
2021

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
22
import { typedjson, useTypedLoaderData } from "remix-typedjson";
33
import { EditSchedulePresenter } from "~/presenters/v3/EditSchedulePresenter.server";
44
import { requireUserId } from "~/services/session.server";
5-
import { ProjectParamSchema } from "~/utils/pathBuilder";
5+
import { EnvironmentParamSchema } from "~/utils/pathBuilder";
66
import { humanToCronSupported } from "~/v3/humanToCron.server";
77
import { UpsertScheduleForm } from "../resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.schedules.new/route";
88

99
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
1010
const userId = await requireUserId(request);
11-
const { projectParam, organizationSlug } = ProjectParamSchema.parse(params);
11+
const { projectParam, envParam, organizationSlug } = EnvironmentParamSchema.parse(params);
1212

1313
const presenter = new EditSchedulePresenter();
1414
const result = await presenter.call({
1515
userId,
1616
projectSlug: projectParam,
17+
environmentSlug: envParam,
1718
});
1819

1920
return typedjson({ ...result, showGenerateField: humanToCronSupported });

0 commit comments

Comments
 (0)