package domain import ( "fmt" "time" "github.com/google/uuid" ) // InvitationEventExpired is the audit event of an expiry that the server made // at the response deadline. No candidate or organization acted, so the event // names no actor and no field. const InvitationEventExpired InvitationEventKind = "expired" // AwaitsAnswer reports the states in which the candidate's answer is still // open: a delivered or a viewed invitation. The planned counter offer is not // among them, because no operation produces it yet. func (invitation Invitation) AwaitsAnswer() bool { return invitation.IsDelivered() && (invitation.State() == InvitationDelivered || invitation.State() == InvitationViewed) } // ExpiryDue reports whether the invitation still awaits an answer at the time // and its response deadline has passed. At the deadline or later the answer is // late, so the expiry is due. func (invitation Invitation) ExpiryDue(at time.Time) bool { return invitation.AwaitsAnswer() && invitation.ResponseDeadlinePassed(at) } // Expire returns the next version in the terminal state expired with the // timeout reason, when the invitation is due at the time. An invitation that is // not due, because it is answered, expired, or its deadline has not passed yet, // is returned as it is with changed false. The expiry keeps the deadline, the // terms, and the answer fields. It creates no consent and no contact grant, and // it never returns a weekly quota or a pending slot that the organization // spent: the pending limit counts only the invitations that still wait. func (invitation Invitation) Expire(at time.Time) (Invitation, bool, error) { const location = "Invitation.Expire" if !validInvitationTime(at) || at.Before(invitation.record.UpdatedAt) { return Invitation{}, false, fmt.Errorf("%s - expiry time must be set and not before the last change: %w", location, ErrInvalidInvitation) } if !invitation.ExpiryDue(at) { return invitation, false, nil } at = at.UTC() next := invitation.Stored() reason := string(InvitationExpiryTimeout) next.State = string(InvitationExpired) next.ExpiryReason = &reason next.Version++ next.UpdatedAt = at expired, err := RestoreInvitation(next) if err != nil { return Invitation{}, false, fmt.Errorf("%s - restore expired invitation: %w", location, err) } return expired, true, nil } // ExpiryEvent builds the audit event of the expired version this invitation has // just reached. The event has no actor, because the server made the change. func (invitation Invitation) ExpiryEvent() (InvitationEvent, error) { const location = "Invitation.ExpiryEvent" if invitation.State() != InvitationExpired { return InvitationEvent{}, fmt.Errorf("%s - the invitation must be expired: %w", location, ErrInvalidInvitation) } id, err := uuid.NewV7() if err != nil { return InvitationEvent{}, fmt.Errorf("%s - generate event ID: %w", location, err) } return InvitationEvent{ ID: id, InvitationID: invitation.record.ID, OrganizationID: invitation.record.OrganizationID, Version: invitation.record.Version, TermsVersion: invitation.record.TermsVersion, Kind: InvitationEventExpired, ActorID: uuid.Nil, ChangedFields: []InvitationChangedField{}, OccurredAt: invitation.record.UpdatedAt, }, nil }