feat: 支持复制考核模板
This commit is contained in:
parent
c4b3ab8e38
commit
7aa356db9b
@ -37,6 +37,7 @@ import {
|
||||
publishPerformanceSurveyAdminPositionGroup,
|
||||
savePerformanceSurveyAdminTemplate,
|
||||
clonePerformanceSurveyAdminTemplateVersion,
|
||||
copyPerformanceSurveyAdminTemplateVersion,
|
||||
publishPerformanceSurveyAdminTemplateVersion,
|
||||
archivePerformanceSurveyAdminPositionAssignment,
|
||||
copyPerformanceSurveyAdminPositionGroup,
|
||||
@ -298,6 +299,10 @@ export async function POST(request: Request, context: RouteContext) {
|
||||
return ok(await clonePerformanceSurveyAdminTemplateVersion(session, path[2]), { status: 201 })
|
||||
}
|
||||
|
||||
if (path[0] === 'admin' && path[1] === 'templates' && path[2] && path[3] === 'copy') {
|
||||
return ok(await copyPerformanceSurveyAdminTemplateVersion(session, path[2]), { status: 201 })
|
||||
}
|
||||
|
||||
if (path[0] === 'admin' && path[1] === 'templates' && path[2] && path[3] === 'publish') {
|
||||
return ok(await publishPerformanceSurveyAdminTemplateVersion(session, path[2], body))
|
||||
}
|
||||
|
||||
@ -142,6 +142,9 @@ function createRequest(configuration = config) {
|
||||
if (path === '/admin/templates/template-1' && options?.method === 'DELETE') {
|
||||
return { id: 'template-1', action: 'deleted' }
|
||||
}
|
||||
if (path.endsWith('/copy')) {
|
||||
return { id: 'copied-version-1', templateId: 'copied-template-1', name: '视觉案例评分(副本)' }
|
||||
}
|
||||
if (path.endsWith('/clone')) return { id: 'draft-version-2' }
|
||||
return {}
|
||||
})
|
||||
@ -437,6 +440,23 @@ describe('PerformanceSurveyAdminConfig', () => {
|
||||
expect(screen.queryByText('使用场景')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('已发布模板可通过修改按钮左侧的复制按钮创建独立草稿', async () => {
|
||||
const request = createRequest()
|
||||
render(<PerformanceSurveyAdminConfig mode="templates" request={request} />)
|
||||
await waitFor(() => expect(screen.getByRole('button', { name: /视觉案例评分.*可用/ })).toBeInTheDocument())
|
||||
|
||||
const copyButton = screen.getByRole('button', { name: '复制模板' })
|
||||
const modifyButton = screen.getByRole('button', { name: '修改模板' })
|
||||
expect(copyButton.nextElementSibling).toBe(modifyButton)
|
||||
|
||||
fireEvent.click(copyButton)
|
||||
|
||||
await waitFor(() => expect(request).toHaveBeenCalledWith(
|
||||
'/admin/templates/version-1/copy',
|
||||
expect.objectContaining({ method: 'POST', body: '{}' }),
|
||||
))
|
||||
})
|
||||
|
||||
test('模板列表将所有已发布版本视为立即可用', async () => {
|
||||
const configuration = {
|
||||
...config,
|
||||
|
||||
@ -1339,6 +1339,33 @@ function TemplateConfigWorkspace({
|
||||
}
|
||||
}
|
||||
|
||||
const copyTemplate = async () => {
|
||||
if (!versionId || !selectedTemplate || !await confirmDiscard()) return
|
||||
setSaving(true)
|
||||
onError(null)
|
||||
setOperationFeedback({ state: 'saving', message: '正在复制模板…' })
|
||||
try {
|
||||
const copied = await request<{ id: string; templateId: string; name: string }>(
|
||||
`/admin/templates/${versionId}/copy`,
|
||||
{ method: 'POST', body: '{}' },
|
||||
)
|
||||
const next = await onChanged(`已复制为“${copied.name}”草稿`)
|
||||
const nextTemplate = next?.templates.find((template) => (
|
||||
template.id === copied.templateId
|
||||
|| template.versions.some((item) => item.id === copied.id)
|
||||
))
|
||||
if (nextTemplate) setSelectedTemplateId(nextTemplate.id)
|
||||
setVersionId(copied.id)
|
||||
setOperationFeedback({ state: 'saved', message: `已复制为“${copied.name}”草稿` })
|
||||
} catch (actionError) {
|
||||
const message = actionError instanceof Error ? actionError.message : '模板复制失败'
|
||||
onError(message)
|
||||
setOperationFeedback({ state: 'error', message: `复制失败:${message}` })
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const publish = async () => {
|
||||
if (!versionId) return
|
||||
if (dirty) {
|
||||
@ -1700,14 +1727,24 @@ function TemplateConfigWorkspace({
|
||||
</div>
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
{!editable && (
|
||||
<button
|
||||
type="button"
|
||||
disabled={saving}
|
||||
onClick={() => void cloneVersion()}
|
||||
className="min-h-11 rounded bg-[#0050cb] px-4 text-[13px] font-bold text-white hover:bg-[#0042a8] disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
{saving ? '处理中…' : '修改模板'}
|
||||
</button>
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
disabled={saving}
|
||||
onClick={() => void copyTemplate()}
|
||||
className="min-h-11 rounded border border-[#1a1c1c] px-4 text-[13px] font-bold text-[#1a1c1c] hover:bg-[#f4f4f4] disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
{saving ? '处理中…' : '复制模板'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
disabled={saving}
|
||||
onClick={() => void cloneVersion()}
|
||||
className="min-h-11 rounded bg-[#0050cb] px-4 text-[13px] font-bold text-white hover:bg-[#0042a8] disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
{saving ? '处理中…' : '修改模板'}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{editable && (
|
||||
<button
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import {
|
||||
assertPerformanceSurveyPositionPlanPreviewMatches,
|
||||
copyPerformanceSurveyTemplateVersion,
|
||||
createPerformanceSurveyManualPositionAssignments,
|
||||
getPerformanceSurveyPositionAdminConfig,
|
||||
listPerformanceSurveyManualPositionAssignments,
|
||||
@ -25,9 +26,11 @@ jest.mock('@/lib/prisma', () => ({
|
||||
update: jest.fn(),
|
||||
},
|
||||
performanceSurveyTemplateVersion: {
|
||||
findUnique: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
},
|
||||
performanceSurveyTemplate: {
|
||||
create: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
},
|
||||
@ -295,6 +298,68 @@ describe('performance survey position admin', () => {
|
||||
expect(prisma.performanceSurveyEmployeeProfile.findMany).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('复制模板会创建独立模板和完整草稿版本', async () => {
|
||||
const definition = structuredClone(DEFAULT_PERFORMANCE_SURVEY_TEMPLATES[0])
|
||||
;(prisma.performanceSurveyTemplateVersion.findUnique as jest.Mock).mockResolvedValue({
|
||||
id: 'version-1',
|
||||
templateId: 'template-1',
|
||||
version: 2,
|
||||
status: 'published',
|
||||
definition,
|
||||
template: {
|
||||
id: 'template-1',
|
||||
code: definition.code,
|
||||
name: definition.name,
|
||||
archivedAt: null,
|
||||
},
|
||||
})
|
||||
const tx = {
|
||||
performanceSurveyTemplate: {
|
||||
create: jest.fn().mockImplementation(async ({ data }) => ({ id: 'template-copy', ...data })),
|
||||
},
|
||||
performanceSurveyTemplateVersion: {
|
||||
create: jest.fn().mockImplementation(async ({ data }) => ({ id: 'version-copy', ...data })),
|
||||
},
|
||||
performanceSurveyConfigAuditLog: {
|
||||
create: jest.fn().mockResolvedValue({ id: 'audit-copy' }),
|
||||
},
|
||||
}
|
||||
;(prisma.$transaction as jest.Mock).mockImplementation(async (callback) => callback(tx))
|
||||
|
||||
await expect(copyPerformanceSurveyTemplateVersion({
|
||||
versionId: 'version-1',
|
||||
actorUserId: 'admin-1',
|
||||
actorName: '管理员',
|
||||
})).resolves.toMatchObject({
|
||||
id: 'version-copy',
|
||||
templateId: 'template-copy',
|
||||
name: `${definition.name}(副本)`,
|
||||
})
|
||||
|
||||
const templateData = tx.performanceSurveyTemplate.create.mock.calls[0][0].data
|
||||
const versionData = tx.performanceSurveyTemplateVersion.create.mock.calls[0][0].data
|
||||
expect(templateData.code).toMatch(/^template_copy_[a-f0-9]{32}$/)
|
||||
expect(templateData.name).toBe(`${definition.name}(副本)`)
|
||||
expect(versionData).toMatchObject({
|
||||
templateId: 'template-copy',
|
||||
version: 1,
|
||||
status: 'draft',
|
||||
definition: expect.objectContaining({
|
||||
code: templateData.code,
|
||||
name: `${definition.name}(副本)`,
|
||||
shortName: `${definition.name}(副本)`,
|
||||
questions: definition.questions,
|
||||
workflow: definition.workflow,
|
||||
}),
|
||||
})
|
||||
expect(tx.performanceSurveyConfigAuditLog.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({
|
||||
action: 'template.copied',
|
||||
targetId: 'template-copy',
|
||||
}),
|
||||
})
|
||||
})
|
||||
|
||||
test('当前或未来岗位方案仍在使用模板时拒绝删除', async () => {
|
||||
;(prisma.performanceSurveyTemplate.findUnique as jest.Mock).mockResolvedValue({
|
||||
id: 'template-1',
|
||||
|
||||
@ -1767,6 +1767,75 @@ export async function createPerformanceSurveyTemplateDraftFromVersion(input: {
|
||||
})
|
||||
}
|
||||
|
||||
export async function copyPerformanceSurveyTemplateVersion(input: {
|
||||
versionId: string
|
||||
actorUserId: string
|
||||
actorName: string
|
||||
reason?: string | null
|
||||
}) {
|
||||
const source = await prisma.performanceSurveyTemplateVersion.findUnique({
|
||||
where: { id: input.versionId },
|
||||
include: { template: true },
|
||||
})
|
||||
if (!source) throw new PerformanceSurveyPositionConfigError('模板版本不存在', 404)
|
||||
if (source.template.archivedAt) throw new PerformanceSurveyPositionConfigError('已归档模板不能复制', 409)
|
||||
|
||||
const sourceDefinition = readTemplateDefinition(source.definition)
|
||||
const sourceName = clean(sourceDefinition.name) || source.template.name
|
||||
const copyName = `${sourceName}(副本)`
|
||||
const copyCode = `template_copy_${randomUUID().replace(/-/g, '')}`
|
||||
const copiedDefinition: PerformanceSurveyTemplateShape = {
|
||||
...sourceDefinition,
|
||||
code: copyCode,
|
||||
name: copyName,
|
||||
shortName: copyName,
|
||||
}
|
||||
|
||||
return prisma.$transaction(async (tx) => {
|
||||
const template = await tx.performanceSurveyTemplate.create({
|
||||
data: {
|
||||
code: copyCode,
|
||||
name: copyName,
|
||||
description: copiedDefinition.description,
|
||||
surveyType: copiedDefinition.surveyType,
|
||||
weight: copiedDefinition.weight,
|
||||
questions: copiedDefinition.questions as any,
|
||||
scoreBands: copiedDefinition.scoreBands as any,
|
||||
isActive: true,
|
||||
},
|
||||
})
|
||||
const version = await tx.performanceSurveyTemplateVersion.create({
|
||||
data: {
|
||||
templateId: template.id,
|
||||
version: 1,
|
||||
status: 'draft',
|
||||
definition: copiedDefinition as any,
|
||||
createdByUserId: input.actorUserId,
|
||||
createdByName: input.actorName,
|
||||
},
|
||||
})
|
||||
await writePerformanceSurveyConfigAuditLog(tx, {
|
||||
action: 'template.copied',
|
||||
targetType: 'template',
|
||||
targetId: template.id,
|
||||
actorUserId: input.actorUserId,
|
||||
actorName: input.actorName,
|
||||
reason: input.reason,
|
||||
beforeSnapshot: source,
|
||||
afterSnapshot: { template, version },
|
||||
metadata: {
|
||||
sourceTemplateId: source.templateId,
|
||||
sourceVersionId: source.id,
|
||||
},
|
||||
})
|
||||
return {
|
||||
id: version.id,
|
||||
templateId: template.id,
|
||||
name: copyName,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function publishPerformanceSurveyTemplateVersion(input: {
|
||||
versionId: string
|
||||
effectiveMonth?: string | null
|
||||
|
||||
@ -29,6 +29,7 @@ import {
|
||||
createPerformanceSurveyManualPositionAssignments,
|
||||
createPerformanceSurveyPositionGroup,
|
||||
createPerformanceSurveyTemplateDraftFromVersion,
|
||||
copyPerformanceSurveyTemplateVersion,
|
||||
getAvailablePerformanceSurveyPositionTemplates,
|
||||
getManualReviewerStages,
|
||||
getPerformanceSurveyPositionAdminConfig,
|
||||
@ -1281,6 +1282,24 @@ export async function clonePerformanceSurveyAdminTemplateVersion(
|
||||
}
|
||||
}
|
||||
|
||||
export async function copyPerformanceSurveyAdminTemplateVersion(
|
||||
session: Session | null,
|
||||
versionId: string,
|
||||
) {
|
||||
const ctx = await getPerformanceSurveyContext(session)
|
||||
ensureSystemAdministrator(ctx)
|
||||
try {
|
||||
return await copyPerformanceSurveyTemplateVersion({
|
||||
versionId,
|
||||
reason: null,
|
||||
actorUserId: ctx.user.id,
|
||||
actorName: getDisplayName(ctx.user),
|
||||
})
|
||||
} catch (error) {
|
||||
return rethrowPositionConfigError(error)
|
||||
}
|
||||
}
|
||||
|
||||
export async function publishPerformanceSurveyAdminTemplateVersion(
|
||||
session: Session | null,
|
||||
versionId: string,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user