feat(invitations): send invitations into the candidate inbox atomically
This commit is contained in:
1 parent
fc35e27af5
commit
1a067221f7
46 files changed
+3276
-113
No files matched your search
@@ -18,18 +18,22 @@ import (
|
||||
|
||||
const defaultInvitationLimit = 20
|
||||
|
||||
// InvitationOperations are the owner-scoped invitation draft reads and
|
||||
// commands served by HTTP.
|
||||
// InvitationOperations are the owner-scoped invitation reads and commands and
|
||||
// the candidate's read of the own inbox served by HTTP.
|
||||
type InvitationOperations interface {
|
||||
Create(ctx context.Context, command invitationusecase.CreateCommand) (domain.OrganizationInvitationView, error)
|
||||
Get(ctx context.Context, actorID, organizationID, invitationID uuid.UUID) (domain.OrganizationInvitationView, error)
|
||||
List(ctx context.Context, command invitationusecase.ListCommand) (invitationusecase.Page, error)
|
||||
Update(ctx context.Context, command invitationusecase.UpdateCommand) (domain.OrganizationInvitationView, error)
|
||||
Send(ctx context.Context, command invitationusecase.SendCommand) (domain.OrganizationInvitationView, error)
|
||||
ListMine(ctx context.Context, command invitationusecase.CandidateListCommand) (invitationusecase.CandidatePage, error)
|
||||
}
|
||||
|
||||
// InvitationHandler serves the organization side of invitation drafts. The
|
||||
// organization comes from the path and the server-side membership, never from
|
||||
// a body, and the candidate is only ever named by the opaque card handle.
|
||||
// InvitationHandler serves the organization side of invitation drafts and
|
||||
// their sending, and the candidate's inbox. The organization comes from the
|
||||
// path and the server-side membership, never from a body, the candidate is
|
||||
// only ever named by the opaque card handle, and the candidate's inbox is
|
||||
// bound to the session.
|
||||
type InvitationHandler struct {
|
||||
operations InvitationOperations
|
||||
}
|
||||
@@ -69,6 +73,20 @@ func (handler *Handler) UpdateOrganizationInvitation(ctx context.Context, reques
|
||||
return handler.invitation.UpdateOrganizationInvitation(ctx, request, params)
|
||||
}
|
||||
|
||||
func (handler *Handler) SendOrganizationInvitation(ctx context.Context, request *api.InvitationCommand, params api.SendOrganizationInvitationParams) (api.SendOrganizationInvitationRes, error) {
|
||||
if handler.invitation == nil {
|
||||
return handler.UnimplementedHandler.SendOrganizationInvitation(ctx, request, params)
|
||||
}
|
||||
return handler.invitation.SendOrganizationInvitation(ctx, request, params)
|
||||
}
|
||||
|
||||
func (handler *Handler) ListMyInvitations(ctx context.Context, params api.ListMyInvitationsParams) (api.ListMyInvitationsRes, error) {
|
||||
if handler.invitation == nil {
|
||||
return handler.UnimplementedHandler.ListMyInvitations(ctx, params)
|
||||
}
|
||||
return handler.invitation.ListMyInvitations(ctx, params)
|
||||
}
|
||||
|
||||
func (handler *InvitationHandler) CreateOrganizationInvitation(
|
||||
ctx context.Context,
|
||||
request *api.OrganizationInvitationCreate,
|
||||
@@ -203,6 +221,66 @@ func (handler *InvitationHandler) UpdateOrganizationInvitation(
|
||||
return &api.OrganizationInvitationHeaders{CacheControl: api.CacheControlNoStoreNoStore, Response: organizationInvitationResponse(view)}, nil
|
||||
}
|
||||
|
||||
func (handler *InvitationHandler) SendOrganizationInvitation(
|
||||
ctx context.Context,
|
||||
request *api.InvitationCommand,
|
||||
params api.SendOrganizationInvitationParams,
|
||||
) (api.SendOrganizationInvitationRes, error) {
|
||||
principal, ok := PrincipalFromContext(ctx)
|
||||
if !ok {
|
||||
return (*api.SendOrganizationInvitationUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
||||
}
|
||||
if request == nil {
|
||||
return (*api.SendOrganizationInvitationBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationBody, "").Response), nil
|
||||
}
|
||||
view, err := handler.operations.Send(ctx, invitationusecase.SendCommand{
|
||||
ActorID: principal.UserID, OrganizationID: params.OrgID, InvitationID: params.InvitationID,
|
||||
IdempotencyKey: params.IdempotencyKey, ExpectedVersion: int64(request.ExpectedVersion),
|
||||
})
|
||||
if err != nil {
|
||||
return invitationSendFailure(ctx, err)
|
||||
}
|
||||
return &api.OrganizationInvitationHeaders{CacheControl: api.CacheControlNoStoreNoStore, Response: organizationInvitationResponse(view)}, nil
|
||||
}
|
||||
|
||||
func (handler *InvitationHandler) ListMyInvitations(ctx context.Context, params api.ListMyInvitationsParams) (api.ListMyInvitationsRes, error) {
|
||||
principal, ok := PrincipalFromContext(ctx)
|
||||
if !ok {
|
||||
return (*api.ListMyInvitationsUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
||||
}
|
||||
command, pointer := candidateInvitationListCommand(principal.UserID, params)
|
||||
if pointer != "" {
|
||||
return (*api.ListMyInvitationsBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationQuery, "/"+pointer).Response), nil
|
||||
}
|
||||
page, err := handler.operations.ListMine(ctx, command)
|
||||
if err != nil {
|
||||
if errors.Is(err, invitationusecase.ErrForbidden) {
|
||||
return (*api.ListMyInvitationsForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
items := make([]api.CandidateInvitationSummary, 0, len(page.Items))
|
||||
for _, item := range page.Items {
|
||||
items = append(items, candidateInvitationSummary(item))
|
||||
}
|
||||
next := api.NilString{}
|
||||
next.SetToNull()
|
||||
if page.HasMore && len(page.Items) > 0 {
|
||||
last := page.Items[len(page.Items)-1]
|
||||
cursor, err := encodeCandidateInvitationCursor(principal.UserID, outbound.CandidateInvitationPosition{
|
||||
DeliveredAt: last.DeliveredAt, InvitationID: last.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("InvitationHandler.ListMyInvitations - encode cursor: %w", err)
|
||||
}
|
||||
next.SetTo(cursor)
|
||||
}
|
||||
return &api.CandidateInvitationListHeaders{
|
||||
CacheControl: api.CacheControlNoStoreNoStore,
|
||||
Response: api.CandidateInvitationList{Items: items, Page: api.PageInfo{HasMore: page.HasMore, NextCursor: next}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// invitationFailure is the public classification of a usecase error. Every
|
||||
// kind has one fixed problem, so a response never depends on which of two
|
||||
// indistinguishable causes happened.
|
||||
@@ -232,7 +310,7 @@ func classifyInvitationError(err error) invitationFailure {
|
||||
return invitationFailureBadKey
|
||||
case errors.Is(err, domain.ErrInvitationVersionConflict), errors.Is(err, outbound.ErrInvitationVersionConflict):
|
||||
return invitationFailureVersionConflict
|
||||
case errors.Is(err, domain.ErrInvitationNotDraft):
|
||||
case errors.Is(err, domain.ErrInvitationNotDraft), errors.Is(err, invitationusecase.ErrSendBlocked):
|
||||
return invitationFailureStateConflict
|
||||
case errors.Is(err, domain.ErrInvalidInvitation), errors.Is(err, domain.ErrInvalidSalaryRange):
|
||||
if _, ok := errors.AsType[*domain.InvitationFieldError](err); ok {
|
||||
@@ -301,6 +379,52 @@ func invitationUpdateFailure(ctx context.Context, err error) (api.UpdateOrganiza
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func invitationSendFailure(ctx context.Context, err error) (api.SendOrganizationInvitationRes, error) {
|
||||
// The organization quota is a 429 with the time when it frees room; it is
|
||||
// not stored under the key, so a later retry can succeed.
|
||||
if limited, ok := errors.AsType[*invitationusecase.QuotaExceededError](err); ok {
|
||||
return problem.RateLimited(ctx, wholeSeconds(time.Until(limited.RetryAt))), nil
|
||||
}
|
||||
switch classifyInvitationError(err) {
|
||||
case invitationFailureForbidden:
|
||||
return (*api.SendOrganizationInvitationForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
||||
case invitationFailureNotFound:
|
||||
return (*api.SendOrganizationInvitationNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
|
||||
case invitationFailureKeyReused:
|
||||
return (*api.SendOrganizationInvitationUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil
|
||||
case invitationFailureBadKey:
|
||||
return (*api.SendOrganizationInvitationBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationHeader, "/Idempotency-Key").Response), nil
|
||||
case invitationFailureInvalid:
|
||||
return (*api.SendOrganizationInvitationBadRequest)(invitationValidationProblem(ctx, err)), nil
|
||||
case invitationFailureVersionConflict:
|
||||
return (*api.SendOrganizationInvitationPreconditionFailed)(&problem.New(ctx, api.ProblemCodeVersionConflict).Response), nil
|
||||
case invitationFailureStateConflict:
|
||||
return (*api.SendOrganizationInvitationConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
|
||||
case invitationFailureOther:
|
||||
return nil, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func candidateInvitationListCommand(actorID uuid.UUID, params api.ListMyInvitationsParams) (invitationusecase.CandidateListCommand, string) {
|
||||
limit, ok := params.Limit.Get()
|
||||
if !ok {
|
||||
limit = defaultInvitationLimit
|
||||
}
|
||||
if limit < 1 || limit > outbound.MaxInvitationPageSize {
|
||||
return invitationusecase.CandidateListCommand{}, "limit"
|
||||
}
|
||||
command := invitationusecase.CandidateListCommand{ActorID: actorID, Limit: limit}
|
||||
if cursor, ok := params.Cursor.Get(); ok {
|
||||
position, err := decodeCandidateInvitationCursor(cursor, actorID)
|
||||
if err != nil {
|
||||
return invitationusecase.CandidateListCommand{}, "cursor"
|
||||
}
|
||||
command.After = &position
|
||||
}
|
||||
return command, ""
|
||||
}
|
||||
|
||||
func invitationListCommand(actorID uuid.UUID, params api.ListOrganizationInvitationsParams) (invitationusecase.ListCommand, string) {
|
||||
limit, ok := params.Limit.Get()
|
||||
if !ok {
|
||||
@@ -493,3 +617,19 @@ func organizationInvitationSummary(view domain.OrganizationInvitationView) api.O
|
||||
CreatedAt: api.Timestamp(view.CreatedAt), UpdatedAt: api.Timestamp(view.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func candidateInvitationSummary(summary domain.CandidateInvitationSummary) api.CandidateInvitationSummary {
|
||||
formats := make([]api.WorkFormat, 0, len(summary.WorkFormats))
|
||||
for _, format := range summary.WorkFormats {
|
||||
formats = append(formats, api.WorkFormat(format))
|
||||
}
|
||||
return api.CandidateInvitationSummary{
|
||||
ID: summary.ID,
|
||||
Organization: api.InvitationOrganization{ID: summary.Organization.ID, Name: summary.Organization.Name},
|
||||
State: api.InvitationState(summary.State), ExpiryReason: invitationExpiryResponse(summary.ExpiryReason),
|
||||
Version: api.InvitationVersion(summary.Version), TermsVersion: api.InvitationTermsVersion(summary.TermsVersion),
|
||||
RoleTitle: api.InvitationRoleTitle(summary.RoleTitle), Salary: salaryRangeResponse(summary.Salary), WorkFormats: formats,
|
||||
ResponseDeadlineAt: api.Timestamp(summary.ResponseDeadlineAt), DeliveredAt: api.Timestamp(summary.DeliveredAt),
|
||||
ViewedAt: nullableTimestamp(summary.ViewedAt), UpdatedAt: api.Timestamp(summary.UpdatedAt),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user