94 lines
3.6 KiB
TypeScript
94 lines
3.6 KiB
TypeScript
import request from './request'
|
|
|
|
export type DirectorySnapshotState = 'UNAVAILABLE' | 'NEVER_SYNCED' | 'FRESH' | 'STALE' | 'CONTEXT_CHANGED'
|
|
export type DirectoryAvailabilityState = 'AVAILABLE' | 'NEVER_SYNCED' | 'UNAVAILABLE'
|
|
export type DirectoryContextState = 'MATCH' | 'CHANGED' | 'UNKNOWN'
|
|
|
|
export interface DirectoryAvailabilityContract {
|
|
snapshotState: DirectorySnapshotState
|
|
/** Additive fields are optional for rolling compatibility with older nodes. */
|
|
availabilityState?: DirectoryAvailabilityState | null
|
|
additionsAllowed?: boolean
|
|
currentRevision?: string | null
|
|
contextState?: DirectoryContextState | null
|
|
}
|
|
|
|
export const directoryAvailability = (value: DirectoryAvailabilityContract): DirectoryAvailabilityState => {
|
|
if (value.availabilityState === 'AVAILABLE' || value.availabilityState === 'NEVER_SYNCED' || value.availabilityState === 'UNAVAILABLE') {
|
|
return value.availabilityState
|
|
}
|
|
// Older backends did not expose availability. Only their FRESH state is safe
|
|
// to treat as usable; all other legacy states deliberately fail closed.
|
|
return value.snapshotState === 'FRESH'
|
|
? 'AVAILABLE'
|
|
: value.snapshotState === 'NEVER_SYNCED' ? 'NEVER_SYNCED' : 'UNAVAILABLE'
|
|
}
|
|
|
|
export interface DirectoryRun {
|
|
runId: string
|
|
trigger: 'MANUAL' | 'STARTUP' | 'SCHEDULED'
|
|
state: 'RUNNING' | 'SUCCEEDED' | 'FAILED'
|
|
startedAt: string | null
|
|
finishedAt: string | null
|
|
complete: boolean
|
|
applied: boolean
|
|
counts: Record<'departments' | 'usersSeen' | 'excluded' | 'created' | 'updated' | 'outOfScope', number>
|
|
errorCode: string | null
|
|
errorMessage: string | null
|
|
}
|
|
|
|
export interface DirectoryStatus extends DirectoryAvailabilityContract {
|
|
configured: boolean
|
|
running: boolean
|
|
maxStatusAgeSeconds: number
|
|
lastSuccessfulRunId: string | null
|
|
lastSuccessfulAt: string | null
|
|
snapshotExpiresAt: string | null
|
|
lastRun: DirectoryRun | null
|
|
}
|
|
|
|
export interface DirectoryDepartment {
|
|
id: string
|
|
name: string
|
|
parentId: string
|
|
// The backend serializes Feishu's potentially-large integer order as text.
|
|
// Keep it as text at the API boundary so it cannot lose precision in JS.
|
|
order: string
|
|
memberCount: number
|
|
}
|
|
|
|
export interface DirectoryOrganizationUser {
|
|
openId: string
|
|
userId: string | null
|
|
name: string
|
|
email: string
|
|
mobile: string
|
|
jobTitle: string
|
|
avatarUrl: string
|
|
departmentIds: string[]
|
|
}
|
|
|
|
export interface DirectoryOrganization extends DirectoryAvailabilityContract {
|
|
lastSuccessfulAt: string | null
|
|
departments: DirectoryDepartment[]
|
|
users: DirectoryOrganizationUser[]
|
|
}
|
|
|
|
// Backend elapsed() caps collection at 300s; allow transport/commit headroom.
|
|
export const DIRECTORY_SYNC_TIMEOUT = 360_000
|
|
export const feishuDirectoryApi = {
|
|
sync: (signal?: AbortSignal): Promise<DirectoryRun> =>
|
|
request.post('/feishu/directory/sync', undefined, { timeout: DIRECTORY_SYNC_TIMEOUT, signal }),
|
|
status: (signal?: AbortSignal): Promise<DirectoryStatus> =>
|
|
request.get('/feishu/directory/status', { signal }),
|
|
/** Departments (+ optional full users). Default omits users so the shell paints quickly. */
|
|
organization: (signal?: AbortSignal, includeUsers = false): Promise<DirectoryOrganization> =>
|
|
request.get('/feishu/directory/organization', { signal, params: { includeUsers } }),
|
|
/** Members for one department tree; used after the organization shell is ready. */
|
|
organizationMembers: (departmentId: string, signal?: AbortSignal, includeDescendants = true): Promise<DirectoryOrganization> =>
|
|
request.get('/feishu/directory/organization/members', {
|
|
signal,
|
|
params: { departmentId, includeDescendants },
|
|
}),
|
|
}
|