66 lines
2.5 KiB
TypeScript
66 lines
2.5 KiB
TypeScript
import {
|
|
autoActivateUserIfEligible,
|
|
isRosterEmployed,
|
|
selectVisibleOrganizationPeople,
|
|
shouldAutoActivate,
|
|
} from '@/lib/auto-personnel-rules'
|
|
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
user: { updateMany: jest.fn() },
|
|
userEmployeeBinding: { findFirst: jest.fn() },
|
|
},
|
|
}))
|
|
|
|
describe('花名册在职判定', () => {
|
|
it('只有明确离职才不算在职', () => {
|
|
expect(isRosterEmployed('RESIGNED')).toBe(false)
|
|
expect(isRosterEmployed('ACTIVE')).toBe(true)
|
|
expect(isRosterEmployed('UNKNOWN')).toBe(true)
|
|
expect(isRosterEmployed(null)).toBe(true)
|
|
expect(isRosterEmployed(undefined)).toBe(true)
|
|
})
|
|
|
|
it('自动激活跟花名册在职走同一口径', () => {
|
|
expect(shouldAutoActivate({ employmentState: 'RESIGNED' })).toBe(false)
|
|
expect(shouldAutoActivate({ employmentState: 'UNKNOWN' })).toBe(true)
|
|
expect(shouldAutoActivate(null)).toBe(false)
|
|
})
|
|
|
|
it('组织架构默认名单不带离职的人,也不看账号是否还启用', () => {
|
|
const people = [
|
|
{ name: '在职且账号启用', employmentState: 'ACTIVE', isActive: true },
|
|
{ name: '离职但账号还没归档', employmentState: 'RESIGNED', isActive: true },
|
|
{ name: '离职且账号已关', employmentState: 'RESIGNED', isActive: false },
|
|
{ name: '在职但账号停用', employmentState: 'ACTIVE', isActive: false },
|
|
{ name: '状态未知', employmentState: 'UNKNOWN', isActive: true },
|
|
]
|
|
|
|
expect(selectVisibleOrganizationPeople(people).map((p) => p.name)).toEqual([
|
|
'在职且账号启用',
|
|
'在职但账号停用',
|
|
'状态未知',
|
|
])
|
|
expect(selectVisibleOrganizationPeople(people, true)).toHaveLength(5)
|
|
})
|
|
})
|
|
|
|
test.each([0, 1])('花名册激活带上原子状态条件,受影响账号数=%d', async (count) => {
|
|
jest.mocked(prisma.userEmployeeBinding.findFirst).mockResolvedValue({
|
|
employeeProfile: { employmentState: 'ACTIVE', levelType: null, positionLevel: null, isLeadership: false },
|
|
} as any)
|
|
jest.mocked(prisma.user.updateMany).mockResolvedValue({ count })
|
|
await expect(autoActivateUserIfEligible('user')).resolves.toBe(count === 1)
|
|
expect(prisma.user.updateMany).toHaveBeenLastCalledWith({
|
|
where: {
|
|
id: 'user', status: 'pending', isActive: false, archivedAt: null,
|
|
statusChangedAt: null, statusChangedBy: null, lastLoginAt: null, loginCount: 0,
|
|
},
|
|
data: {
|
|
status: 'active', isActive: true, statusChangedAt: expect.any(Date), statusChangedBy: 'auto-花名册在册',
|
|
},
|
|
})
|
|
})
|