nidus-sync/sync/mailer.go

147 lines
4.4 KiB
Go
Raw Normal View History

2026-02-28 23:17:30 +00:00
package sync
import (
"bytes"
"fmt"
"io"
2026-02-28 23:17:30 +00:00
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/html"
2026-03-31 17:34:08 +00:00
"github.com/Gleipnir-Technology/nidus-sync/platform"
"github.com/Gleipnir-Technology/nidus-sync/platform/pdf"
2026-02-28 23:17:30 +00:00
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
2026-03-31 17:34:08 +00:00
"github.com/rs/zerolog/log"
2026-02-28 23:17:30 +00:00
)
type contentMailer struct {
Config html.ContentConfig
2026-02-28 23:17:30 +00:00
DocumentID string
LogoURL string
Organization *models.Organization
PoolImageURL string
QRCodeURL string
ReportURL string
}
2026-03-31 17:34:08 +00:00
func getMailer1(w http.ResponseWriter, r *http.Request) {
path := "/mailer/mode-1/preview"
content, err := pdf.GeneratePDF(r.Context(), path)
if err != nil {
respondError(w, "generate pdf failure", err, http.StatusInternalServerError)
return
}
err = writePDF(w, content, "mailer-mode-1.pdf")
if err != nil {
respondError(w, "copy error", err, http.StatusInternalServerError)
return
}
}
func getMailer2(w http.ResponseWriter, r *http.Request) {
2026-03-31 20:05:35 +00:00
path := "/mailer/mode-2/preview"
content, err := pdf.GeneratePDF(r.Context(), path)
if err != nil {
respondError(w, "generate pdf failure", err, http.StatusInternalServerError)
return
}
err = writePDF(w, content, "mailer-mode-2.pdf")
if err != nil {
respondError(w, "copy error", err, http.StatusInternalServerError)
return
}
2026-03-31 17:34:08 +00:00
}
func getMailer3(w http.ResponseWriter, r *http.Request) {
code := chi.URLParam(r, "code")
if code == "" {
http.Error(w, "empty code", http.StatusBadRequest)
return
}
content, err := pdf.GeneratePDF(r.Context(), code)
if err != nil {
respondError(w, "generate pdf failure", err, http.StatusInternalServerError)
return
}
2026-03-31 17:34:08 +00:00
filename := fmt.Sprintf("compliance-mailer-%s.pdf", code)
err = writePDF(w, content, filename)
if err != nil {
respondError(w, "copy error", err, http.StatusInternalServerError)
return
}
2026-02-28 23:17:30 +00:00
}
2026-03-31 17:34:08 +00:00
func writePDF(w http.ResponseWriter, content []byte, filename string) error {
w.Header().Set("Content-Type", "application/pdf")
disposition := fmt.Sprintf("attachment; filename=\"%s\"", filename)
w.Header().Set("Content-Disposition", disposition)
_, err := io.Copy(w, bytes.NewReader(content))
return err
}
func getMailer1Preview(w http.ResponseWriter, r *http.Request) {
html.RenderOrError(w, "sync/mailer-1.html", contentMailer{})
}
func getMailer2Preview(w http.ResponseWriter, r *http.Request) {
2026-03-31 20:05:35 +00:00
ctx := r.Context()
org, err := models.FindOrganization(ctx, db.PGInstance.BobDB, 1)
//org, err := platform.OrganizationByID(ctx, 1)
if err != nil {
http.Error(w, "no comp", http.StatusInternalServerError)
return
}
html.RenderOrError(w, "sync/mailer-2.html", contentMailer{
Config: html.NewContentConfig(),
DocumentID: "abc-123",
LogoURL: config.MakeURLNidus("/api/district/delta-mvcd/logo"),
Organization: org,
PoolImageURL: config.MakeURLNidus("/mailer/pool/random"),
QRCodeURL: config.MakeURLNidus("/qr-code/marketing"),
ReportURL: "https://nidus.cloud",
})
2026-03-31 17:34:08 +00:00
}
func getMailer3Preview(w http.ResponseWriter, r *http.Request) {
2026-02-28 23:17:30 +00:00
code := chi.URLParam(r, "code")
if code == "" {
http.Error(w, "empty code", http.StatusBadRequest)
return
}
ctx := r.Context()
comp, err := models.ComplianceReportRequests.Query(
models.Preload.ComplianceReportRequest.Lead(),
2026-02-28 23:17:30 +00:00
models.SelectWhere.ComplianceReportRequests.PublicID.EQ(code),
).One(ctx, db.PGInstance.BobDB)
if err != nil {
http.Error(w, "no comp", http.StatusInternalServerError)
return
}
lead := comp.R.Lead
org, err := models.FindOrganization(ctx, db.PGInstance.BobDB, lead.OrganizationID)
2026-02-28 23:17:30 +00:00
if err != nil {
http.Error(w, "no comp", http.StatusInternalServerError)
return
}
doc_id := uuid.New()
2026-03-31 17:34:08 +00:00
html.RenderOrError(w, "sync/mailer-3.html", contentMailer{
Config: html.NewContentConfig(),
DocumentID: doc_id.String(),
2026-02-28 23:17:30 +00:00
LogoURL: config.MakeURLNidus("/api/district/%s/logo", org.Slug.GetOr("unset")),
Organization: org,
PoolImageURL: config.MakeURLNidus("/api/compliance-request/image/pool/%s", code),
QRCodeURL: config.MakeURLNidus("/qr-code/mailer/%s", code),
ReportURL: config.MakeURLReport("/mailer/%s", code),
})
}
2026-03-31 17:34:08 +00:00
func getMailerPoolRandom(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
err := platform.WriteTileRandom(ctx, w)
if err != nil {
log.Error().Err(err).Msg("failed to do random tile")
http.Error(w, "failed to do tile", http.StatusInternalServerError)
return
}
}