feat(consent): add versioned append-only consent ledger C1-C5

This commit is contained in:
shira committed 2026-10-06 14:52:33 +03:00
1 parent 008254e44a
commit de49f93b0b
64 files changed
+9244 -132

No files matched your search

@@ -22,6 +22,11 @@ import (
const (
foreignKeyViolationCode = "23503"
// restrictViolationCode is raised by an ON DELETE RESTRICT foreign key.
restrictViolationCode = "23001"
// consentSubjectForeignKey restricts the delete of an account with
// consent records.
consentSubjectForeignKey = "consent_records_subject_id_fkey"
// sessionTouchInterval is the minimum age of last_seen_at before Touch
// writes it again. It bounds session writes to one per minute per session
@@ -216,7 +221,9 @@ func (repository userRepository) UpdateRegistration(ctx context.Context, user do
if err != nil {
return domain.User{}, fmt.Errorf("userRepository.UpdateRegistration - update user: %w", mapNoRows(err, outbound.ErrUserVersionConflict))
}
return userFromRow(row)
// The statement returns the users columns through a CTE, so sqlc gives
// it its own row type with the same fields.
return userFromRow(sqlc.User(row))
}
func (repository userRepository) MarkEmailVerified(
@@ -251,11 +258,26 @@ func (repository userRepository) DeleteUnverifiedRegisteredBefore(ctx context.Co
BatchLimit: limit,
})
if err != nil {
return 0, fmt.Errorf("userRepository.DeleteUnverifiedRegisteredBefore - delete users: %w", mapIdentityError(err))
return 0, fmt.Errorf("userRepository.DeleteUnverifiedRegisteredBefore - delete users: %w", mapCleanupError(err))
}
return deleted, nil
}
// mapCleanupError reports the restricting consent foreign key as a
// transaction conflict. The cleanup checks consent rows in its statement
// snapshot, so a consent committed for a candidate account after the
// snapshot but before the account lock passes that check and then fails
// the foreign key at the end of the statement. Nothing was deleted, and the
// next run sees the consent and skips or removes the account.
func mapCleanupError(err error) error {
var pgError *pgconn.PgError
if errors.As(err, &pgError) && pgError.Code == restrictViolationCode && pgError.ConstraintName == consentSubjectForeignKey {
return fmt.Errorf("postgres.mapCleanupError - consent recorded concurrently: %w: %w",
outbound.ErrTransactionConflict, &databaseError{code: pgError.Code, constraint: pgError.ConstraintName})
}
return mapIdentityError(err)
}
func userFromRow(row sqlc.User) (domain.User, error) {
email, err := domain.RestoreEmailAddress(row.Email, row.EmailNormalized)
if err != nil {