搜尋開發指南

試試「多租戶」、「啟動」或「outbox」。
搜尋僅使用本站文件,不會傳送至外部服務。

文件目錄

NEXUSPRO / HANDBOOK

組織與員工

從組織樹、職位目錄到員工生命週期與在職分析,沿著目前已存在的 HR 契約閱讀資料範圍。

內容核對 2026-10-05·圖文指南

需要核對上游同步欄位、主管關係、離職歷史與排程?請讀獨立的 eHRMS 同步板塊(2026-10-05 核對)。

本章目標與前置條件

讀完後,你應能找到員工、組織、職位、上游同步與在職分析的程式碼,並知道每個操作的資料範圍。前置條件是能閱讀 REST API、UUID、分頁與基本組織樹;不需要先接觸 eHRMS 帳號。

功能現況

區塊目前來源可核實的功能前端入口與現況
員工list(q、status、include_offboarded、org_unit_id、category)、create、detail、update、profile、stats、leave-records、transitions、password-reset、activate-accounts、bulk-reassign;沒有 delete,離職與復聘走 transitions/workspace/employees → features/hr-employees:列表、詳情、profile、請假紀錄、帳號開通、CSV 匯出與 source-sync 開關,附多組元件測試
組織org chart(含上游 source_hierarchy)、org units 的 list/create/update(沒有 delete,以 closed 關閉),以及員工掛接與批次改派/hr/org → features/hr-organization:樹狀轉換、上游層級顯示與 SWR mutation
職位positions 的 list/get/create/update/delete;停用透過 status=disabled/hr/positions → features/hr-positions:表格、編輯、刪除、cache revalidation,以及送出 dry_run=false 的「同步 eHRMS」按鈕:一按就是正式同步,套用整個 HR 目錄(職位、組織、員工、主管與退場),不只職位
上游同步手動 POST /v1/hr/ehrms/sync(預設 dry-run);租戶排程開關 GET/PUT /v1/hr/source-sync 與 POST /v1/hr/source-sync/initialize(正式寫入)/workspace/employees 的同步開關與初始化、/hr/positions 的正式同步;真實上游連線仍需受控環境
分析/hr/turnover/monthly 與 /hr/turnover/annual/workspace/turnover → features/hr-turnover:月份/年度查詢與未來期間防呆

HR 路由都註冊在 internal/api/v1/routes.go 的 registerHRRoutes。每條路由的 permission point、risk、tenant_wide 與 target_employee 以 internal/route/testdata/registry.golden 為準;api/openapi.yaml 只有部分端點在描述文字提到權限,不能當依據。HR 路由分成兩類:

  • tenant-wide:org chart、org units、positions、turnover、/hr/ehrms/sync、source-sync,以及 bulk-reassign(它沒有逐員工的範圍判定,只能以 tenant-wide 收斂)。
  • 依資料範圍過濾(tenant_wide=false):員工 list、stats、create、activate-accounts,以及所有帶 :employeeId 的路由;單筆路由另外帶 TargetEmployee。因此不能把「持有 hr.employee.read」誤讀成可以讀全租戶。

核心概念

組織與職位是租戶治理資料

hr.org_unit、hr.position、hr.employee 是授權資源鍵;實際資料表是 public.org_units、public.positions、public.employees,都在 tenant transaction 內讀寫。組織樹的 parent_id 與推導出的 path 決定 department/department subtree 資料範圍,重掛節點不是單純的 UI 排序。

PUT /v1/hr/org-units/:orgUnitId 是整筆替換:name、closed、show_in_org_chart 必填且不得為 null;parent_id、name_en、code、manager_position_id、source 也必須出現在 body,但可以明確傳 null(代表清空),少帶任何一個都回 422;未宣告的鍵會被拒。它在 org tree lock 下於同一 transaction 改寫整棵子樹的 path,沒有版本欄位或 CAS。組織沒有 delete,closed=true 會向下傳染,也不能把節點搬到自己的後代之下。職位的 PUT 同樣是整筆替換、沒有版本(name、status 必填,name_en、level、sort_order、org_unit_id 必須出現但可為 null):刪除是 high-risk,帶上游 source(如 eHRMS)時回 409 position_upstream_source,仍被組織的 manager_position_id 或員工/任職歷程的 position_id 引用時回 409 position_in_use;停用(status=disabled)則保留歷史引用。HR 裡有樂觀鎖的只有 employee profile 與 source-sync 開關(兩者都用 expected_version)。

