prevent overlapping sprints

This commit is contained in:
Oliver Bryan
2026-01-17 02:46:49 +00:00
parent f3523a9a83
commit 0dcfe1b66b
2 changed files with 34 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
import { Sprint } from "@sprint/shared";
import { eq } from "drizzle-orm";
import { and, eq, gte, lte } from "drizzle-orm";
import { db } from "../client";
export async function createSprint(
@@ -25,3 +25,18 @@ export async function createSprint(
export async function getSprintsByProject(projectId: number) {
return await db.select().from(Sprint).where(eq(Sprint.projectId, projectId));
}
export async function hasOverlappingSprints(projectId: number, startDate: Date, endDate: Date) {
const overlapping = await db
.select({ id: Sprint.id })
.from(Sprint)
.where(
and(
eq(Sprint.projectId, projectId),
lte(Sprint.startDate, endDate),
gte(Sprint.endDate, startDate),
),
)
.limit(1);
return overlapping.length > 0;
}

View File

@@ -1,6 +1,11 @@
import { SprintCreateRequestSchema } from "@sprint/shared";
import type { AuthedRequest } from "../../auth/middleware";
import { createSprint, getOrganisationMemberRole, getProjectByID } from "../../db/queries";
import {
createSprint,
getOrganisationMemberRole,
getProjectByID,
hasOverlappingSprints,
} from "../../db/queries";
import { errorResponse, parseJsonBody } from "../../validation";
export default async function sprintCreate(req: AuthedRequest) {
@@ -11,19 +16,27 @@ export default async function sprintCreate(req: AuthedRequest) {
const project = await getProjectByID(projectId);
if (!project) {
return errorResponse(`project not found: ${projectId}`, "PROJECT_NOT_FOUND", 404);
return errorResponse(`Project not found: ${projectId}`, "PROJECT_NOT_FOUND", 404);
}
const membership = await getOrganisationMemberRole(project.organisationId, req.userId);
if (!membership) {
return errorResponse("not a member of this organisation", "NOT_MEMBER", 403);
return errorResponse("Not a member of this organisation", "NOT_MEMBER", 403);
}
if (membership.role !== "owner" && membership.role !== "admin") {
return errorResponse("only owners and admins can create sprints", "PERMISSION_DENIED", 403);
return errorResponse("Only owners and admins can create sprints", "PERMISSION_DENIED", 403);
}
const sprint = await createSprint(project.id, name, color, new Date(startDate), new Date(endDate));
const start = new Date(startDate);
const end = new Date(endDate);
const hasOverlap = await hasOverlappingSprints(project.id, start, end);
if (hasOverlap) {
return errorResponse("Sprint dates overlap with an existing sprint", "SPRINT_OVERLAP", 400);
}
const sprint = await createSprint(project.id, name, color, start, end);
return Response.json(sprint);
}