feat(invitations): implement pending invitations feature with polling and dialog
Added functionality for managing pending invitations, including a polling mechanism to update the invitation count in real-time. Introduced a new dialog component for users to view and respond to their invitations without navigating away from their current context. Updated API endpoints for fetching and managing invitations, and enhanced the user interface with relevant internationalization support. Refs: #1303
This commit is contained in:
@@ -30,6 +30,26 @@ const (
|
||||
// EnableRBAC=true. Subject to 1-minute sliding-window dedup so a
|
||||
// probing client cannot fill the table.
|
||||
AuditActionAccessDenied AuditAction = "rbac.access_denied"
|
||||
// AuditActionInvitationSent fires when an Owner issues a new
|
||||
// tenant invitation. Actor is the inviter, TargetUserID is the
|
||||
// invitee. Note: AuditActionMemberAdded only fires when the
|
||||
// invitee actually accepts and the tenant_members row is created.
|
||||
AuditActionInvitationSent AuditAction = "rbac.invitation_sent"
|
||||
// AuditActionInvitationAccepted fires when the invitee accepts a
|
||||
// pending invitation. Actor is the invitee (acting on their own
|
||||
// inbox); the matching rbac.member_added is emitted in the same
|
||||
// transaction.
|
||||
AuditActionInvitationAccepted AuditAction = "rbac.invitation_accepted"
|
||||
// AuditActionInvitationDeclined fires when the invitee rejects a
|
||||
// pending invitation. Actor and target are the same user.
|
||||
AuditActionInvitationDeclined AuditAction = "rbac.invitation_declined"
|
||||
// AuditActionInvitationRevoked fires when a tenant Owner cancels
|
||||
// a still-pending invitation before the invitee acts. Actor is
|
||||
// the Owner; target is the invitee.
|
||||
AuditActionInvitationRevoked AuditAction = "rbac.invitation_revoked"
|
||||
// AuditActionInvitationExpired fires when the lazy sweep transitions
|
||||
// an overdue pending row to expired. Actor is empty (system).
|
||||
AuditActionInvitationExpired AuditAction = "rbac.invitation_expired"
|
||||
)
|
||||
|
||||
// AuditOutcome distinguishes successful mutations from middleware-level
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// TenantInvitationRepository persists tenant_invitations rows that
|
||||
// capture the "pending invite" intent before it becomes a real
|
||||
// tenant_members row. All read methods skip GORM soft-deleted rows.
|
||||
type TenantInvitationRepository interface {
|
||||
// Create inserts a new pending invitation. Returns the conflict
|
||||
// error sentinel (ErrPendingInvitationExists) if the (tenant_id,
|
||||
// invitee) partial unique index rejects the insert.
|
||||
Create(ctx context.Context, inv *types.TenantInvitation) error
|
||||
|
||||
// GetByID fetches an invitation by surrogate id, or (nil, nil) if
|
||||
// missing. Used by Accept/Decline/Revoke handlers.
|
||||
GetByID(ctx context.Context, id uint64) (*types.TenantInvitation, error)
|
||||
|
||||
// GetPendingByPair returns the in-flight pending invitation for
|
||||
// (tenant, invitee) or (nil, nil) when none exists. Used by the
|
||||
// service layer to short-circuit duplicate Create calls before
|
||||
// they hit the unique index.
|
||||
GetPendingByPair(ctx context.Context, tenantID uint64, inviteeUserID string) (*types.TenantInvitation, error)
|
||||
|
||||
// ListByTenant returns invitations for the tenant ordered by
|
||||
// id DESC (newest first). includeTerminal controls whether
|
||||
// non-pending rows (accepted/declined/revoked/expired) are
|
||||
// returned — the management UI shows pending by default, with
|
||||
// a "history" toggle for the rest.
|
||||
ListByTenant(ctx context.Context, tenantID uint64, includeTerminal bool) ([]*types.TenantInvitation, error)
|
||||
|
||||
// CountByTenantList counts invitation rows matching the same filter as
|
||||
// ListByTenant (tenant_id + optional pending-only vs include-terminal).
|
||||
CountByTenantList(ctx context.Context, tenantID uint64, includeTerminal bool) (int64, error)
|
||||
|
||||
// ListByTenantPage returns invitations for the tenant with id DESC
|
||||
// paging, same filtering semantics as ListByTenant.
|
||||
ListByTenantPage(ctx context.Context, tenantID uint64, includeTerminal bool, offset, limit int) ([]*types.TenantInvitation, error)
|
||||
|
||||
// ListByInvitee returns invitations addressed to the user across
|
||||
// all tenants. Ordered by id DESC. Used both by the inbox page
|
||||
// (includeTerminal=false) and by /me/invitations/history if/when
|
||||
// we expose history (includeTerminal=true).
|
||||
ListByInvitee(ctx context.Context, inviteeUserID string, includeTerminal bool) ([]*types.TenantInvitation, error)
|
||||
|
||||
// CountPendingByInvitee returns the number of pending invitations
|
||||
// for the user. Used by the avatar-row badge so the UI doesn't
|
||||
// have to fetch the full list just to render a count.
|
||||
CountPendingByInvitee(ctx context.Context, inviteeUserID string) (int64, error)
|
||||
|
||||
// MarkStatusIfPending atomically transitions a row from pending
|
||||
// to the supplied status. respondedAt is recorded as the
|
||||
// transition time. Returns gorm.ErrRecordNotFound when the row
|
||||
// does not exist or is already in a non-pending state, which the
|
||||
// service layer maps to ErrInvitationNotPending — that error is
|
||||
// the "stale state machine" signal callers care about.
|
||||
MarkStatusIfPending(
|
||||
ctx context.Context,
|
||||
id uint64,
|
||||
status types.TenantInvitationStatus,
|
||||
respondedAt time.Time,
|
||||
) error
|
||||
|
||||
// SweepExpired transitions all pending rows whose expires_at is
|
||||
// before `now` into the expired state. Returns the affected row
|
||||
// count so callers can decide whether to emit per-row audit
|
||||
// events (we don't — the volume could be high and the audit-log
|
||||
// table already records the original invitation_sent rows).
|
||||
SweepExpired(ctx context.Context, now time.Time) (int64, error)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// TenantInvitationService is the business-logic layer over
|
||||
// TenantInvitationRepository. It enforces the lifecycle invariants
|
||||
// described in TenantInvitationStatus, runs the lazy expiry sweep, and
|
||||
// brokers the accept-time hand-off into TenantMemberService so that
|
||||
// "accept" produces both the invitation transition AND the
|
||||
// tenant_members row in a single audit story.
|
||||
type TenantInvitationService interface {
|
||||
// Create issues a new pending invitation for (tenantID, inviteeUserID)
|
||||
// with the given role. Caller passes the inviter user id (may be
|
||||
// nil for service-internal flows). Returns the freshly created row
|
||||
// or a sentinel error (ErrPendingInvitationExists / ErrAlreadyMember /
|
||||
// ErrInvalidTenantRole) for the common conflicts.
|
||||
Create(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
inviteeUserID string,
|
||||
role types.TenantRole,
|
||||
invitedBy *string,
|
||||
message string,
|
||||
) (*types.TenantInvitation, error)
|
||||
|
||||
// Accept transitions the pending row into accepted AND creates the
|
||||
// active tenant_members row in the same transaction. The callerUserID
|
||||
// MUST equal inv.InviteeUserID — the service rejects anyone else.
|
||||
// Returns the freshly created membership for the response shape.
|
||||
Accept(ctx context.Context, invID uint64, callerUserID string) (*types.TenantMember, error)
|
||||
|
||||
// Decline transitions the pending row into declined. Same caller
|
||||
// rule as Accept: only the invitee themselves.
|
||||
Decline(ctx context.Context, invID uint64, callerUserID string) error
|
||||
|
||||
// Revoke transitions the pending row into revoked. Callable by an
|
||||
// Owner of the tenant the invitation belongs to (the handler
|
||||
// applies the Owner gate via g.Owner() route middleware, so this
|
||||
// method does not re-check role).
|
||||
Revoke(ctx context.Context, invID uint64) error
|
||||
|
||||
// GetByID returns an invitation or (nil, nil). Used by handlers to
|
||||
// authorise per-row operations (e.g. is the caller the invitee?).
|
||||
GetByID(ctx context.Context, invID uint64) (*types.TenantInvitation, error)
|
||||
|
||||
// ListByTenant returns invitations for a tenant. Always runs the
|
||||
// lazy expiry sweep before reading so stale-pending rows surface
|
||||
// as expired in the UI.
|
||||
ListByTenant(ctx context.Context, tenantID uint64, includeTerminal bool) ([]*types.TenantInvitation, error)
|
||||
|
||||
// ListTenantInvitationsPage paginates invitations for the tenant
|
||||
// management UI after the lazy sweep (same filtering as ListByTenant).
|
||||
ListTenantInvitationsPage(ctx context.Context, tenantID uint64, includeTerminal bool, page, pageSize int) ([]*types.TenantInvitation, int64, error)
|
||||
|
||||
// ListByInvitee returns invitations addressed to the user across
|
||||
// all tenants. Always runs the lazy sweep first.
|
||||
ListByInvitee(ctx context.Context, inviteeUserID string, includeTerminal bool) ([]*types.TenantInvitation, error)
|
||||
|
||||
// CountPendingByInvitee returns the user's pending invitation count
|
||||
// after running the lazy sweep, so the inbox badge can poll a
|
||||
// lightweight endpoint without paginating the full list.
|
||||
CountPendingByInvitee(ctx context.Context, inviteeUserID string) (int64, error)
|
||||
}
|
||||
@@ -30,6 +30,16 @@ type TenantMemberRepository interface {
|
||||
// ordered by joined_at ascending.
|
||||
ListByTenant(ctx context.Context, tenantID uint64) ([]*types.TenantMember, error)
|
||||
|
||||
// CountFilteredByTenant counts active memberships in tenant. Optional
|
||||
// search matches user email/username (join users table); empty search
|
||||
// counts all memberships.
|
||||
CountFilteredByTenant(ctx context.Context, tenantID uint64, search string) (int64, error)
|
||||
|
||||
// ListPagedByTenant returns active memberships sorted joined_at ASC, id ASC.
|
||||
// search filters by user email/username (join users table); empty
|
||||
// search lists all memberships in tenant.
|
||||
ListPagedByTenant(ctx context.Context, tenantID uint64, search string, offset, limit int) ([]*types.TenantMember, error)
|
||||
|
||||
// UpdateRole changes the role of an existing active membership. Returns
|
||||
// gorm.ErrRecordNotFound if no active row matches.
|
||||
UpdateRole(ctx context.Context, userID string, tenantID uint64, role types.TenantRole) error
|
||||
|
||||
@@ -33,6 +33,10 @@ type TenantMemberService interface {
|
||||
// ListByTenant returns every active membership inside the tenant.
|
||||
ListByTenant(ctx context.Context, tenantID uint64) ([]*types.TenantMember, error)
|
||||
|
||||
// ListMembersPage lists members with pagination. Query matches email or
|
||||
// username case-insensitively; empty query returns full tenant list slice.
|
||||
ListMembersPage(ctx context.Context, tenantID uint64, query string, page, pageSize int) ([]*types.TenantMember, int64, error)
|
||||
|
||||
// HasAnyMembers reports whether the tenant has at least one active
|
||||
// member. The auth middleware uses this to recover orphan tenants
|
||||
// (e.g. API-key-only tenants that never had a human member): the
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TenantInvitationStatus enumerates the lifecycle states of a single
|
||||
// invitation row. The legal transitions are:
|
||||
//
|
||||
// pending -> accepted | declined | revoked | expired
|
||||
//
|
||||
// All non-pending states are terminal; once a row leaves pending it is
|
||||
// kept for the audit trail and a new pending row can be issued.
|
||||
type TenantInvitationStatus string
|
||||
|
||||
const (
|
||||
// TenantInvitationStatusPending is the initial state: the invitee
|
||||
// has not yet acted and the row has not been revoked or aged out.
|
||||
TenantInvitationStatusPending TenantInvitationStatus = "pending"
|
||||
// TenantInvitationStatusAccepted means the invitee confirmed and
|
||||
// a corresponding active tenant_members row has been created in
|
||||
// the same transaction.
|
||||
TenantInvitationStatusAccepted TenantInvitationStatus = "accepted"
|
||||
// TenantInvitationStatusDeclined means the invitee rejected the
|
||||
// invitation. The row stays for auditability; a new pending
|
||||
// invitation can be issued afterwards.
|
||||
TenantInvitationStatusDeclined TenantInvitationStatus = "declined"
|
||||
// TenantInvitationStatusRevoked means a tenant Owner cancelled
|
||||
// the pending invitation before the invitee acted.
|
||||
TenantInvitationStatusRevoked TenantInvitationStatus = "revoked"
|
||||
// TenantInvitationStatusExpired means the row outlived its
|
||||
// expires_at without being accepted/declined/revoked. Set by the
|
||||
// lazy-sweep run before every List/Accept/Decline.
|
||||
TenantInvitationStatusExpired TenantInvitationStatus = "expired"
|
||||
)
|
||||
|
||||
// IsTerminal reports whether s is a non-pending state. Used by the
|
||||
// service layer to short-circuit accept/decline/revoke on rows that
|
||||
// have already been finalised.
|
||||
func (s TenantInvitationStatus) IsTerminal() bool {
|
||||
switch s {
|
||||
case TenantInvitationStatusAccepted,
|
||||
TenantInvitationStatusDeclined,
|
||||
TenantInvitationStatusRevoked,
|
||||
TenantInvitationStatusExpired:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TenantInvitation is one pending or finalised invitation issued by an
|
||||
// Owner of `TenantID` to the user identified by `InviteeUserID`. The
|
||||
// row is created in `pending` state when the Owner clicks "invite"; it
|
||||
// flips to a terminal state when the invitee accepts/declines, the
|
||||
// Owner revokes, or the row expires.
|
||||
//
|
||||
// A tenant_members row is created only on accept; the invitation table
|
||||
// is therefore the single source of truth for "pending intent" without
|
||||
// polluting the authoritative member roster.
|
||||
type TenantInvitation struct {
|
||||
// Surrogate primary key.
|
||||
ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
// TenantID references tenants.id.
|
||||
TenantID uint64 `json:"tenant_id" gorm:"not null;index"`
|
||||
// InviteeUserID references users.id (always a registered user; the
|
||||
// handler resolves the email to a User row before insertion).
|
||||
InviteeUserID string `json:"invitee_user_id" gorm:"type:varchar(36);not null;index"`
|
||||
// InvitedBy records the user id that issued this invitation. NULL
|
||||
// for invitations created via service-internal / synthetic actors
|
||||
// (mirrors the same treatment TenantMember.InvitedBy gets).
|
||||
InvitedBy *string `json:"invited_by,omitempty" gorm:"type:varchar(36)"`
|
||||
// Role the invitee will receive in tenant_members if they accept.
|
||||
Role TenantRole `json:"role" gorm:"type:varchar(20);not null"`
|
||||
// Status holds the lifecycle state. Default pending; mutated to
|
||||
// accepted/declined/revoked/expired exactly once.
|
||||
Status TenantInvitationStatus `json:"status" gorm:"type:varchar(20);not null;default:'pending'"`
|
||||
// Message is an optional free-text note the Owner can include in
|
||||
// the invitation (e.g. "joining the design squad — welcome!").
|
||||
Message string `json:"message,omitempty" gorm:"type:varchar(500)"`
|
||||
// ExpiresAt is when this row auto-flips to expired if still pending.
|
||||
// Set at creation time from RBAC_INVITATION_TTL (default 7d).
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
// RespondedAt records when the row left pending. Nil while pending.
|
||||
RespondedAt *time.Time `json:"responded_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
}
|
||||
|
||||
// TableName binds TenantInvitation to the tenant_invitations table.
|
||||
func (TenantInvitation) TableName() string { return "tenant_invitations" }
|
||||
|
||||
// IsExpired reports whether this invitation is past its expires_at at
|
||||
// the given reference time. The service layer uses this both for the
|
||||
// lazy sweep and to reject an accept/decline arriving after timeout.
|
||||
func (inv *TenantInvitation) IsExpired(at time.Time) bool {
|
||||
if inv == nil || inv.ExpiresAt.IsZero() {
|
||||
return false
|
||||
}
|
||||
return inv.ExpiresAt.Before(at)
|
||||
}
|
||||
|
||||
// TenantInvitationResponse is the API projection joined with tenant
|
||||
// name and inviter / invitee user fields the UI needs. The model is
|
||||
// intentionally NOT serialised directly so we don't leak DeletedAt /
|
||||
// UpdatedAt and lock the DB schema into the public API.
|
||||
type TenantInvitationResponse struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
TenantName string `json:"tenant_name,omitempty"`
|
||||
InviteeUserID string `json:"invitee_user_id"`
|
||||
InviteeEmail string `json:"invitee_email,omitempty"`
|
||||
InviteeName string `json:"invitee_name,omitempty"`
|
||||
InvitedBy *string `json:"invited_by,omitempty"`
|
||||
InviterEmail string `json:"inviter_email,omitempty"`
|
||||
InviterName string `json:"inviter_name,omitempty"`
|
||||
Role TenantRole `json:"role"`
|
||||
Status TenantInvitationStatus `json:"status"`
|
||||
Message string `json:"message,omitempty"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
RespondedAt *time.Time `json:"responded_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
Reference in New Issue
Block a user