feat(qaops): 接入问答组织补全与区域使用统计
This commit is contained in:
+93
-2
@@ -17,7 +17,59 @@ export const qaColumns = [
|
||||
] as const
|
||||
|
||||
export type QAColumn = (typeof qaColumns)[number]['key']
|
||||
export interface QAOrganizationVersion {
|
||||
state: 'ready' | 'empty'
|
||||
directory_id: string
|
||||
directory_source: string
|
||||
synced_at: string | null
|
||||
revision_id: string
|
||||
basis: 'current_directory'
|
||||
}
|
||||
export interface QARecordOrganization {
|
||||
version: QAOrganizationVersion
|
||||
state: string
|
||||
departments: string[]
|
||||
region: string
|
||||
source_department: string
|
||||
source_region: string
|
||||
department_origin: string
|
||||
region_origin: string
|
||||
}
|
||||
export function parseQAOrganizationVersion(value: unknown): QAOrganizationVersion {
|
||||
const v = object(value)
|
||||
if (!['ready', 'empty'].includes(text(v.state)) || v.basis !== 'current_directory') return invalid()
|
||||
const result = {
|
||||
state: v.state as QAOrganizationVersion['state'],
|
||||
directory_id: text(v.directory_id),
|
||||
directory_source: text(v.directory_source),
|
||||
synced_at: v.synced_at === null ? null : instant(v.synced_at),
|
||||
revision_id: text(v.revision_id),
|
||||
basis: 'current_directory' as const,
|
||||
}
|
||||
if (
|
||||
result.state === 'ready' &&
|
||||
(!result.directory_id ||
|
||||
!result.synced_at ||
|
||||
!['weekly_report_live', 'weekly_report_snapshot'].includes(result.directory_source))
|
||||
)
|
||||
return invalid()
|
||||
if (result.state === 'empty' && (result.directory_id || result.revision_id || result.synced_at !== null))
|
||||
return invalid()
|
||||
return result
|
||||
}
|
||||
export function mergeQAPages(current: QARecordPage, next: QARecordPage): QARecordPage {
|
||||
if (
|
||||
current.organization?.directory_id !== next.organization?.directory_id ||
|
||||
current.organization?.revision_id !== next.organization?.revision_id
|
||||
)
|
||||
throw new Error('通讯录或区域配置已变化,请重新查询,不能混合不同版本的分页结果')
|
||||
return {
|
||||
...next,
|
||||
items: [...current.items, ...next.items.filter((item) => !current.items.some((old) => old.id === item.id))],
|
||||
}
|
||||
}
|
||||
export interface QARecord {
|
||||
organization?: QARecordOrganization
|
||||
id: string
|
||||
status: string
|
||||
question: string
|
||||
@@ -41,6 +93,7 @@ export interface QARecord {
|
||||
language?: string
|
||||
}
|
||||
export interface QARecordPage {
|
||||
organization?: QAOrganizationVersion
|
||||
items: QARecord[]
|
||||
columns: QAColumn[]
|
||||
total: number
|
||||
@@ -121,6 +174,36 @@ export function parseQARecord(value: unknown): QARecord {
|
||||
if (typeof row.is_correct !== 'boolean' && row.is_correct !== null) return invalid()
|
||||
item.is_correct = row.is_correct
|
||||
}
|
||||
if (row.organization !== undefined) {
|
||||
const o = object(row.organization)
|
||||
if (
|
||||
!Array.isArray(o.departments) ||
|
||||
![
|
||||
'matched',
|
||||
'unassigned',
|
||||
'cross_region',
|
||||
'unmatched',
|
||||
'identity_conflict',
|
||||
'no_identity',
|
||||
'directory_missing',
|
||||
].includes(text(o.state))
|
||||
)
|
||||
return invalid()
|
||||
const origin = (v: unknown) => {
|
||||
const s = text(v)
|
||||
return ['source_snapshot', 'current_directory', 'unavailable'].includes(s) ? s : invalid()
|
||||
}
|
||||
item.organization = {
|
||||
version: parseQAOrganizationVersion(o.version),
|
||||
state: text(o.state),
|
||||
departments: o.departments.map(text),
|
||||
region: text(o.region),
|
||||
source_department: text(o.source_department),
|
||||
source_region: text(o.source_region),
|
||||
department_origin: origin(o.department_origin),
|
||||
region_origin: origin(o.region_origin),
|
||||
}
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
@@ -137,10 +220,18 @@ export function parseQARecordPage(value: unknown): QARecordPage {
|
||||
return invalid()
|
||||
if (new Set(items.map(({ id }) => id)).size !== items.length || new Set(columns).size !== columns.length)
|
||||
return invalid()
|
||||
return { items, columns, total, has_more: page.has_more, next_cursor: nextCursor, as_of: instant(page.as_of) }
|
||||
return {
|
||||
items,
|
||||
columns,
|
||||
total,
|
||||
has_more: page.has_more,
|
||||
next_cursor: nextCursor,
|
||||
as_of: instant(page.as_of),
|
||||
...(page.organization === undefined ? {} : { organization: parseQAOrganizationVersion(page.organization) }),
|
||||
}
|
||||
}
|
||||
|
||||
async function getPayload(path: string, signal?: AbortSignal): Promise<unknown> {
|
||||
export async function getPayload(path: string, signal?: AbortSignal): Promise<unknown> {
|
||||
const response = await fetch(path, { credentials: 'same-origin', cache: 'no-store', signal })
|
||||
const raw: unknown = await response.json().catch(() => null)
|
||||
if (!response.ok) {
|
||||
|
||||
Reference in New Issue
Block a user