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,55 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
const (
|
||||
invitationCursorVersion = 1
|
||||
invitationCursorBytes = 61
|
||||
maxInvitationCursorTime = uint64(253402300799)
|
||||
)
|
||||
|
||||
// encodeInvitationCursor binds the position to the account and the
|
||||
// organization that read the page, so a cursor of another reader or tenant is
|
||||
// refused instead of continuing a foreign listing.
|
||||
func encodeInvitationCursor(actorID, organizationID uuid.UUID, position outbound.InvitationListPosition) (string, error) {
|
||||
seconds, nanos := position.CreatedAt.Unix(), position.CreatedAt.Nanosecond()
|
||||
if actorID == uuid.Nil || organizationID == uuid.Nil || position.InvitationID == uuid.Nil || position.CreatedAt.IsZero() ||
|
||||
position.CreatedAt.Year() < 1 || position.CreatedAt.Year() > 9999 || seconds < 0 || uint64(seconds) > maxInvitationCursorTime ||
|
||||
nanos < 0 || nanos >= 1_000_000_000 || nanos%int(time.Microsecond) != 0 {
|
||||
return "", errors.New("invitation cursor requires an actor, organization, and valid page position")
|
||||
}
|
||||
var value [invitationCursorBytes]byte
|
||||
value[0] = invitationCursorVersion
|
||||
copy(value[1:17], actorID[:])
|
||||
copy(value[17:33], organizationID[:])
|
||||
binary.BigEndian.PutUint64(value[33:41], uint64(seconds))
|
||||
binary.BigEndian.PutUint32(value[41:45], uint32(nanos))
|
||||
copy(value[45:61], position.InvitationID[:])
|
||||
return base64.RawURLEncoding.EncodeToString(value[:]), nil
|
||||
}
|
||||
|
||||
func decodeInvitationCursor(cursor string, actorID, organizationID uuid.UUID) (outbound.InvitationListPosition, error) {
|
||||
value, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err != nil || len(value) != invitationCursorBytes || actorID == uuid.Nil || organizationID == uuid.Nil ||
|
||||
base64.RawURLEncoding.EncodeToString(value) != cursor || value[0] != invitationCursorVersion ||
|
||||
uuid.UUID(value[1:17]) != actorID || uuid.UUID(value[17:33]) != organizationID {
|
||||
return outbound.InvitationListPosition{}, errors.New("invitation cursor is invalid")
|
||||
}
|
||||
seconds := binary.BigEndian.Uint64(value[33:41])
|
||||
nanos := binary.BigEndian.Uint32(value[41:45])
|
||||
invitationID := uuid.UUID(value[45:61])
|
||||
if seconds > uint64(math.MaxInt64) || seconds > maxInvitationCursorTime || nanos >= 1_000_000_000 || nanos%1_000 != 0 || invitationID == uuid.Nil {
|
||||
return outbound.InvitationListPosition{}, errors.New("invitation cursor position is invalid")
|
||||
}
|
||||
return outbound.InvitationListPosition{CreatedAt: time.Unix(int64(seconds), int64(nanos)).UTC(), InvitationID: invitationID}, nil
|
||||
}
|
||||
Reference in new issue
Block a user