Files

95 lines
4.0 KiB
Go

//go:build e2e
package postgres
import (
"context"
"errors"
"fmt"
"regexp"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"lincodex/backend/internal/port/outbound"
)
// notificationFixtureEmail limits the notification fixture to the synthetic
// accounts of the isolated browser suite.
var notificationFixtureEmail = regexp.MustCompile(`@example\.test$`)
// syntheticNoticeEventType is the synthetic inbox event the fixture replays.
const syntheticNoticeEventType = "notification.synthetic_delivered"
// CheckNotificationFixtureScope accepts only a synthetic account and an
// existing organization. It is available only in e2e-tagged builds.
func CheckNotificationFixtureScope(ctx context.Context, pool *pgxpool.Pool, recipientID, organizationID uuid.UUID) error {
const location = "postgres.CheckNotificationFixtureScope"
if pool == nil || recipientID == uuid.Nil || organizationID == uuid.Nil {
return fmt.Errorf("%s - invalid fixture identity: %w", location, ErrCompanyFixtureScope)
}
var email string
err := pool.QueryRow(ctx, `SELECT email FROM users WHERE id = $1`, recipientID).Scan(&email)
if errors.Is(err, pgx.ErrNoRows) {
return fmt.Errorf("%s - recipient: %w", location, outbound.ErrUserNotFound)
}
if err != nil {
return fmt.Errorf("%s - read recipient: %w", location, sanitizedDatabaseError(err))
}
if !notificationFixtureEmail.MatchString(email) {
return fmt.Errorf("%s - recipient is outside the fixture scope: %w", location, ErrCompanyFixtureScope)
}
var exists bool
if err := pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM organizations WHERE id = $1)`, organizationID).Scan(&exists); err != nil {
return fmt.Errorf("%s - read organization: %w", location, sanitizedDatabaseError(err))
}
if !exists {
return fmt.Errorf("%s - organization: %w", location, outbound.ErrOrganizationNotFound)
}
return nil
}
// NotificationFixtureEvent reads a synthetic inbox event from the outbox as
// the envelope a consumer receives for it, so the fixture can deliver it
// again the way an at-least-once bus does.
func NotificationFixtureEvent(ctx context.Context, pool *pgxpool.Pool, eventID uuid.UUID) (outbound.ConsumedEvent, error) {
const location = "postgres.NotificationFixtureEvent"
var event outbound.ConsumedEvent
var payload []byte
err := pool.QueryRow(ctx, `
SELECT event_id, event_type, schema_version, aggregate_type, aggregate_id, aggregate_version, payload::text, occurred_at
FROM outbox_events
WHERE event_id = $1 AND event_type = $2`, eventID, syntheticNoticeEventType).Scan(
&event.EventID, &event.EventType, &event.SchemaVersion, &event.AggregateType, &event.AggregateID,
&event.AggregateVersion, &payload, &event.OccurredAt)
if errors.Is(err, pgx.ErrNoRows) {
return outbound.ConsumedEvent{}, fmt.Errorf("%s - synthetic event: %w", location, outbound.ErrNotificationInboxEntryNotFound)
}
if err != nil {
return outbound.ConsumedEvent{}, fmt.Errorf("%s - read outbox event: %w", location, sanitizedDatabaseError(err))
}
event.Payload = payload
event.OccurredAt = event.OccurredAt.UTC().Truncate(time.Microsecond)
return event, nil
}
// HideNotificationFixtureOrganization records that a synthetic candidate
// hides an organization, the visibility change that removes the content of a
// synthetic notice. The browser suite cannot use the public operation for an
// organization the candidate has not seen yet.
func HideNotificationFixtureOrganization(ctx context.Context, pool *pgxpool.Pool, recipientID, organizationID uuid.UUID) error {
const location = "postgres.HideNotificationFixtureOrganization"
if err := CheckNotificationFixtureScope(ctx, pool, recipientID, organizationID); err != nil {
return err
}
if _, err := pool.Exec(ctx, `
INSERT INTO candidate_hidden_organizations (user_id, organization_id, hidden_at)
VALUES ($1, $2, now())
ON CONFLICT DO NOTHING`, recipientID, organizationID); err != nil {
return fmt.Errorf("%s - hide organization: %w", location, sanitizedDatabaseError(err))
}
return nil
}