nidus-sync/platform/email/initial.go
Eli Ribble 2538638c9d
Create generic backend process, fix background interdependencies
This refactor was born out of the inter-dependency cycles developing
between the "background" module and just about every other module which
was caused by the background module becoming a dependency of every
module that needed to background work and the fact that the background
module was also supposedly responsible for the logic for processing
those tasks.

Instead the "background" module is now very, very shallow and relies
entirely on the Postgres NOTIFY logic for triggering jobs. There's a new
table, `job` which holds just a type and single row ID.

All told, this means that jobs can be added to the queue as part of the
API-level or platform-level transaction, ensuring atomicity, and
processing coordination is handled by the platform module, which can
depend on anything.
2026-03-16 19:52:29 +00:00

58 lines
1.7 KiB
Go

package email
import (
"context"
"fmt"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
//"github.com/rs/zerolog/log"
)
type contentEmailInitial struct {
Base contentEmailBase
Destination string
URLSubscribe string
}
func maybeSendInitialEmail(ctx context.Context, destination string) error {
err := EnsureInDB(ctx, destination)
if err != nil {
return fmt.Errorf("Failed to add email recipient to database: %w", err)
}
rows, err := models.CommsEmailLogs.Query(
models.SelectWhere.CommsEmailLogs.Destination.EQ(destination),
models.SelectWhere.CommsEmailLogs.TemplateID.EQ(templateInitialID),
).All(ctx, db.PGInstance.BobDB)
// We already sent an initial email
if len(rows) > 0 {
return nil
}
return sendEmailInitialContact(ctx, destination)
}
func urlEmailInBrowser(public_id string) string {
return config.MakeURLReport("/email/render/%s", public_id)
}
func urlUnsubscribe(email string) string {
return config.MakeURLReport("/email/unsubscribe?email=%s", email)
}
func sendEmailInitialContact(ctx context.Context, destination string) error {
//data := pgtypes.HStore{}
data := make(map[string]string, 0)
source := config.ForwardEmailRMOAddress
data["Destination"] = destination
data["Source"] = source
data["URLLogo"] = config.MakeURLReport("/static/img/nidus-logo-no-lettering-64.png")
data["URLSubscribe"] = config.MakeURLReport("/email/confirm?email=%s", destination)
data["URLUnsubscribe"] = urlUnsubscribe(destination)
subject := "Welcome"
err := sendEmailBegin(ctx, source, destination, templateInitialID, subject, data)
if err != nil {
return fmt.Errorf("Failed to send initial email to %s: %w", err)
}
return nil
}