Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🧑💻 Add endpoint for deleting a poll (#2123)
Luke Vella78f0c784c853
diff --git a/apps/web/src/app/api/private/[...route]/route.test.ts b/apps/web/src/app/api/private/[...route]/route.test.tsindex 39cad681c..bff213680 100644--- a/apps/web/src/app/api/private/[...route]/route.test.ts+++ b/apps/web/src/app/api/private/[...route]/route.test.ts@@ -1,5 +1,11 @@import { beforeEach, describe, expect, it, vi } from "vitest";+const mockDeletePoll = vi.fn();++vi.mock("@/features/poll/mutations", () => ({+ deletePoll: (...args: unknown[]) => mockDeletePoll(...args),+}));+vi.mock("@rallly/database", () => ({prisma: {spaceApiKey: {@@ -463,4 +469,80 @@ describe("Private API - /polls", () => {expect(res.headers.get("content-type")).toContain("text/html");});});++ describe("Delete poll", () => {+ it("should delete a poll", async () => {+ mockDeletePoll.mockResolvedValue({ id: "test-poll-id" });++ const res = await app.request("/api/private/polls/test-poll-id", {+ method: "DELETE",+ headers: {+ Authorization: `Bearer ${testApiKey}`,+ },+ });++ expect(res.status).toBe(200);+ const json = await res.json();+ expect(json.data.id).toBe("test-poll-id");+ expect(json.data.deleted).toBe(true);++ expect(mockDeletePoll).toHaveBeenCalledWith(+ "test-poll-id",+ "test-space-id",+ );+ });++ it("should return 404 when poll not found", async () => {+ mockDeletePoll.mockResolvedValue(null);++ const res = await app.request("/api/private/polls/nonexistent-poll", {+ method: "DELETE",+ headers: {+ Authorization: `Bearer ${testApiKey}`,+ },+ });++ expect(res.status).toBe(404);+ const json = await res.json();+ expect(json.error.code).toBe("POLL_NOT_FOUND");+ });++ it("should return 404 when poll belongs to different space", async () => {+ mockDeletePoll.mockResolvedValue(null);++ const res = await app.request("/api/private/polls/other-space-poll", {+ method: "DELETE",+ headers: {+ Authorization: `Bearer ${testApiKey}`,+ },+ });++ expect(res.status).toBe(404);+ const json = await res.json();+ expect(json.error.code).toBe("POLL_NOT_FOUND");+ });++ it("should return 401 without authorization", async () => {+ const res = await app.request("/api/private/polls/test-poll-id", {+ method: "DELETE",+ });++ expect(res.status).toBe(401);+ });++ it("should return 404 when poll is already deleted", async () => {+ mockDeletePoll.mockResolvedValue(null);++ const res = await app.request("/api/private/polls/deleted-poll-id", {+ method: "DELETE",+ headers: {+ Authorization: `Bearer ${testApiKey}`,+ },+ });++ expect(res.status).toBe(404);+ const json = await res.json();+ expect(json.error.code).toBe("POLL_NOT_FOUND");+ });+ });});diff --git a/apps/web/src/app/api/private/[...route]/route.ts b/apps/web/src/app/api/private/[...route]/route.tsindex 9524f5486..7f1320b18 100644--- a/apps/web/src/app/api/private/[...route]/route.ts+++ b/apps/web/src/app/api/private/[...route]/route.ts@@ -10,11 +10,13 @@ import {validator,} from "hono-openapi";import { rateLimiter } from "hono-rate-limiter";+import { deletePoll } from "@/features/poll/mutations";import { isKvEnabled, kv } from "@/lib/kv";import { isSupportedTimeZone } from "@/utils/supported-time-zones";import {createPollInputSchema,createPollSuccessResponseSchema,+ deletePollSuccessResponseSchema,errorResponseSchema,} from "../schemas";import { spaceApiKeyAuth } from "../utils/api-key";@@ -48,7 +50,6 @@ app.get(title: "Rallly Private API",version: "0.0.1",},- servers: [{ url: "/api/private" }],components: {securitySchemes: {bearerAuth: {@@ -279,7 +280,70 @@ app.post(},);+app.delete(+ "/polls/:pollId",+ spaceApiKeyAuth,+ rateLimiter<Env>({+ windowMs: 60 * 1000,+ limit: 60,+ keyGenerator: (c) => {+ const { apiKeyId } = c.get("apiAuth");+ return `private-api:polls-delete:${apiKeyId}`;+ },+ store: isKvEnabled() ? new RedisStore({ client: kv }) : undefined,+ }),+ describeRoute({+ tags: ["Polls"],+ summary: "Delete a poll",+ description:+ "Deletes a poll by ID. The poll must belong to the space associated with the API key.",+ security: [{ bearerAuth: [] }],+ responses: {+ 200: {+ description: "Poll deleted successfully",+ content: {+ "application/json": {+ schema: resolver(deletePollSuccessResponseSchema),+ },+ },+ },+ 404: {+ description: "Poll not found",+ content: {+ "application/json": {+ schema: resolver(errorResponseSchema),+ },+ },+ },+ },+ }),+ async (c) => {+ const { pollId } = c.req.param();+ const { spaceId } = c.get("apiAuth");++ const result = await deletePoll(pollId, spaceId);++ if (!result) {+ return c.json(+ apiError(+ "POLL_NOT_FOUND",+ "Poll not found or does not belong to this space.",+ ),+ 404,+ );+ }++ return c.json({+ data: {+ id: result.id,+ deleted: true as const,+ },+ });+ },+);+export { app };export const GET = handle(app);export const POST = handle(app);+export const DELETE = handle(app);diff --git a/apps/web/src/app/api/private/schemas.ts b/apps/web/src/app/api/private/schemas.tsindex be265d61e..158897e2e 100644--- a/apps/web/src/app/api/private/schemas.ts+++ b/apps/web/src/app/api/private/schemas.ts@@ -161,3 +161,12 @@ export const errorResponseSchema = z}),}).openapi("ErrorResponse");++export const deletePollSuccessResponseSchema = z+ .object({+ data: z.object({+ id: z.string().openapi({ example: "p_123abc" }),+ deleted: z.literal(true).openapi({ example: true }),+ }),+ })+ .openapi("DeletePollResponse");diff --git a/apps/web/src/features/poll/mutations.ts b/apps/web/src/features/poll/mutations.tsnew file mode 100644index 000000000..09ee736fb--- /dev/null+++ b/apps/web/src/features/poll/mutations.ts@@ -0,0 +1,23 @@+import { prisma } from "@rallly/database";++export const deletePoll = async (pollId: string, spaceId: string) => {+ const poll = await prisma.poll.findFirst({+ where: {+ id: pollId,+ spaceId,+ deletedAt: null,+ },+ select: { id: true },+ });++ if (!poll) {+ return null;+ }++ await prisma.poll.update({+ where: { id: pollId },+ data: { deleted: true, deletedAt: new Date() },+ });++ return { id: pollId };+};