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,418 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// ErrInvalidInvitation reports an invitation value that breaks the public
|
||||
// contract or cannot be restored from storage.
|
||||
var ErrInvalidInvitation = errors.New("invalid invitation")
|
||||
|
||||
// InvitationFieldError names the field of an invitation request that failed.
|
||||
// Pointer is a JSON Pointer into the request body, such as
|
||||
// /terms/salary/salary_to. The error carries the reason only, never a value:
|
||||
// terms are employer text, and a rejected value is not echoed.
|
||||
type InvitationFieldError struct {
|
||||
Pointer string
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (err *InvitationFieldError) Error() string {
|
||||
return fmt.Sprintf("invitation field %s: %s", err.Pointer, err.Reason)
|
||||
}
|
||||
|
||||
func (err *InvitationFieldError) Unwrap() error { return ErrInvalidInvitation }
|
||||
|
||||
func invalidInvitationField(pointer, reason string) error {
|
||||
return &InvitationFieldError{Pointer: pointer, Reason: reason}
|
||||
}
|
||||
|
||||
const (
|
||||
maxInvitationRoleTitleRunes = 200
|
||||
maxInvitationTaskRunes = 300
|
||||
maxInvitationTeamRunes = 1000
|
||||
maxInvitationTechnologyRunes = 100
|
||||
maxInvitationLocationRunes = 100
|
||||
maxInvitationNoteRunes = 300
|
||||
maxInvitationPackageTextRunes = 300
|
||||
maxInvitationTasks = 20
|
||||
maxInvitationTechnologies = 30
|
||||
maxInvitationWorkFormats = 3
|
||||
// MaxInvitationTermsBytes bounds the encoded terms of one version. The
|
||||
// longest text fields fit with room even when every character takes four
|
||||
// bytes; the database repeats the bound with a margin for jsonb spacing.
|
||||
MaxInvitationTermsBytes = 98304
|
||||
)
|
||||
|
||||
// InvitationOfferType is the type of the offer.
|
||||
type InvitationOfferType string
|
||||
|
||||
const (
|
||||
InvitationOfferFullTime InvitationOfferType = "full_time"
|
||||
InvitationOfferPartTime InvitationOfferType = "part_time"
|
||||
InvitationOfferContract InvitationOfferType = "contract"
|
||||
InvitationOfferInternship InvitationOfferType = "internship"
|
||||
)
|
||||
|
||||
func (offer InvitationOfferType) IsKnown() bool {
|
||||
switch offer {
|
||||
case InvitationOfferFullTime, InvitationOfferPartTime, InvitationOfferContract, InvitationOfferInternship:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// InvitationCommunicationChannel is how the organization plans to reach the
|
||||
// candidate after an acceptance. It never selects a contact field.
|
||||
type InvitationCommunicationChannel string
|
||||
|
||||
const (
|
||||
InvitationChannelEmail InvitationCommunicationChannel = "email"
|
||||
InvitationChannelPhone InvitationCommunicationChannel = "phone"
|
||||
InvitationChannelTelegram InvitationCommunicationChannel = "telegram"
|
||||
InvitationChannelVideoCall InvitationCommunicationChannel = "video_call"
|
||||
)
|
||||
|
||||
func (channel InvitationCommunicationChannel) IsKnown() bool {
|
||||
switch channel {
|
||||
case InvitationChannelEmail, InvitationChannelPhone, InvitationChannelTelegram, InvitationChannelVideoCall:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// InvitationCommunicationMethod is part of the terms: a draft without it
|
||||
// cannot exist.
|
||||
type InvitationCommunicationMethod struct {
|
||||
Channel InvitationCommunicationChannel `json:"channel"`
|
||||
Details *string `json:"details"`
|
||||
}
|
||||
|
||||
// InvitationPackageStatus says whether a package condition is offered.
|
||||
type InvitationPackageStatus string
|
||||
|
||||
const (
|
||||
InvitationPackageProvided InvitationPackageStatus = "provided"
|
||||
InvitationPackageNotProvided InvitationPackageStatus = "not_provided"
|
||||
)
|
||||
|
||||
// InvitationPackageItem is one required package condition. "Not provided" is
|
||||
// an explicit choice with no description; a missing condition is not the same
|
||||
// thing and fails validation.
|
||||
type InvitationPackageItem struct {
|
||||
Status InvitationPackageStatus `json:"status"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
// InvitationPackage is the required section of the package (PRD FR12). The
|
||||
// work format is the separate required term WorkFormats.
|
||||
type InvitationPackage struct {
|
||||
Bonus InvitationPackageItem `json:"bonus"`
|
||||
Equipment InvitationPackageItem `json:"equipment"`
|
||||
Training InvitationPackageItem `json:"training"`
|
||||
HealthInsurance InvitationPackageItem `json:"health_insurance"`
|
||||
}
|
||||
|
||||
// InvitationTerms is everything a candidate sees before deciding. The same
|
||||
// value is the request field of a draft and the stored snapshot of a terms
|
||||
// version. It holds no data of the candidate.
|
||||
type InvitationTerms struct {
|
||||
RoleTitle string `json:"role_title"`
|
||||
Specialization SpecializationCode `json:"specialization"`
|
||||
Grade GradeCode `json:"grade"`
|
||||
OfferType InvitationOfferType `json:"offer_type"`
|
||||
Tasks []string `json:"tasks"`
|
||||
Team string `json:"team"`
|
||||
TechStack []string `json:"tech_stack"`
|
||||
WorkFormats []NeedWorkFormat `json:"work_formats"`
|
||||
Location *string `json:"location"`
|
||||
CommunicationMethod InvitationCommunicationMethod `json:"communication_method"`
|
||||
Salary SalaryRange `json:"salary"`
|
||||
Package InvitationPackage `json:"package"`
|
||||
}
|
||||
|
||||
// Validate applies the rules the schema cannot express and repeats the
|
||||
// schema rules a stored value must also satisfy. The error is an
|
||||
// *InvitationFieldError with the pointer of the first failing field.
|
||||
func (terms InvitationTerms) Validate() error {
|
||||
checks := []func() error{
|
||||
func() error {
|
||||
if !validInvitationLine(terms.RoleTitle, maxInvitationRoleTitleRunes) {
|
||||
return invalidInvitationField("/terms/role_title", "must be one line of 1 to 200 characters")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
if !terms.Specialization.IsKnown() {
|
||||
return invalidInvitationField("/terms/specialization", "must be a known specialization")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
if !terms.Grade.IsKnown() {
|
||||
return invalidInvitationField("/terms/grade", "must be a known grade")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
if !terms.OfferType.IsKnown() {
|
||||
return invalidInvitationField("/terms/offer_type", "must be a known offer type")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
terms.validateTasks,
|
||||
func() error {
|
||||
if !validInvitationText(terms.Team, maxInvitationTeamRunes) {
|
||||
return invalidInvitationField("/terms/team", "must be text of 1 to 1000 characters")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
terms.validateTechStack,
|
||||
terms.validateWorkFormats,
|
||||
terms.validateLocation,
|
||||
terms.validateCommunicationMethod,
|
||||
terms.validateSalary,
|
||||
terms.Package.validate,
|
||||
}
|
||||
for _, check := range checks {
|
||||
if err := check(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (terms InvitationTerms) validateTasks() error {
|
||||
if len(terms.Tasks) < 1 || len(terms.Tasks) > maxInvitationTasks {
|
||||
return invalidInvitationField("/terms/tasks", "must have 1 to 20 tasks")
|
||||
}
|
||||
for index, task := range terms.Tasks {
|
||||
if !validInvitationLine(task, maxInvitationTaskRunes) {
|
||||
return invalidInvitationField("/terms/tasks/"+strconv.Itoa(index), "must be one line of 1 to 300 characters")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateTechStack compares names without regard to letter case, so Go and
|
||||
// GO are one technology. The pointer names the later of two equal items.
|
||||
func (terms InvitationTerms) validateTechStack() error {
|
||||
if len(terms.TechStack) < 1 || len(terms.TechStack) > maxInvitationTechnologies {
|
||||
return invalidInvitationField("/terms/tech_stack", "must have 1 to 30 technologies")
|
||||
}
|
||||
seen := make(map[string]struct{}, len(terms.TechStack))
|
||||
for index, name := range terms.TechStack {
|
||||
pointer := "/terms/tech_stack/" + strconv.Itoa(index)
|
||||
if !validInvitationLine(name, maxInvitationTechnologyRunes) {
|
||||
return invalidInvitationField(pointer, "must be one line of 1 to 100 characters")
|
||||
}
|
||||
folded := strings.ToLower(name)
|
||||
if _, duplicate := seen[folded]; duplicate {
|
||||
return invalidInvitationField(pointer, "must not repeat a technology")
|
||||
}
|
||||
seen[folded] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (terms InvitationTerms) validateWorkFormats() error {
|
||||
if len(terms.WorkFormats) < 1 || len(terms.WorkFormats) > maxInvitationWorkFormats {
|
||||
return invalidInvitationField("/terms/work_formats", "must have 1 to 3 work formats")
|
||||
}
|
||||
for index, format := range terms.WorkFormats {
|
||||
pointer := "/terms/work_formats/" + strconv.Itoa(index)
|
||||
if !format.IsKnown() {
|
||||
return invalidInvitationField(pointer, "must be a known work format")
|
||||
}
|
||||
if slices.Contains(terms.WorkFormats[:index], format) {
|
||||
return invalidInvitationField(pointer, "must not repeat a work format")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateLocation requires a place of work whenever the role has an office:
|
||||
// only a fully remote role may leave it null.
|
||||
func (terms InvitationTerms) validateLocation() error {
|
||||
officeFormat := slices.Contains(terms.WorkFormats, NeedWorkFormatOffice) || slices.Contains(terms.WorkFormats, NeedWorkFormatHybrid)
|
||||
if terms.Location == nil {
|
||||
if officeFormat {
|
||||
return invalidInvitationField("/terms/location", "is required for the office or hybrid format")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !validInvitationLine(*terms.Location, maxInvitationLocationRunes) {
|
||||
return invalidInvitationField("/terms/location", "must be one line of 1 to 100 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (terms InvitationTerms) validateCommunicationMethod() error {
|
||||
method := terms.CommunicationMethod
|
||||
if !method.Channel.IsKnown() {
|
||||
return invalidInvitationField("/terms/communication_method/channel", "must be a known channel")
|
||||
}
|
||||
if method.Details != nil && !validInvitationLine(*method.Details, maxInvitationNoteRunes) {
|
||||
return invalidInvitationField("/terms/communication_method/details", "must be one line of 1 to 300 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (terms InvitationTerms) validateSalary() error {
|
||||
err := terms.Salary.Validate()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if rangeError, ok := errors.AsType[*SalaryRangeError](err); ok {
|
||||
return invalidInvitationField("/terms/salary/"+rangeError.Field, rangeError.Reason)
|
||||
}
|
||||
return invalidInvitationField("/terms/salary", "must be a valid monthly RUB range")
|
||||
}
|
||||
|
||||
func (pack InvitationPackage) validate() error {
|
||||
items := []struct {
|
||||
name string
|
||||
item InvitationPackageItem
|
||||
}{
|
||||
{"bonus", pack.Bonus}, {"equipment", pack.Equipment},
|
||||
{"training", pack.Training}, {"health_insurance", pack.HealthInsurance},
|
||||
}
|
||||
for _, entry := range items {
|
||||
if err := entry.item.validate("/terms/package/" + entry.name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (item InvitationPackageItem) validate(pointer string) error {
|
||||
switch item.Status {
|
||||
case InvitationPackageProvided:
|
||||
if item.Description == nil {
|
||||
return invalidInvitationField(pointer+"/description", "is required for a provided condition")
|
||||
}
|
||||
if !validInvitationLine(*item.Description, maxInvitationPackageTextRunes) {
|
||||
return invalidInvitationField(pointer+"/description", "must be one line of 1 to 300 characters")
|
||||
}
|
||||
case InvitationPackageNotProvided:
|
||||
if item.Description != nil {
|
||||
return invalidInvitationField(pointer, "a condition that is not provided has no description")
|
||||
}
|
||||
default:
|
||||
return invalidInvitationField(pointer+"/status", "must be provided or not_provided")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clone returns a deep copy, so a caller cannot change a stored value.
|
||||
func (terms InvitationTerms) Clone() InvitationTerms {
|
||||
cloned := terms
|
||||
cloned.Tasks = slices.Clone(terms.Tasks)
|
||||
cloned.TechStack = slices.Clone(terms.TechStack)
|
||||
cloned.WorkFormats = slices.Clone(terms.WorkFormats)
|
||||
cloned.Location = cloneString(terms.Location)
|
||||
cloned.CommunicationMethod.Details = cloneString(terms.CommunicationMethod.Details)
|
||||
cloned.Package = InvitationPackage{
|
||||
Bonus: terms.Package.Bonus.clone(), Equipment: terms.Package.Equipment.clone(),
|
||||
Training: terms.Package.Training.clone(), HealthInsurance: terms.Package.HealthInsurance.clone(),
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
func (item InvitationPackageItem) clone() InvitationPackageItem {
|
||||
item.Description = cloneString(item.Description)
|
||||
return item
|
||||
}
|
||||
|
||||
func cloneString(value *string) *string {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copied := *value
|
||||
return &copied
|
||||
}
|
||||
|
||||
// Equal reports whether two terms hold the same content. A reordered list is
|
||||
// a different content, because the order of tasks and technologies is shown.
|
||||
func (terms InvitationTerms) Equal(other InvitationTerms) bool {
|
||||
return reflect.DeepEqual(terms, other)
|
||||
}
|
||||
|
||||
// EncodeInvitationTerms returns the canonical JSON of a validated snapshot.
|
||||
func EncodeInvitationTerms(terms InvitationTerms) ([]byte, error) {
|
||||
if err := terms.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("domain.EncodeInvitationTerms - validate terms: %w", err)
|
||||
}
|
||||
encoded, err := json.Marshal(terms)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("domain.EncodeInvitationTerms - encode terms: %w", err)
|
||||
}
|
||||
if len(encoded) > MaxInvitationTermsBytes {
|
||||
return nil, fmt.Errorf("domain.EncodeInvitationTerms - terms exceed %d bytes: %w", MaxInvitationTermsBytes, ErrInvalidInvitation)
|
||||
}
|
||||
return encoded, nil
|
||||
}
|
||||
|
||||
// DecodeInvitationTerms restores a stored snapshot. Unknown fields, trailing
|
||||
// data, and rule violations fail, so a row changed outside the application is
|
||||
// not served.
|
||||
func DecodeInvitationTerms(encoded []byte) (InvitationTerms, error) {
|
||||
const location = "domain.DecodeInvitationTerms"
|
||||
if len(encoded) == 0 || len(encoded) > MaxInvitationTermsBytes {
|
||||
return InvitationTerms{}, fmt.Errorf("%s - stored terms are outside their size bound: %w", location, ErrInvalidInvitation)
|
||||
}
|
||||
var terms InvitationTerms
|
||||
if err := decodeStrictJSON(encoded, &terms); err != nil {
|
||||
return InvitationTerms{}, fmt.Errorf("%s - decode terms: %w: %w", location, ErrInvalidInvitation, err)
|
||||
}
|
||||
if err := terms.Validate(); err != nil {
|
||||
return InvitationTerms{}, fmt.Errorf("%s - validate terms: %w", location, err)
|
||||
}
|
||||
return terms, nil
|
||||
}
|
||||
|
||||
// validInvitationLine accepts one line of plain text: 1 to maxRunes
|
||||
// characters, no control character, and no ASCII whitespace at either end.
|
||||
func validInvitationLine(value string, maxRunes int) bool {
|
||||
if !validInvitationText(value, maxRunes) {
|
||||
return false
|
||||
}
|
||||
return !strings.ContainsAny(value, "\r\n\t")
|
||||
}
|
||||
|
||||
// validInvitationText accepts plain text of 1 to maxRunes characters that may
|
||||
// contain line breaks and tabs but no other control character, and does not
|
||||
// start or end with ASCII whitespace.
|
||||
func validInvitationText(value string, maxRunes int) bool {
|
||||
if value == "" || !utf8.ValidString(value) || utf8.RuneCountInString(value) > maxRunes {
|
||||
return false
|
||||
}
|
||||
if strings.TrimFunc(value, isASCIISpace) != value {
|
||||
return false
|
||||
}
|
||||
for _, character := range value {
|
||||
if unicode.IsControl(character) && character != '\n' && character != '\r' && character != '\t' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isASCIISpace(character rune) bool {
|
||||
switch character {
|
||||
case ' ', '\t', '\n', '\v', '\f', '\r':
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user