package domain import ( "fmt" "log/slog" "slices" "time" "github.com/google/uuid" ) const redactedContactDisclosure = "[redacted contact disclosure]" // ContactDisclosure holds the contact values that one active grant allows an // organization to read now. It is the only domain value that carries contact // values, so it redacts itself in fmt and slog output. It names no candidate // and no organization: the caller already knows both from its own request. type ContactDisclosure struct { invitationID uuid.UUID grantID uuid.UUID fields []ContactField grantedAt time.Time fullName *string email *string phone *string telegram *string } // NewContactDisclosure builds the values of an active grant from the profile. // Only the kinds of the grant are read, and a kind yields its value only when // the profile holds it as a confirmed claim, so a suggestion that nobody // confirmed is never disclosed. A kind the profile no longer holds stays nil. func NewContactDisclosure(grant ContactGrant, profile CandidateProfile) (ContactDisclosure, error) { const location = "domain.NewContactDisclosure" if grant.IsZero() || grant.Status() != ContactGrantActive { return ContactDisclosure{}, fmt.Errorf("%s - only an active grant discloses contacts: %w", location, ErrInvalidContactGrant) } content := profile.Content() disclosure := ContactDisclosure{ invitationID: grant.InvitationID(), grantID: grant.ID(), fields: grant.Fields().Fields(), grantedAt: grant.GrantedAt(), } for _, field := range disclosure.fields { switch field { case ContactFieldFullName: disclosure.fullName = confirmedContactValue(content.FullName()) case ContactFieldEmail: disclosure.email = confirmedContactValue(content.Email()) case ContactFieldPhone: disclosure.phone = confirmedContactValue(content.Phone()) case ContactFieldTelegram: disclosure.telegram = confirmedContactValue(content.Telegram()) } } return disclosure, nil } // confirmedContactValue returns the value of a claim the candidate confirmed, // and nil for a missing claim or an unconfirmed suggestion. func confirmedContactValue(claim ProfileClaim[string], present bool) *string { if !present || claim.Status() != ProfileClaimConfirmed { return nil } value := claim.Value() return &value } // InvitationID is the invitation whose acceptance created the grant. func (disclosure ContactDisclosure) InvitationID() uuid.UUID { return disclosure.invitationID } // GrantID is the grant that allowed the read. func (disclosure ContactDisclosure) GrantID() uuid.UUID { return disclosure.grantID } // Fields are the kinds that the grant covers, in canonical order. func (disclosure ContactDisclosure) Fields() []ContactField { return slices.Clone(disclosure.fields) } // GrantedAt is the time of the consent that the grant stores. func (disclosure ContactDisclosure) GrantedAt() time.Time { return disclosure.grantedAt } // FullName returns the full name and whether it is disclosed. func (disclosure ContactDisclosure) FullName() (string, bool) { return valueOfContact(disclosure.fullName) } // Email returns the email address and whether it is disclosed. func (disclosure ContactDisclosure) Email() (string, bool) { return valueOfContact(disclosure.email) } // Phone returns the phone number and whether it is disclosed. func (disclosure ContactDisclosure) Phone() (string, bool) { return valueOfContact(disclosure.phone) } // Telegram returns the Telegram username and whether it is disclosed. func (disclosure ContactDisclosure) Telegram() (string, bool) { return valueOfContact(disclosure.telegram) } func valueOfContact(value *string) (string, bool) { if value == nil { return "", false } return *value, true } func (disclosure ContactDisclosure) String() string { return redactedContactDisclosure } func (disclosure ContactDisclosure) GoString() string { return redactedContactDisclosure } func (disclosure ContactDisclosure) Format(state fmt.State, _ rune) { writeRedacted(state, redactedContactDisclosure) } // LogValue carries the grant identifier and the number of kinds only. func (disclosure ContactDisclosure) LogValue() slog.Value { return slog.GroupValue(slog.String("grant_id", disclosure.grantID.String()), slog.Int("fields", len(disclosure.fields))) }