Add compliance page mailing list base
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -87,6 +87,8 @@ const (
|
||||
EmailAttachmentEntityType uint16 = 61
|
||||
ComplianceFrameworkEntityType uint16 = 62
|
||||
ComplianceExternalURLEntityType uint16 = 63
|
||||
MailingListEntityType uint16 = 64
|
||||
MailingListSubscriberEntityType uint16 = 65
|
||||
)
|
||||
|
||||
func NewEntityFromID(id gid.GID) (any, bool) {
|
||||
@@ -215,6 +217,10 @@ func NewEntityFromID(id gid.GID) (any, bool) {
|
||||
return &ComplianceFramework{ID: id}, true
|
||||
case ComplianceExternalURLEntityType:
|
||||
return &ComplianceExternalURL{ID: id}, true
|
||||
case MailingListEntityType:
|
||||
return &MailingList{ID: id}, true
|
||||
case MailingListSubscriberEntityType:
|
||||
return &MailingListSubscriber{ID: id}, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
|
||||
170
pkg/coredata/mailing_list.go
Normal file
170
pkg/coredata/mailing_list.go
Normal file
@@ -0,0 +1,170 @@
|
||||
// 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/mail"
|
||||
)
|
||||
|
||||
type MailingList struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
ReplyTo *mail.Addr `db:"reply_to"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
func (ml *MailingList) AuthorizationAttributes(ctx context.Context, conn pg.Conn) (map[string]string, error) {
|
||||
q := `SELECT organization_id FROM mailing_lists WHERE id = $1 LIMIT 1;`
|
||||
|
||||
var organizationID gid.GID
|
||||
if err := conn.QueryRow(ctx, q, ml.ID).Scan(&organizationID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrResourceNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("cannot query mailing list authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
return map[string]string{"organization_id": organizationID.String()}, nil
|
||||
}
|
||||
|
||||
func (ml *MailingList) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
id gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
reply_to,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
mailing_lists
|
||||
WHERE
|
||||
%s
|
||||
AND id = @mailing_list_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"mailing_list_id": id}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query mailing list: %w", err)
|
||||
}
|
||||
|
||||
result, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[MailingList])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
return fmt.Errorf("cannot collect mailing list: %w", err)
|
||||
}
|
||||
|
||||
*ml = result
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ml *MailingList) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE mailing_lists
|
||||
SET
|
||||
reply_to = @reply_to,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
AND id = @mailing_list_id;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_id": ml.ID,
|
||||
"reply_to": ml.ReplyTo,
|
||||
"updated_at": ml.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
tag, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update mailing list: %w", err)
|
||||
}
|
||||
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ml *MailingList) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO mailing_lists (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
reply_to,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
@mailing_list_id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@reply_to,
|
||||
@created_at,
|
||||
@updated_at
|
||||
);
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_id": ml.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": ml.OrganizationID,
|
||||
"reply_to": ml.ReplyTo,
|
||||
"created_at": ml.CreatedAt,
|
||||
"updated_at": ml.UpdatedAt,
|
||||
}
|
||||
|
||||
if _, err := conn.Exec(ctx, q, args); err != nil {
|
||||
return fmt.Errorf("cannot insert mailing list: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
316
pkg/coredata/mailing_list_subscriber.go
Normal file
316
pkg/coredata/mailing_list_subscriber.go
Normal file
@@ -0,0 +1,316 @@
|
||||
// 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"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
MailingListSubscriber struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
MailingListID gid.GID `db:"mailing_list_id"`
|
||||
FullName string `db:"full_name"`
|
||||
Email mail.Addr `db:"email"`
|
||||
Status MailingListSubscriberStatus `db:"status"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
MailingListSubscribers []*MailingListSubscriber
|
||||
)
|
||||
|
||||
func (cns *MailingListSubscriber) AuthorizationAttributes(ctx context.Context, conn pg.Conn) (map[string]string, error) {
|
||||
q := `SELECT organization_id FROM mailing_list_subscribers WHERE id = $1 LIMIT 1;`
|
||||
|
||||
var organizationID gid.GID
|
||||
if err := conn.QueryRow(ctx, q, cns.ID).Scan(&organizationID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrResourceNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("cannot query mailing list subscriber authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
return map[string]string{"organization_id": organizationID.String()}, nil
|
||||
}
|
||||
|
||||
func (cns *MailingListSubscriber) CursorKey(orderBy MailingListSubscriberOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case MailingListSubscriberOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(cns.ID, cns.CreatedAt)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (cns *MailingListSubscriber) LoadByMailingListIDAndEmail(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
email mail.Addr,
|
||||
) 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 email = @email
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_id": mailingListID,
|
||||
"email": email,
|
||||
}
|
||||
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) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO mailing_list_subscribers (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
mailing_list_id,
|
||||
full_name,
|
||||
email,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
@mailing_list_subscriber_id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@mailing_list_id,
|
||||
@full_name,
|
||||
@email,
|
||||
@status,
|
||||
@created_at,
|
||||
@updated_at
|
||||
);
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_subscriber_id": cns.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": cns.OrganizationID,
|
||||
"mailing_list_id": cns.MailingListID,
|
||||
"full_name": cns.FullName,
|
||||
"email": cns.Email,
|
||||
"status": cns.Status,
|
||||
"created_at": cns.CreatedAt,
|
||||
"updated_at": cns.UpdatedAt,
|
||||
}
|
||||
|
||||
if _, err := conn.Exec(ctx, q, args); err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
|
||||
return ErrResourceAlreadyExists
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot insert mailing list subscriber: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cns *MailingListSubscriber) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE mailing_list_subscribers
|
||||
SET
|
||||
status = @status,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
AND id = @mailing_list_subscriber_id;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"mailing_list_subscriber_id": cns.ID,
|
||||
"status": cns.Status,
|
||||
"updated_at": cns.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
tag, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update mailing list subscriber: %w", err)
|
||||
}
|
||||
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cns *MailingListSubscriber) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
DELETE FROM
|
||||
mailing_list_subscribers
|
||||
WHERE
|
||||
%s
|
||||
AND id = @mailing_list_subscriber_id;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"mailing_list_subscriber_id": cns.ID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete mailing list subscriber: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cnss *MailingListSubscribers) CountByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT
|
||||
COUNT(id)
|
||||
FROM
|
||||
mailing_list_subscribers
|
||||
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())
|
||||
|
||||
row := conn.QueryRow(ctx, q, args)
|
||||
|
||||
var count int
|
||||
err := row.Scan(&count)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("cannot count mailing list subscribers: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (cnss *MailingListSubscribers) LoadByMailingListID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mailingListID gid.GID,
|
||||
cursor *page.Cursor[MailingListSubscriberOrderField],
|
||||
) 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 %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 subscribers: %w", err)
|
||||
}
|
||||
|
||||
subscribers, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MailingListSubscriber])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect mailing list subscribers: %w", err)
|
||||
}
|
||||
|
||||
*cnss = subscribers
|
||||
|
||||
return nil
|
||||
}
|
||||
36
pkg/coredata/mailing_list_subscriber_order_field.go
Normal file
36
pkg/coredata/mailing_list_subscriber_order_field.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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 MailingListSubscriberOrderField string
|
||||
|
||||
const (
|
||||
MailingListSubscriberOrderFieldCreatedAt MailingListSubscriberOrderField = "CREATED_AT"
|
||||
)
|
||||
|
||||
func (f MailingListSubscriberOrderField) String() string {
|
||||
return string(f)
|
||||
}
|
||||
|
||||
func (f MailingListSubscriberOrderField) Column() string {
|
||||
switch f {
|
||||
case MailingListSubscriberOrderFieldCreatedAt:
|
||||
return "created_at"
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", f))
|
||||
}
|
||||
58
pkg/coredata/mailing_list_subscriber_status.go
Normal file
58
pkg/coredata/mailing_list_subscriber_status.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// 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 MailingListSubscriberStatus string
|
||||
|
||||
const (
|
||||
MailingListSubscriberStatusPending MailingListSubscriberStatus = "PENDING"
|
||||
MailingListSubscriberStatusConfirmed MailingListSubscriberStatus = "CONFIRMED"
|
||||
)
|
||||
|
||||
func (s MailingListSubscriberStatus) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func (s *MailingListSubscriberStatus) 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 MailingListSubscriberStatus: %T", value)
|
||||
}
|
||||
|
||||
switch str {
|
||||
case "PENDING":
|
||||
*s = MailingListSubscriberStatusPending
|
||||
case "CONFIRMED":
|
||||
*s = MailingListSubscriberStatusConfirmed
|
||||
default:
|
||||
return fmt.Errorf("invalid MailingListSubscriberStatus value: %q", str)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MailingListSubscriberStatus) Value() (driver.Value, error) {
|
||||
return s.String(), nil
|
||||
}
|
||||
50
pkg/coredata/migrations/20260225T000000Z.sql
Normal file
50
pkg/coredata/migrations/20260225T000000Z.sql
Normal file
@@ -0,0 +1,50 @@
|
||||
CREATE TABLE mailing_lists (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
organization_id TEXT NOT NULL REFERENCES organizations(id) ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
reply_to CITEXT,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
UNIQUE (organization_id)
|
||||
);
|
||||
|
||||
CREATE TYPE mailing_list_subscriber_status AS ENUM (
|
||||
'PENDING',
|
||||
'CONFIRMED'
|
||||
);
|
||||
|
||||
CREATE TABLE mailing_list_subscribers (
|
||||
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,
|
||||
full_name TEXT NOT NULL,
|
||||
email CITEXT NOT NULL,
|
||||
status mailing_list_subscriber_status NOT NULL DEFAULT 'PENDING',
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
UNIQUE (mailing_list_id, email)
|
||||
);
|
||||
|
||||
ALTER TABLE trust_centers
|
||||
ADD COLUMN mailing_list_id TEXT REFERENCES mailing_lists(id) ON UPDATE CASCADE ON DELETE RESTRICT;
|
||||
|
||||
INSERT INTO mailing_lists (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
SELECT
|
||||
generate_gid(decode_base64_unpadded(tc.tenant_id), 64),
|
||||
tc.tenant_id,
|
||||
tc.organization_id,
|
||||
NOW(),
|
||||
NOW()
|
||||
FROM trust_centers tc;
|
||||
|
||||
UPDATE trust_centers tc
|
||||
SET mailing_list_id = ml.id
|
||||
FROM mailing_lists ml
|
||||
WHERE ml.organization_id = tc.organization_id;
|
||||
@@ -35,6 +35,7 @@ type (
|
||||
TenantID gid.TenantID `db:"tenant_id"`
|
||||
Active bool `db:"active"`
|
||||
Slug string `db:"slug"`
|
||||
MailingListID *gid.GID `db:"mailing_list_id"`
|
||||
LogoFileID *gid.GID `db:"logo_file_id"`
|
||||
DarkLogoFileID *gid.GID `db:"dark_logo_file_id"`
|
||||
NonDisclosureAgreementFileID *gid.GID `db:"non_disclosure_agreement_file_id"`
|
||||
@@ -79,6 +80,7 @@ SELECT
|
||||
id,
|
||||
organization_id,
|
||||
tenant_id,
|
||||
mailing_list_id,
|
||||
logo_file_id,
|
||||
dark_logo_file_id,
|
||||
active,
|
||||
@@ -129,6 +131,7 @@ SELECT
|
||||
id,
|
||||
organization_id,
|
||||
tenant_id,
|
||||
mailing_list_id,
|
||||
logo_file_id,
|
||||
dark_logo_file_id,
|
||||
active,
|
||||
@@ -179,6 +182,7 @@ SELECT
|
||||
id,
|
||||
organization_id,
|
||||
tenant_id,
|
||||
mailing_list_id,
|
||||
logo_file_id,
|
||||
dark_logo_file_id,
|
||||
active,
|
||||
@@ -224,6 +228,7 @@ INSERT INTO trust_centers (
|
||||
id,
|
||||
organization_id,
|
||||
tenant_id,
|
||||
mailing_list_id,
|
||||
logo_file_id,
|
||||
dark_logo_file_id,
|
||||
active,
|
||||
@@ -235,6 +240,7 @@ INSERT INTO trust_centers (
|
||||
@id,
|
||||
@organization_id,
|
||||
@tenant_id,
|
||||
@mailing_list_id,
|
||||
@logo_file_id,
|
||||
@dark_logo_file_id,
|
||||
@active,
|
||||
@@ -249,6 +255,7 @@ INSERT INTO trust_centers (
|
||||
"id": tc.ID,
|
||||
"organization_id": tc.OrganizationID,
|
||||
"tenant_id": tc.TenantID,
|
||||
"mailing_list_id": tc.MailingListID,
|
||||
"logo_file_id": tc.LogoFileID,
|
||||
"dark_logo_file_id": tc.DarkLogoFileID,
|
||||
"active": tc.Active,
|
||||
|
||||
Reference in New Issue
Block a user