gongxiangp2/lib/employment-state.ts

54 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** 上游员工状态码。5 已离职,2 试用、3 正式均视为在职。 */
const STATUS_CODE_RESIGNED = 5
const STATUS_CODE_ACTIVE = [2, 3]
const RESIGNED_STATUS_KEYWORDS = ['离职', '停用', '退休']
export const EMPLOYMENT_STATE = {
ACTIVE: 'ACTIVE',
RESIGNED: 'RESIGNED',
UNKNOWN: 'UNKNOWN',
} as const
function clean(value: unknown): string | null {
if (value === null || value === undefined) return null
const normalized = String(value).trim()
return normalized === '' ? null : normalized
}
/**
* 将各上游来源的明确信号归一成系统使用的在职状态。
*
* 花名册里有一批离职人员没有钉钉 userid:其 employmentStatus 已明确为「离职」,
* 但旧逻辑只在 userid 存在时查询离职表/注册表,最终把这些人保守落成 UNKNOWN。
* 明确的离职信号必须优先于在职状态码;其余模糊中文和空值仍保持 UNKNOWN。
*/
export function resolveEmploymentState(signals: {
hasResignationRecord: boolean
rawEmploymentStatus?: unknown
registeredStatusCode?: number | null
}): string {
const rawStatus = clean(signals.rawEmploymentStatus)
const rawStatusCode = rawStatus ? Number(rawStatus) : Number.NaN
const rawExplicitlyResigned = Boolean(
rawStatus &&
(rawStatusCode === STATUS_CODE_RESIGNED ||
RESIGNED_STATUS_KEYWORDS.some((keyword) => rawStatus.includes(keyword))),
)
if (
signals.hasResignationRecord ||
rawExplicitlyResigned ||
signals.registeredStatusCode === STATUS_CODE_RESIGNED
) {
return EMPLOYMENT_STATE.RESIGNED
}
if (
signals.registeredStatusCode !== null &&
signals.registeredStatusCode !== undefined &&
STATUS_CODE_ACTIVE.includes(signals.registeredStatusCode)
) {
return EMPLOYMENT_STATE.ACTIVE
}
return EMPLOYMENT_STATE.UNKNOWN
}