feat(qa-ops): 支持自定义区域与分级部门批量归属
build / build (push) Successful in 1m8s

This commit is contained in:
姚章浩
2026-10-18 21:50:29 +08:00
parent 8835a473dd
commit 76671e0784
33 changed files with 865 additions and 100 deletions
+53 -7
View File
@@ -1,4 +1,45 @@
export const regionNames = ['国内工业', 'APAC', 'EMEA', 'America'] as const
export interface RegionDefinition {
id: string
name: string
}
export const defaultRegions = (): RegionDefinition[] => regionNames.map((name) => ({ id: name, name }))
const reservedRegions = [
'unassigned',
'cross_region',
'unmatched',
'identity_conflict',
'no_identity',
'directory_missing',
'__proto__',
'constructor',
'prototype',
]
export function parseRegionDefinitions(raw: unknown): RegionDefinition[] {
if (!Array.isArray(raw) || raw.length > 100) return invalid()
const ids = new Set<string>(),
names = new Set<string>()
return raw.map((item) => {
const r = obj(item),
id = str(r.id),
name = str(r.name)
for (const value of [id, name])
if (
!value ||
value.trim() !== value ||
new TextEncoder().encode(value).length > 256 ||
/[\x00\r\n]/.test(value) ||
reservedRegions.includes(value)
)
return invalid()
if (ids.has(id) || names.has(name)) return invalid()
ids.add(id)
names.add(name)
return { id, name }
})
}
export const organizationRegions = (state: Organization | null): RegionDefinition[] =>
state?.regions ?? defaultRegions()
export interface RegionRule {
department_id: string
region: string
@@ -13,6 +54,7 @@ export interface DepartmentRegion {
inherited_from: string
}
export interface Organization {
regions?: RegionDefinition[]
state: 'empty' | 'ready'
directory_id: string
directory_source: string
@@ -49,7 +91,9 @@ export function instant(value: unknown): string | null {
export function parseOrganization(value: unknown): Organization {
const s = obj(value),
v = obj(s.view),
regions = obj(v.regions)
regions = obj(v.regions),
definitions = s.regions === undefined ? defaultRegions() : parseRegionDefinitions(s.regions),
regionIDs = definitions.map((r) => r.id)
if (
(s.state !== 'empty' && s.state !== 'ready') ||
!Array.isArray(s.rules) ||
@@ -60,13 +104,12 @@ export function parseOrganization(value: unknown): Organization {
const rules = s.rules.map((raw) => {
const r = obj(raw)
const region = str(r.region)
if (!regionNames.some((x) => x === region)) return invalid()
if (!regionIDs.includes(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()
if (typeof d.explicit !== 'boolean' || (d.region !== '' && !regionIDs.includes(String(d.region)))) return invalid()
return {
id: str(d.id),
parent_id: str(d.parent_id),
@@ -86,9 +129,12 @@ export function parseOrganization(value: unknown): Organization {
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.keys(regions).length !== regionIDs.length || Object.keys(regions).some((id) => !regionIDs.includes(id)))
return invalid()
const regionCounts = Object.fromEntries(regionIDs.map((r) => [r, count(regions[r])]))
if (Object.values(regionCounts).reduce((a, b) => a + b, 0) + unassigned + conflicting !== total) return invalid()
const result: Organization = {
regions: definitions,
state: s.state,
directory_id: str(s.directory_id),
directory_source: str(s.directory_source),
@@ -140,13 +186,13 @@ export const previewOrganization = (state: Organization, rules: RegionRule[], si
requestOrganization(
'/preview',
'POST',
{ directory_id: state.directory_id, base_revision: state.revision_id, rules },
{ directory_id: state.directory_id, base_revision: state.revision_id, regions: organizationRegions(state), 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 },
{ directory_id: state.directory_id, base_revision: state.revision_id, regions: organizationRegions(state), rules },
signal
)