feat(qaops): 接通网页通讯录持久化同步与后台接管
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
export interface DirectorySyncResult {
|
||||
directory_id: string
|
||||
departments: number
|
||||
people: number
|
||||
synced_at: string
|
||||
}
|
||||
export interface DirectorySyncJob {
|
||||
id: string
|
||||
state: 'pending' | 'running' | 'retry' | 'completed' | 'failed' | 'cancelled'
|
||||
attempt: number
|
||||
max_attempts: number
|
||||
updated_at: string
|
||||
error_code: string
|
||||
result: DirectorySyncResult | null
|
||||
}
|
||||
export interface DirectorySyncStatus {
|
||||
configured: boolean
|
||||
worker_ready: boolean
|
||||
job: DirectorySyncJob | null
|
||||
}
|
||||
export function completedSyncAction(
|
||||
s: DirectorySyncStatus,
|
||||
last: string,
|
||||
dirty: boolean
|
||||
): 'none' | 'preserve' | 'reload' {
|
||||
if (s.job?.state !== 'completed' || s.job.id === last) return 'none'
|
||||
return dirty ? 'preserve' : 'reload'
|
||||
}
|
||||
export const directorySyncActive = (s: DirectorySyncStatus | null) =>
|
||||
Boolean(s?.job && ['pending', 'running', 'retry'].includes(s.job.state))
|
||||
import { invalid as bad, obj as object, str as text, count, instant as nullableInstant } from './qaOrg'
|
||||
function instant(v: unknown): string {
|
||||
return nullableInstant(v) ?? bad()
|
||||
}
|
||||
export function parseDirectorySync(raw: unknown): DirectorySyncStatus {
|
||||
const s = object(raw)
|
||||
if (typeof s.configured !== 'boolean' || typeof s.worker_ready !== 'boolean') return bad()
|
||||
let job: DirectorySyncJob | null = null
|
||||
if (s.job !== null) {
|
||||
const j = object(s.job)
|
||||
if (!['pending', 'running', 'retry', 'completed', 'failed', 'cancelled'].includes(text(j.state))) return bad()
|
||||
let result: DirectorySyncResult | null = null
|
||||
if (j.result !== null) {
|
||||
const r = object(j.result)
|
||||
result = {
|
||||
directory_id: text(r.directory_id),
|
||||
departments: count(r.departments),
|
||||
people: count(r.people),
|
||||
synced_at: instant(r.synced_at),
|
||||
}
|
||||
if (!result.directory_id || !result.departments || !result.people) return bad()
|
||||
}
|
||||
job = {
|
||||
id: text(j.id),
|
||||
state: j.state as DirectorySyncJob['state'],
|
||||
attempt: count(j.attempt),
|
||||
max_attempts: count(j.max_attempts),
|
||||
updated_at: instant(j.updated_at),
|
||||
error_code: text(j.error_code),
|
||||
result,
|
||||
}
|
||||
if (!job.id || !job.max_attempts || job.attempt > job.max_attempts || (job.state === 'completed' && !result))
|
||||
return bad()
|
||||
}
|
||||
return { configured: s.configured, worker_ready: s.worker_ready, job }
|
||||
}
|
||||
async function request(method: string, operationID?: string, signal?: AbortSignal) {
|
||||
const response = await fetch('/api/v1/qa-ops/organization/sync', {
|
||||
method,
|
||||
credentials: 'same-origin',
|
||||
cache: 'no-store',
|
||||
signal,
|
||||
headers:
|
||||
method === 'POST'
|
||||
? {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Governance-Action': 'qa-organization',
|
||||
'Idempotency-Key': operationID ?? '',
|
||||
}
|
||||
: undefined,
|
||||
body: method === 'POST' ? '{}' : undefined,
|
||||
})
|
||||
const raw: unknown = await response.json().catch(() => null)
|
||||
if (!response.ok) {
|
||||
const error = raw && typeof raw === 'object' ? (raw as Record<string, unknown>).error : undefined
|
||||
throw new Error(typeof error === 'string' ? error : `通讯录同步请求失败(HTTP ${response.status})`)
|
||||
}
|
||||
return object(raw)
|
||||
}
|
||||
export async function fetchDirectorySync(signal?: AbortSignal) {
|
||||
return parseDirectorySync((await request('GET', undefined, signal)).data)
|
||||
}
|
||||
export async function startDirectorySync(operationID: string, signal?: AbortSignal) {
|
||||
if (!operationID || operationID.trim() !== operationID || operationID.length > 128) return bad()
|
||||
const envelope = await request('POST', operationID, signal),
|
||||
data = object(envelope.data)
|
||||
if (
|
||||
(envelope.operation !== 'created' && envelope.operation !== 'replayed') ||
|
||||
typeof data.deduplicated !== 'boolean' ||
|
||||
!text(data.job_id)
|
||||
)
|
||||
return bad()
|
||||
return { job_id: text(data.job_id), deduplicated: data.deduplicated, operation: envelope.operation }
|
||||
}
|
||||
Reference in New Issue
Block a user