feat(qaops): 增加版本化部门区域归属配置与预览
This commit is contained in:
+65
-49
@@ -4,6 +4,7 @@ import { useOptionalAuth } from './auth'
|
||||
import { fetchQARecord, fetchQARecords, fetchQAStatus, qaCellValue, qaColumns } from './qaOps'
|
||||
import type { QAQuery, QARecord, QARecordPage, QAStatus } from './qaOps'
|
||||
import './qaOps.css'
|
||||
import { QARegions } from './QARegions'
|
||||
|
||||
interface ViewProps {
|
||||
page: QARecordPage | null
|
||||
@@ -229,6 +230,7 @@ function initialDraft() {
|
||||
}
|
||||
|
||||
export function QAOperations() {
|
||||
const [view, setView] = useState<'records' | 'regions'>('records')
|
||||
const auth = useOptionalAuth()
|
||||
const canRead = auth?.identity?.role === 'admin'
|
||||
const [draft, setDraft] = useState(initialDraft)
|
||||
@@ -362,7 +364,7 @@ export function QAOperations() {
|
||||
<section className="qa-ops" aria-label="问答运营">
|
||||
<header className="qa-ops-header">
|
||||
<h2>
|
||||
问答运营 <span>/ 问答记录</span>
|
||||
问答运营 <span>/ {view === 'regions' ? '区域归属' : '问答记录'}</span>
|
||||
</h2>
|
||||
<p>真实问答只读查询;原始记录、源端判断与治理复检分开保留。</p>
|
||||
</header>
|
||||
@@ -372,55 +374,69 @@ export function QAOperations() {
|
||||
</p>
|
||||
) : (
|
||||
<>
|
||||
<form className="qa-ops-query" onSubmit={submit}>
|
||||
<label>
|
||||
开始时间
|
||||
<input
|
||||
type="datetime-local"
|
||||
required
|
||||
value={draft.start}
|
||||
onChange={(event) => setDraft({ ...draft, start: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
结束时间(不含)
|
||||
<input
|
||||
type="datetime-local"
|
||||
required
|
||||
value={draft.end}
|
||||
onChange={(event) => setDraft({ ...draft, end: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label className="qa-ops-keyword">
|
||||
关键词
|
||||
<input
|
||||
type="search"
|
||||
maxLength={200}
|
||||
placeholder="检索原始问题"
|
||||
value={draft.keyword}
|
||||
onChange={(event) => setDraft({ ...draft, keyword: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<button type="submit" disabled={loading}>
|
||||
查询
|
||||
<nav className="qa-region-tabs" aria-label="问答运营视图">
|
||||
<button type="button" aria-pressed={view === 'records'} onClick={() => setView('records')}>
|
||||
问答记录
|
||||
</button>
|
||||
</form>
|
||||
<p className="qa-ops-slice-note">
|
||||
当前切片:问答查询与覆盖复检。外语翻译、AI评估及人员映射尚未接入;空值不默认填“否”。
|
||||
</p>
|
||||
<QAOperationsView
|
||||
page={page}
|
||||
status={status}
|
||||
error={error}
|
||||
loading={loading}
|
||||
detail={detail}
|
||||
detailLoading={detailLoading}
|
||||
detailError={detailError}
|
||||
onSelect={(id) => void select(id)}
|
||||
onCloseDetail={close}
|
||||
onLoadMore={() => void loadMore()}
|
||||
onRetry={() => setRevision((value) => value + 1)}
|
||||
/>
|
||||
<button type="button" aria-pressed={view === 'regions'} onClick={() => setView('regions')}>
|
||||
区域使用
|
||||
</button>
|
||||
</nav>
|
||||
{view === 'regions' ? (
|
||||
<QARegions />
|
||||
) : (
|
||||
<>
|
||||
<form className="qa-ops-query" onSubmit={submit}>
|
||||
<label>
|
||||
开始时间
|
||||
<input
|
||||
type="datetime-local"
|
||||
required
|
||||
value={draft.start}
|
||||
onChange={(event) => setDraft({ ...draft, start: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
结束时间(不含)
|
||||
<input
|
||||
type="datetime-local"
|
||||
required
|
||||
value={draft.end}
|
||||
onChange={(event) => setDraft({ ...draft, end: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label className="qa-ops-keyword">
|
||||
关键词
|
||||
<input
|
||||
type="search"
|
||||
maxLength={200}
|
||||
placeholder="检索原始问题"
|
||||
value={draft.keyword}
|
||||
onChange={(event) => setDraft({ ...draft, keyword: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<button type="submit" disabled={loading}>
|
||||
查询
|
||||
</button>
|
||||
</form>
|
||||
<p className="qa-ops-slice-note">
|
||||
当前切片:问答查询与覆盖复检。外语翻译、AI评估及人员映射尚未接入;空值不默认填“否”。
|
||||
</p>
|
||||
<QAOperationsView
|
||||
page={page}
|
||||
status={status}
|
||||
error={error}
|
||||
loading={loading}
|
||||
detail={detail}
|
||||
detailLoading={detailLoading}
|
||||
detailError={detailError}
|
||||
onSelect={(id) => void select(id)}
|
||||
onCloseDetail={close}
|
||||
onLoadMore={() => void loadMore()}
|
||||
onRetry={() => setRevision((value) => value + 1)}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { RegionMappingView } from './QARegions'
|
||||
import { parseOrganization } from './qaOrg'
|
||||
const state = parseOrganization({
|
||||
state: 'ready',
|
||||
directory_id: 'd',
|
||||
directory_source: 'weekly_report_snapshot',
|
||||
directory_synced_at: '2026-09-27T14:03:44Z',
|
||||
revision_id: 'r',
|
||||
updated_by: 'fixture-admin',
|
||||
updated_at: null,
|
||||
rules: [{ department_id: 'a', region: 'APAC' }],
|
||||
view: {
|
||||
departments: [
|
||||
{
|
||||
id: 'a',
|
||||
parent_id: '',
|
||||
name: '<script>test</script>',
|
||||
path: '示例公司/<script>test</script>',
|
||||
region: 'APAC',
|
||||
explicit: true,
|
||||
inherited_from: '',
|
||||
},
|
||||
],
|
||||
regions: { 国内工业: 0, APAC: 1, EMEA: 0, America: 0 },
|
||||
total_people: 2,
|
||||
unassigned_people: 0,
|
||||
conflicting_people: 1,
|
||||
missing_departments: [],
|
||||
},
|
||||
})
|
||||
const props = {
|
||||
state,
|
||||
preview: null,
|
||||
rules: state.rules,
|
||||
selectedRegion: 'APAC',
|
||||
search: '',
|
||||
busy: false,
|
||||
error: '',
|
||||
notice: '',
|
||||
dirty: false,
|
||||
onRegion: vi.fn(),
|
||||
onSearch: vi.fn(),
|
||||
onToggle: vi.fn(),
|
||||
onPreview: vi.fn(),
|
||||
onSave: vi.fn(),
|
||||
onReload: vi.fn(),
|
||||
onRemoveMissing: vi.fn(),
|
||||
}
|
||||
describe('region mapping workbench', () => {
|
||||
it('shows a dated historical roster, conflicts and no invented performance verdict', () => {
|
||||
const html = renderToStaticMarkup(<RegionMappingView {...props} />)
|
||||
expect(html).toContain('历史快照')
|
||||
expect(html).toContain('跨区域待核对:1')
|
||||
expect(html).toContain('配置区域归属不等于纳入绩效考核')
|
||||
expect(html).not.toContain('<script>test</script>')
|
||||
expect(html).not.toContain('达标率')
|
||||
expect(html).toMatch(/disabled=""[^>]*>保存归属/)
|
||||
})
|
||||
it('requires a preview of the current draft before enabling save', () => {
|
||||
const html = renderToStaticMarkup(<RegionMappingView {...props} dirty preview={state} />)
|
||||
expect(html).toContain('预览结果(未保存)')
|
||||
expect(html).not.toMatch(/disabled=""[^>]*>保存归属/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,246 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { changeRegionRule, fetchOrganization, previewOrganization, regionNames, saveOrganization } from './qaOrg'
|
||||
import type { Organization, RegionRule } from './qaOrg'
|
||||
import './qaRegions.css'
|
||||
interface Props {
|
||||
state: Organization | null
|
||||
preview: Organization | null
|
||||
rules: RegionRule[]
|
||||
selectedRegion: string
|
||||
search: string
|
||||
busy: boolean
|
||||
error: string
|
||||
notice: string
|
||||
dirty: boolean
|
||||
onRegion: (value: string) => void
|
||||
onSearch: (value: string) => void
|
||||
onToggle: (id: string, checked: boolean) => void
|
||||
onRemoveMissing: (id: string) => void
|
||||
onPreview: () => void
|
||||
onSave: () => void
|
||||
onReload: () => void
|
||||
}
|
||||
export function RegionMappingView(p: Props) {
|
||||
const display = p.preview ?? p.state
|
||||
const available = p.state?.state === 'ready'
|
||||
const departments =
|
||||
display?.view.departments.filter((d) => d.path.toLowerCase().includes(p.search.toLowerCase())) ?? []
|
||||
return (
|
||||
<section className="qa-regions" aria-label="区域归属配置">
|
||||
<div className="qa-region-heading">
|
||||
<h3>区域归属配置</h3>
|
||||
<button type="button" disabled={p.busy} onClick={p.onReload}>
|
||||
{p.dirty ? '放弃修改并重读' : '重新读取'}
|
||||
</button>
|
||||
</div>
|
||||
{p.state?.directory_synced_at ? (
|
||||
<p className="qa-ops-muted">
|
||||
{p.state.directory_source === 'weekly_report_snapshot' ? '历史快照' : '全量同步快照'} · 通讯录同步时间:
|
||||
{new Date(p.state.directory_synced_at).toLocaleString('zh-CN', { hour12: false })} · 配置版本:
|
||||
{p.state.revision_id || '尚未保存'}
|
||||
</p>
|
||||
) : null}
|
||||
<p className="qa-ops-muted">
|
||||
父部门包含子部门,子部门单独配置优先。配置区域归属不等于纳入绩效考核;跨区域多部门人员先待核对。
|
||||
</p>
|
||||
{p.error ? (
|
||||
<p role="alert" className="error-banner">
|
||||
{p.error}
|
||||
</p>
|
||||
) : null}
|
||||
{p.notice ? <p role="status">{p.notice}</p> : null}
|
||||
{!p.state ? (
|
||||
<p role="status">{p.busy ? '正在读取通讯录…' : '尚未读取通讯录'}</p>
|
||||
) : !available ? (
|
||||
<p className="qa-ops-empty">尚无完整通讯录快照。先接入已验证的全量同步结果,不能使用按需人员缓存或编造部门。</p>
|
||||
) : (
|
||||
<>
|
||||
<div className="qa-region-filters">
|
||||
<label>
|
||||
选择区域
|
||||
<select value={p.selectedRegion} disabled={p.busy} onChange={(e) => p.onRegion(e.target.value)}>
|
||||
{regionNames.map((r) => (
|
||||
<option key={r}>{r}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
搜索完整部门路径
|
||||
<input
|
||||
type="search"
|
||||
value={p.search}
|
||||
onChange={(e) => p.onSearch(e.target.value)}
|
||||
placeholder="输入部门名称或完整路径"
|
||||
/>
|
||||
</label>
|
||||
<button type="button" disabled={p.busy || !p.dirty} onClick={p.onPreview}>
|
||||
预览影响
|
||||
</button>
|
||||
<button type="button" disabled={p.busy || !p.dirty || !p.preview} onClick={p.onSave}>
|
||||
保存归属
|
||||
</button>
|
||||
</div>
|
||||
{display ? (
|
||||
<div className="qa-region-preview" aria-label="归属人数预览">
|
||||
<strong>{p.preview ? '预览结果(未保存)' : '已保存归属'}</strong>
|
||||
<span>通讯录人数:{display.view.total_people}</span>
|
||||
{regionNames.map((r) => (
|
||||
<span key={r}>
|
||||
{r}:{display.view.regions[r]}
|
||||
</span>
|
||||
))}
|
||||
<span>未归属:{display.view.unassigned_people}</span>
|
||||
<span>跨区域待核对:{display.view.conflicting_people}</span>
|
||||
</div>
|
||||
) : null}
|
||||
{p.dirty && !p.preview ? (
|
||||
<p className="qa-ops-muted">勾选已修改,人数仍为已保存结果;请预览后保存。</p>
|
||||
) : null}
|
||||
{display?.view.missing_departments.length ? (
|
||||
<div role="alert" className="qa-ops-warning">
|
||||
部分已配置部门不在当前通讯录中,原规则保留,需核对:
|
||||
{display.view.missing_departments.map((id) => (
|
||||
<span key={id}>
|
||||
{id}{' '}
|
||||
<button type="button" disabled={p.busy} onClick={() => p.onRemoveMissing(id)}>
|
||||
移除此规则
|
||||
</button>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
<div className="qa-region-tree-scroll">
|
||||
<table className="qa-region-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>直接配置</th>
|
||||
<th>完整部门路径</th>
|
||||
<th>{p.dirty && !p.preview ? '已保存归属' : '生效归属'}</th>
|
||||
<th>依据</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{departments.map((d) => (
|
||||
<tr key={d.id}>
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
aria-label={`将 ${d.path} 直接归入 ${p.selectedRegion}`}
|
||||
checked={p.rules.some((r) => r.department_id === d.id && r.region === p.selectedRegion)}
|
||||
disabled={p.busy}
|
||||
onChange={(e) => p.onToggle(d.id, e.target.checked)}
|
||||
/>
|
||||
</td>
|
||||
<td>{d.path}</td>
|
||||
<td>{d.region || '未归属'}</td>
|
||||
<td>{d.explicit ? '直接配置' : d.inherited_from ? '继承上级' : '未配置'}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{!departments.length ? <p>没有匹配的部门路径</p> : null}
|
||||
</div>
|
||||
<p className="qa-ops-muted">
|
||||
此处仅预览通讯录归属人数;2.0 使用次数、考核名单与绩效统计尚未接入。不会将未知在职状态当作已确认在职人数。
|
||||
</p>
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
function signature(rules: RegionRule[]) {
|
||||
return JSON.stringify([...rules].sort((a, b) => a.department_id.localeCompare(b.department_id)))
|
||||
}
|
||||
export function QARegions() {
|
||||
const [state, setState] = useState<Organization | null>(null),
|
||||
[preview, setPreview] = useState<Organization | null>(null),
|
||||
[rules, setRules] = useState<RegionRule[]>([])
|
||||
const [region, setRegion] = useState<string>(regionNames[0]),
|
||||
[search, setSearch] = useState(''),
|
||||
[busy, setBusy] = useState(false),
|
||||
[error, setError] = useState(''),
|
||||
[notice, setNotice] = useState('')
|
||||
const request = useRef<AbortController | null>(null)
|
||||
const accept = (value: Organization) => {
|
||||
setState(value)
|
||||
setRules(value.rules)
|
||||
setPreview(null)
|
||||
}
|
||||
const load = async () => {
|
||||
request.current?.abort()
|
||||
const controller = new AbortController()
|
||||
request.current = controller
|
||||
setBusy(true)
|
||||
setError('')
|
||||
setNotice('')
|
||||
setPreview(null)
|
||||
try {
|
||||
const value = await fetchOrganization(controller.signal)
|
||||
if (!controller.signal.aborted) accept(value)
|
||||
} catch (e) {
|
||||
if (!controller.signal.aborted) setError(e instanceof Error ? e.message : '读取失败')
|
||||
} finally {
|
||||
if (!controller.signal.aborted) setBusy(false)
|
||||
}
|
||||
}
|
||||
useEffect(() => {
|
||||
void load()
|
||||
return () => request.current?.abort()
|
||||
}, [])
|
||||
const edit = (next: RegionRule[]) => {
|
||||
setRules(next)
|
||||
setPreview(null)
|
||||
setNotice('')
|
||||
setError('')
|
||||
}
|
||||
const act = async (save: boolean) => {
|
||||
if (!state || busy || (save && !preview)) return
|
||||
const controller = new AbortController()
|
||||
request.current?.abort()
|
||||
request.current = controller
|
||||
setBusy(true)
|
||||
setError('')
|
||||
setNotice('')
|
||||
try {
|
||||
const value = await (save
|
||||
? saveOrganization(state, rules, controller.signal)
|
||||
: previewOrganization(state, rules, controller.signal))
|
||||
if (!controller.signal.aborted) {
|
||||
if (save) {
|
||||
const readback = await fetchOrganization(controller.signal)
|
||||
if (controller.signal.aborted) return
|
||||
accept(readback)
|
||||
if (readback.revision_id !== value.revision_id) throw new Error('保存后配置再次变化,已读取最新版本,请核对')
|
||||
setNotice('区域归属已保存并重新读取')
|
||||
} else setPreview(value)
|
||||
}
|
||||
} catch (e) {
|
||||
if (!controller.signal.aborted) {
|
||||
setPreview(null)
|
||||
setError(e instanceof Error ? e.message : '操作失败')
|
||||
}
|
||||
} finally {
|
||||
if (!controller.signal.aborted) setBusy(false)
|
||||
}
|
||||
}
|
||||
return (
|
||||
<RegionMappingView
|
||||
state={state}
|
||||
preview={preview}
|
||||
rules={rules}
|
||||
selectedRegion={region}
|
||||
search={search}
|
||||
busy={busy}
|
||||
error={error}
|
||||
notice={notice}
|
||||
dirty={Boolean(state && signature(rules) !== signature(state.rules))}
|
||||
onRegion={setRegion}
|
||||
onSearch={setSearch}
|
||||
onToggle={(id, checked) => edit(changeRegionRule(rules, id, region, checked))}
|
||||
onRemoveMissing={(id) => edit(rules.filter((r) => r.department_id !== id))}
|
||||
onPreview={() => void act(false)}
|
||||
onSave={() => void act(true)}
|
||||
onReload={() => void load()}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { fetchOrganization, parseOrganization, changeRegionRule } from './qaOrg'
|
||||
|
||||
export const directoryFixture = {
|
||||
state: 'ready',
|
||||
directory_id: 'dir-fixture',
|
||||
directory_source: 'weekly_report_snapshot',
|
||||
directory_synced_at: '2026-09-27T14:03:44Z',
|
||||
revision_id: '',
|
||||
updated_by: '',
|
||||
updated_at: null,
|
||||
rules: [],
|
||||
view: {
|
||||
departments: [
|
||||
{
|
||||
id: 'root',
|
||||
parent_id: '',
|
||||
name: '示例公司',
|
||||
path: '示例公司',
|
||||
region: '',
|
||||
explicit: false,
|
||||
inherited_from: '',
|
||||
},
|
||||
],
|
||||
regions: { 国内工业: 0, APAC: 0, EMEA: 0, America: 0 },
|
||||
total_people: 1,
|
||||
unassigned_people: 1,
|
||||
conflicting_people: 0,
|
||||
missing_departments: [],
|
||||
},
|
||||
}
|
||||
describe('department region configuration', () => {
|
||||
it('parses an honest dated snapshot without exposing employee identities', () => {
|
||||
const state = parseOrganization(directoryFixture)
|
||||
expect(state.directory_source).toBe('weekly_report_snapshot')
|
||||
expect(state.view.unassigned_people).toBe(1)
|
||||
expect(() =>
|
||||
parseOrganization({ ...directoryFixture, view: { ...directoryFixture.view, total_people: 0 } })
|
||||
).toThrow()
|
||||
expect(() =>
|
||||
parseOrganization({ ...directoryFixture, view: { ...directoryFixture.view, departments: [{}, {}] } })
|
||||
).toThrow()
|
||||
})
|
||||
it('changes only a direct stable ID rule, leaving sibling and child overrides alone', () => {
|
||||
const rules = [
|
||||
{ department_id: 'parent', region: '国内工业' },
|
||||
{ department_id: 'child', region: 'APAC' },
|
||||
]
|
||||
expect(changeRegionRule(rules, 'parent', 'EMEA', true)).toEqual([
|
||||
{ department_id: 'child', region: 'APAC' },
|
||||
{ department_id: 'parent', region: 'EMEA' },
|
||||
])
|
||||
expect(changeRegionRule(rules, 'parent', '国内工业', false)).toEqual([{ department_id: 'child', region: 'APAC' }])
|
||||
})
|
||||
it('surfaces store errors rather than displaying an empty directory', async () => {
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn().mockResolvedValue({ ok: false, status: 503, json: async () => ({ error: '通讯录不可用' }) })
|
||||
)
|
||||
await expect(fetchOrganization()).rejects.toThrow('通讯录不可用')
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
)
|
||||
@@ -0,0 +1,124 @@
|
||||
.qa-regions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
gap: 12px;
|
||||
flex: 1;
|
||||
}
|
||||
.qa-region-heading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
}
|
||||
.qa-region-heading h3 {
|
||||
margin: 0;
|
||||
}
|
||||
.qa-region-filters {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: end;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.qa-region-filters label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
}
|
||||
.qa-region-filters label:nth-child(2) {
|
||||
flex: 1;
|
||||
min-width: 220px;
|
||||
}
|
||||
.qa-region-filters input,
|
||||
.qa-region-filters select {
|
||||
min-height: 36px;
|
||||
padding: 6px 10px;
|
||||
border: 1px solid var(--line, #d8e1eb);
|
||||
border-radius: 6px;
|
||||
background: var(--panel, #fff);
|
||||
color: inherit;
|
||||
}
|
||||
.qa-region-preview {
|
||||
display: flex;
|
||||
gap: 8px 18px;
|
||||
flex-wrap: wrap;
|
||||
padding: 12px 14px;
|
||||
border: 1px solid var(--line, #d8e1eb);
|
||||
background: var(--panel-muted, #f3f7fc);
|
||||
border-radius: 6px;
|
||||
}
|
||||
.qa-region-tree-scroll {
|
||||
min-height: 120px;
|
||||
overflow: auto;
|
||||
flex: 1;
|
||||
border: 1px solid var(--line, #d8e1eb);
|
||||
border-radius: 6px;
|
||||
}
|
||||
.qa-region-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
text-align: left;
|
||||
font-size: 13px;
|
||||
}
|
||||
.qa-region-table th,
|
||||
.qa-region-table td {
|
||||
padding: 10px 12px;
|
||||
border-bottom: 1px solid var(--line, #d8e1eb);
|
||||
}
|
||||
.qa-region-table th {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
background: var(--panel, #fff);
|
||||
white-space: nowrap;
|
||||
}
|
||||
.qa-region-table td:nth-child(2) {
|
||||
min-width: 240px;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.qa-region-table input[type='checkbox'] {
|
||||
width: 17px;
|
||||
height: 17px;
|
||||
cursor: pointer;
|
||||
accent-color: #1765c1;
|
||||
}
|
||||
.qa-region-tabs {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
.qa-region-tabs button[aria-pressed='true'] {
|
||||
background: #e5efff;
|
||||
border-color: #8db8f0;
|
||||
color: #154a88;
|
||||
}
|
||||
.qa-regions button:disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.5;
|
||||
}
|
||||
.qa-regions p {
|
||||
margin: 0;
|
||||
}
|
||||
.qa-ops-warning > span {
|
||||
display: inline-block;
|
||||
margin: 4px 8px;
|
||||
}
|
||||
.qa-regions :focus-visible {
|
||||
outline: 2px solid #1765c1;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
@media (max-width: 700px) {
|
||||
.qa-region-filters {
|
||||
align-items: stretch;
|
||||
}
|
||||
.qa-region-filters label {
|
||||
flex: 1;
|
||||
}
|
||||
.qa-region-tree-scroll {
|
||||
min-height: 300px;
|
||||
}
|
||||
.qa-region-preview {
|
||||
font-size: 13px;
|
||||
}
|
||||
.qa-region-heading {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user