Send mailing list emails
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -31,6 +31,9 @@ type (
|
||||
ID gid.GID `db:"id"`
|
||||
RecipientEmail string `db:"recipient_email"`
|
||||
RecipientName string `db:"recipient_name"`
|
||||
ReplyTo *mail.Addr `db:"reply_to"`
|
||||
UnsubscribeURL *string `db:"unsubscribe_url"`
|
||||
MailingListUpdateID *gid.GID `db:"mailing_list_update_id"`
|
||||
Subject string `db:"subject"`
|
||||
TextBody string `db:"text_body"`
|
||||
HtmlBody *string `db:"html_body"`
|
||||
@@ -44,6 +47,14 @@ type (
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
SentAt *time.Time `db:"sent_at"`
|
||||
}
|
||||
|
||||
Emails []*Email
|
||||
|
||||
EmailOptions struct {
|
||||
ReplyTo *mail.Addr
|
||||
UnsubscribeURL *string
|
||||
MailingListUpdateID *gid.GID
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -62,9 +73,10 @@ func NewEmail(
|
||||
subject string,
|
||||
textBody string,
|
||||
htmlBody *string,
|
||||
opts *EmailOptions,
|
||||
) *Email {
|
||||
now := time.Now()
|
||||
return &Email{
|
||||
e := &Email{
|
||||
ID: gid.New(gid.NilTenant, EmailEntityType),
|
||||
RecipientName: recipientName,
|
||||
RecipientEmail: recipientEmail.String(),
|
||||
@@ -77,6 +89,14 @@ func NewEmail(
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if opts != nil {
|
||||
e.ReplyTo = opts.ReplyTo
|
||||
e.UnsubscribeURL = opts.UnsubscribeURL
|
||||
e.MailingListUpdateID = opts.MailingListUpdateID
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Email) Insert(
|
||||
@@ -85,40 +105,100 @@ func (e *Email) Insert(
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO emails (
|
||||
id, recipient_email, recipient_name, subject, text_body, html_body,
|
||||
status, attempt_count, max_attempts, created_at, updated_at
|
||||
id,
|
||||
recipient_email,
|
||||
recipient_name,
|
||||
reply_to, unsubscribe_url,
|
||||
mailing_list_update_id,
|
||||
subject,
|
||||
text_body,
|
||||
html_body,
|
||||
status,
|
||||
attempt_count,
|
||||
max_attempts,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
@id, @recipient_email, @recipient_name, @subject, @text_body, @html_body,
|
||||
@status, @attempt_count, @max_attempts, @created_at, @updated_at
|
||||
@id,
|
||||
@recipient_email,
|
||||
@recipient_name,
|
||||
@reply_to,
|
||||
@unsubscribe_url,
|
||||
@mailing_list_update_id,
|
||||
@subject,
|
||||
@text_body,
|
||||
@html_body,
|
||||
@status,
|
||||
@attempt_count,
|
||||
@max_attempts,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": e.ID,
|
||||
"recipient_email": e.RecipientEmail,
|
||||
"recipient_name": e.RecipientName,
|
||||
"subject": e.Subject,
|
||||
"text_body": e.TextBody,
|
||||
"html_body": e.HtmlBody,
|
||||
"status": e.Status,
|
||||
"attempt_count": e.AttemptCount,
|
||||
"max_attempts": e.MaxAttempts,
|
||||
"created_at": e.CreatedAt,
|
||||
"updated_at": e.UpdatedAt,
|
||||
"id": e.ID,
|
||||
"recipient_email": e.RecipientEmail,
|
||||
"recipient_name": e.RecipientName,
|
||||
"reply_to": e.ReplyTo,
|
||||
"unsubscribe_url": e.UnsubscribeURL,
|
||||
"mailing_list_update_id": e.MailingListUpdateID,
|
||||
"subject": e.Subject,
|
||||
"text_body": e.TextBody,
|
||||
"html_body": e.HtmlBody,
|
||||
"status": e.Status,
|
||||
"attempt_count": e.AttemptCount,
|
||||
"max_attempts": e.MaxAttempts,
|
||||
"created_at": e.CreatedAt,
|
||||
"updated_at": e.UpdatedAt,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (emails Emails) BulkInsert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
if len(emails) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
rows := make([][]any, 0, len(emails))
|
||||
for _, e := range emails {
|
||||
rows = append(rows, []any{
|
||||
e.ID,
|
||||
e.RecipientEmail,
|
||||
e.RecipientName,
|
||||
e.ReplyTo,
|
||||
e.UnsubscribeURL,
|
||||
e.MailingListUpdateID,
|
||||
e.Subject,
|
||||
e.TextBody,
|
||||
e.HtmlBody,
|
||||
e.CreatedAt,
|
||||
e.UpdatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
_, err := conn.CopyFrom(
|
||||
ctx,
|
||||
pgx.Identifier{"emails"},
|
||||
[]string{"id", "recipient_email", "recipient_name", "reply_to", "unsubscribe_url", "mailing_list_update_id", "subject", "text_body", "html_body", "created_at", "updated_at"},
|
||||
pgx.CopyFromRows(rows),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *Email) LoadNextPendingForUpdateSkipLocked(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id, recipient_email, recipient_name, subject, text_body, html_body,
|
||||
id, recipient_email, recipient_name, reply_to, unsubscribe_url, mailing_list_update_id, subject, text_body, html_body,
|
||||
status, processing_started_at, attempt_count, max_attempts,
|
||||
last_attempted_at, last_error, created_at, updated_at, sent_at
|
||||
FROM emails
|
||||
|
||||
@@ -89,6 +89,7 @@ const (
|
||||
ComplianceExternalURLEntityType uint16 = 63
|
||||
MailingListEntityType uint16 = 64
|
||||
MailingListSubscriberEntityType uint16 = 65
|
||||
MailingListUpdateEntityType uint16 = 66
|
||||
)
|
||||
|
||||
func NewEntityFromID(id gid.GID) (any, bool) {
|
||||
@@ -221,6 +222,8 @@ func NewEntityFromID(id gid.GID) (any, bool) {
|
||||
return &MailingList{ID: id}, true
|
||||
case MailingListSubscriberEntityType:
|
||||
return &MailingListSubscriber{ID: id}, true
|
||||
case MailingListUpdateEntityType:
|
||||
return &MailingListUpdate{ID: id}, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
|
||||
@@ -67,6 +67,56 @@ func (cns *MailingListSubscriber) CursorKey(orderBy MailingListSubscriberOrderFi
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (cns *MailingListSubscriber) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
id gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
full_name,
|
||||
email,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
mailing_list_subscribers
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": id,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query mailing list subscriber: %w", err)
|
||||
}
|
||||
|
||||
subscriber, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[MailingListSubscriber])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect mailing list subscriber: %w", err)
|
||||
}
|
||||
|
||||
*cns = subscriber
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cns *MailingListSubscriber) LoadByMailingListIDAndEmail(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
@@ -227,11 +277,15 @@ WHERE
|
||||
args := pgx.StrictNamedArgs{"mailing_list_subscriber_id": cns.ID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
tag, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete mailing list subscriber: %w", err)
|
||||
}
|
||||
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -267,6 +321,50 @@ WHERE
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (cnss *MailingListSubscribers) LoadAllConfirmedByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
full_name,
|
||||
email,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
mailing_list_subscribers
|
||||
WHERE
|
||||
%s
|
||||
AND mailing_list_id = @mailing_list_id
|
||||
AND status = 'CONFIRMED'
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"mailing_list_id": mailingListID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query confirmed mailing list subscribers: %w", err)
|
||||
}
|
||||
|
||||
subscribers, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MailingListSubscriber])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect confirmed mailing list subscribers: %w", err)
|
||||
}
|
||||
|
||||
*cnss = subscribers
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cnss *MailingListSubscribers) LoadByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
|
||||
385
pkg/coredata/mailing_list_update.go
Normal file
385
pkg/coredata/mailing_list_update.go
Normal file
@@ -0,0 +1,385 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
MailingListUpdate struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
MailingListID gid.GID `db:"mailing_list_id"`
|
||||
Title string `db:"title"`
|
||||
Body string `db:"body"`
|
||||
Status MailingListUpdateStatus `db:"status"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
MailingListUpdateItems []*MailingListUpdate
|
||||
)
|
||||
|
||||
func (mlu *MailingListUpdate) CursorKey(orderBy MailingListUpdateOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case MailingListUpdateOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(mlu.ID, mlu.CreatedAt)
|
||||
case MailingListUpdateOrderFieldUpdatedAt:
|
||||
return page.NewCursorKey(mlu.ID, mlu.UpdatedAt)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (mlu *MailingListUpdate) AuthorizationAttributes(ctx context.Context, conn pg.Conn) (map[string]string, error) {
|
||||
q := `SELECT organization_id FROM mailing_list_updates WHERE id = $1 LIMIT 1;`
|
||||
|
||||
var organizationID gid.GID
|
||||
if err := conn.QueryRow(ctx, q, mlu.ID).Scan(&organizationID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrResourceNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("cannot query mailing list update authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
return map[string]string{"organization_id": organizationID.String()}, nil
|
||||
}
|
||||
|
||||
func (mlu *MailingListUpdate) Insert(ctx context.Context, conn pg.Conn, scope Scoper) error {
|
||||
q := `
|
||||
INSERT INTO mailing_list_updates (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
title,
|
||||
body,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@mailing_list_id,
|
||||
@title,
|
||||
@body,
|
||||
@status,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": mlu.ID,
|
||||
"organization_id": mlu.OrganizationID,
|
||||
"mailing_list_id": mlu.MailingListID,
|
||||
"title": mlu.Title,
|
||||
"body": mlu.Body,
|
||||
"status": mlu.Status,
|
||||
"created_at": mlu.CreatedAt,
|
||||
"updated_at": mlu.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (mlu *MailingListUpdate) Update(ctx context.Context, conn pg.Conn, scope Scoper) error {
|
||||
q := `
|
||||
UPDATE mailing_list_updates
|
||||
SET
|
||||
title = @title,
|
||||
body = @body,
|
||||
status = @status,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": mlu.ID,
|
||||
"title": mlu.Title,
|
||||
"body": mlu.Body,
|
||||
"status": mlu.Status,
|
||||
"updated_at": mlu.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
tag, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update mailing list update: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mlu *MailingListUpdate) Delete(ctx context.Context, conn pg.Conn, scope Scoper) error {
|
||||
q := `
|
||||
DELETE FROM mailing_list_updates
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": mlu.ID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
tag, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete mailing list update: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mlu *MailingListUpdate) LoadByID(ctx context.Context, conn pg.Conn, scope Scoper, id gid.GID) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
title,
|
||||
body,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM mailing_list_updates
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
LIMIT 1;
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": id,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query mailing list update: %w", err)
|
||||
}
|
||||
|
||||
result, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[MailingListUpdate])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
return fmt.Errorf("cannot collect mailing list update: %w", err)
|
||||
}
|
||||
|
||||
*mlu = result
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mlul *MailingListUpdateItems) LoadSentByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
cursor *page.Cursor[MailingListUpdateOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
title,
|
||||
body,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM mailing_list_updates
|
||||
WHERE
|
||||
%s
|
||||
AND mailing_list_id = @mailing_list_id
|
||||
AND status = 'SENT'
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_id": mailingListID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query sent mailing list updates: %w", err)
|
||||
}
|
||||
|
||||
results, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MailingListUpdate])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect sent mailing list updates: %w", err)
|
||||
}
|
||||
|
||||
*mlul = results
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mlul *MailingListUpdateItems) LoadByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
cursor *page.Cursor[MailingListUpdateOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
title,
|
||||
body,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM mailing_list_updates
|
||||
WHERE
|
||||
%s
|
||||
AND mailing_list_id = @mailing_list_id
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_id": mailingListID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query mailing list updates: %w", err)
|
||||
}
|
||||
|
||||
results, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MailingListUpdate])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect mailing list updates: %w", err)
|
||||
}
|
||||
|
||||
*mlul = results
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mlul *MailingListUpdateItems) CountByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT COUNT(*)
|
||||
FROM mailing_list_updates
|
||||
WHERE
|
||||
%s
|
||||
AND mailing_list_id = @mailing_list_id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_id": mailingListID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
var count int
|
||||
if err := conn.QueryRow(ctx, q, args).Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("cannot count mailing list updates: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (mlu *MailingListUpdate) LoadNextEnqueuedForUpdateSkipLocked(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
title,
|
||||
body,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM mailing_list_updates
|
||||
WHERE status = 'ENQUEUED'
|
||||
ORDER BY updated_at ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
`
|
||||
|
||||
rows, err := conn.Query(ctx, q)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query enqueued mailing list updates: %w", err)
|
||||
}
|
||||
|
||||
result, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[MailingListUpdate])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
return fmt.Errorf("cannot collect enqueued mailing list update: %w", err)
|
||||
}
|
||||
|
||||
*mlu = result
|
||||
return nil
|
||||
}
|
||||
|
||||
func ResetStaleProcessingMailingListUpdates(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
staleAfter time.Duration,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE mailing_list_updates
|
||||
SET status = 'ENQUEUED', updated_at = NOW()
|
||||
WHERE status = 'PROCESSING'
|
||||
AND updated_at < NOW() - @stale_after::interval
|
||||
`
|
||||
_, err := conn.Exec(ctx, q, pgx.StrictNamedArgs{"stale_after": staleAfter})
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot reset stale processing mailing list updates: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
39
pkg/coredata/mailing_list_update_order_field.go
Normal file
39
pkg/coredata/mailing_list_update_order_field.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import "fmt"
|
||||
|
||||
type MailingListUpdateOrderField string
|
||||
|
||||
const (
|
||||
MailingListUpdateOrderFieldCreatedAt MailingListUpdateOrderField = "CREATED_AT"
|
||||
MailingListUpdateOrderFieldUpdatedAt MailingListUpdateOrderField = "UPDATED_AT"
|
||||
)
|
||||
|
||||
func (f MailingListUpdateOrderField) String() string {
|
||||
return string(f)
|
||||
}
|
||||
|
||||
func (f MailingListUpdateOrderField) Column() string {
|
||||
switch f {
|
||||
case MailingListUpdateOrderFieldCreatedAt:
|
||||
return "created_at"
|
||||
case MailingListUpdateOrderFieldUpdatedAt:
|
||||
return "updated_at"
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", f))
|
||||
}
|
||||
64
pkg/coredata/mailing_list_update_status.go
Normal file
64
pkg/coredata/mailing_list_update_status.go
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type MailingListUpdateStatus string
|
||||
|
||||
const (
|
||||
MailingListUpdateStatusDraft MailingListUpdateStatus = "DRAFT"
|
||||
MailingListUpdateStatusEnqueued MailingListUpdateStatus = "ENQUEUED"
|
||||
MailingListUpdateStatusProcessing MailingListUpdateStatus = "PROCESSING"
|
||||
MailingListUpdateStatusSent MailingListUpdateStatus = "SENT"
|
||||
)
|
||||
|
||||
func (s MailingListUpdateStatus) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func (s *MailingListUpdateStatus) Scan(value any) error {
|
||||
var str string
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
str = v
|
||||
case []byte:
|
||||
str = string(v)
|
||||
default:
|
||||
return fmt.Errorf("unsupported type for MailingListUpdateStatus: %T", value)
|
||||
}
|
||||
|
||||
switch str {
|
||||
case "DRAFT":
|
||||
*s = MailingListUpdateStatusDraft
|
||||
case "ENQUEUED":
|
||||
*s = MailingListUpdateStatusEnqueued
|
||||
case "PROCESSING":
|
||||
*s = MailingListUpdateStatusProcessing
|
||||
case "SENT":
|
||||
*s = MailingListUpdateStatusSent
|
||||
default:
|
||||
return fmt.Errorf("invalid MailingListUpdateStatus value: %q", str)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MailingListUpdateStatus) Value() (driver.Value, error) {
|
||||
return s.String(), nil
|
||||
}
|
||||
18
pkg/coredata/migrations/20260305T120000Z.sql
Normal file
18
pkg/coredata/migrations/20260305T120000Z.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
CREATE TYPE mailing_list_update_status AS ENUM ('DRAFT', 'ENQUEUED', 'PROCESSING', 'SENT');
|
||||
|
||||
CREATE TABLE mailing_list_updates (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
organization_id TEXT NOT NULL REFERENCES organizations(id) ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
mailing_list_id TEXT NOT NULL REFERENCES mailing_lists(id) ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
title TEXT NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
status mailing_list_update_status NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
ALTER TABLE emails
|
||||
ADD COLUMN reply_to TEXT,
|
||||
ADD COLUMN unsubscribe_url TEXT,
|
||||
ADD COLUMN mailing_list_update_id TEXT REFERENCES mailing_list_updates(id) ON DELETE SET NULL;
|
||||
@@ -120,6 +120,57 @@ LIMIT 1;
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tc *TrustCenter) LoadByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
tenant_id,
|
||||
mailing_list_id,
|
||||
logo_file_id,
|
||||
dark_logo_file_id,
|
||||
active,
|
||||
slug,
|
||||
non_disclosure_agreement_file_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
trust_centers
|
||||
WHERE
|
||||
%s
|
||||
AND mailing_list_id = @mailing_list_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"mailing_list_id": mailingListID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query trust center by mailing list id: %w", err)
|
||||
}
|
||||
|
||||
trustCenter, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[TrustCenter])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect trust center: %w", err)
|
||||
}
|
||||
|
||||
*tc = trustCenter
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tc *TrustCenter) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
|
||||
Reference in New Issue
Block a user