feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings
This commit is contained in:
1 parent
a5990d3fc6
commit
676b3c6e24
59 files changed
+4926
-194
No files matched your search
@@ -30,6 +30,7 @@ type InvitationOperations interface {
|
||||
GetMine(ctx context.Context, actorID, invitationID uuid.UUID) (domain.CandidateInvitationView, error)
|
||||
ViewMine(ctx context.Context, actorID, invitationID uuid.UUID) (domain.CandidateInvitationView, error)
|
||||
DeclineMine(ctx context.Context, command invitationusecase.DeclineCommand) (domain.CandidateInvitationView, error)
|
||||
AcceptMine(ctx context.Context, command invitationusecase.AcceptCommand) (domain.CandidateInvitationView, error)
|
||||
}
|
||||
|
||||
// InvitationHandler serves the organization side of invitation drafts and
|
||||
|
||||
@@ -31,6 +31,66 @@ func (handler *Handler) DeclineMyInvitation(ctx context.Context, request *api.In
|
||||
return handler.invitation.DeclineMyInvitation(ctx, request, params)
|
||||
}
|
||||
|
||||
func (handler *Handler) AcceptMyInvitation(ctx context.Context, request *api.InvitationAcceptance, params api.AcceptMyInvitationParams) (api.AcceptMyInvitationRes, error) {
|
||||
if handler.invitation == nil {
|
||||
return handler.UnimplementedHandler.AcceptMyInvitation(ctx, request, params)
|
||||
}
|
||||
return handler.invitation.AcceptMyInvitation(ctx, request, params)
|
||||
}
|
||||
|
||||
// AcceptMyInvitation accepts the current terms of an invitation with the consent
|
||||
// C4. The fields come from the body as kinds only; the server takes the values
|
||||
// from the profile, and the request never carries them.
|
||||
func (handler *InvitationHandler) AcceptMyInvitation(
|
||||
ctx context.Context,
|
||||
request *api.InvitationAcceptance,
|
||||
params api.AcceptMyInvitationParams,
|
||||
) (api.AcceptMyInvitationRes, error) {
|
||||
principal, ok := PrincipalFromContext(ctx)
|
||||
if !ok {
|
||||
return (*api.AcceptMyInvitationUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
||||
}
|
||||
if request == nil {
|
||||
return (*api.AcceptMyInvitationBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationBody, "").Response), nil
|
||||
}
|
||||
fields := make([]domain.ContactField, 0, len(request.ConsentC4.Fields))
|
||||
for _, field := range request.ConsentC4.Fields {
|
||||
fields = append(fields, domain.ContactField(field))
|
||||
}
|
||||
view, err := handler.operations.AcceptMine(ctx, invitationusecase.AcceptCommand{
|
||||
ActorID: principal.UserID, InvitationID: params.InvitationID, IdempotencyKey: params.IdempotencyKey,
|
||||
ExpectedVersion: int64(request.ExpectedVersion), TermsVersion: int64(request.TermsVersion),
|
||||
TextVersion: string(request.ConsentC4.TextVersion), Fields: fields,
|
||||
})
|
||||
if err != nil {
|
||||
return invitationAcceptFailure(ctx, err)
|
||||
}
|
||||
return &api.CandidateInvitationHeaders{CacheControl: api.CacheControlNoStoreNoStore, Response: candidateInvitationResponse(view)}, nil
|
||||
}
|
||||
|
||||
func invitationAcceptFailure(ctx context.Context, err error) (api.AcceptMyInvitationRes, error) {
|
||||
if errors.Is(err, invitationusecase.ErrAnswerBlocked) {
|
||||
return (*api.AcceptMyInvitationConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
|
||||
}
|
||||
switch classifyInvitationError(err) {
|
||||
case invitationFailureForbidden:
|
||||
return (*api.AcceptMyInvitationForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
||||
case invitationFailureNotFound:
|
||||
return (*api.AcceptMyInvitationNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
|
||||
case invitationFailureKeyReused:
|
||||
return (*api.AcceptMyInvitationUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil
|
||||
case invitationFailureBadKey:
|
||||
return (*api.AcceptMyInvitationBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationHeader, "/Idempotency-Key").Response), nil
|
||||
case invitationFailureInvalid:
|
||||
return (*api.AcceptMyInvitationBadRequest)(invitationValidationProblem(ctx, err)), nil
|
||||
case invitationFailureVersionConflict:
|
||||
return (*api.AcceptMyInvitationPreconditionFailed)(&problem.New(ctx, api.ProblemCodeVersionConflict).Response), nil
|
||||
case invitationFailureStateConflict, invitationFailureOther:
|
||||
return nil, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (handler *InvitationHandler) GetMyInvitation(ctx context.Context, params api.GetMyInvitationParams) (api.GetMyInvitationRes, error) {
|
||||
principal, ok := PrincipalFromContext(ctx)
|
||||
if !ok {
|
||||
@@ -123,10 +183,9 @@ func invitationDeclineFailure(ctx context.Context, err error) (api.DeclineMyInvi
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// candidateInvitationResponse maps the candidate's view. The consent offer
|
||||
// and the contact grant belong to the acceptance, which is not served yet,
|
||||
// so an acceptance is impossible now and both are null, as the contract
|
||||
// requires when no acceptance can be made.
|
||||
// candidateInvitationResponse maps the candidate's view. The consent offer is
|
||||
// set only while an acceptance is possible, and the contact grant only after
|
||||
// the acceptance; otherwise both are null, as the contract requires.
|
||||
func candidateInvitationResponse(view domain.CandidateInvitationView) api.CandidateInvitation {
|
||||
response := api.CandidateInvitation{
|
||||
ID: view.ID,
|
||||
@@ -138,7 +197,13 @@ func candidateInvitationResponse(view domain.CandidateInvitationView) api.Candid
|
||||
RespondedAt: nullableTimestamp(view.RespondedAt), UpdatedAt: api.Timestamp(view.UpdatedAt),
|
||||
}
|
||||
response.ContactConsent.SetToNull()
|
||||
if view.ContactConsent != nil {
|
||||
response.ContactConsent.SetTo(contactConsentOfferResponse(*view.ContactConsent))
|
||||
}
|
||||
response.ContactGrant.SetToNull()
|
||||
if view.ContactGrant != nil {
|
||||
response.ContactGrant.SetTo(contactGrantResponse(*view.ContactGrant))
|
||||
}
|
||||
if view.Decline == nil {
|
||||
response.Decline.SetToNull()
|
||||
return response
|
||||
@@ -154,3 +219,43 @@ func candidateInvitationResponse(view domain.CandidateInvitationView) api.Candid
|
||||
response.Decline.SetTo(record)
|
||||
return response
|
||||
}
|
||||
|
||||
func contactConsentOfferResponse(offer domain.ContactConsentOffer) api.ContactConsentOffer {
|
||||
fields := make([]api.ContactFieldKind, 0, len(offer.AvailableFields))
|
||||
for _, field := range offer.AvailableFields {
|
||||
fields = append(fields, api.ContactFieldKind(field))
|
||||
}
|
||||
return api.ContactConsentOffer{
|
||||
Text: api.ContactConsentText{
|
||||
TextVersion: api.ConsentTextVersion(offer.Text.TextVersion), Text: offer.Text.Text,
|
||||
},
|
||||
AvailableFields: fields,
|
||||
}
|
||||
}
|
||||
|
||||
// contactGrantResponse maps the grant as the candidate reads it: the kinds, the
|
||||
// status, and whether the organization can read them now, never a value.
|
||||
func contactGrantResponse(view domain.ContactGrantView) api.ContactGrant {
|
||||
fields := make([]api.ContactFieldKind, 0, len(view.Fields))
|
||||
for _, field := range view.Fields {
|
||||
fields = append(fields, api.ContactFieldKind(field))
|
||||
}
|
||||
response := api.ContactGrant{
|
||||
ID: view.ID, Fields: fields, Status: api.ContactGrantState(view.Status), Readable: view.Readable,
|
||||
TermsVersion: api.InvitationTermsVersion(view.TermsVersion),
|
||||
ConsentTextVersion: api.ConsentTextVersion(view.ConsentTextVersion),
|
||||
GrantedAt: api.Timestamp(view.GrantedAt),
|
||||
}
|
||||
if view.BlockedBy == nil {
|
||||
response.BlockedBy.SetToNull()
|
||||
} else {
|
||||
response.BlockedBy.SetTo(api.ContactAccessBlock(*view.BlockedBy))
|
||||
}
|
||||
response.RevokedAt = nullableTimestamp(view.RevokedAt)
|
||||
if view.RevocationReason == nil {
|
||||
response.RevocationReason.SetToNull()
|
||||
} else {
|
||||
response.RevocationReason.SetTo(api.ContactRevocationReason(*view.RevocationReason))
|
||||
}
|
||||
return response
|
||||
}
|
||||
@@ -94,3 +94,28 @@ func TestCandidateInvitationResponseCarriesTheDeclineAndNoCandidateData(t *testi
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvitationAcceptFailuresMapToTheContractStatuses(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
text := &domain.InvitationFieldError{Pointer: "/consent_c4/text_version", Reason: "x"}
|
||||
for name, test := range map[string]struct {
|
||||
err error
|
||||
want string
|
||||
}{
|
||||
"employer": {invitationusecase.ErrForbidden, "*api.AcceptMyInvitationForbidden"},
|
||||
"unknown": {fmt.Errorf("wrapped: %w", outbound.ErrInvitationNotFound), "*api.AcceptMyInvitationNotFound"},
|
||||
"reused key": {invitationusecase.ErrIdempotencyKeyReused, "*api.AcceptMyInvitationUnprocessableEntity"},
|
||||
"bad key": {invitationusecase.ErrInvalidIdempotencyKey, "*api.AcceptMyInvitationBadRequest"},
|
||||
"consent text": {fmt.Errorf("wrapped: %w", text), "*api.AcceptMyInvitationBadRequest"},
|
||||
"stale version": {fmt.Errorf("wrapped: %w", domain.ErrInvitationVersionConflict), "*api.AcceptMyInvitationPreconditionFailed"},
|
||||
"terminal or late": {fmt.Errorf("wrapped: %w", invitationusecase.ErrAnswerBlocked), "*api.AcceptMyInvitationConflict"},
|
||||
} {
|
||||
response, unexpected := invitationAcceptFailure(ctx, test.err)
|
||||
if unexpected != nil || fmt.Sprintf("%T", response) != test.want {
|
||||
t.Errorf("%s: failure = %T, %v; want %s", name, response, unexpected, test.want)
|
||||
}
|
||||
}
|
||||
if _, unexpected := invitationAcceptFailure(ctx, errors.New("storage failed")); unexpected == nil {
|
||||
t.Error("an unknown failure produced a public response instead of reaching the responder")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user