Skip to content

Commit e338061

Browse files
committed
Don’t allow upserting schedules when archived
1 parent 9da2768 commit e338061

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ export class EditSchedulePresenter {
7070
throw new ServiceValidationError("No matching environment for project", 404);
7171
}
7272

73-
if (environment.archivedAt) {
74-
throw new ServiceValidationError("This branch is archived", 400);
75-
}
76-
7773
//get the latest BackgroundWorker
7874
const latestWorker = await findCurrentWorkerFromEnvironment(environment, this.#prismaClient);
7975

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Schedule = {
1414
};
1515

1616
export class CheckScheduleService extends BaseService {
17-
public async call(projectId: string, schedule: Schedule) {
17+
public async call(projectId: string, schedule: Schedule, environmentIds: string[]) {
1818
//validate the cron expression
1919
try {
2020
CronPattern.parse(schedule.cron);
@@ -61,28 +61,34 @@ export class CheckScheduleService extends BaseService {
6161
);
6262
}
6363

64-
//if creating a schedule, check they're under the limits
65-
if (!schedule.friendlyId) {
66-
//check they're within their limit
67-
const project = await this._prisma.project.findFirst({
68-
where: {
69-
id: projectId,
70-
},
71-
select: {
72-
organizationId: true,
73-
environments: {
74-
select: {
75-
id: true,
76-
type: true,
77-
},
64+
//check they're within their limit
65+
const project = await this._prisma.project.findFirst({
66+
where: {
67+
id: projectId,
68+
},
69+
select: {
70+
organizationId: true,
71+
environments: {
72+
select: {
73+
id: true,
74+
type: true,
75+
archivedAt: true,
7876
},
7977
},
80-
});
78+
},
79+
});
8180

82-
if (!project) {
83-
throw new ServiceValidationError("Project not found");
84-
}
81+
if (!project) {
82+
throw new ServiceValidationError("Project not found");
83+
}
8584

85+
const environments = project.environments.filter((env) => environmentIds.includes(env.id));
86+
if (environments.some((env) => env.archivedAt)) {
87+
throw new ServiceValidationError("Can't add or edit a schedule for an archived branch");
88+
}
89+
90+
//if creating a schedule, check they're under the limits
91+
if (!schedule.friendlyId) {
8692
const limit = await getLimit(project.organizationId, "schedules", 100_000_000);
8793
const schedulesCount = await CheckScheduleService.getUsedSchedulesCount({
8894
prisma: this._prisma,

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,16 @@ export async function syncDeclarativeSchedules(
523523
);
524524

525525
//this throws errors if the schedule is invalid
526-
await checkSchedule.call(environment.projectId, {
527-
cron: task.schedule.cron,
528-
timezone: task.schedule.timezone,
529-
taskIdentifier: task.id,
530-
friendlyId: existingSchedule?.friendlyId,
531-
});
526+
await checkSchedule.call(
527+
environment.projectId,
528+
{
529+
cron: task.schedule.cron,
530+
timezone: task.schedule.timezone,
531+
taskIdentifier: task.id,
532+
friendlyId: existingSchedule?.friendlyId,
533+
},
534+
[environment.id]
535+
);
532536

533537
if (existingSchedule) {
534538
const schedule = await prisma.taskSchedule.update({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class UpsertTaskScheduleService extends BaseService {
2929
public async call(projectId: string, schedule: UpsertTaskScheduleServiceOptions) {
3030
//this throws errors if the schedule is invalid
3131
const checkSchedule = new CheckScheduleService(this._prisma);
32-
await checkSchedule.call(projectId, schedule);
32+
await checkSchedule.call(projectId, schedule, schedule.environments);
3333

3434
const deduplicationKey =
3535
typeof schedule.deduplicationKey === "string" && schedule.deduplicationKey !== ""

0 commit comments

Comments
 (0)