feat(invitations): check contact grants on every read, cap pending invitations, and expire them at the deadline

This commit is contained in:
shira committed 2026-10-09 19:06:50 +03:00
1 parent 676b3c6e24
commit 12a7213d4a
67 files changed
+4255 -149

No files matched your search

@@ -0,0 +1,48 @@
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
}