Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions apps/docs/api-reference/campaigns/get-campaigns.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: get /v1/campaigns
---
106 changes: 106 additions & 0 deletions apps/docs/api-reference/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,112 @@
}
},
"/v1/campaigns": {
"get": {
"parameters": [
{
"schema": { "type": "string", "example": "1" },
"required": false,
"name": "page",
"in": "query",
"description": "Page number for pagination (default: 1)"
},
{
"schema": {
"type": "string",
"enum": [
"DRAFT",
"SCHEDULED",
"SENDING",
"PAUSED",
"SENT",
"CANCELLED"
],
"example": "DRAFT"
},
"required": false,
"name": "status",
"in": "query",
"description": "Filter campaigns by status"
},
{
"schema": { "type": "string", "example": "newsletter" },
"required": false,
"name": "search",
"in": "query",
"description": "Search campaigns by name or subject"
}
],
"responses": {
"200": {
"description": "Get list of campaigns",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"campaigns": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" },
"from": { "type": "string" },
"subject": { "type": "string" },
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
},
"status": {
"type": "string",
"enum": [
"DRAFT",
"SCHEDULED",
"SENDING",
"PAUSED",
"SENT",
"CANCELLED"
]
},
"scheduledAt": {
"type": "string",
"nullable": true,
"format": "date-time"
},
"total": { "type": "integer" },
"sent": { "type": "integer" },
"delivered": { "type": "integer" },
"unsubscribed": { "type": "integer" }
},
"required": [
"id",
"name",
"from",
"subject",
"createdAt",
"updatedAt",
"status",
"scheduledAt",
"total",
"sent",
"delivered",
"unsubscribed"
]
}
},
"totalPage": { "type": "integer" }
},
"required": ["campaigns", "totalPage"]
}
}
}
}
}
},
"post": {
"requestBody": {
"required": true,
Expand Down
126 changes: 126 additions & 0 deletions apps/web/src/server/public-api/api/campaigns/get-campaigns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { createRoute, z } from "@hono/zod-openapi";
import { CampaignStatus, Prisma } from "@prisma/client";
import { PublicAPIApp } from "~/server/public-api/hono";
import { db } from "~/server/db";

const statuses = Object.values(CampaignStatus) as [CampaignStatus];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix the type assertion for enum values.

The type assertion as [CampaignStatus] creates a tuple with a single element, but z.enum() requires a tuple type of [string, ...string[]] (at least one element with rest). This could cause TypeScript errors.

🔎 Proposed fix
-const statuses = Object.values(CampaignStatus) as [CampaignStatus];
+const statuses = Object.values(CampaignStatus) as [CampaignStatus, ...CampaignStatus[]];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const statuses = Object.values(CampaignStatus) as [CampaignStatus];
const statuses = Object.values(CampaignStatus) as [CampaignStatus, ...CampaignStatus[]];
🤖 Prompt for AI Agents
In apps/web/src/server/public-api/api/campaigns/get-campaigns.ts around line 6,
the current type assertion "as [CampaignStatus]" creates a single-element tuple
which is incompatible with z.enum; change the assertion to a rest tuple
signature (e.g., "as [CampaignStatus, ...CampaignStatus[]]") so
Object.values(CampaignStatus) is typed as a proper non-empty tuple for z.enum,
or alternatively replace z.enum(...) usage with z.nativeEnum(CampaignStatus) if
that better suits the codebase.


const route = createRoute({
method: "get",
path: "/v1/campaigns",
request: {
query: z.object({
page: z.string().optional().openapi({
description: "Page number for pagination (default: 1)",
example: "1",
}),
status: z.enum(statuses).optional().openapi({
description: "Filter campaigns by status",
example: "DRAFT",
}),
search: z.string().optional().openapi({
description: "Search campaigns by name or subject",
example: "newsletter",
}),
}),
},
responses: {
200: {
description: "Get list of campaigns",
content: {
"application/json": {
schema: z.object({
campaigns: z.array(
z.object({
id: z.string(),
name: z.string(),
from: z.string(),
subject: z.string(),
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
status: z.string(),
scheduledAt: z.string().datetime().nullable(),
total: z.number().int(),
sent: z.number().int(),
delivered: z.number().int(),
unsubscribed: z.number().int(),
})
),
totalPage: z.number().int(),
}),
},
},
},
},
});

function getCampaigns(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = c.var.team;
const pageParam = c.req.query("page");
const statusParam = c.req.query("status") as
| Prisma.EnumCampaignStatusFilter<"Campaign">
| undefined;
const searchParam = c.req.query("search");

const page = pageParam ? Number(pageParam) : 1;
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Jan 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Missing validation for the page query parameter. Invalid values like non-numeric strings, zero, or negative numbers will cause database errors. Consider adding numeric validation to the Zod schema or validating before use.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/web/src/server/public-api/api/campaigns/get-campaigns.ts, line 66:

<comment>Missing validation for the `page` query parameter. Invalid values like non-numeric strings, zero, or negative numbers will cause database errors. Consider adding numeric validation to the Zod schema or validating before use.</comment>

<file context>
@@ -1,133 +1,126 @@
-    const page = pageParam ? Number(pageParam) : 1;
-    const limit = 30;
-    const offset = (page - 1) * limit;
+		const page = pageParam ? Number(pageParam) : 1;
+		const limit = 30;
+		const offset = (page - 1) * limit;
</file context>
Suggested change
const page = pageParam ? Number(pageParam) : 1;
const parsedPage = pageParam ? Number(pageParam) : 1;
const page = Number.isNaN(parsedPage) || parsedPage < 1 ? 1 : Math.floor(parsedPage);
Fix with Cubic

const limit = 30;
const offset = (page - 1) * limit;

const whereConditions: Prisma.CampaignWhereInput = {
teamId: team.id,
};

if (statusParam) {
whereConditions.status = statusParam;
}

if (searchParam) {
whereConditions.OR = [
{
name: {
contains: searchParam,
mode: "insensitive",
},
},
{
subject: {
contains: searchParam,
mode: "insensitive",
},
},
];
}

const countP = db.campaign.count({ where: whereConditions });

const campaignsP = db.campaign.findMany({
where: whereConditions,
select: {
id: true,
name: true,
from: true,
subject: true,
createdAt: true,
updatedAt: true,
status: true,
scheduledAt: true,
total: true,
sent: true,
delivered: true,
unsubscribed: true,
},
orderBy: {
createdAt: "desc",
},
skip: offset,
take: limit,
});

const [campaigns, count] = await Promise.all([campaignsP, countP]);

return c.json({ campaigns, totalPage: Math.ceil(count / limit) });
});
}

export default getCampaigns;
2 changes: 2 additions & 0 deletions apps/web/src/server/public-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import deleteDomain from "./api/domains/delete-domain";
import sendBatch from "./api/emails/batch-email";
import createCampaign from "./api/campaigns/create-campaign";
import getCampaign from "./api/campaigns/get-campaign";
import getCampaigns from "./api/campaigns/get-campaigns";
import scheduleCampaign from "./api/campaigns/schedule-campaign";
import pauseCampaign from "./api/campaigns/pause-campaign";
import resumeCampaign from "./api/campaigns/resume-campaign";
Expand Down Expand Up @@ -50,6 +51,7 @@ deleteContact(app);
/**Campaign related APIs */
createCampaign(app);
getCampaign(app);
getCampaigns(app);
scheduleCampaign(app);
pauseCampaign(app);
resumeCampaign(app);
Expand Down