49 lines
2.2 KiB
Go
49 lines
2.2 KiB
Go
package invitation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const contactAccessLocation = "invitation.contactAccessOf"
|
|
|
|
// contactAccessOf derives the contact access that the organization sees
|
|
// through one invitation. An invitation without a grant is not_granted. A
|
|
// revoked grant, and a grant that the contact read refuses now, are closed.
|
|
// Only an active grant that the read allows is available, and it lists the
|
|
// kinds of the grant.
|
|
//
|
|
// The derivation uses the checks and the locks of the contact read, through
|
|
// readableContactGrant, so the view never claims access that a read would
|
|
// refuse. It runs in the caller's transaction, and the locks it takes last
|
|
// until that transaction ends. Every state is checked, not only the accepted
|
|
// one, because a withdrawn invitation keeps its grant.
|
|
func contactAccessOf(ctx context.Context, unit outbound.UnitOfWork, invitation domain.Invitation) (domain.InvitationContactAccessView, error) {
|
|
organizationID := invitation.OrganizationID()
|
|
grant, err := unit.ContactGrants().GetByInvitation(ctx, organizationID, invitation.ID())
|
|
if errors.Is(err, outbound.ErrContactGrantNotFound) {
|
|
return domain.NewInvitationContactAccessView(domain.InvitationContactAccessNone, nil), nil
|
|
}
|
|
if err != nil {
|
|
return domain.InvitationContactAccessView{}, fmt.Errorf("%s - read contact grant: %w", contactAccessLocation, err)
|
|
}
|
|
// A revoked grant never becomes readable again, so the unlocked read is final.
|
|
if grant.Status() == domain.ContactGrantRevoked {
|
|
return domain.NewInvitationContactAccessView(domain.InvitationContactAccessClosed, nil), nil
|
|
}
|
|
stored, readable, err := readableContactGrant(ctx, unit, grant, organizationID)
|
|
switch {
|
|
case errors.Is(err, outbound.ErrContactGrantNotFound):
|
|
return domain.NewInvitationContactAccessView(domain.InvitationContactAccessClosed, nil), nil
|
|
case err != nil:
|
|
return domain.InvitationContactAccessView{}, fmt.Errorf("%s - check contact read: %w", contactAccessLocation, err)
|
|
case !readable:
|
|
return domain.NewInvitationContactAccessView(domain.InvitationContactAccessClosed, nil), nil
|
|
}
|
|
return domain.NewInvitationContactAccessView(domain.InvitationContactAccessAvailable, stored.Fields().Fields()), nil
|
|
}
|