Various new modules for mailer
This commit is contained in:
parent
985a2ab186
commit
558412cfb4
6 changed files with 627 additions and 0 deletions
60
sync/mailer.go
Normal file
60
sync/mailer.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
//"strconv"
|
||||
|
||||
"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"
|
||||
//"github.com/google/uuid"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type contentMailer struct {
|
||||
Config contentConfig
|
||||
DocumentID string
|
||||
LogoURL string
|
||||
Organization *models.Organization
|
||||
PoolImageURL string
|
||||
QRCodeURL string
|
||||
ReportURL string
|
||||
}
|
||||
|
||||
// func getMailer(ctx context.Context, r *http.Request, org *models.Organization, user *models.User) (*response[contentMailer], *errorWithStatus) {
|
||||
func getMailer(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
func getMailerPreview(w http.ResponseWriter, r *http.Request) {
|
||||
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.Site(),
|
||||
models.SelectWhere.ComplianceReportRequests.PublicID.EQ(code),
|
||||
).One(ctx, db.PGInstance.BobDB)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, "no comp", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
site := comp.R.Site
|
||||
org, err := models.FindOrganization(ctx, db.PGInstance.BobDB, site.OrganizationID)
|
||||
if err != nil {
|
||||
http.Error(w, "no comp", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
html.RenderOrError(w, "sync/mailer.html", contentMailer{
|
||||
Config: newContentConfig(),
|
||||
DocumentID: "00000000-0000-0000-0000-000000000000",
|
||||
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),
|
||||
})
|
||||
}
|
||||
14
sync/parcel.go
Normal file
14
sync/parcel.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
)
|
||||
|
||||
type contentParcel struct{}
|
||||
|
||||
func getParcel(ctx context.Context, r *http.Request, org *models.Organization, user *models.User) (*response[contentParcel], *errorWithStatus) {
|
||||
return newResponse("sync/parcel.html", contentParcel{}), nil
|
||||
}
|
||||
82
sync/qr.go
Normal file
82
sync/qr.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"github.com/skip2/go-qrcode"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func getQRCodeMailer(w http.ResponseWriter, r *http.Request) {
|
||||
code := chi.URLParam(r, "code")
|
||||
if code == "" {
|
||||
respondError(w, "There should always be a id", nil, http.StatusBadRequest)
|
||||
}
|
||||
content := config.MakeURLReport("/mailer/%s", code)
|
||||
writeQRCode(w, r, content)
|
||||
}
|
||||
|
||||
func getQRCodeReport(w http.ResponseWriter, r *http.Request) {
|
||||
code := chi.URLParam(r, "code")
|
||||
if code == "" {
|
||||
respondError(w, "There should always be a code", nil, http.StatusBadRequest)
|
||||
}
|
||||
content := config.MakeURLNidus("/report/%s", code)
|
||||
writeQRCode(w, r, content)
|
||||
}
|
||||
func writeQRCode(w http.ResponseWriter, r *http.Request, content string) {
|
||||
// Get optional size parameter (default to 256)
|
||||
size := 256
|
||||
if sizeStr := r.URL.Query().Get("size"); sizeStr != "" {
|
||||
var err error
|
||||
size, err = strconv.Atoi(sizeStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid 'size' parameter, must be an integer", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Get optional error correction level (default to Medium)
|
||||
level := qrcode.Medium
|
||||
if levelStr := r.URL.Query().Get("level"); levelStr != "" {
|
||||
switch levelStr {
|
||||
case "L", "l":
|
||||
level = qrcode.Low
|
||||
case "M", "m":
|
||||
level = qrcode.Medium
|
||||
case "Q", "q":
|
||||
level = qrcode.High
|
||||
case "H", "h":
|
||||
level = qrcode.Highest
|
||||
default:
|
||||
respondError(w, "Invalid 'level' parameter, must be L, M, Q, or H", nil, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the QR code
|
||||
var qr *qrcode.QRCode
|
||||
var err error
|
||||
qr, err = qrcode.New(content, level)
|
||||
if err != nil {
|
||||
respondError(w, "Error generating QR code", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the appropriate content type
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
|
||||
// Generate PNG and write directly to the response writer
|
||||
png, err := qr.PNG(size)
|
||||
if err != nil {
|
||||
respondError(w, "Error encoding QR code to PNG", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = w.Write(png)
|
||||
if err != nil {
|
||||
respondError(w, "Error writing response", err, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue