Files
lincodex/backend/internal/adapter/primary/http/v1/contacts.go
T

92 lines
3.9 KiB
Go

package v1
import (
"context"
"errors"
"lincodex/backend/internal/adapter/primary/http/api"
"lincodex/backend/internal/adapter/primary/http/problem"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
func (handler *Handler) GetOrganizationInvitationContacts(ctx context.Context, params api.GetOrganizationInvitationContactsParams) (api.GetOrganizationInvitationContactsRes, error) {
if handler.invitation == nil {
return handler.UnimplementedHandler.GetOrganizationInvitationContacts(ctx, params)
}
return handler.invitation.GetOrganizationInvitationContacts(ctx, params)
}
// GetOrganizationInvitationContacts serves the only operation that returns
// contact values. The company middleware has already checked the session, the
// owner membership, and the path, and the usecase repeats the owner check in
// its transaction. Every failure of the grant read answers the same not found,
// so the response never tells which condition applies.
func (handler *InvitationHandler) GetOrganizationInvitationContacts(
ctx context.Context,
params api.GetOrganizationInvitationContactsParams,
) (api.GetOrganizationInvitationContactsRes, error) {
principal, ok := PrincipalFromContext(ctx)
if !ok {
return (*api.GetOrganizationInvitationContactsUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
}
disclosure, err := handler.operations.ReadContacts(ctx, principal.UserID, params.OrgID, params.InvitationID)
if err != nil {
return contactsReadFailure(ctx, err)
}
return &api.ContactDisclosureHeaders{CacheControl: api.CacheControlNoStoreNoStore, Response: contactDisclosureResponse(disclosure)}, nil
}
func contactsReadFailure(ctx context.Context, err error) (api.GetOrganizationInvitationContactsRes, error) {
// A missing grant is the not found of the contract. It is handled before the
// shared classification, which treats an unexpected missing grant as a
// storage defect on the other invitation paths.
if errors.Is(err, outbound.ErrContactGrantNotFound) {
return (*api.GetOrganizationInvitationContactsNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
}
switch classifyInvitationError(err) {
case invitationFailureForbidden:
return (*api.GetOrganizationInvitationContactsForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
case invitationFailureNotFound:
return (*api.GetOrganizationInvitationContactsNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
case invitationFailureKeyReused, invitationFailureBadKey, invitationFailureInvalid,
invitationFailureVersionConflict, invitationFailureStateConflict, invitationFailureOther:
return nil, err
}
return nil, err
}
// contactDisclosureResponse maps the disclosure field by field. A kind that the
// grant does not cover, or that the profile no longer holds, is null.
func contactDisclosureResponse(disclosure domain.ContactDisclosure) api.ContactDisclosure {
fields := make([]api.ContactFieldKind, 0, len(disclosure.Fields()))
for _, field := range disclosure.Fields() {
fields = append(fields, api.ContactFieldKind(field))
}
response := api.ContactDisclosure{
InvitationID: disclosure.InvitationID(), GrantID: disclosure.GrantID(),
Fields: fields, GrantedAt: api.Timestamp(disclosure.GrantedAt()),
}
if value, present := disclosure.FullName(); present {
response.FullName = api.NewNilContactFullName(api.ContactFullName(value))
} else {
response.FullName.SetToNull()
}
if value, present := disclosure.Email(); present {
response.Email = api.NewNilEmailAddress(api.EmailAddress(value))
} else {
response.Email.SetToNull()
}
if value, present := disclosure.Phone(); present {
response.Phone = api.NewNilContactPhoneNumber(api.ContactPhoneNumber(value))
} else {
response.Phone.SetToNull()
}
if value, present := disclosure.Telegram(); present {
response.Telegram = api.NewNilContactTelegramUsername(api.ContactTelegramUsername(value))
} else {
response.Telegram.SetToNull()
}
return response
}