員工資料分成 identity、profile 與 assignment

Employee 是 HR 聚合的主體,profile 端點處理可編輯欄位與 profile contract(PUT .../profile 必須帶 expected_version),assignment 及生命週期操作則由 service 的 transition 規則處理;離職、復聘都走 POST .../transitions,復聘會建立新員工列並回 201。

狀態只能經 transitions 改變:建檔只接受 onboarding(預設)、probation、active,PUT /v1/hr/employees/:employeeId 不接受 status,已離職列的一般更新、profile 更新與 password-reset 都回 409 employee_offboarded(30101),bulk-reassign 遇到離職者則以 422(employee_ids[n]/offboarded)整批回滾。POST .../transitions 的 body 為 to(必填)、reason、offboard_kind,允許的轉移由 internal/domain/hr.go 的 EmployeeTransitionAllowed 單一矩陣決定,矩陣外回 409 employee_transition_not_allowed(30100):

目前狀態可轉為
onboardingprobation、active、offboarded
probationactive、offboarded
activeleave_suspended、offboarded
leave_suspendedactive、offboarded
offboardedonboarding、active(復聘,建立新列)

轉為 offboarded 必須帶 offboard_kind(voluntary、layoff、retired、other),其他目標不得帶;復聘必須帶 reason。復聘的新列沿用舊列的 org_unit_id、manager_id、position_id 並重新驗證(單位已關閉、主管已離職或職位停用都會擋下復聘),不沿用 employee_no、company_email、account_id,並以 rehired_from_employee_id 連回舊列。

建檔在同一交易發 hr.employee.created,狀態轉移發 hr.employee.status_changed(一般 PUT 與 bulk-reassign 不發事件)。worker 的 internal/worker/hr.go 據此在轉為 offboarded 時停用已連結的帳號,並且只在 WORKFLOW_JML_AUTO_START_ENABLED=true(預設 false)時開啟入職/離職 JML run;新建員工沒有帳號,預設員工角色要到帳號開通(hr.employee.account_requested)時才綁定,細節見工作流與審批與非同步工作。

帳號開通不等於建立員工,建立與更新也都不能改帳號綁定(後端 9a26adc9):POST /v1/hr/employees 的 account_id 只接受省略或 null;PUT /v1/hr/employees/:employeeId 只接受省略或回送鎖定列的原值(含 null→null)。首次綁定、換綁與清除一律回 422/10430,field_errors 為 account_id/immutable,整筆更新不留任何副作用;範圍外或不存在的員工仍先回 404。檢查落在 service(employee_validation.go 與 employees.go 的 applyEmployeeUpdate),account_id 只由開通流程的 compare-and-set(LinkEmployeeAccount:尚未連結且非離職才寫入)寫入,activate-accounts 也不是任意帳號換綁 API。

activate-accounts 一次最多 200 人,逐筆套用資料範圍並回 outcome(not_found、offboarded、already_linked、company_email_missing、email_in_use、accepted),全部被拒仍回 200;accepted 只代表受理,帳號建立與邀請信由 worker 在交易外完成;公司信箱對到既有 active 帳號時不是直接沿用,而是先停用並走接管重驗(見身分與存取)。bulk-reassign 同樣最多 200 人,在一筆交易內執行,任一員工失敗就整批回滾。

GET /v1/hr/employees/:employeeId/leave-records 除了 hr.employee.read,還會對同一 target 再檢查 attendance.leave.read,兩個範圍取交集。員工 CSV 匯出沒有後端端點:前端 features/hr-employees/employeeExportApi.ts 逐員工先以 POST /v1/authz/check 檢查 hr.employee/export,再讀 detail、profile、/v1/leave/balances 與 leave-records 組成報表;入口需要 hr.employee.export 與 hr.employee.read。

搜尋與上游組織層級

