warmbly/internal/api/handler/campaign_import.go

91 lines
3.1 KiB
Go

package handler
import (
"bytes"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/warmbly/warmbly/internal/api/middleware"
"github.com/warmbly/warmbly/internal/app/campaignimport"
"github.com/warmbly/warmbly/internal/errx"
)
// campaignImportMaxBytes bounds the combined sequence + leads upload. A
// competitor export is a handful of steps plus a lead list; 10 MB matches
// the existing mailbox-import ceiling and comfortably covers tens of
// thousands of leads.
const campaignImportMaxBytes = 10 << 20
// ImportCampaign handles POST /v1/campaign-imports: a multipart form with a
// `platform` field ("instantly" or "smartlead"), an optional `name`, a
// required `sequence` file (the platform's sequence/steps export, saved as
// JSON), and an optional `leads` file (a lead-list CSV export). Creates one
// real draft campaign with its steps in order and, if leads were given,
// imports and enrolls them -- removing the single biggest switching-cost
// barrier a team migrating off either competitor otherwise hits.
func (h *Handler) ImportCampaign(c *gin.Context) {
orgID := middleware.GetOrganizationID(c)
if orgID == nil {
errx.JSON(c, errx.ErrNoOrganization)
return
}
userID := middleware.GetUserID(c)
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, campaignImportMaxBytes)
if err := c.Request.ParseMultipartForm(campaignImportMaxBytes); err != nil {
errx.JSON(c, errx.NewWithIdentifier(errx.BadRequest, "campaign_import_too_large", "Send a file up to 10 MB as multipart form data."))
return
}
platform := campaignimport.Platform(c.Request.FormValue("platform"))
if platform != campaignimport.PlatformInstantly && platform != campaignimport.PlatformSmartlead {
errx.JSON(c, errx.New(errx.BadRequest, `platform must be "instantly" or "smartlead"`))
return
}
in := campaignimport.Input{
Name: c.Request.FormValue("name"),
Platform: platform,
}
sequenceFile, _, err := c.Request.FormFile("sequence")
if err != nil {
errx.JSON(c, errx.New(errx.BadRequest, "a sequence export file is required (field \"sequence\")"))
return
}
defer sequenceFile.Close()
sequenceBytes, err := io.ReadAll(io.LimitReader(sequenceFile, campaignImportMaxBytes+1))
if err != nil || len(sequenceBytes) > campaignImportMaxBytes {
errx.JSON(c, errx.New(errx.BadRequest, "the sequence file is too large"))
return
}
in.SequenceJSON = bytes.NewReader(sequenceBytes)
if leadsFile, _, err := c.Request.FormFile("leads"); err == nil {
defer leadsFile.Close()
leadsBytes, err := io.ReadAll(io.LimitReader(leadsFile, campaignImportMaxBytes+1))
if err != nil || len(leadsBytes) > campaignImportMaxBytes {
errx.JSON(c, errx.New(errx.BadRequest, "the leads file is too large"))
return
}
in.LeadsCSV = bytes.NewReader(leadsBytes)
}
result, xerr := h.CampaignImportService.Import(c.Request.Context(), userID, *orgID, in)
if xerr != nil {
errx.JSON(c, xerr)
return
}
resp := gin.H{
"campaign": result.Campaign,
"steps_created": result.StepsCreated,
}
if result.Leads != nil {
resp["leads"] = result.Leads
}
c.JSON(http.StatusCreated, resp)
}