Add reported functionality

Signed-off-by: Ronni Skansing <rskansing@gmail.com>
This commit is contained in:
Ronni Skansing committed 2025-09-17 21:36:20 +02:00
1 parent c1709c24aa
commit 854e0243d0
17 files changed
+619 -59

No files matched your search

+69
View File
@@ -3,6 +3,8 @@ package controller
import (
"bytes"
"encoding/csv"
"io"
"strings"
"time"
"github.com/go-errors/errors"
@@ -935,3 +937,70 @@ func (c *Campaign) GetAllCampaignStats(g *gin.Context) {
}
c.Response.OK(g, stats)
}
// UploadReportedCSV uploads a CSV file with reported recipients
func (c *Campaign) UploadReportedCSV(g *gin.Context) {
// handle session
session, _, ok := c.handleSession(g)
if !ok {
return
}
// parse campaign id
id, ok := c.handleParseIDParam(g)
if !ok {
return
}
// get the uploaded file
file, header, err := g.Request.FormFile("file")
if err != nil {
c.Response.ValidationFailed(g, "file", err)
return
}
defer file.Close()
// validate file extension
if !strings.HasSuffix(strings.ToLower(header.Filename), ".csv") {
c.Response.ValidationFailed(g, "file", errors.New("file must be a CSV"))
return
}
// read file content
content, err := io.ReadAll(file)
if err != nil {
c.Response.ValidationFailed(g, "file", err)
return
}
// parse CSV
reader := csv.NewReader(strings.NewReader(string(content)))
records, err := reader.ReadAll()
if err != nil {
c.Logger.Errorw("failed to parse CSV file", "error", err)
c.Response.ValidationFailed(g, "file", errors.New("failed to parse CSV file: "+err.Error()))
return
}
if len(records) < 2 {
c.Logger.Debugw("CSV file has insufficient rows", "rows", len(records))
c.Response.ValidationFailed(g, "file", errors.New("CSV file must have header and at least one data row"))
return
}
c.Logger.Debugw("processing CSV", "rows", len(records), "headers", records[0])
// process CSV
processed, skipped, err := c.CampaignService.ProcessReportedCSV(g.Request.Context(), session, id, records)
if err != nil {
c.Logger.Errorw("failed to process reported CSV", "error", err)
if ok := c.handleErrors(g, err); !ok {
return
}
}
c.Response.OK(g, gin.H{
"processed": processed,
"skipped": skipped,
"message": "CSV processed successfully",
})
}