GET /v1/hr/employees?q= 修剪後為空視為未帶、超過 64 字回 400,以不分大小寫子字串比對 display_name、員工檔案的 english_name、company_email 與 employee_no(%、_、\ 視為字面)。GET /v1/hr/org-chart?q= 比對 display_name、english_name、employee_no,只過濾回傳的員工,不影響主管解析。前端 hooks/api/useEmployeeNameMatches.ts 以 q 加 include_offboarded=true 取回全部分頁,組織頁、/workspace/attendance 與 /workspace/clock 的關鍵字搜尋靠它補上英文名比對(需要 hr.employee.read)。

Migration 000068_org_source_hierarchy.sql 在 public.org_units 保存上游的 source_depth、source_parent_code、source_manager_employee_no(以 source_hierarchy_present 標示)。只有 source sync 會寫入,手動 PUT 會保留既有快照;遷移前的列要等下一次成功的 source sync 才有值。GET /v1/hr/org-chart 的每個 unit 以 source_hierarchy 回傳這些事實與 manager_resolution(not_assigned/resolved/unresolved;同租戶、同 source 的外部員工編號恰好對到一人才算 resolved)。這是呈現用的上游事實,與授權用的 parent_id/path 分開,不能拿來判斷資料範圍。

eHRMS 同步是來源重建,不是名稱比對

SyncEHRMS 先載入本地 positions、org units、employees,再依 position code、unit code、external employee id 或 employee number 做確定性比對。以員工編號找到、但本地列不是同一個 eHRMS 來源身分(external_source/external_employee_id 不符或為空)時不會接管:該列被略過並記 row_errors(external_source/source_conflict),因為這兩個溯源欄位不可變。同步會依序處理職位、開放組織、員工、manager,再處理來源不再出現的資料;source-owned 資料與手動資料不可混為一談。

入口有兩條,行為不同:

  • 手動 POST /v1/hr/ehrms/sync:同步執行、上限 30 分鐘;空物件 body 等於只 dry-run HR 目錄。它同樣受租戶 source-sync 准入約束:開關關閉時連 dry-run 也回 409 source_sync_disabled(30400),已有來源工作在途或初始化排隊中回 409 source_sync_active(30401),上游未設定回 503。選項為 dry_run(預設 true)、include_resigned、leave、balances_year(2000–2100)與 activate_accounts(dry-run 時忽略)。回應只有計數、row_errors 與問題摘要,不含上游欄位值;/hr/positions 的同步按鈕送的是 dry_run=false,屬正式同步。
  • 租戶排程 source-sync:PUT /v1/hr/source-sync 必須同時帶 enabled 與 expected_version(不符回 409 stale_version);POST /v1/hr/source-sync/initialize 要求開關已開(否則 409 source_sync_disabled)並帶 1–160 字的 Idempotency-Key,回 202 排入首次匯入;已成功初始化或仍有工作執行中時會被拒。開啟後 worker 每 30 分鐘一輪:eHRMS HR 同步 →(EHRMS_SYNC_ACTIVATE_ACCOUNTS=true 時)帳號開通 → 在初始化與每日維護輪(當日第一輪或上一輪未成功)加跑假別目錄與年度餘額 → 當年整年的考勤批次(含來源請假明細;初始化用排入時的年度)。這條路徑沒有 dry-run,全部是正式寫入;EHRMS_SYNC_ENABLED、EHRMS_SYNC_FULL_ON_START 只是相容舊設定。

程式位置:手動路由 internal/api/v1/hr_ehrms.go → 編排 internal/wiring/ehrms.go(fetch → HR → accounts → leave → balances)→ internal/service/hr/ehrms_sync.go 的 SyncEHRMS;上游 client 在 internal/platform/ehrms;排程開關在 internal/api/v1/source_sync.go 與 internal/service/sourcesync,worker 在 cmd/worker/ehrms_sync.go。

一條請求鏈

架構流程圖 · Mermaid

正在繪製架構圖…

查看圖表原始碼
flowchart LR
  UI["Next page"] --> SWR["feature api.ts + SWR"]
  SWR --> ME["/v1/me identity"]
  SWR --> Gin["Gin /v1/hr/*"]
  Gin --> Auth["route point + target scope"]
  Auth --> Service["service/hr"]
  Service --> Repo["Repository + tenant tx"]
  Repo --> DB[("PostgreSQL + RLS")]

