// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // 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/iam/policy" "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.Querier, resourceIDs []gid.GID, ) (policy.AttributesByID, error) { q := `SELECT id, organization_id FROM mailing_lists WHERE id = ANY(@resource_ids::text[])` args := pgx.StrictNamedArgs{ "resource_ids": resourceIDs, } rows, err := conn.Query(ctx, q, args) if err != nil { return nil, fmt.Errorf("cannot query authorization attributes: %w", err) } defer rows.Close() attrsByID := make(policy.AttributesByID) for rows.Next() { var id, organizationID gid.GID if err := rows.Scan(&id, &organizationID); err != nil { return nil, fmt.Errorf("cannot scan authorization attributes: %w", err) } attrsByID[id] = policy.Attributes{ "organization_id": organizationID.String(), } } if err := rows.Err(); err != nil { return nil, fmt.Errorf("cannot iterate authorization attributes: %w", err) } return attrsByID, nil } func (ml *MailingList) LoadByID( ctx context.Context, conn pg.Querier, 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.Tx, 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.Tx, 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 }