-
-
Notifications
You must be signed in to change notification settings - Fork 216
add campaign api #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
add campaign api #274
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Campaign" ADD COLUMN "isApi" BOOLEAN NOT NULL DEFAULT false; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -119,12 +119,13 @@ export const campaignRouter = createTRPCRouter({ | |||||||||||||||||||||||||||||||||||||||||||
subject: z.string().optional(), | ||||||||||||||||||||||||||||||||||||||||||||
previewText: z.string().optional(), | ||||||||||||||||||||||||||||||||||||||||||||
content: z.string().optional(), | ||||||||||||||||||||||||||||||||||||||||||||
html: z.string().optional(), | ||||||||||||||||||||||||||||||||||||||||||||
contactBookId: z.string().optional(), | ||||||||||||||||||||||||||||||||||||||||||||
replyTo: z.string().array().optional(), | ||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
.mutation(async ({ ctx: { db, team, campaign: campaignOld }, input }) => { | ||||||||||||||||||||||||||||||||||||||||||||
const { campaignId, ...data } = input; | ||||||||||||||||||||||||||||||||||||||||||||
const { campaignId, html: htmlInput, ...data } = input; | ||||||||||||||||||||||||||||||||||||||||||||
if (data.contactBookId) { | ||||||||||||||||||||||||||||||||||||||||||||
const contactBook = await db.contactBook.findUnique({ | ||||||||||||||||||||||||||||||||||||||||||||
where: { id: data.contactBookId }, | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -143,22 +144,29 @@ export const campaignRouter = createTRPCRouter({ | |||||||||||||||||||||||||||||||||||||||||||
domainId = domain.id; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
let html: string | null = null; | ||||||||||||||||||||||||||||||||||||||||||||
let htmlToSave: string | undefined; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if (data.content) { | ||||||||||||||||||||||||||||||||||||||||||||
const jsonContent = data.content ? JSON.parse(data.content) : null; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
const renderer = new EmailRenderer(jsonContent); | ||||||||||||||||||||||||||||||||||||||||||||
html = await renderer.render(); | ||||||||||||||||||||||||||||||||||||||||||||
htmlToSave = await renderer.render(); | ||||||||||||||||||||||||||||||||||||||||||||
} else if (typeof htmlInput === "string") { | ||||||||||||||||||||||||||||||||||||||||||||
htmlToSave = htmlInput; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
149
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Guard JSON parsing and drop unnecessary await Prevent crashes on invalid JSON; - if (data.content) {
- const jsonContent = data.content ? JSON.parse(data.content) : null;
-
- const renderer = new EmailRenderer(jsonContent);
- htmlToSave = await renderer.render();
- } else if (typeof htmlInput === "string") {
+ if (data.content) {
+ let jsonContent: unknown = null;
+ try {
+ jsonContent = JSON.parse(data.content);
+ } catch {
+ throw new TRPCError({ code: "BAD_REQUEST", message: "Invalid content JSON" });
+ }
+ const renderer = new EmailRenderer(jsonContent as any);
+ htmlToSave = renderer.render();
+ } else if (typeof htmlInput === "string") {
htmlToSave = htmlInput;
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
const campaignUpdateData: Prisma.CampaignUpdateInput = { | ||||||||||||||||||||||||||||||||||||||||||||
...data, | ||||||||||||||||||||||||||||||||||||||||||||
domainId, | ||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if (htmlToSave !== undefined) { | ||||||||||||||||||||||||||||||||||||||||||||
campaignUpdateData.html = htmlToSave; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
const campaign = await db.campaign.update({ | ||||||||||||||||||||||||||||||||||||||||||||
where: { id: campaignId }, | ||||||||||||||||||||||||||||||||||||||||||||
data: { | ||||||||||||||||||||||||||||||||||||||||||||
...data, | ||||||||||||||||||||||||||||||||||||||||||||
html, | ||||||||||||||||||||||||||||||||||||||||||||
domainId, | ||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||
data: campaignUpdateData, | ||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||
return campaign; | ||||||||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -240,6 +248,7 @@ export const campaignRouter = createTRPCRouter({ | |||||||||||||||||||||||||||||||||||||||||||
from: campaign.from, | ||||||||||||||||||||||||||||||||||||||||||||
subject: campaign.subject, | ||||||||||||||||||||||||||||||||||||||||||||
content: campaign.content, | ||||||||||||||||||||||||||||||||||||||||||||
html: campaign.html, | ||||||||||||||||||||||||||||||||||||||||||||
teamId: team.id, | ||||||||||||||||||||||||||||||||||||||||||||
domainId: campaign.domainId, | ||||||||||||||||||||||||||||||||||||||||||||
contactBookId: campaign.contactBookId, | ||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { createRoute, z } from "@hono/zod-openapi"; | ||
import { PublicAPIApp } from "~/server/public-api/hono"; | ||
import { | ||
campaignCreateSchema, | ||
CampaignCreateInput, | ||
campaignResponseSchema, | ||
parseScheduledAt, | ||
} from "~/server/public-api/schemas/campaign-schema"; | ||
import { | ||
createCampaignFromApi, | ||
getCampaignForTeam, | ||
scheduleCampaign, | ||
} from "~/server/service/campaign-service"; | ||
const route = createRoute({ | ||
method: "post", | ||
path: "/v1/campaigns", | ||
request: { | ||
body: { | ||
required: true, | ||
content: { | ||
"application/json": { | ||
schema: campaignCreateSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
200: { | ||
description: "Create a campaign", | ||
content: { | ||
"application/json": { | ||
schema: campaignResponseSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
function createCampaign(app: PublicAPIApp) { | ||
app.openapi(route, async (c) => { | ||
const team = c.var.team; | ||
const body: CampaignCreateInput = c.req.valid("json"); | ||
|
||
const campaign = await createCampaignFromApi({ | ||
teamId: team.id, | ||
apiKeyId: team.apiKeyId, | ||
name: body.name, | ||
from: body.from, | ||
subject: body.subject, | ||
previewText: body.previewText, | ||
content: body.content, | ||
html: body.html, | ||
contactBookId: body.contactBookId, | ||
replyTo: body.replyTo, | ||
cc: body.cc, | ||
bcc: body.bcc, | ||
batchSize: body.batchSize, | ||
}); | ||
|
||
if (body.sendNow || body.scheduledAt) { | ||
const scheduledAtInput = body.sendNow | ||
? new Date() | ||
: parseScheduledAt(body.scheduledAt); | ||
|
||
await scheduleCampaign({ | ||
campaignId: campaign.id, | ||
teamId: team.id, | ||
scheduledAt: scheduledAtInput, | ||
batchSize: body.batchSize, | ||
}); | ||
} | ||
|
||
const latestCampaign = await getCampaignForTeam({ | ||
campaignId: campaign.id, | ||
teamId: team.id, | ||
}); | ||
|
||
return c.json(latestCampaign); | ||
}); | ||
} | ||
|
||
export default createCampaign; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createRoute, z } from "@hono/zod-openapi"; | ||
import { PublicAPIApp } from "~/server/public-api/hono"; | ||
import { getCampaignForTeam } from "~/server/service/campaign-service"; | ||
import { campaignResponseSchema } from "~/server/public-api/schemas/campaign-schema"; | ||
|
||
const route = createRoute({ | ||
method: "get", | ||
path: "/v1/campaigns/{campaignId}", | ||
request: { | ||
params: z.object({ | ||
campaignId: z | ||
.string() | ||
.min(1) | ||
.openapi({ | ||
param: { | ||
name: "campaignId", | ||
in: "path", | ||
}, | ||
example: "cmp_123", | ||
}), | ||
}), | ||
}, | ||
responses: { | ||
200: { | ||
description: "Get campaign details", | ||
content: { | ||
"application/json": { | ||
schema: campaignResponseSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
function getCampaign(app: PublicAPIApp) { | ||
app.openapi(route, async (c) => { | ||
const team = c.var.team; | ||
const campaignId = c.req.param("campaignId"); | ||
|
||
const campaign = await getCampaignForTeam({ | ||
campaignId, | ||
teamId: team.id, | ||
}); | ||
|
||
return c.json(campaign); | ||
}); | ||
} | ||
|
||
export default getCampaign; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ctx.campaign id instead of input; avoid id forgery and missing input
Rely on
campaignOld.id
from context (already authorized) instead ofinput.campaignId
.As per coding guidelines
Also applies to: 167-170
🤖 Prompt for AI Agents