- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 224
 
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
          
     Merged
      
      
    
  
     Merged
                    add campaign api #274
Changes from 6 commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      cf52cd8
              
                add campaign api
              
              
                KMKoushik e8a2985
              
                uodate
              
              
                KMKoushik 64e4f6a
              
                stuff
              
              
                KMKoushik 4b32764
              
                stuff
              
              
                KMKoushik 75a6083
              
                sdk changes
              
              
                KMKoushik 22d1673
              
                stuff
              
              
                KMKoushik a41c280
              
                stuff
              
              
                KMKoushik 31452cc
              
                stuff
              
              
                KMKoushik File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
          Some comments aren't visible on the classic Files Changed page.
        
There are no files selected for viewing
Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  
        
          
          
            2 changes: 2 additions & 0 deletions
          
          2 
        
  apps/web/prisma/migrations/20251013114734_add_api_to_campaign/migration.sql
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -97,6 +97,7 @@ function CampaignEditor({ | |
| campaign: Campaign & { imageUploadSupported: boolean }; | ||
| }) { | ||
| const router = useRouter(); | ||
| const isApiCampaign = campaign.isApi; | ||
| const contactBooksQuery = api.contacts.getContactBooks.useQuery({}); | ||
| const utils = api.useUtils(); | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -124,6 +125,9 @@ function CampaignEditor({ | |
| const getUploadUrl = api.campaign.generateImagePresignedUrl.useMutation(); | ||
| 
     | 
||
| function updateEditorContent() { | ||
| if (isApiCampaign) { | ||
| return; | ||
| } | ||
| updateCampaignMutation.mutate({ | ||
| campaignId: campaign.id, | ||
| content: JSON.stringify(json), | ||
| 
        
          
        
         | 
    @@ -142,8 +146,6 @@ function CampaignEditor({ | |
| ); | ||
| } | ||
| 
     | 
||
| console.log("file type: ", file.type); | ||
| 
     | 
||
| const { uploadUrl, imageUrl } = await getUploadUrl.mutateAsync({ | ||
| name: file.name, | ||
| type: file.type, | ||
| 
          
            
          
           | 
    @@ -175,7 +177,12 @@ function CampaignEditor({ | |
| value={name} | ||
| onChange={(e) => setName(e.target.value)} | ||
| className=" border-0 focus:ring-0 focus:outline-none px-0.5 w-[300px]" | ||
| disabled={isApiCampaign} | ||
| readOnly={isApiCampaign} | ||
| onBlur={() => { | ||
| if (isApiCampaign) { | ||
| return; | ||
| } | ||
| if (name === campaign.name || !name) { | ||
| return; | ||
| } | ||
| 
          
            
          
           | 
    @@ -228,6 +235,9 @@ function CampaignEditor({ | |
| setSubject(e.target.value); | ||
| }} | ||
| onBlur={() => { | ||
| if (isApiCampaign) { | ||
| return; | ||
| } | ||
| if (subject === campaign.subject || !subject) { | ||
| return; | ||
| } | ||
| 
        
          
        
         | 
    @@ -245,6 +255,8 @@ function CampaignEditor({ | |
| ); | ||
| }} | ||
| className="mt-1 py-1 text-sm block w-full outline-none border-b border-transparent focus:border-border bg-transparent" | ||
| disabled={isApiCampaign} | ||
| readOnly={isApiCampaign} | ||
| /> | ||
| <AccordionTrigger className="py-0"></AccordionTrigger> | ||
| </div> | ||
| 
        
          
        
         | 
    @@ -263,6 +275,9 @@ function CampaignEditor({ | |
| className="mt-1 py-1 w-full text-sm outline-none border-b border-transparent focus:border-border bg-transparent" | ||
| placeholder="Friendly name<[email protected]>" | ||
| onBlur={() => { | ||
| if (isApiCampaign) { | ||
| return; | ||
| } | ||
| if (from === campaign.from || !from) { | ||
| return; | ||
| } | ||
| 
        
          
        
         | 
    @@ -279,6 +294,8 @@ function CampaignEditor({ | |
| } | ||
| ); | ||
| }} | ||
| disabled={isApiCampaign} | ||
| readOnly={isApiCampaign} | ||
| /> | ||
| </div> | ||
| <div className="flex items-center gap-4"> | ||
| 
        
          
        
         | 
    @@ -294,6 +311,9 @@ function CampaignEditor({ | |
| className="mt-1 py-1 text-sm block w-full outline-none border-b border-transparent bg-transparent focus:border-border" | ||
| placeholder="[email protected]" | ||
| onBlur={() => { | ||
| if (isApiCampaign) { | ||
| return; | ||
| } | ||
| if (replyTo === campaign.replyTo[0]) { | ||
| return; | ||
| } | ||
| 
        
          
        
         | 
    @@ -310,6 +330,8 @@ function CampaignEditor({ | |
| } | ||
| ); | ||
| }} | ||
| disabled={isApiCampaign} | ||
| readOnly={isApiCampaign} | ||
| /> | ||
| </div> | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -324,6 +346,9 @@ function CampaignEditor({ | |
| setPreviewText(e.target.value); | ||
| }} | ||
| onBlur={() => { | ||
| if (isApiCampaign) { | ||
| return; | ||
| } | ||
| if ( | ||
| previewText === campaign.previewText || | ||
| !previewText | ||
| 
        
          
        
         | 
    @@ -344,6 +369,8 @@ function CampaignEditor({ | |
| ); | ||
| }} | ||
| className="mt-1 py-1 text-sm block w-full outline-none border-b border-transparent bg-transparent focus:border-border" | ||
| disabled={isApiCampaign} | ||
| readOnly={isApiCampaign} | ||
| /> | ||
| </div> | ||
| <div className=" flex items-center gap-2"> | ||
| 
        
          
        
         | 
    @@ -355,7 +382,11 @@ function CampaignEditor({ | |
| ) : ( | ||
| <Select | ||
| value={contactBookId ?? ""} | ||
| disabled={isApiCampaign} | ||
| onValueChange={(val) => { | ||
| if (isApiCampaign) { | ||
| return; | ||
| } | ||
| // Update the campaign's contactBookId | ||
| updateCampaignMutation.mutate( | ||
| { | ||
| 
          
            
          
           | 
    @@ -395,22 +426,29 @@ function CampaignEditor({ | |
| </AccordionItem> | ||
| </Accordion> | ||
| 
     | 
||
| <div className=" rounded-lg bg-gray-50 w-[700px] mx-auto p-10"> | ||
| <div className="w-[600px] mx-auto"> | ||
| <Editor | ||
| initialContent={json} | ||
| onUpdate={(content) => { | ||
| setJson(content.getJSON()); | ||
| setIsSaving(true); | ||
| deboucedUpdateCampaign(); | ||
| }} | ||
| variables={["email", "firstName", "lastName"]} | ||
| uploadImage={ | ||
| campaign.imageUploadSupported ? handleFileChange : undefined | ||
| } | ||
| /> | ||
| {isApiCampaign ? ( | ||
| <p className="text-sm text-center text-muted-foreground"> | ||
| Email created from API. Campaign content can only be updated via | ||
| API. | ||
| </p> | ||
| ) : ( | ||
| <div className=" rounded-lg bg-gray-50 w-[700px] mx-auto p-10"> | ||
| <div className="w-[600px] mx-auto"> | ||
| <Editor | ||
| initialContent={json} | ||
| onUpdate={(content) => { | ||
| setJson(content.getJSON()); | ||
| setIsSaving(true); | ||
| deboucedUpdateCampaign(); | ||
| }} | ||
| variables={["email", "firstName", "lastName"]} | ||
| uploadImage={ | ||
| campaign.imageUploadSupported ? handleFileChange : undefined | ||
| } | ||
| /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            82 changes: 82 additions & 0 deletions
          
          82 
        
  apps/web/src/server/public-api/api/campaigns/create-campaign.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | 
        
          
          
            49 changes: 49 additions & 0 deletions
          
          49 
        
  apps/web/src/server/public-api/api/campaigns/get-campaign.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
🛠️ Refactor suggestion | 🟠 Major
Guard JSON parsing and drop unnecessary await
Prevent crashes on invalid JSON;
render()is synchronous.📝 Committable suggestion
🤖 Prompt for AI Agents