Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/webapp/app/models/schedules.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Prisma } from "~/db.server";

export function scheduleUniqWhereClause(
projectId: string,
scheduleId: string
): Prisma.TaskScheduleWhereUniqueInput {
if (scheduleId.startsWith("sched_")) {
return {
friendlyId: scheduleId,
projectId,
};
}

return {
projectId_deduplicationKey: {
projectId,
deduplicationKey: scheduleId,
},
};
}

export function scheduleWhereClause(
projectId: string,
scheduleId: string
): Prisma.TaskScheduleWhereInput {
if (scheduleId.startsWith("sched_")) {
return {
friendlyId: scheduleId,
projectId,
};
}

return {
projectId,
deduplicationKey: scheduleId,
};
}
6 changes: 2 additions & 4 deletions apps/webapp/app/presenters/v3/ViewSchedulePresenter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { displayableEnvironment } from "~/models/runtimeEnvironment.server";
import { clickhouseClient } from "~/services/clickhouseInstance.server";
import { nextScheduledTimestamps } from "~/v3/utils/calculateNextSchedule.server";
import { NextRunListPresenter } from "./NextRunListPresenter.server";
import { scheduleWhereClause } from "~/models/schedules.server";

type ViewScheduleOptions = {
userId?: string;
Expand Down Expand Up @@ -63,10 +64,7 @@ export class ViewSchedulePresenter {
},
active: true,
},
where: {
friendlyId,
projectId,
},
where: scheduleWhereClause(projectId, friendlyId),
});

if (!schedule) {
Expand Down
18 changes: 9 additions & 9 deletions apps/webapp/app/routes/api.v1.schedules.$scheduleId.activate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ActionFunctionArgs } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";
import { truncateSync } from "fs";
import { z } from "zod";
import { prisma } from "~/db.server";
import { scheduleUniqWhereClause, scheduleWhereClause } from "~/models/schedules.server";
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
import { authenticateApiRequest } from "~/services/apiAuth.server";

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

try {
const existingSchedule = await prisma.taskSchedule.findFirst({
where: {
friendlyId: parsedParams.data.scheduleId,
projectId: authenticationResult.environment.projectId,
},
where: scheduleWhereClause(
authenticationResult.environment.projectId,
parsedParams.data.scheduleId
),
});

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

await prisma.taskSchedule.update({
where: {
friendlyId: parsedParams.data.scheduleId,
projectId: authenticationResult.environment.projectId,
},
where: scheduleUniqWhereClause(
authenticationResult.environment.projectId,
parsedParams.data.scheduleId
),
data: {
active: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ActionFunctionArgs } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";
import { z } from "zod";
import { prisma } from "~/db.server";
import { scheduleUniqWhereClause, scheduleWhereClause } from "~/models/schedules.server";
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
import { authenticateApiRequest } from "~/services/apiAuth.server";

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

try {
const existingSchedule = await prisma.taskSchedule.findFirst({
where: {
friendlyId: parsedParams.data.scheduleId,
projectId: authenticationResult.environment.projectId,
},
where: scheduleWhereClause(
authenticationResult.environment.projectId,
parsedParams.data.scheduleId
),
});

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

await prisma.taskSchedule.update({
where: {
friendlyId: parsedParams.data.scheduleId,
projectId: authenticationResult.environment.projectId,
},
where: scheduleUniqWhereClause(
authenticationResult.environment.projectId,
parsedParams.data.scheduleId
),
data: {
active: false,
},
Expand Down
9 changes: 5 additions & 4 deletions apps/webapp/app/routes/api.v1.schedules.$scheduleId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { json } from "@remix-run/server-runtime";
import { ScheduleObject, UpdateScheduleOptions } from "@trigger.dev/core/v3";
import { z } from "zod";
import { Prisma, prisma } from "~/db.server";
import { scheduleUniqWhereClause } from "~/models/schedules.server";
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
import { authenticateApiRequest } from "~/services/apiAuth.server";
import { UpsertSchedule } from "~/v3/schedules";
Expand Down Expand Up @@ -36,10 +37,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
case "DELETE": {
try {
const deletedSchedule = await prisma.taskSchedule.delete({
where: {
friendlyId: parsedParams.data.scheduleId,
projectId: authenticationResult.environment.projectId,
},
where: scheduleUniqWhereClause(
authenticationResult.environment.projectId,
parsedParams.data.scheduleId
),
});

return json(
Expand Down
5 changes: 2 additions & 3 deletions apps/webapp/app/v3/services/upsertTaskSchedule.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { calculateNextScheduledTimestampFromNow } from "../utils/calculateNextSc
import { BaseService, ServiceValidationError } from "./baseService.server";
import { CheckScheduleService } from "./checkSchedule.server";
import { scheduleEngine } from "../scheduleEngine.server";
import { scheduleWhereClause } from "~/models/schedules.server";

export type UpsertTaskScheduleServiceOptions = UpsertSchedule;

Expand Down Expand Up @@ -37,9 +38,7 @@ export class UpsertTaskScheduleService extends BaseService {

const existingSchedule = schedule.friendlyId
? await this._prisma.taskSchedule.findFirst({
where: {
friendlyId: schedule.friendlyId,
},
where: scheduleWhereClause(projectId, schedule.friendlyId),
})
: await this._prisma.taskSchedule.findFirst({
where: {
Expand Down