前端的 cache key 會包含 identity;例如員工 detail 的查詢要等 /v1/me 得到 tenant/account 後才發送。後端仍需重新判斷 scope,不能把前端是否顯示按鈕當作授權。

實作步驟與示例

  1. 先在 api/openapi.yaml 確認 operation、欄位與錯誤,權限與資料範圍則對 internal/route/testdata/registry.golden。handler 都在 internal/api/v1/:員工 hr_employees.go、hr_employee_*.go(stats、bulk-reassign、帳號開通、password reset)、hr_profile*.go、hr_leave_records.go;組織 hr_org.go、hr_org_chart.go;職位 hr_positions.go;同步 hr_ehrms.go、source_sync.go;分析 hr_turnover.go。
  2. 若是新欄位,按 migration、query/sqlc、Repository、service、handler、測試的順序落地;不要直接在 handler 寫 SQL。
  3. 針對單筆員工操作確認 TargetEmployee 與資料範圍;跨租戶或 scope 外的結果不能透過改寫 ID 繞過。
  4. 前端在 features/hr-employees/api.ts 以 schema 解析 wire response,再讓頁面元件呈現;欄位新增要同步 adapter、表單與測試。
http
GET /v1/hr/employees?status=active&q=chen&page=1&page_size=20
Authorization: Bearer <由執行環境注入>

上例只展示契約形狀,不提供 token,也不代表本輪已打 API。q 會同時比對中文姓名、英文名、公司信箱與員編;status 與 include_offboarded 不能同時帶(400);兩者都沒帶時預設不含離職者。要驗證,先在有授權的本地環境讀回 tenant 內資料,再檢查分頁 total、欄位策略與 audit;不要用別人的 .env 或共用資料庫做試驗。

驗證矩陣

驗證問題程式碼/測試證據仍需另外做的驗收
員工 CRUD 與欄位 contractinternal/api/v1/hr_employees_test.go、features/hr-employees/api.test.tsx有授權帳號的 API happy path 與負向 scope
帳號綁定封口internal/service/hr/employee_account_binding_guard_test.go、tests/contract/hr_account_binding_guard_test.go、tests/integration/postgres/hr_employee_account_binding_guard_test.go、tests/integration/postgres/hr_employee_account_binding_race_test.go前端建檔/編輯不送 account_id 變更;開通流程的實際連結
雙名搜尋tests/integration/postgres/hr_employee_name_search_test.go、hooks/api/useEmployeeNameMatches.test.tsx真實資料在組織頁與出勤頁的搜尋結果
組織樹 mapping 與改派internal/service/hr/org_chart_test.go、internal/service/hr/org_source_test.go、tests/integration/postgres/hr_org_test.go、tests/integration/postgres/hr_employee_bulk_reassign_test.go、features/hr-organization/tree.test.ts真實租戶樹與跨租戶阻擋
職位生命週期internal/api/v1/hr_positions_filter_test.go、tests/integration/postgres/hr_org_test.go、features/hr-positions/api.test.ts停用後的歷史引用與被引用時刪除被拒
eHRMS row isolationinternal/service/hr/ehrms_sync_rows_test.go、internal/api/v1/hr_ehrms_test.go受控上游快照、dry-run 影響數與正式同步
source-sync 開關與排程internal/api/v1/source_sync_test.go、cmd/worker/ehrms_sync_test.go、tests/integration/postgres/source_sync_test.go受控上游的首次初始化與 30 分鐘排程
turnover 月/年計算internal/service/hr/turnover_metrics_test.go、features/hr-turnover/api.test.tsxruntime 期間、時區與實際資料核對

