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

+107 -9
View File
@@ -2,6 +2,7 @@ package invitation
import (
"context"
"errors"
"fmt"
"time"
@@ -25,8 +26,12 @@ func (service *Service) GetMine(ctx context.Context, actorID, invitationID uuid.
if actorID == uuid.Nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - actor ID must be set: %w", getMineLocation, ErrForbidden)
}
now, err := service.now(getMineLocation)
if err != nil {
return domain.CandidateInvitationView{}, err
}
var view domain.CandidateInvitationView
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := authorizeCandidate(ctx, unit, actorID); err != nil {
return err
}
@@ -34,7 +39,7 @@ func (service *Service) GetMine(ctx context.Context, actorID, invitationID uuid.
if err != nil {
return err
}
view, err = candidateView(ctx, unit, entry)
view, err = service.candidateView(ctx, unit, entry, now)
return err
})
if err != nil {
@@ -85,7 +90,7 @@ func (service *Service) ViewMine(ctx context.Context, actorID, invitationID uuid
}
entry.Invitation = viewed
}
view, err = candidateView(ctx, unit, entry)
view, err = service.candidateView(ctx, unit, entry, now)
return err
})
if err != nil {
@@ -123,25 +128,118 @@ func readCandidateEntry(
// organization from seeing the candidate, while the candidate needs to know
// who sent the offer to answer it. Pushed content, such as the inbox
// notification, follows the hiding instead.
func candidateView(ctx context.Context, unit outbound.UnitOfWork, entry outbound.CandidateInvitationEntry) (domain.CandidateInvitationView, error) {
const location = "invitation.candidateView"
//
// The consent offer is set while an acceptance is possible, and the contact
// grant once the invitation is accepted. The grant is readable only while the
// organization may read the profile, so a hidden profile shows the grant as
// blocked by visibility. at is the time of the command that builds the view.
func (service *Service) candidateView(
ctx context.Context,
unit outbound.UnitOfWork,
entry outbound.CandidateInvitationEntry,
at time.Time,
) (domain.CandidateInvitationView, error) {
const location = "invitation.Service.candidateView"
current := entry.Invitation
var decline *domain.InvitationDecline
if entry.Invitation.State() == domain.InvitationDeclined {
stored, err := unit.Invitations().GetDecline(ctx, entry.Invitation.OrganizationID(), entry.Invitation.ID())
if current.State() == domain.InvitationDeclined {
stored, err := unit.Invitations().GetDecline(ctx, current.OrganizationID(), current.ID())
if err != nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - read decline: %w", location, err)
}
decline = &stored
}
view, err := entry.Invitation.CandidateView(domain.InvitationOrganizationRef{
ID: entry.Invitation.OrganizationID(), Name: entry.OrganizationName,
view, err := current.CandidateView(domain.InvitationOrganizationRef{
ID: current.OrganizationID(), Name: entry.OrganizationName,
}, decline)
if err != nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - build view: %w", location, err)
}
switch {
case current.State() == domain.InvitationAccepted:
grant, err := unit.ContactGrants().GetByInvitation(ctx, current.OrganizationID(), current.ID())
if err != nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - read contact grant: %w", location, err)
}
visible, err := candidateVisibleTo(ctx, unit, current.CandidateID(), current.OrganizationID())
if err != nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - read visibility: %w", location, err)
}
grantView := grant.CandidateView(visible)
view.ContactGrant = &grantView
case acceptancePossible(current, at):
offer, offered, err := contactConsentOffer(ctx, unit, current, entry.OrganizationName, at)
if err != nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - build consent offer: %w", location, err)
}
if offered {
view.ContactConsent = &offer
}
}
return view, nil
}
// acceptancePossible reports the states and the time in which the candidate
// may accept: a delivered or viewed invitation before its deadline.
func acceptancePossible(invitation domain.Invitation, at time.Time) bool {
state := invitation.State()
return (state == domain.InvitationDelivered || state == domain.InvitationViewed) &&
invitation.IsDelivered() && !invitation.ResponseDeadlinePassed(at)
}
// candidateVisibleTo reports whether the organization may read the profile of
// the candidate now: the publication is effective, and the candidate has not
// hidden the organization. The read model of the profile applies C2 and the
// revocation of C2; the hiding is a record of its own.
func candidateVisibleTo(ctx context.Context, unit outbound.UnitOfWork, candidateID, organizationID uuid.UUID) (bool, error) {
const location = "invitation.candidateVisibleTo"
settings, err := unit.PrivacySettings().ReadSettings(ctx, candidateID)
if err != nil {
return false, fmt.Errorf("%s - read privacy settings: %w", location, err)
}
if settings.Publication.State() != domain.PublicationPublished {
return false, nil
}
_, err = unit.PrivacySettings().GetHiddenOrganization(ctx, candidateID, organizationID)
switch {
case err == nil:
return false, nil
case errors.Is(err, outbound.ErrCandidatePrivacySettingsNotFound):
return true, nil
default:
return false, fmt.Errorf("%s - read hidden organization: %w", location, err)
}
}
// contactConsentOffer builds the consent offer of an invitation: the rendered
// C4 text and the kinds that the profile holds. It reports false when the
// profile holds no kind, because a grant must cover at least one field.
func contactConsentOffer(
ctx context.Context,
unit outbound.UnitOfWork,
invitation domain.Invitation,
organizationName string,
at time.Time,
) (domain.ContactConsentOffer, bool, error) {
profile, err := unit.CandidateProfiles().Get(ctx, invitation.CandidateID())
if err != nil {
return domain.ContactConsentOffer{}, false, fmt.Errorf("read candidate profile: %w", err)
}
held := heldContactFields(profile)
if len(held) == 0 {
return domain.ContactConsentOffer{}, false, nil
}
text, err := unit.ConsentTexts().Current(ctx, domain.ConsentPurposeContactDisclosure, at)
if err != nil {
return domain.ContactConsentOffer{}, false, fmt.Errorf("read consent C4 text: %w", err)
}
offer, err := domain.NewContactConsentOffer(text, organizationName, held)
if err != nil {
return domain.ContactConsentOffer{}, false, fmt.Errorf("build offer: %w", err)
}
return offer, true, nil
}
// notBefore keeps a clock that stepped back from recording a change before
// the last one.
func notBefore(now, last time.Time) time.Time {