feat(invitations): add organization invitation drafts with salary range rules
This commit is contained in:
1 parent
28337d84cb
commit
bd23c839fd
36 files changed
+7018
-83
No files matched your search
@@ -0,0 +1,100 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ErrInvalidSalaryRange reports a salary range that breaks the money rules.
|
||||
var ErrInvalidSalaryRange = errors.New("invalid salary range")
|
||||
|
||||
const (
|
||||
// SalaryCurrencyRUB is the only currency of the MVP.
|
||||
SalaryCurrencyRUB = "RUB"
|
||||
// SalaryPeriodMonth is the only pay period of the MVP.
|
||||
SalaryPeriodMonth = "month"
|
||||
// SalaryTaxNet is a net ("after personal income tax") amount.
|
||||
SalaryTaxNet = "net"
|
||||
// SalaryTaxGross is a gross ("before personal income tax") amount.
|
||||
SalaryTaxGross = "gross"
|
||||
|
||||
// SalaryStepRubles is the step of every salary bound (PRD FR13).
|
||||
SalaryStepRubles int64 = 1000
|
||||
// MaxSalaryRubles caps a bound, so the integer width test below cannot
|
||||
// overflow: the largest product is 20 * MaxSalaryRubles.
|
||||
MaxSalaryRubles int64 = 100_000_000
|
||||
)
|
||||
|
||||
// SalaryRange is a monthly range [From; To] in whole rubles with its tax
|
||||
// basis. Amounts are exact integers; no float takes part in any comparison.
|
||||
type SalaryRange struct {
|
||||
SalaryFrom int64 `json:"salary_from"`
|
||||
SalaryTo int64 `json:"salary_to"`
|
||||
Currency string `json:"currency"`
|
||||
Period string `json:"period"`
|
||||
TaxBasis string `json:"tax_basis"`
|
||||
}
|
||||
|
||||
// SalaryRangeError names the field of the range that failed. The field is the
|
||||
// JSON name inside the range, so a caller can build the pointer of its own
|
||||
// request from it. The error never carries an amount.
|
||||
type SalaryRangeError struct {
|
||||
Field string
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (err *SalaryRangeError) Error() string {
|
||||
return fmt.Sprintf("salary range field %s: %s", err.Field, err.Reason)
|
||||
}
|
||||
|
||||
func (err *SalaryRangeError) Unwrap() error { return ErrInvalidSalaryRange }
|
||||
|
||||
// Validate applies the rules of PRD FR13:
|
||||
//
|
||||
// - the currency is RUB, the period is a month, and the base is net or gross;
|
||||
// - A and B are positive multiples of 1000 (and at most MaxSalaryRubles);
|
||||
// - A < B;
|
||||
// - the width 2 * (B - A) / (A + B), the range relative to its midpoint, is
|
||||
// from 10% to 40% inclusive.
|
||||
//
|
||||
// The width test is the integer pair 20 * (B - A) >= A + B and
|
||||
// 5 * (B - A) <= A + B, which holds exactly at 10% and 40% without a rounding
|
||||
// error. The first failing field is reported; a reversed or too narrow or wide
|
||||
// range is attributed to salary_to.
|
||||
func (salary SalaryRange) Validate() error {
|
||||
if err := validSalaryBound("salary_from", salary.SalaryFrom); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validSalaryBound("salary_to", salary.SalaryTo); err != nil {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case salary.Currency != SalaryCurrencyRUB:
|
||||
return &SalaryRangeError{Field: "currency", Reason: "must be RUB"}
|
||||
case salary.Period != SalaryPeriodMonth:
|
||||
return &SalaryRangeError{Field: "period", Reason: "must be month"}
|
||||
case salary.TaxBasis != SalaryTaxNet && salary.TaxBasis != SalaryTaxGross:
|
||||
return &SalaryRangeError{Field: "tax_basis", Reason: "must be net or gross"}
|
||||
case salary.SalaryFrom >= salary.SalaryTo:
|
||||
return &SalaryRangeError{Field: "salary_to", Reason: "must be above salary_from"}
|
||||
}
|
||||
width := salary.SalaryTo - salary.SalaryFrom
|
||||
sum := salary.SalaryTo + salary.SalaryFrom
|
||||
if 20*width < sum {
|
||||
return &SalaryRangeError{Field: "salary_to", Reason: "the range is narrower than 10% of its midpoint"}
|
||||
}
|
||||
if 5*width > sum {
|
||||
return &SalaryRangeError{Field: "salary_to", Reason: "the range is wider than 40% of its midpoint"}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validSalaryBound(field string, amount int64) error {
|
||||
if amount < SalaryStepRubles || amount > MaxSalaryRubles {
|
||||
return &SalaryRangeError{Field: field, Reason: "must be from 1000 to 100000000 rubles"}
|
||||
}
|
||||
if amount%SalaryStepRubles != 0 {
|
||||
return &SalaryRangeError{Field: field, Reason: "must be a multiple of 1000"}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user