feat(qaops): 增加版本化部门区域归属配置与预览
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
export const regionNames = ['国内工业', 'APAC', 'EMEA', 'America'] as const
|
||||
export interface RegionRule {
|
||||
department_id: string
|
||||
region: string
|
||||
}
|
||||
export interface DepartmentRegion {
|
||||
id: string
|
||||
parent_id: string
|
||||
name: string
|
||||
path: string
|
||||
region: string
|
||||
explicit: boolean
|
||||
inherited_from: string
|
||||
}
|
||||
export interface Organization {
|
||||
state: 'empty' | 'ready'
|
||||
directory_id: string
|
||||
directory_source: string
|
||||
directory_synced_at: string | null
|
||||
revision_id: string
|
||||
updated_by: string
|
||||
updated_at: string | null
|
||||
rules: RegionRule[]
|
||||
view: {
|
||||
departments: DepartmentRegion[]
|
||||
regions: Record<string, number>
|
||||
total_people: number
|
||||
unassigned_people: number
|
||||
conflicting_people: number
|
||||
missing_departments: string[]
|
||||
}
|
||||
}
|
||||
function invalid(): never {
|
||||
throw new Error('通讯录或区域配置响应格式无效')
|
||||
}
|
||||
function obj(value: unknown): Record<string, unknown> {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) return invalid()
|
||||
return value as Record<string, unknown>
|
||||
}
|
||||
function str(value: unknown): string {
|
||||
return typeof value === 'string' ? value : invalid()
|
||||
}
|
||||
function count(value: unknown): number {
|
||||
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : invalid()
|
||||
}
|
||||
function instant(value: unknown): string | null {
|
||||
return value === null ? null : typeof value === 'string' && Number.isFinite(Date.parse(value)) ? value : invalid()
|
||||
}
|
||||
export function parseOrganization(value: unknown): Organization {
|
||||
const s = obj(value),
|
||||
v = obj(s.view),
|
||||
regions = obj(v.regions)
|
||||
if (
|
||||
(s.state !== 'empty' && s.state !== 'ready') ||
|
||||
!Array.isArray(s.rules) ||
|
||||
!Array.isArray(v.departments) ||
|
||||
!Array.isArray(v.missing_departments)
|
||||
)
|
||||
return invalid()
|
||||
const rules = s.rules.map((raw) => {
|
||||
const r = obj(raw)
|
||||
const region = str(r.region)
|
||||
if (!regionNames.some((x) => x === region)) return invalid()
|
||||
return { department_id: str(r.department_id), region }
|
||||
})
|
||||
const departments = v.departments.map((raw) => {
|
||||
const d = obj(raw)
|
||||
if (typeof d.explicit !== 'boolean' || (d.region !== '' && !regionNames.some((x) => x === d.region)))
|
||||
return invalid()
|
||||
return {
|
||||
id: str(d.id),
|
||||
parent_id: str(d.parent_id),
|
||||
name: str(d.name),
|
||||
path: str(d.path),
|
||||
region: str(d.region),
|
||||
explicit: d.explicit,
|
||||
inherited_from: str(d.inherited_from),
|
||||
}
|
||||
})
|
||||
if (
|
||||
departments.some((d) => !d.id || !d.path) ||
|
||||
new Set(departments.map((d) => d.id)).size !== departments.length ||
|
||||
new Set(rules.map((r) => r.department_id)).size !== rules.length
|
||||
)
|
||||
return invalid()
|
||||
const total = count(v.total_people),
|
||||
unassigned = count(v.unassigned_people),
|
||||
conflicting = count(v.conflicting_people)
|
||||
const regionCounts = Object.fromEntries(regionNames.map((r) => [r, count(regions[r])]))
|
||||
if (Object.values(regionCounts).reduce((a, b) => a + b, 0) + unassigned + conflicting !== total) return invalid()
|
||||
const result: Organization = {
|
||||
state: s.state,
|
||||
directory_id: str(s.directory_id),
|
||||
directory_source: str(s.directory_source),
|
||||
directory_synced_at: instant(s.directory_synced_at),
|
||||
revision_id: str(s.revision_id),
|
||||
updated_by: str(s.updated_by),
|
||||
updated_at: instant(s.updated_at),
|
||||
rules,
|
||||
view: {
|
||||
departments,
|
||||
regions: regionCounts,
|
||||
total_people: total,
|
||||
unassigned_people: unassigned,
|
||||
conflicting_people: conflicting,
|
||||
missing_departments: v.missing_departments.map(str),
|
||||
},
|
||||
}
|
||||
if (s.state === 'ready' && (!result.directory_id || !result.directory_synced_at || !departments.length || !total))
|
||||
return invalid()
|
||||
return result
|
||||
}
|
||||
export function changeRegionRule(rules: RegionRule[], id: string, region: string, checked: boolean): RegionRule[] {
|
||||
return [...rules.filter((r) => r.department_id !== id), ...(checked ? [{ department_id: id, region }] : [])]
|
||||
}
|
||||
async function requestOrganization(
|
||||
path: string,
|
||||
method: string,
|
||||
body: unknown,
|
||||
signal?: AbortSignal
|
||||
): Promise<Organization> {
|
||||
const response = await fetch(`/api/v1/qa-ops/organization${path}`, {
|
||||
method,
|
||||
credentials: 'same-origin',
|
||||
cache: 'no-store',
|
||||
signal,
|
||||
headers:
|
||||
method === 'GET' ? undefined : { 'Content-Type': 'application/json', 'X-Governance-Action': 'qa-organization' },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
})
|
||||
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 parseOrganization(obj(raw).data)
|
||||
}
|
||||
export const fetchOrganization = (signal?: AbortSignal) => requestOrganization('', 'GET', undefined, signal)
|
||||
export const previewOrganization = (state: Organization, rules: RegionRule[], signal?: AbortSignal) =>
|
||||
requestOrganization(
|
||||
'/preview',
|
||||
'POST',
|
||||
{ directory_id: state.directory_id, base_revision: state.revision_id, rules },
|
||||
signal
|
||||
)
|
||||
export const saveOrganization = (state: Organization, rules: RegionRule[], signal?: AbortSignal) =>
|
||||
requestOrganization(
|
||||
'/regions',
|
||||
'PUT',
|
||||
{ directory_id: state.directory_id, base_revision: state.revision_id, rules },
|
||||
signal
|
||||
)
|
||||
Reference in New Issue
Block a user