nidus-sync/rmo/email.go

124 lines
3.3 KiB
Go
Raw Normal View History

package rmo
import (
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
2026-02-02 19:54:32 +00:00
"github.com/Gleipnir-Technology/nidus-sync/html"
"github.com/Gleipnir-Technology/nidus-sync/platform/email"
"github.com/aarondl/opt/omit"
"github.com/gorilla/mux"
)
type ContentEmail struct {
2026-02-02 19:54:32 +00:00
Email string
}
func getEmailByCode(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["code"]
//id := r.FormValue("id")
if id == "" {
http.Error(w, "You must specify an id", http.StatusBadRequest)
return
}
ctx := r.Context()
email_log, err := models.CommsEmailLogs.Query(
models.SelectWhere.CommsEmailLogs.PublicID.EQ(id),
).One(ctx, db.PGInstance.BobDB)
if err != nil {
respondError(w, "Failed to query email_log: %w", err, http.StatusInternalServerError)
return
}
html, err := email.RenderHTML(email_log.TemplateID, email_log.TemplateData)
if err != nil {
respondError(w, "Failed to render email_log: %w", err, http.StatusInternalServerError)
return
}
w.Write(html)
}
func getEmailReportUnsubscribe(w http.ResponseWriter, r *http.Request) {
2026-02-02 19:54:32 +00:00
email := r.FormValue("email")
html.RenderOrError(
w,
"rmo/email-unsubscribe.html",
ContentEmail{
Email: email,
},
)
}
func getEmailConfirm(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
if email == "" {
respondError(w, "Not sure what to do with an empty email", nil, http.StatusBadRequest)
return
}
html.RenderOrError(
w,
"rmo/email-confirm.html",
ContentEmail{
Email: email,
},
)
}
func getEmailConfirmComplete(w http.ResponseWriter, r *http.Request) {
html.RenderOrError(
w,
"rmo/email-confirm-complete.html",
map[string]string{},
)
}
func getEmailUnsubscribe(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
html.RenderOrError(
w,
"rmo/email-unsubscribe.html",
ContentEmail{
Email: email,
},
)
}
func getEmailUnsubscribeComplete(w http.ResponseWriter, r *http.Request) {
html.RenderOrError(
w,
"rmo/email-unsubscribe-complete.html",
map[string]string{},
)
}
func postEmailConfirm(w http.ResponseWriter, r *http.Request) {
email := r.PostFormValue("email")
if email == "" {
respondError(w, "Not sure what to do with an empty email", nil, http.StatusBadRequest)
return
}
ctx := r.Context()
email_contact, err := models.FindCommsEmailContact(ctx, db.PGInstance.BobDB, email)
if err != nil {
respondError(w, "Email not in the database", err, http.StatusNotFound)
return
}
err = email_contact.Update(ctx, db.PGInstance.BobDB, &models.CommsEmailContactSetter{
Confirmed: omit.From(true),
})
http.Redirect(w, r, "/email/confirm/complete", http.StatusFound)
}
func postEmailUnsubscribe(w http.ResponseWriter, r *http.Request) {
email := r.PostFormValue("email")
if email == "" {
respondError(w, "Not sure what to do with an empty email", nil, http.StatusBadRequest)
return
}
ctx := r.Context()
email_contact, err := models.FindCommsEmailContact(ctx, db.PGInstance.BobDB, email)
if err != nil {
respondError(w, "Email not in the database", err, http.StatusNotFound)
return
}
err = email_contact.Update(ctx, db.PGInstance.BobDB, &models.CommsEmailContactSetter{
IsSubscribed: omit.From(false),
})
http.Redirect(w, r, "/email/unsubscribe/complete", http.StatusFound)
2026-02-02 19:54:32 +00:00
}