feat(platform): add the notification inbox and deterministic match search
This commit is contained in:
1 parent
5edff21d2d
commit
0b11021b16
169 files changed
+17083
-443
No files matched your search
@@ -18,9 +18,13 @@ import (
|
||||
|
||||
var _ outbound.NotificationInboxRepository = notificationInboxRepository{}
|
||||
|
||||
// notificationInboxRecipientConstraint is the foreign key of the recipient in
|
||||
// migration 00032; the organization key has its own name.
|
||||
const notificationInboxRecipientConstraint = "notification_inbox_events_recipient_fkey"
|
||||
|
||||
// NewNotificationInboxRepository creates a direct store for inbox operations.
|
||||
// Transaction-bound callers can construct the same repository around the
|
||||
// queries of their current UnitOfWork.
|
||||
// Transaction-bound callers use UnitOfWork.NotificationInbox, which builds
|
||||
// the same repository around the queries of the current transaction.
|
||||
func NewNotificationInboxRepository(pool *pgxpool.Pool) outbound.NotificationInboxRepository {
|
||||
return notificationInboxRepository{queries: sqlc.New(pool)}
|
||||
}
|
||||
@@ -29,6 +33,11 @@ type notificationInboxRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
// NotificationInbox binds the inbox repository to this transaction.
|
||||
func (unit *UnitOfWork) NotificationInbox() outbound.NotificationInboxRepository {
|
||||
return notificationInboxRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
func (repository notificationInboxRepository) Store(
|
||||
ctx context.Context,
|
||||
entry domain.NotificationInboxEntry,
|
||||
@@ -52,7 +61,18 @@ func (repository notificationInboxRepository) Store(
|
||||
OccurredAt: pgTime(entry.OccurredAt()),
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s - insert recipient event: %w", location, sanitizedDatabaseError(err))
|
||||
sanitized := sanitizedDatabaseError(err)
|
||||
var failure *databaseError
|
||||
if errors.As(sanitized, &failure) && failure.code == foreignKeyViolationCode {
|
||||
// The statement failed on the recipient or the organization row, so
|
||||
// the surrounding transaction is aborted.
|
||||
reference := outbound.ErrNotificationInboxReferenceMissing
|
||||
if failure.constraint == notificationInboxRecipientConstraint {
|
||||
reference = outbound.ErrNotificationInboxRecipientMissing
|
||||
}
|
||||
return false, fmt.Errorf("%s - insert recipient event: %w: %w", location, reference, sanitized)
|
||||
}
|
||||
return false, fmt.Errorf("%s - insert recipient event: %w", location, sanitized)
|
||||
}
|
||||
if rows == 1 {
|
||||
return true, nil
|
||||
@@ -116,16 +136,16 @@ func (repository notificationInboxRepository) List(
|
||||
func (repository notificationInboxRepository) MarkRead(
|
||||
ctx context.Context,
|
||||
recipientUserID uuid.UUID,
|
||||
organizationID uuid.UUID,
|
||||
organizationID *uuid.UUID,
|
||||
eventID uuid.UUID,
|
||||
at time.Time,
|
||||
) (domain.NotificationInboxEntry, error) {
|
||||
const location = "notificationInboxRepository.MarkRead"
|
||||
if recipientUserID == uuid.Nil || organizationID == uuid.Nil || eventID == uuid.Nil || at.IsZero() {
|
||||
return domain.NotificationInboxEntry{}, fmt.Errorf("%s - recipient, organization, event, and read time are required: %w", location, domain.ErrInvalidNotificationInboxEntry)
|
||||
if recipientUserID == uuid.Nil || (organizationID != nil && *organizationID == uuid.Nil) || eventID == uuid.Nil || at.IsZero() {
|
||||
return domain.NotificationInboxEntry{}, fmt.Errorf("%s - recipient, event, and read time are required: %w", location, domain.ErrInvalidNotificationInboxEntry)
|
||||
}
|
||||
row, err := repository.queries.MarkNotificationInboxEventRead(ctx, sqlc.MarkNotificationInboxEventReadParams{
|
||||
RecipientUserID: pgUUID(recipientUserID), OrganizationID: pgUUID(organizationID),
|
||||
RecipientUserID: pgUUID(recipientUserID), OrganizationID: notificationInboxOptionalUUID(organizationID),
|
||||
EventID: pgUUID(eventID), ReadAt: pgTime(at),
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
@@ -141,6 +161,26 @@ func (repository notificationInboxRepository) MarkRead(
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (repository notificationInboxRepository) OrganizationNameForRecipient(
|
||||
ctx context.Context,
|
||||
recipientUserID, organizationID uuid.UUID,
|
||||
) (string, error) {
|
||||
const location = "notificationInboxRepository.OrganizationNameForRecipient"
|
||||
if recipientUserID == uuid.Nil || organizationID == uuid.Nil {
|
||||
return "", fmt.Errorf("%s - recipient and organization are required: %w", location, domain.ErrInvalidNotificationInboxEntry)
|
||||
}
|
||||
name, err := repository.queries.GetNotificationOrganizationNameForRecipient(ctx, sqlc.GetNotificationOrganizationNameForRecipientParams{
|
||||
RecipientUserID: pgUUID(recipientUserID), OrganizationID: pgUUID(organizationID),
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", fmt.Errorf("%s - read organization: %w", location, outbound.ErrNotificationInboxSourceNotVisible)
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s - read organization: %w", location, sanitizedDatabaseError(err))
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func validateInboxEntryForStore(entry domain.NotificationInboxEntry) error {
|
||||
_, err := domain.RestoreNotificationInboxEntry(domain.NotificationInboxEntryInput{
|
||||
EventID: entry.EventID(), RecipientUserID: entry.RecipientUserID(), OrganizationID: entry.OrganizationID(),
|
||||
|
||||
Reference in new issue
Block a user