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:
wizardchen
2026-05-17 21:19:59 +08:00
committed by lyingbug
parent 3bb0d6d763
commit 884ac44283
36 changed files with 4378 additions and 440 deletions
+193
View File
@@ -0,0 +1,193 @@
import { get, post, del } from '@/utils/request'
import type { TenantRole } from '@/api/tenant/members'
// TenantInvitationStatus mirrors internal/types/tenant_invitation.go's
// five-state machine. pending is the only non-terminal state; the rest
// are recorded for the audit trail.
export type TenantInvitationStatus =
| 'pending'
| 'accepted'
| 'declined'
| 'revoked'
| 'expired'
// TenantInvitation is the API projection of a tenant_invitations row,
// hydrated with the inviter / invitee user fields and the tenant name
// when the backend has them. Missing optional fields render as the
// raw id in the UI rather than dropping the row.
export interface TenantInvitation {
id: number
tenant_id: number
tenant_name?: string
invitee_user_id: string
invitee_email?: string
invitee_name?: string
invited_by?: string | null
inviter_email?: string
inviter_name?: string
role: TenantRole
status: TenantInvitationStatus
message?: string
expires_at: string
responded_at?: string | null
created_at: string
}
export interface ListInvitationsResponse {
success: boolean
data?: {
invitations: TenantInvitation[]
total: number
page?: number
page_size?: number
}
message?: string
}
export interface ListTenantInvitationsParams {
includeTerminal?: boolean
page?: number
page_size?: number
}
function buildTenantInvitationsQuery(options: ListTenantInvitationsParams): string {
const u = new URLSearchParams()
if (options.includeTerminal) u.set('include_terminal', 'true')
if (options.page != null && options.page > 0) u.set('page', String(options.page))
if (options.page_size != null && options.page_size > 0) u.set('page_size', String(options.page_size))
const qs = u.toString()
return qs ? `?${qs}` : ''
}
export interface CreateInvitationRequest {
email: string
role: TenantRole
message?: string
}
export interface CreateInvitationResponse {
success: boolean
data?: TenantInvitation
message?: string
}
export interface SimpleResponse {
success: boolean
message?: string
}
export interface AcceptInvitationResponse {
success: boolean
data?: {
membership: {
tenant_id: number
role: TenantRole
status: string
joined_at: string
}
}
message?: string
}
export interface PendingCountResponse {
success: boolean
data?: { pending_count: number }
message?: string
}
/**
* List invitations for a tenant. Defaults to pending only; pass
* includeTerminal=true to also surface accepted/declined/revoked/
* expired rows for the history view. Supports `page` / `page_size`.
* Backend: GET /api/v1/tenants/:id/invitations (Viewer+).
*/
export async function listTenantInvitations(
tenantId: number,
options: ListTenantInvitationsParams = {},
): Promise<ListInvitationsResponse> {
const qs = buildTenantInvitationsQuery(options)
return (await get(
`/api/v1/tenants/${tenantId}/invitations${qs}`,
)) as unknown as ListInvitationsResponse
}
/**
* Send a new invitation. The invitee will see it in /me/invitations
* and must accept before they actually become a member.
* Backend: POST /api/v1/tenants/:id/invitations (Owner+).
*
* 404 when the email is not a registered user (ask them to register).
* 409 when an existing pending invitation already covers this pair, or
* when the invitee is already an active member.
*/
export async function createInvitation(
tenantId: number,
body: CreateInvitationRequest,
): Promise<CreateInvitationResponse> {
return (await post(
`/api/v1/tenants/${tenantId}/invitations`,
body,
)) as unknown as CreateInvitationResponse
}
/**
* Revoke a still-pending invitation. Already-finalised rows return
* 409; rows from another tenant render as 404 to avoid existence
* leaks across tenants.
* Backend: DELETE /api/v1/tenants/:id/invitations/:inv_id (Owner+).
*/
export async function revokeInvitation(
tenantId: number,
invId: number,
): Promise<SimpleResponse> {
return (await del(
`/api/v1/tenants/${tenantId}/invitations/${invId}`,
)) as unknown as SimpleResponse
}
/**
* List MY invitations. Defaults to pending only — the inbox page
* filters terminal rows by default; pass includeTerminal=true for a
* history view if/when the UI grows one.
* Backend: GET /api/v1/me/invitations (authenticated).
*/
export async function listMyInvitations(
options: { includeTerminal?: boolean } = {},
): Promise<ListInvitationsResponse> {
const qs = options.includeTerminal ? '?include_terminal=true' : ''
return (await get(`/api/v1/me/invitations${qs}`)) as unknown as ListInvitationsResponse
}
/**
* Lightweight pending-count endpoint used by the avatar-row badge
* poller. Separate from the list endpoint so polling doesn't transfer
* the full payload every cycle.
* Backend: GET /api/v1/me/invitations/pending-count (authenticated).
*/
export async function getMyPendingInvitationCount(): Promise<PendingCountResponse> {
return (await get(
`/api/v1/me/invitations/pending-count`,
)) as unknown as PendingCountResponse
}
/**
* Accept one of MY pending invitations. On success the backend also
* creates the tenant_members row in the same flow; the caller should
* then refresh memberships in the auth store.
* Backend: POST /api/v1/me/invitations/:inv_id/accept (authenticated).
*/
export async function acceptInvitation(invId: number): Promise<AcceptInvitationResponse> {
return (await post(
`/api/v1/me/invitations/${invId}/accept`,
)) as unknown as AcceptInvitationResponse
}
/**
* Decline one of MY pending invitations.
* Backend: POST /api/v1/me/invitations/:inv_id/decline (authenticated).
*/
export async function declineInvitation(invId: number): Promise<SimpleResponse> {
return (await post(
`/api/v1/me/invitations/${invId}/decline`,
)) as unknown as SimpleResponse
}
+52 -4
View File
@@ -24,10 +24,30 @@ export interface ListMembersResponse {
data?: {
members: TenantMember[]
total: number
page?: number
page_size?: number
}
message?: string
}
export interface ListMembersParams {
page?: number
page_size?: number
/** 按邮箱/用户名筛选(服务端模糊匹配) */
q?: string
}
function buildMembersQuery(params: ListMembersParams | undefined): string {
if (!params) return ''
const u = new URLSearchParams()
if (params.page != null && params.page > 0) u.set('page', String(params.page))
if (params.page_size != null && params.page_size > 0) u.set('page_size', String(params.page_size))
const q = params.q?.trim()
if (q) u.set('q', q)
const qs = u.toString()
return qs ? `?${qs}` : ''
}
export interface AddMemberRequest {
email: string
role: TenantRole
@@ -45,11 +65,39 @@ export interface SimpleResponse {
}
/**
* List all active members of the given tenant.
* Backend: GET /api/v1/tenants/:id/members (Viewer+).
* 分页列出租户成员。
* Backend: GET /api/v1/tenants/:id/members (Viewer+)。查询参数:`q`、`page`、`page_size`。
*/
export async function listMembers(tenantId: number): Promise<ListMembersResponse> {
return (await get(`/api/v1/tenants/${tenantId}/members`)) as unknown as ListMembersResponse
export async function listMembers(
tenantId: number,
params: ListMembersParams = {},
): Promise<ListMembersResponse> {
const qs = buildMembersQuery(params)
return (await get(
`/api/v1/tenants/${tenantId}/members${qs}`,
)) as unknown as ListMembersResponse
}
/**
* 遍历分页拉取租户的全部成员(每页最大 100,最多 500 页兜底)。
* 用于「退出空间」等对全量成员的轻量校验;普通表格请直接使用 {@link listMembers} 分页接口。
*/
export async function fetchAllTenantMembers(tenantId: number): Promise<TenantMember[]> {
const pageSize = 100
let page = 1
const out: TenantMember[] = []
let total = Number.POSITIVE_INFINITY
for (let guard = 0; guard < 500 && out.length < total; guard++) {
const resp = await listMembers(tenantId, { page, page_size: pageSize })
if (!resp.success || !resp.data) break
total = resp.data.total
const batch = resp.data.members || []
if (batch.length === 0 && page >= 2) break
out.push(...batch)
if (batch.length < pageSize) break
page++
}
return out
}
/**