常見錯誤

  • 看到 /v1/hr/employees/:employeeId 就直接在 SQL 以 ID 查詢:應先完成 principal、target 與 scope 判斷。
  • 把 employee profile、登入 account、外部 eHRMS row 當成一張表;它們的 ownership 與生命週期不同。
  • 用姓名匹配同步資料;同步實作使用 code/external id,姓名只可作顯示與搜尋。
  • 以為手動建立、員編相同的員工會被同步自動接管;來源身分不符的列只會被略過並回報 source_conflict。
  • 在建立或更新員工的 body 帶 account_id 想直接綁定、換綁或清除帳號;一律 422,請走 activate-accounts。
  • 把 dry_run=true 的計數當成已寫入;dry-run 必須確認沒有 mutation,再按授權執行正式同步。source-sync 的開關與初始化沒有 dry-run,一律是正式寫入。
  • 以為 org unit 或 position 的 PUT 有版本保護;兩者都是整筆替換、沒有 CAS,送出前要以最新資料為基礎。
  • 把 source_hierarchy 當成授權階層;資料範圍只看本地 parent_id/path。
  • 以為只給 hr.org_unit.read 就看不到員工:GET /v1/hr/org-chart 是 tenant-wide,會回傳全租戶(預設不含離職)的員工名冊與主管關係,不受 hr.employee 範圍限制。
  • 用前端 fixture 或單元測試結果宣稱真實 HR 已可用;目前尚未在本文執行後端 API、eHRMS 或部署驗證。

前端整合更新(2026-10-01)

已提交 2036dd6 的主管/代理人名稱解析改用未篩選目錄,避免列表篩選後顯示 ID;43d889a 的批次異動不再要求先存在 elevation session。這不繞過後端 route permission 或 data scope;缺權限仍應拒絕。當時前端尚未提交的組織、匯出、假勤與職位改動記錄在版本日誌;2026-10-05 核對時前端 c490b4b 的工作樹已無未提交改動。

相關文件

內容來源與核實範圍

以下路徑相對於所列業務倉庫;核實層級為「已讀所列來源」。歷史日誌的測試與部署結果屬於當時記錄,不是本輪業務測試或部署驗收。

  • nexus-pro-be-plus/internal/api/v1/routes.go
  • nexus-pro-be-plus/internal/api/v1/hr_employees.go
  • nexus-pro-be-plus/internal/api/v1/hr_org.go
  • nexus-pro-be-plus/internal/api/v1/hr_org_chart.go
  • nexus-pro-be-plus/internal/api/v1/hr_positions.go
  • nexus-pro-be-plus/internal/api/v1/hr_ehrms.go
  • nexus-pro-be-plus/internal/api/v1/hr_required_body.go
  • nexus-pro-be-plus/internal/api/v1/source_sync.go
  • nexus-pro-be-plus/internal/route/testdata/registry.golden
  • nexus-pro-be-plus/internal/domain/hr.go
  • nexus-pro-be-plus/internal/service/hr/employees.go
  • nexus-pro-be-plus/internal/service/hr/employee_validation.go
  • nexus-pro-be-plus/internal/service/hr/transitions.go
  • nexus-pro-be-plus/internal/service/hr/org.go
  • nexus-pro-be-plus/internal/service/hr/org_chart.go
  • nexus-pro-be-plus/internal/service/hr/org_chart_source.go
  • nexus-pro-be-plus/internal/service/hr/ehrms_sync.go
  • nexus-pro-be-plus/internal/service/hr/ehrms_sync_employees.go
  • nexus-pro-be-plus/internal/service/hr/turnover.go
  • nexus-pro-be-plus/internal/worker/hr.go
  • nexus-pro-be-plus/internal/service/sourcesync/service.go
  • nexus-pro-be-plus/internal/wiring/ehrms.go
  • nexus-pro-be-plus/cmd/worker/ehrms_sync.go
  • nexus-pro-be-plus/db/queries/employees.sql
  • nexus-pro-be-plus/db/migrations/000068_org_source_hierarchy.sql
  • nexus-pro-web-plus/features/hr-employees/api.ts
  • nexus-pro-web-plus/features/hr-employees/sourceSyncApi.ts
  • nexus-pro-web-plus/features/hr-employees/employeeExportApi.ts
  • nexus-pro-web-plus/features/hr-organization/api.ts
  • nexus-pro-web-plus/features/hr-positions/api.ts
  • nexus-pro-web-plus/features/hr-turnover/api.ts
  • nexus-pro-web-plus/hooks/api/useEmployeeNameMatches.ts
NexusPro 開發指南以程式碼為準 · 以驗證為據