This in a pretty huge change. At a high level we're adding the concept of a 'contact' which is a person or organization that has zero or more contact methods (email, phone). This ended up cascading a number of changes, including critically to the publicreprt schema. In the end it seemed safer to get to the point where I'm confident we aren't using any of the old fields for storing reporter information (though I haven't deleted the columns yet) so I removed the code for defining those columns. At this point I think it's not possible for me to regenerate the bob schema due to the interdependencies between my various schemas, so the migration is well-and-truly happening.
53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
package email
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
modelcomms "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
|
//"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func maybeSendInitialEmail(ctx context.Context, txn db.Ex, contact modelcomms.Contact, destination string) error {
|
|
err := EnsureInDB(ctx, txn, contact, 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
|
|
}
|