package postgres import ( "context" "errors" "fmt" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5" "lincodex/backend/internal/adapter/secondary/postgres/sqlc" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) // CountPendingNeedInvitations counts the waiting invitations of one need. The // count is only exact under the organization lock, which the caller holds. func (repository invitationRepository) CountPendingNeedInvitations( ctx context.Context, organizationID, needID uuid.UUID, ) (int64, error) { const location = "invitationRepository.CountPendingNeedInvitations" if organizationID == uuid.Nil || needID == uuid.Nil { return 0, fmt.Errorf("%s - organization and need IDs must be set: %w", location, domain.ErrInvalidInvitation) } count, err := repository.queries.CountPendingNeedInvitations(ctx, sqlc.CountPendingNeedInvitationsParams{ OrganizationID: pgUUID(organizationID), NeedID: pgUUID(needID), }) if err != nil { return 0, fmt.Errorf("%s - count pending invitations: %w", location, mapInvitationError(err)) } return count, nil } // LatestDeclineTime reads the latest decline of the pair of the organization // and the candidate. It reports false when the pair has none. func (repository invitationRepository) LatestDeclineTime( ctx context.Context, organizationID, candidateID uuid.UUID, ) (time.Time, bool, error) { const location = "invitationRepository.LatestDeclineTime" if organizationID == uuid.Nil || candidateID == uuid.Nil { return time.Time{}, false, fmt.Errorf("%s - organization and candidate IDs must be set: %w", location, domain.ErrInvalidInvitation) } declinedAt, err := repository.queries.LatestInvitationDecline(ctx, sqlc.LatestInvitationDeclineParams{ OrganizationID: pgUUID(organizationID), CandidateID: pgUUID(candidateID), }) if errors.Is(err, pgx.ErrNoRows) { return time.Time{}, false, nil } if err != nil { return time.Time{}, false, fmt.Errorf("%s - read latest decline: %w", location, mapInvitationError(err)) } if !declinedAt.Valid { return time.Time{}, false, fmt.Errorf("%s - declined invitation has no answer time: %w", location, domain.ErrInvalidInvitation) } return declinedAt.Time.UTC(), true, nil } // GetForDeadline locks the invitation row first and reads it with its scope // afterwards, as the other locking reads of invitations do. func (repository invitationRepository) GetForDeadline( ctx context.Context, invitationID uuid.UUID, ) (domain.Invitation, error) { const location = "invitationRepository.GetForDeadline" if invitationID == uuid.Nil { return domain.Invitation{}, fmt.Errorf("%s - invitation ID must be set: %w", location, domain.ErrInvalidInvitation) } organizationID, err := repository.queries.LockInvitationByID(ctx, pgUUID(invitationID)) if err != nil { return domain.Invitation{}, fmt.Errorf("%s - lock invitation: %w", location, mapInvitationNoRows(err)) } if !organizationID.Valid { return domain.Invitation{}, fmt.Errorf("%s - invitation has no organization: %w", location, domain.ErrInvalidInvitation) } invitation, err := repository.Get(ctx, uuid.UUID(organizationID.Bytes), invitationID) if err != nil { return domain.Invitation{}, fmt.Errorf("%s - read locked invitation: %w", location, err) } return invitation, nil } // Expire stores the expired version. The conditional update is the // compare-and-swap that makes the expiry idempotent: a row that another // transaction already answered or expired, or whose deadline has not passed at // the expiry time, matches nothing and the caller learns of the conflict. func (repository invitationRepository) Expire( ctx context.Context, expectedVersion int64, next domain.Invitation, event domain.InvitationEvent, ) error { const location = "invitationRepository.Expire" stored := next.Stored() if expectedVersion < 1 || stored.Version != expectedVersion+1 || stored.State != string(domain.InvitationExpired) || stored.ExpiryReason == nil || event.Kind != domain.InvitationEventExpired { return fmt.Errorf("%s - the next version must be the expiry 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.ExpireInvitation(ctx, sqlc.ExpireInvitationParams{ NextVersion: stored.Version, ExpiredAt: pgTime(stored.UpdatedAt), OrganizationID: pgUUID(stored.OrganizationID), InvitationID: pgUUID(stored.ID), ExpectedVersion: expectedVersion, }) if errors.Is(err, pgx.ErrNoRows) { return fmt.Errorf("%s - expire invitation: %w", location, outbound.ErrInvitationVersionConflict) } if err != nil { return fmt.Errorf("%s - expire invitation: %w", location, mapInvitationError(err)) } return repository.insertEvent(ctx, location, event) }