Files

51 lines
1.9 KiB
Go

package invitation
import (
"context"
"fmt"
"time"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
// sendLimitsReached applies the two send limits of the product policy in force
// at the send: the number of invitations that wait for an answer under the
// need, and the pause of the pair of the organization and the candidate after a
// decline. Both answer the same conflict, so the caller refuses the send with
// one state conflict and says nothing about which limit applied.
//
// The pending count is exact because the caller holds the organization lock:
// every send takes that lock first, and every other change of a waiting
// invitation only removes it from the count. An invitation without a need has
// no need limit, and a direct invitation is bounded by the quota alone.
//
// The pause counts a decline only. The planned no-deal state has no operation
// that produces it, so it has no pause yet.
func (service *Service) sendLimitsReached(
ctx context.Context,
unit outbound.UnitOfWork,
command SendCommand,
current domain.Invitation,
at time.Time,
) (bool, error) {
policy, _, err := effectiveInvitationPolicy(ctx, unit, at, sendLocation)
if err != nil {
return false, err
}
if need := current.Snapshot().Need; need != nil {
pending, err := unit.Invitations().CountPendingNeedInvitations(ctx, command.OrganizationID, need.NeedID)
if err != nil {
return false, fmt.Errorf("%s - count pending invitations of the need: %w", sendLocation, err)
}
if pending >= int64(policy.MaxPendingPerNeed) {
return true, nil
}
}
declinedAt, found, err := unit.Invitations().LatestDeclineTime(ctx, command.OrganizationID, current.CandidateID())
if err != nil {
return false, fmt.Errorf("%s - read the latest decline of the candidate: %w", sendLocation, err)
}
return found && at.Before(declinedAt.Add(policy.RepeatAfterDecline.Duration())), nil
}