245 lines
11 KiB
Go
245 lines
11 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
var _ outbound.InvitationDeclineResultRepository = invitationRepository{}
|
|
|
|
func (unit *UnitOfWork) InvitationDeclineResults() outbound.InvitationDeclineResultRepository {
|
|
return invitationRepository{queries: unit.queries}
|
|
}
|
|
|
|
func (repository invitationRepository) GetCandidateEntry(
|
|
ctx context.Context,
|
|
candidateID, invitationID uuid.UUID,
|
|
) (outbound.CandidateInvitationEntry, error) {
|
|
const location = "invitationRepository.GetCandidateEntry"
|
|
if candidateID == uuid.Nil || invitationID == uuid.Nil {
|
|
return outbound.CandidateInvitationEntry{}, fmt.Errorf("%s - candidate and invitation IDs must be set: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
row, err := repository.queries.ReadCandidateInvitationEntry(ctx, sqlc.ReadCandidateInvitationEntryParams{
|
|
CandidateID: pgUUID(candidateID), InvitationID: pgUUID(invitationID),
|
|
})
|
|
if err != nil {
|
|
return outbound.CandidateInvitationEntry{}, fmt.Errorf("%s - read invitation: %w", location, mapInvitationNoRows(err))
|
|
}
|
|
return restoreCandidateInvitationRow(location, sqlc.ListCandidateInvitationsRow(row), candidateID)
|
|
}
|
|
|
|
func (repository invitationRepository) GetCandidateEntryForUpdate(
|
|
ctx context.Context,
|
|
candidateID, invitationID uuid.UUID,
|
|
) (outbound.CandidateInvitationEntry, error) {
|
|
const location = "invitationRepository.GetCandidateEntryForUpdate"
|
|
if candidateID == uuid.Nil || invitationID == uuid.Nil {
|
|
return outbound.CandidateInvitationEntry{}, fmt.Errorf("%s - candidate and invitation IDs must be set: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
// The row is locked by a statement of its own and read afterwards, as
|
|
// for the organization side: a locking read joined to the terms would,
|
|
// after waiting for a concurrent commit, pair the new terms version with
|
|
// the terms row it fetched before the wait.
|
|
if _, err := repository.queries.LockCandidateInvitation(ctx, sqlc.LockCandidateInvitationParams{
|
|
CandidateID: pgUUID(candidateID), InvitationID: pgUUID(invitationID),
|
|
}); err != nil {
|
|
return outbound.CandidateInvitationEntry{}, fmt.Errorf("%s - lock invitation: %w", location, mapInvitationNoRows(err))
|
|
}
|
|
entry, err := repository.GetCandidateEntry(ctx, candidateID, invitationID)
|
|
if err != nil {
|
|
return outbound.CandidateInvitationEntry{}, fmt.Errorf("%s - read locked invitation: %w", location, err)
|
|
}
|
|
return entry, nil
|
|
}
|
|
|
|
func (repository invitationRepository) RecordView(
|
|
ctx context.Context,
|
|
expectedVersion int64,
|
|
next domain.Invitation,
|
|
event domain.InvitationEvent,
|
|
) error {
|
|
const location = "invitationRepository.RecordView"
|
|
stored := next.Stored()
|
|
if expectedVersion < 1 || stored.Version != expectedVersion+1 || stored.State != string(domain.InvitationViewed) ||
|
|
stored.ViewedAt == nil || event.Kind != domain.InvitationEventViewed {
|
|
return fmt.Errorf("%s - the next version must be the view that follows the expected one: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
if err := repository.insertEventPrerequisites(location, next, event, stored.Version); err != nil {
|
|
return err
|
|
}
|
|
_, err := repository.queries.RecordInvitationView(ctx, sqlc.RecordInvitationViewParams{
|
|
NextVersion: stored.Version, ViewedAt: pgTime(*stored.ViewedAt), OrganizationID: pgUUID(stored.OrganizationID),
|
|
InvitationID: pgUUID(stored.ID), ExpectedVersion: expectedVersion,
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return fmt.Errorf("%s - record view: %w", location, outbound.ErrInvitationVersionConflict)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("%s - record view: %w", location, mapInvitationError(err))
|
|
}
|
|
return repository.insertEvent(ctx, location, event)
|
|
}
|
|
|
|
func (repository invitationRepository) Decline(
|
|
ctx context.Context,
|
|
expectedVersion int64,
|
|
next domain.Invitation,
|
|
decline domain.InvitationDecline,
|
|
event domain.InvitationEvent,
|
|
) error {
|
|
const location = "invitationRepository.Decline"
|
|
stored := next.Stored()
|
|
if expectedVersion < 1 || stored.Version != expectedVersion+1 || stored.State != string(domain.InvitationDeclined) ||
|
|
stored.RespondedAt == nil || event.Kind != domain.InvitationEventDeclined {
|
|
return fmt.Errorf("%s - the next version must be the decline that follows the expected one: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
if err := decline.Validate(); err != nil {
|
|
return fmt.Errorf("%s - validate decline: %w", location, err)
|
|
}
|
|
if decline.InvitationID != stored.ID || decline.OrganizationID != stored.OrganizationID || !decline.DeclinedAt.Equal(*stored.RespondedAt) {
|
|
return fmt.Errorf("%s - the decline must describe the declined version: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
if err := repository.insertEventPrerequisites(location, next, event, stored.Version); err != nil {
|
|
return err
|
|
}
|
|
_, err := repository.queries.DeclineInvitation(ctx, sqlc.DeclineInvitationParams{
|
|
NextVersion: stored.Version, DeclinedAt: pgTime(*stored.RespondedAt), OrganizationID: pgUUID(stored.OrganizationID),
|
|
InvitationID: pgUUID(stored.ID), ExpectedVersion: expectedVersion,
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return fmt.Errorf("%s - decline invitation: %w", location, outbound.ErrInvitationVersionConflict)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("%s - decline invitation: %w", location, mapInvitationError(err))
|
|
}
|
|
comment := pgtype.Text{}
|
|
if decline.Choice.Comment != nil {
|
|
comment = pgtype.Text{String: *decline.Choice.Comment, Valid: true}
|
|
}
|
|
if err := repository.queries.InsertInvitationDecline(ctx, sqlc.InsertInvitationDeclineParams{
|
|
InvitationID: pgUUID(decline.InvitationID), OrganizationID: pgUUID(decline.OrganizationID),
|
|
Reason: string(decline.Choice.Reason), Comment: comment, DeclinedAt: pgTime(decline.DeclinedAt),
|
|
}); err != nil {
|
|
return fmt.Errorf("%s - store decline: %w", location, mapInvitationError(err))
|
|
}
|
|
return repository.insertEvent(ctx, location, event)
|
|
}
|
|
|
|
func (repository invitationRepository) GetDecline(
|
|
ctx context.Context,
|
|
organizationID, invitationID uuid.UUID,
|
|
) (domain.InvitationDecline, error) {
|
|
const location = "invitationRepository.GetDecline"
|
|
if organizationID == uuid.Nil || invitationID == uuid.Nil {
|
|
return domain.InvitationDecline{}, fmt.Errorf("%s - organization and invitation IDs must be set: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
row, err := repository.queries.ReadInvitationDecline(ctx, sqlc.ReadInvitationDeclineParams{
|
|
OrganizationID: pgUUID(organizationID), InvitationID: pgUUID(invitationID),
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.InvitationDecline{}, fmt.Errorf("%s - read decline: %w", location, outbound.ErrInvitationDeclineNotFound)
|
|
}
|
|
if err != nil {
|
|
return domain.InvitationDecline{}, fmt.Errorf("%s - read decline: %w", location, mapInvitationError(err))
|
|
}
|
|
if !row.InvitationID.Valid || !row.OrganizationID.Valid || !row.DeclinedAt.Valid {
|
|
return domain.InvitationDecline{}, fmt.Errorf("%s - stored decline has no scope: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
decline := domain.InvitationDecline{
|
|
InvitationID: uuid.UUID(row.InvitationID.Bytes), OrganizationID: uuid.UUID(row.OrganizationID.Bytes),
|
|
Choice: domain.InvitationDeclineChoice{Reason: domain.InvitationDeclineReason(row.Reason)},
|
|
DeclinedAt: row.DeclinedAt.Time.UTC(),
|
|
}
|
|
if row.Comment.Valid {
|
|
comment := row.Comment.String
|
|
decline.Choice.Comment = &comment
|
|
}
|
|
if decline.InvitationID != invitationID || decline.OrganizationID != organizationID {
|
|
return domain.InvitationDecline{}, fmt.Errorf("%s - stored decline has another scope: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
if err := decline.Validate(); err != nil {
|
|
return domain.InvitationDecline{}, fmt.Errorf("%s - validate stored decline: %w", location, err)
|
|
}
|
|
return decline, nil
|
|
}
|
|
|
|
func (repository invitationRepository) SaveDeclineResult(
|
|
ctx context.Context,
|
|
scope [outbound.IdempotencyDigestSize]byte,
|
|
result outbound.InvitationDeclineResult,
|
|
) error {
|
|
const location = "invitationRepository.SaveDeclineResult"
|
|
if scope == ([outbound.IdempotencyDigestSize]byte{}) {
|
|
return fmt.Errorf("%s - idempotency scope must be set: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
if err := validateInvitationDeclineResult(result); err != nil {
|
|
return fmt.Errorf("%s - validate outcome: %w", location, err)
|
|
}
|
|
if err := repository.queries.InsertInvitationDeclineResult(ctx, sqlc.InsertInvitationDeclineResultParams{
|
|
ScopeHash: scope[:], InvitationID: pgUUID(result.InvitationID), Outcome: string(result.Outcome),
|
|
}); err != nil {
|
|
return fmt.Errorf("%s - store outcome: %w", location, mapInvitationDeclineResultError(err))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (repository invitationRepository) GetDeclineResult(
|
|
ctx context.Context,
|
|
scope [outbound.IdempotencyDigestSize]byte,
|
|
) (outbound.InvitationDeclineResult, error) {
|
|
const location = "invitationRepository.GetDeclineResult"
|
|
if scope == ([outbound.IdempotencyDigestSize]byte{}) {
|
|
return outbound.InvitationDeclineResult{}, fmt.Errorf("%s - idempotency scope must be set: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
row, err := repository.queries.GetInvitationDeclineResult(ctx, scope[:])
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return outbound.InvitationDeclineResult{}, outbound.ErrInvitationDeclineResultNotFound
|
|
}
|
|
if err != nil {
|
|
return outbound.InvitationDeclineResult{}, fmt.Errorf("%s - read outcome: %w", location, mapInvitationError(err))
|
|
}
|
|
if !row.InvitationID.Valid {
|
|
return outbound.InvitationDeclineResult{}, fmt.Errorf("%s - stored outcome has no invitation: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
result := outbound.InvitationDeclineResult{
|
|
InvitationID: uuid.UUID(row.InvitationID.Bytes), Outcome: outbound.InvitationDeclineOutcome(row.Outcome),
|
|
}
|
|
if err := validateInvitationDeclineResult(result); err != nil {
|
|
return outbound.InvitationDeclineResult{}, fmt.Errorf("%s - validate stored outcome: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func validateInvitationDeclineResult(result outbound.InvitationDeclineResult) error {
|
|
const location = "postgres.validateInvitationDeclineResult"
|
|
switch result.Outcome {
|
|
case outbound.InvitationDeclineDeclined:
|
|
if result.InvitationID == uuid.Nil {
|
|
return fmt.Errorf("%s - a declined outcome names the invitation: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
case outbound.InvitationDeclineNotFound, outbound.InvitationDeclineStateConflict:
|
|
// A refusal names the identifier of the path, which may name nothing.
|
|
default:
|
|
return fmt.Errorf("%s - outcome is unknown: %w", location, domain.ErrInvalidInvitation)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func mapInvitationDeclineResultError(err error) error {
|
|
var pgError *pgconn.PgError
|
|
if errors.As(err, &pgError) && pgError.Code == foreignKeyViolationCode && pgError.ConstraintName == "invitation_decline_results_idempotency_fkey" {
|
|
return fmt.Errorf("postgres.mapInvitationDeclineResultError - idempotency record no longer exists: %w", outbound.ErrIdempotencyRecordNotFound)
|
|
}
|
|
return mapInvitationError(err)
|
|
}
|