feat: persist candidate questionnaires and enforce company quotas

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 15:47:10 +03:00
1 parent 73c7caa225
commit 3a61a8e8bb
51 files changed
+6280 -38

No files matched your search

@@ -47,6 +47,10 @@ var (
// ErrAssessmentCategoryNotOpen reports that the category has no opening.
ErrAssessmentCategoryNotOpen = errors.New("assessment category is not open")
// ErrAssessmentCatalogBindingImmutable reports an attempt to edit or remove
// an immutable reviewed catalog binding.
ErrAssessmentCatalogBindingImmutable = errors.New("assessment catalog binding is immutable")
)
// MaxAssessmentFamilyPageSize is the largest page that ListFamilies returns.
@@ -126,6 +130,15 @@ type AssessmentBankRepository interface {
// the draft's own numbers are not stored.
CreateCategoryOpening(ctx context.Context, draft domain.AssessmentCategoryOpeningDraft) (domain.AssessmentCategoryOpening, error)
// BindCategoryCatalog attaches reviewed public metadata to an opening. The
// adapter pins it to the exact currently published family manifest.
BindCategoryCatalog(ctx context.Context, opening domain.AssessmentCategoryOpening, metadata domain.AssessmentCategoryCatalogMetadata) error
// ListCurrentSpecializationAvailability returns ready public specializations
// from one database snapshot. Legacy openings or bindings whose policy,
// coverage, or published-family manifest changed are omitted.
ListCurrentSpecializationAvailability(ctx context.Context) ([]domain.SpecializationAvailability, error)
// DatabaseTime returns the database clock. It stamps the effective time
// of a policy version, so the version in force now is judged against it
// and not against the application clock.
@@ -0,0 +1,61 @@
package outbound
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var ErrCompanyQuotaSnapshotChanged = errors.New("company quota snapshot changed")
// CompanyQuotaDecision is the result of an atomic invitation or card-view
// reservation. Reused means the same invitation or a recent view already
// counted, so it is allowed without another usage charge. A denied decision
// has Reused false and RetryAt set to the earliest expiry that can free room,
// in UTC.
type CompanyQuotaDecision struct {
Allowed bool
Reused bool
RetryAt time.Time
}
// CompanyQuotaRepository atomically reserves per-organization invitation and
// candidate-card usage. Implementations are transaction-bound: a caller
// creates the invitation or applies the visibility change in the same
// transaction, so a failed business operation cannot leave quota charged.
//
// Writers lock rows in this order: organization, active membership,
// invitation or visibility record, then quota usage. A repository may take an
// organization lock again inside the same transaction, but must not invert
// this order. Each method rechecks the organization's stored verification
// level, basis, and changed_at against the snapshot in CompanyQuota before it
// writes; a caller cannot grant itself a tier by supplying a different level.
//
// Invitation usage is keyed by organization and invitation ID and remains
// until the organization is deleted. The unique key makes retries idempotent
// even when they arrive with a different request idempotency key; pruning by
// the rolling quota window would allow an old invitation to count again.
// Card-view usage is keyed by organization and candidate, counts only when
// last_seen is strictly after the rolling-window cutoff, and expires
// logically at that boundary. Bounded cleanup is opportunistic after a
// successful reservation, so physical row removal has no exact-time promise.
// Repeated views update last_seen monotonically with the greater of the
// stored and current times.
type CompanyQuotaRepository interface {
// ReserveInvitation counts a new invitation against the current policy
// limit. An existing invitation ID is returned as allowed and reused
// without a second charge, regardless of its age or the request key. A
// denied new invitation writes no usage and reports RetryAt after the
// earliest currently counted invitation expires.
ReserveInvitation(ctx context.Context, quota domain.CompanyQuota, invitationID uuid.UUID) (CompanyQuotaDecision, error)
// ReserveCardView counts a distinct candidate within the rolling card-view
// window. A repeat while the prior view is active is allowed and reused,
// and refreshes last_seen with a monotonic update. A denied new view writes
// no usage and reports RetryAt after the earliest active view expires.
ReserveCardView(ctx context.Context, quota domain.CompanyQuota, candidateID uuid.UUID) (CompanyQuotaDecision, error)
}
@@ -0,0 +1,34 @@
package outbound
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var ErrCandidateQuestionnaireVersionConflict = errors.New("candidate questionnaire changed concurrently")
// CandidateQuestionnaireRepositories exposes the questionnaire repository
// bound to a UnitOfWork transaction.
type CandidateQuestionnaireRepositories interface {
CandidateQuestionnaires() CandidateQuestionnaireRepository
}
// CandidateQuestionnaireRepository reads and replaces only the questionnaire
// owned by userID. A missing row is the version-0 empty questionnaire.
type CandidateQuestionnaireRepository interface {
Get(ctx context.Context, userID uuid.UUID) (domain.CandidateQuestionnaire, error)
// Save atomically advances the expected version and replaces the answer
// rows. A concurrent identical winner is returned as an idempotent success;
// a different winner returns ErrCandidateQuestionnaireVersionConflict.
Save(ctx context.Context, expectedVersion int64, questionnaire domain.CandidateQuestionnaire) (domain.CandidateQuestionnaire, error)
// AdvanceOnboarding advances only candidate_questionnaire to
// candidate_assessment; later onboarding steps are left unchanged.
AdvanceOnboarding(ctx context.Context, userID uuid.UUID, at time.Time) error
}
@@ -51,9 +51,11 @@ type UnitOfWork interface {
IdentityRepositories
ConsentRepositories
CandidateProfileRepositories
CandidateQuestionnaireRepositories
AccountDeletionRequests() AccountDeletionRequests
ConsentRevocationEffects() ConsentRevocationEffects
OrganizationRepositories
CompanyQuotas() CompanyQuotaRepository
AssessmentBankRepositories
Policies() PolicyRepository
Deliveries() DeliveryRepository