Skip to content

Commit 134942b

Browse files
authored
The schedule HTTP API now accepts deduplicationKeys as well as schedule IDs in the URL (#2329)
1 parent b153324 commit 134942b

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Prisma } from "~/db.server";
2+
3+
export function scheduleUniqWhereClause(
4+
projectId: string,
5+
scheduleId: string
6+
): Prisma.TaskScheduleWhereUniqueInput {
7+
if (scheduleId.startsWith("sched_")) {
8+
return {
9+
friendlyId: scheduleId,
10+
projectId,
11+
};
12+
}
13+
14+
return {
15+
projectId_deduplicationKey: {
16+
projectId,
17+
deduplicationKey: scheduleId,
18+
},
19+
};
20+
}
21+
22+
export function scheduleWhereClause(
23+
projectId: string,
24+
scheduleId: string
25+
): Prisma.TaskScheduleWhereInput {
26+
if (scheduleId.startsWith("sched_")) {
27+
return {
28+
friendlyId: scheduleId,
29+
projectId,
30+
};
31+
}
32+
33+
return {
34+
projectId,
35+
deduplicationKey: scheduleId,
36+
};
37+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { displayableEnvironment } from "~/models/runtimeEnvironment.server";
44
import { clickhouseClient } from "~/services/clickhouseInstance.server";
55
import { nextScheduledTimestamps } from "~/v3/utils/calculateNextSchedule.server";
66
import { NextRunListPresenter } from "./NextRunListPresenter.server";
7+
import { scheduleWhereClause } from "~/models/schedules.server";
78

89
type ViewScheduleOptions = {
910
userId?: string;
@@ -63,10 +64,7 @@ export class ViewSchedulePresenter {
6364
},
6465
active: true,
6566
},
66-
where: {
67-
friendlyId,
68-
projectId,
69-
},
67+
where: scheduleWhereClause(projectId, friendlyId),
7068
});
7169

7270
if (!schedule) {

apps/webapp/app/routes/api.v1.schedules.$scheduleId.activate.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ActionFunctionArgs } from "@remix-run/server-runtime";
22
import { json } from "@remix-run/server-runtime";
3-
import { truncateSync } from "fs";
43
import { z } from "zod";
54
import { prisma } from "~/db.server";
5+
import { scheduleUniqWhereClause, scheduleWhereClause } from "~/models/schedules.server";
66
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
77
import { authenticateApiRequest } from "~/services/apiAuth.server";
88

@@ -34,21 +34,21 @@ export async function action({ request, params }: ActionFunctionArgs) {
3434

3535
try {
3636
const existingSchedule = await prisma.taskSchedule.findFirst({
37-
where: {
38-
friendlyId: parsedParams.data.scheduleId,
39-
projectId: authenticationResult.environment.projectId,
40-
},
37+
where: scheduleWhereClause(
38+
authenticationResult.environment.projectId,
39+
parsedParams.data.scheduleId
40+
),
4141
});
4242

4343
if (!existingSchedule) {
4444
return json({ error: "Schedule not found" }, { status: 404 });
4545
}
4646

4747
await prisma.taskSchedule.update({
48-
where: {
49-
friendlyId: parsedParams.data.scheduleId,
50-
projectId: authenticationResult.environment.projectId,
51-
},
48+
where: scheduleUniqWhereClause(
49+
authenticationResult.environment.projectId,
50+
parsedParams.data.scheduleId
51+
),
5252
data: {
5353
active: true,
5454
},

apps/webapp/app/routes/api.v1.schedules.$scheduleId.deactivate.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ActionFunctionArgs } from "@remix-run/server-runtime";
22
import { json } from "@remix-run/server-runtime";
33
import { z } from "zod";
44
import { prisma } from "~/db.server";
5+
import { scheduleUniqWhereClause, scheduleWhereClause } from "~/models/schedules.server";
56
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
67
import { authenticateApiRequest } from "~/services/apiAuth.server";
78

@@ -33,21 +34,21 @@ export async function action({ request, params }: ActionFunctionArgs) {
3334

3435
try {
3536
const existingSchedule = await prisma.taskSchedule.findFirst({
36-
where: {
37-
friendlyId: parsedParams.data.scheduleId,
38-
projectId: authenticationResult.environment.projectId,
39-
},
37+
where: scheduleWhereClause(
38+
authenticationResult.environment.projectId,
39+
parsedParams.data.scheduleId
40+
),
4041
});
4142

4243
if (!existingSchedule) {
4344
return json({ error: "Schedule not found" }, { status: 404 });
4445
}
4546

4647
await prisma.taskSchedule.update({
47-
where: {
48-
friendlyId: parsedParams.data.scheduleId,
49-
projectId: authenticationResult.environment.projectId,
50-
},
48+
where: scheduleUniqWhereClause(
49+
authenticationResult.environment.projectId,
50+
parsedParams.data.scheduleId
51+
),
5152
data: {
5253
active: false,
5354
},

apps/webapp/app/routes/api.v1.schedules.$scheduleId.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { json } from "@remix-run/server-runtime";
33
import { ScheduleObject, UpdateScheduleOptions } from "@trigger.dev/core/v3";
44
import { z } from "zod";
55
import { Prisma, prisma } from "~/db.server";
6+
import { scheduleUniqWhereClause } from "~/models/schedules.server";
67
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
78
import { authenticateApiRequest } from "~/services/apiAuth.server";
89
import { UpsertSchedule } from "~/v3/schedules";
@@ -36,10 +37,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
3637
case "DELETE": {
3738
try {
3839
const deletedSchedule = await prisma.taskSchedule.delete({
39-
where: {
40-
friendlyId: parsedParams.data.scheduleId,
41-
projectId: authenticationResult.environment.projectId,
42-
},
40+
where: scheduleUniqWhereClause(
41+
authenticationResult.environment.projectId,
42+
parsedParams.data.scheduleId
43+
),
4344
});
4445

4546
return json(

apps/webapp/app/v3/services/upsertTaskSchedule.server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { calculateNextScheduledTimestampFromNow } from "../utils/calculateNextSc
77
import { BaseService, ServiceValidationError } from "./baseService.server";
88
import { CheckScheduleService } from "./checkSchedule.server";
99
import { scheduleEngine } from "../scheduleEngine.server";
10+
import { scheduleWhereClause } from "~/models/schedules.server";
1011

1112
export type UpsertTaskScheduleServiceOptions = UpsertSchedule;
1213

@@ -37,9 +38,7 @@ export class UpsertTaskScheduleService extends BaseService {
3738

3839
const existingSchedule = schedule.friendlyId
3940
? await this._prisma.taskSchedule.findFirst({
40-
where: {
41-
friendlyId: schedule.friendlyId,
42-
},
41+
where: scheduleWhereClause(projectId, schedule.friendlyId),
4342
})
4443
: await this._prisma.taskSchedule.findFirst({
4544
where: {

0 commit comments

Comments
 (0)