feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings

This commit is contained in:
shira committed 2026-10-09 18:03:35 +03:00
1 parent a5990d3fc6
commit 676b3c6e24
59 files changed
+4926 -194

No files matched your search

+55 -4
View File
@@ -27,6 +27,9 @@ const (
// candidate's answer for the responsiveness metrics, never a consent or
// a hire, and it holds neither the reason nor the comment.
InvitationEventDeclined InvitationEventKind = "declined"
// InvitationEventAccepted is the audit event of an acceptance. It names no
// field: the consent C4 and the grant are stored in their own records.
InvitationEventAccepted InvitationEventKind = "accepted"
)
const (
@@ -46,6 +49,7 @@ var (
// answerEventKinds name the audit event of each answer state.
answerEventKinds = map[InvitationState]InvitationEventKind{
InvitationViewed: InvitationEventViewed, InvitationDeclined: InvitationEventDeclined,
InvitationAccepted: InvitationEventAccepted,
}
)
@@ -234,8 +238,44 @@ func (invitation Invitation) Decline(choice InvitationDeclineChoice, at time.Tim
return declined, decline, nil
}
// AnswerEvent builds the audit event of the version that a view or a
// decline by actor has just produced. It names no field and holds neither
// Accept returns the next version in the state accepted. The candidate may
// accept a delivered or viewed invitation before its deadline and only at the
// terms version that the candidate read; any other state, a late command, and
// a stale terms version are refused. The deadline and the terms are kept. The
// consent C4, the contact grant, and the audit event belong to the same
// transaction and are built by the caller.
func (invitation Invitation) Accept(termsVersion int64, at time.Time) (Invitation, error) {
const location = "Invitation.Accept"
if !validInvitationTime(at) || at.Before(invitation.record.UpdatedAt) {
return Invitation{}, fmt.Errorf("%s - accept time must be set and not before the last change: %w", location, ErrInvalidInvitation)
}
if !invitation.acceptable() || !invitation.IsDelivered() || invitation.ResponseDeadlinePassed(at) {
return Invitation{}, fmt.Errorf("%s - %w", location, ErrInvitationNotAnswerable)
}
if termsVersion != invitation.record.TermsVersion {
return Invitation{}, fmt.Errorf("%s - the candidate read another terms version: %w", location, ErrInvitationVersionConflict)
}
at = at.UTC()
next := invitation.Stored()
next.State = string(InvitationAccepted)
next.Version++
next.UpdatedAt = at
next.RespondedAt = &at
accepted, err := RestoreInvitation(next)
if err != nil {
return Invitation{}, fmt.Errorf("%s - restore accepted invitation: %w", location, err)
}
return accepted, nil
}
// acceptable reports the states from which the candidate may accept: the
// delivered and viewed invitation. The planned counter offer is not one.
func (invitation Invitation) acceptable() bool {
return invitation.State() == InvitationDelivered || invitation.State() == InvitationViewed
}
// AnswerEvent builds the audit event of the version that a view, a decline,
// or an acceptance by actor has just produced. It names no field and holds neither
// the reason nor the comment of a decline.
func (invitation Invitation) AnswerEvent(actorID uuid.UUID) (InvitationEvent, error) {
const location = "Invitation.AnswerEvent"
@@ -257,6 +297,12 @@ func (invitation Invitation) AnswerEvent(actorID uuid.UUID) (InvitationEvent, er
}, nil
}
// answeredInvitationState reports the states that record the candidate's answer:
// a declined or an accepted invitation has its answer time.
func answeredInvitationState(state InvitationState) bool {
return state == InvitationDeclined || state == InvitationAccepted
}
// storedInvitationAnswerDefect checks the view and the answer against the
// state: a delivered invitation has neither, a viewed one has its view, a
// declined one has its answer, and neither precedes the delivery.
@@ -267,8 +313,8 @@ func storedInvitationAnswerDefect(stored StoredInvitation) string {
return "a delivered invitation has no view or answer"
case state == InvitationViewed && (stored.ViewedAt == nil || stored.RespondedAt != nil):
return "a viewed invitation has a view and no answer"
case state == InvitationDeclined && stored.RespondedAt == nil:
return "a declined invitation has an answer"
case answeredInvitationState(state) && stored.RespondedAt == nil:
return "a declined or accepted invitation has an answer"
case stored.ViewedAt != nil && (stored.DeliveredAt == nil || stored.ViewedAt.Before(*stored.DeliveredAt)):
return "a view follows the delivery"
case stored.RespondedAt != nil && (stored.DeliveredAt == nil || stored.RespondedAt.Before(*stored.DeliveredAt)):
@@ -305,6 +351,11 @@ type CandidateInvitationView struct {
RespondedAt *time.Time
UpdatedAt time.Time
Decline *InvitationDeclineView
// ContactConsent is set while an acceptance is possible, and ContactGrant
// once the invitation was accepted. The usecase fills both: they need the
// profile, the consent texts, and the visibility, which the view does not.
ContactConsent *ContactConsentOffer
ContactGrant *ContactGrantView
}
// CandidateView returns the invitation for the candidate it is addressed to.