867 lines
33 KiB
TypeScript
867 lines
33 KiB
TypeScript
import { randomUUID } from 'crypto'
|
|
|
|
import { getEmployeeByUserId, BINDING_STATUS } from '@/lib/employee-directory'
|
|
import { isRosterEmployed } from '@/lib/auto-personnel-rules'
|
|
import { canUserEditTaskDetailAssets } from '@/lib/user-permission-inherit'
|
|
import { canManageMaterial } from '@/lib/material-authorization'
|
|
import { getDingtalkSubAdminUserIds } from '@/lib/dingtalk-client'
|
|
import { EmployeeManagerResolutionError, resolveOrganizationSubAdminProfile } from '@/lib/employee-manager'
|
|
import { canAccessMaterialPrivacy, TASK_DETAIL_PRIVACY_SELECT } from '@/lib/task-detail-privacy'
|
|
import { isProductPackageType } from '@/lib/material-type-mapper'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { getPublicAppOrigin } from '@/lib/public-app-url'
|
|
import { buildDingtalkEntryPath } from '@/lib/safe-redirect'
|
|
import { isSystemAdmin } from '@/lib/department-permission'
|
|
import { groupTaskDetailImageEdits, TASK_DETAIL_PREVIEW_TYPES } from '@/lib/task-detail-edit-groups'
|
|
import {
|
|
cleanupTaskDetailChangeStaging,
|
|
resolveTaskDetailStagedFile,
|
|
withTaskDetailStagedUploads,
|
|
type TaskDetailStagedFile,
|
|
} from '@/lib/task-detail-change-staging'
|
|
import { queueTaskDetailChangeNotification, TASK_DETAIL_REVIEW_PROMPT } from '@/lib/task-detail-change-notifications'
|
|
import {
|
|
getTaskDetailReviewSources,
|
|
hasCurrentTaskDetailReviewImages,
|
|
readTaskDetailReviewImage,
|
|
type TaskDetailReviewSide,
|
|
} from '@/lib/task-detail-change-review'
|
|
import {
|
|
assertTaskDetailGroupSelection,
|
|
assertTaskDetailImageEditReady,
|
|
batchEditTaskDetailImages,
|
|
TaskDetailAssetEditError,
|
|
type TaskDetailEditGroup,
|
|
} from '@/lib/task-detail-asset-edit'
|
|
import { TASK_DETAIL_UPLOAD_LIMITS } from '@/lib/task-detail-upload-policy'
|
|
|
|
const REQUEST_STATUS = {
|
|
PENDING: 'pending_manager_approval',
|
|
APPLYING: 'applying',
|
|
APPLIED: 'applied',
|
|
REJECTED: 'rejected',
|
|
EXPIRED: 'expired',
|
|
APPLY_FAILED: 'apply_failed',
|
|
} as const
|
|
|
|
const APPROVAL_WINDOW_MS = 24 * 60 * 60 * 1000
|
|
const STALE_APPLYING_MS = 30 * 60 * 1000
|
|
|
|
export class TaskDetailChangeApprovalError extends Error {
|
|
constructor(message: string, public statusCode = 400) {
|
|
super(message)
|
|
this.name = 'TaskDetailChangeApprovalError'
|
|
}
|
|
}
|
|
|
|
type SingleImageChangeOperation =
|
|
| { kind: 'delete'; assetId: string }
|
|
| { kind: 'rename'; assetId: string; newFileName: string }
|
|
| { kind: 'replace'; assetId: string; uploadId: string }
|
|
| { kind: 'append'; group: TaskDetailEditGroup; uploadId: string }
|
|
| {
|
|
kind: 'insert'
|
|
group: 'detail'
|
|
uploadId: string
|
|
afterAssetId: string
|
|
beforeAssetId: string
|
|
}
|
|
|
|
type GroupImagesOperation = {
|
|
kind: 'group_images'
|
|
group: TaskDetailEditGroup
|
|
assetIds: string[]
|
|
uploadIds: string[]
|
|
}
|
|
|
|
export type TaskDetailChangeOperationInput = SingleImageChangeOperation | GroupImagesOperation
|
|
|
|
type StoreSingle<T extends SingleImageChangeOperation> =
|
|
T extends { uploadId: string } ? Omit<T, 'uploadId'> & { stagedFile: TaskDetailStagedFile } : T
|
|
|
|
export type StoredTaskDetailChangeOperation =
|
|
| (StoreSingle<SingleImageChangeOperation> & { group?: TaskDetailEditGroup })
|
|
| (Omit<GroupImagesOperation, 'uploadIds'> & { stagedFiles: TaskDetailStagedFile[] })
|
|
|
|
type Actor = {
|
|
id: string
|
|
role?: string | null
|
|
isSuperAdmin?: boolean
|
|
}
|
|
|
|
function clean(value: unknown) {
|
|
const text = String(value ?? '').trim()
|
|
return text || null
|
|
}
|
|
|
|
function normalizeOperations(value: unknown): TaskDetailChangeOperationInput[] {
|
|
const rows = Array.isArray(value) ? value : []
|
|
if (rows.length === 0) throw new TaskDetailChangeApprovalError('图片变更不能为空')
|
|
if (rows.length > TASK_DETAIL_UPLOAD_LIMITS.maxTotalImageCount) {
|
|
throw new TaskDetailChangeApprovalError(`一次最多提交 ${TASK_DETAIL_UPLOAD_LIMITS.maxTotalImageCount} 项图片变更`)
|
|
}
|
|
|
|
const operations: TaskDetailChangeOperationInput[] = []
|
|
const targetAssetIds = new Set<string>()
|
|
for (const raw of rows) {
|
|
const row = (raw || {}) as Record<string, unknown>
|
|
const kind = clean(row.kind)
|
|
if (kind === 'group_images') {
|
|
const group = clean(row.group)
|
|
if (group !== 'main' && group !== 'sku' && group !== 'detail') {
|
|
throw new TaskDetailChangeApprovalError('整组变更的图片分组无效')
|
|
}
|
|
if (
|
|
!Array.isArray(row.assetIds)
|
|
|| row.assetIds.length === 0
|
|
|| row.assetIds.length > TASK_DETAIL_UPLOAD_LIMITS.maxTotalPreviewCount
|
|
|| !Array.isArray(row.uploadIds)
|
|
|| row.uploadIds.length > TASK_DETAIL_UPLOAD_LIMITS.maxImagesPerGroup
|
|
) {
|
|
throw new TaskDetailChangeApprovalError('整组变更的图片数量或参数无效')
|
|
}
|
|
const assetIds = row.assetIds.map((id) => typeof id === 'string' ? id.trim() : '')
|
|
const uploadIds = row.uploadIds.map((id) => typeof id === 'string' ? id.trim() : '')
|
|
if (assetIds.includes('') || uploadIds.includes('') || new Set(uploadIds).size !== uploadIds.length) {
|
|
throw new TaskDetailChangeApprovalError('整组变更包含无效或重复的上传会话')
|
|
}
|
|
if (group === 'sku' && uploadIds.length > 0 && uploadIds.length !== assetIds.length) {
|
|
throw new TaskDetailChangeApprovalError('整组替换 SKU 图时,新图数量必须与原图数量一致')
|
|
}
|
|
operations.push({ kind, group, assetIds, uploadIds })
|
|
continue
|
|
}
|
|
if (kind === 'delete') {
|
|
const assetId = clean(row.assetId)
|
|
if (!assetId) throw new TaskDetailChangeApprovalError('删除操作缺少图片标识')
|
|
if (targetAssetIds.has(assetId)) throw new TaskDetailChangeApprovalError('同一图片不能重复修改')
|
|
targetAssetIds.add(assetId)
|
|
operations.push({ kind, assetId })
|
|
continue
|
|
}
|
|
if (kind === 'rename') {
|
|
const assetId = clean(row.assetId)
|
|
const newFileName = clean(row.newFileName)
|
|
if (!assetId || !newFileName) throw new TaskDetailChangeApprovalError('改名操作参数不完整')
|
|
if (targetAssetIds.has(assetId)) throw new TaskDetailChangeApprovalError('同一图片不能重复修改')
|
|
targetAssetIds.add(assetId)
|
|
operations.push({ kind, assetId, newFileName })
|
|
continue
|
|
}
|
|
if (kind === 'replace') {
|
|
const assetId = clean(row.assetId)
|
|
const uploadId = clean(row.uploadId)
|
|
if (!assetId || !uploadId) throw new TaskDetailChangeApprovalError('替换操作参数不完整')
|
|
if (targetAssetIds.has(assetId)) throw new TaskDetailChangeApprovalError('同一图片不能重复修改')
|
|
targetAssetIds.add(assetId)
|
|
operations.push({ kind, assetId, uploadId })
|
|
continue
|
|
}
|
|
if (kind === 'append') {
|
|
const group = clean(row.group)
|
|
const uploadId = clean(row.uploadId)
|
|
if ((group !== 'main' && group !== 'sku' && group !== 'detail') || !uploadId) {
|
|
throw new TaskDetailChangeApprovalError('追加图片操作参数不完整')
|
|
}
|
|
operations.push({ kind, group, uploadId })
|
|
continue
|
|
}
|
|
if (kind === 'insert') {
|
|
const group = clean(row.group)
|
|
const uploadId = clean(row.uploadId)
|
|
const afterAssetId = clean(row.afterAssetId)
|
|
const beforeAssetId = clean(row.beforeAssetId)
|
|
if (group !== 'detail' || !uploadId || !afterAssetId || !beforeAssetId) {
|
|
throw new TaskDetailChangeApprovalError('详情图插入位置或图片参数不完整')
|
|
}
|
|
operations.push({ kind, group, uploadId, afterAssetId, beforeAssetId })
|
|
continue
|
|
}
|
|
throw new TaskDetailChangeApprovalError(`不支持的图片变更类型:${kind || '空'}`)
|
|
}
|
|
return operations
|
|
}
|
|
|
|
async function loadAuthorizedContext(materialId: string, actor: Actor) {
|
|
const [material, employee] = await Promise.all([
|
|
prisma.material.findFirst({
|
|
where: { id: materialId, deletedAt: null },
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
materialType: true,
|
|
...TASK_DETAIL_PRIVACY_SELECT,
|
|
taskStatus: true,
|
|
isIncompleteShell: true,
|
|
taskNo: true,
|
|
productModel: true,
|
|
updatedAt: true,
|
|
assets: {
|
|
where: { assetType: { in: Object.values(TASK_DETAIL_PREVIEW_TYPES) } },
|
|
select: { id: true, assetType: true },
|
|
},
|
|
},
|
|
}),
|
|
getEmployeeByUserId(actor.id),
|
|
])
|
|
if (!material) throw new TaskDetailChangeApprovalError('素材不存在或已删除', 404)
|
|
if (!(await canAccessMaterialPrivacy(material, actor.id))) {
|
|
throw new TaskDetailChangeApprovalError('无权限访问该私密素材', 403)
|
|
}
|
|
if (!isProductPackageType(material.materialType)) {
|
|
throw new TaskDetailChangeApprovalError('只有任务-详情素材支持图片变更')
|
|
}
|
|
// fetched 是上传完成后的外部获取状态,不能当作仍未完成上传。
|
|
if (
|
|
material.isIncompleteShell
|
|
|| (material.taskStatus !== 'completed' && material.taskStatus !== 'fetched')
|
|
) {
|
|
throw new TaskDetailChangeApprovalError('素材尚未完成上传,暂不能修改图片')
|
|
}
|
|
|
|
let allowed = await canManageMaterial(
|
|
{ id: actor.id, role: actor.role, isSuperAdmin: actor.isSuperAdmin },
|
|
{ id: material.id, userId: material.userId },
|
|
)
|
|
if (!allowed) allowed = await canUserEditTaskDetailAssets(actor.id)
|
|
if (!allowed) throw new TaskDetailChangeApprovalError('您没有权限修改此素材', 403)
|
|
return { material, employee }
|
|
}
|
|
|
|
function employeeSnapshot(employee: NonNullable<Awaited<ReturnType<typeof getEmployeeByUserId>>>) {
|
|
return {
|
|
employeeProfileId: employee.employeeProfileId,
|
|
userId: employee.userId,
|
|
displayName: employee.displayName,
|
|
jobNumber: employee.jobNumber,
|
|
dingtalkUserId: employee.dingtalkUserId,
|
|
departmentName: employee.departmentName,
|
|
departmentPath: employee.departmentPath,
|
|
positionTitle: employee.positionTitle,
|
|
positionLevel: employee.positionLevel,
|
|
levelType: employee.levelType,
|
|
isLeadership: employee.isLeadership,
|
|
managerName: employee.managerName,
|
|
managerDingtalkUserId: employee.managerDingtalkUserId,
|
|
}
|
|
}
|
|
|
|
async function loadSubAdminDingtalkUserIds() {
|
|
try {
|
|
return await getDingtalkSubAdminUserIds()
|
|
} catch {
|
|
throw new TaskDetailChangeApprovalError('暂时无法读取钉钉子管理员,请稍后重试', 503)
|
|
}
|
|
}
|
|
|
|
async function resolveManager(
|
|
employee: NonNullable<Awaited<ReturnType<typeof getEmployeeByUserId>>>,
|
|
subAdminDingtalkUserIds: string[],
|
|
) {
|
|
let resolvedManager
|
|
try {
|
|
resolvedManager = await resolveOrganizationSubAdminProfile(employee, subAdminDingtalkUserIds)
|
|
} catch (error) {
|
|
if (error instanceof EmployeeManagerResolutionError) {
|
|
throw new TaskDetailChangeApprovalError(error.message, 409)
|
|
}
|
|
throw error
|
|
}
|
|
const managerDingtalkUserId = clean(resolvedManager.dingtalkUserId)
|
|
if (!managerDingtalkUserId) {
|
|
throw new TaskDetailChangeApprovalError('直属主管缺少钉钉唯一值,暂不能提交图片变更申请', 409)
|
|
}
|
|
const manager = await prisma.performanceSurveyEmployeeProfile.findUnique({
|
|
where: { dingtalkUserId: managerDingtalkUserId },
|
|
include: {
|
|
userBindings: {
|
|
where: { status: BINDING_STATUS.ACTIVE },
|
|
select: { userId: true },
|
|
take: 1,
|
|
},
|
|
},
|
|
})
|
|
if (!manager || !isRosterEmployed(manager.employmentState)) {
|
|
throw new TaskDetailChangeApprovalError('直属主管账号无有效在职档案,暂不能提交图片变更申请', 409)
|
|
}
|
|
return {
|
|
employee: manager,
|
|
userId: manager.userBindings[0]?.userId || null,
|
|
dingtalkUserId: managerDingtalkUserId,
|
|
name: manager.displayName || manager.realName || employee.managerName || '直属主管',
|
|
}
|
|
}
|
|
|
|
function actionUrl(requestId: string) {
|
|
return `${getPublicAppOrigin()}${buildDingtalkEntryPath(`/task-detail-change-requests/${requestId}`)}`
|
|
}
|
|
|
|
async function queueManagerNotification(request: {
|
|
id: string
|
|
managerUserId: string | null
|
|
managerDingtalkUserId: string
|
|
managerName: string
|
|
requesterName: string
|
|
material: { taskNo: string | null; productModel: string }
|
|
operations: unknown[]
|
|
expiresAt: Date
|
|
}) {
|
|
await queueTaskDetailChangeNotification({
|
|
requestId: request.id,
|
|
recipientUserId: request.managerUserId,
|
|
dingtalkUserId: request.managerDingtalkUserId,
|
|
recipientName: request.managerName,
|
|
eventType: 'task_detail_change.requested',
|
|
businessKey: `task-detail-change:${request.id}:requested`,
|
|
title: '任务详情图片变更待确认',
|
|
content: [
|
|
`**素材:** ${request.material.taskNo || request.material.productModel}`,
|
|
`**提交人:** ${request.requesterName}`,
|
|
`**变更数量:** ${request.operations.length} 项`,
|
|
`**截止时间:** ${request.expiresAt.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })}`,
|
|
'',
|
|
'> 未确认前正式素材不会改变;超过截止时间将自动否决。',
|
|
'',
|
|
TASK_DETAIL_REVIEW_PROMPT,
|
|
].join('\n'),
|
|
actionUrl: actionUrl(request.id),
|
|
actionTitle: '立即确认',
|
|
})
|
|
}
|
|
|
|
async function queueRequesterResultNotification(request: any, label: string) {
|
|
const dingtalkUserId = clean(request.requesterEmployee?.dingtalkUserId)
|
|
if (!dingtalkUserId) return
|
|
await queueTaskDetailChangeNotification({
|
|
requestId: request.id,
|
|
recipientUserId: request.requesterUserId,
|
|
dingtalkUserId,
|
|
recipientName: request.requesterName,
|
|
eventType: `task_detail_change.${request.status}`,
|
|
businessKey: `task-detail-change:${request.id}:${request.status}`,
|
|
title: '任务详情图片变更结果',
|
|
content: `您的图片变更申请已${label}。\n\n> 素材:${request.material.taskNo || request.material.productModel}`,
|
|
actionUrl: actionUrl(request.id),
|
|
actionTitle: '查看结果',
|
|
})
|
|
}
|
|
|
|
function storeOperations(
|
|
operations: TaskDetailChangeOperationInput[],
|
|
groups: Map<TaskDetailEditGroup, TaskDetailChangeOperationInput[]>,
|
|
staged: Map<string, TaskDetailStagedFile>,
|
|
): StoredTaskDetailChangeOperation[] {
|
|
const getStagedFile = (uploadId: string) => {
|
|
const stagedFile = staged.get(uploadId)
|
|
if (!stagedFile) throw new TaskDetailChangeApprovalError('图片持久暂存结果缺失', 500)
|
|
return stagedFile
|
|
}
|
|
const groupsByOperation = new Map(
|
|
Array.from(groups, ([group, edits]) => edits.map((edit) => [edit, group] as const)).flat(),
|
|
)
|
|
return operations.map((operation) => {
|
|
if (operation.kind === 'group_images') {
|
|
const { uploadIds, ...rest } = operation
|
|
return { ...rest, stagedFiles: uploadIds.map(getStagedFile) }
|
|
}
|
|
if ('assetId' in operation) {
|
|
const group = groupsByOperation.get(operation)
|
|
if (operation.kind !== 'replace') return { ...operation, group }
|
|
const { uploadId, ...rest } = operation
|
|
return { ...rest, group, stagedFile: getStagedFile(uploadId) }
|
|
}
|
|
const { uploadId, ...rest } = operation
|
|
return { ...rest, stagedFile: getStagedFile(uploadId) }
|
|
})
|
|
}
|
|
|
|
function applyOperations(requestId: string, operations: StoredTaskDetailChangeOperation[]) {
|
|
return operations.map((operation) => {
|
|
if (operation.kind === 'group_images') {
|
|
const { stagedFiles, ...rest } = operation
|
|
return {
|
|
...rest,
|
|
files: stagedFiles.map((file) => ({ stagedFile: resolveTaskDetailStagedFile(requestId, file) })),
|
|
}
|
|
}
|
|
if (!('stagedFile' in operation)) return operation
|
|
return {
|
|
...operation,
|
|
stagedFile: resolveTaskDetailStagedFile(requestId, operation.stagedFile),
|
|
}
|
|
})
|
|
}
|
|
|
|
export async function submitTaskDetailChange(
|
|
materialId: string,
|
|
actor: Actor,
|
|
rawOperations: unknown,
|
|
) {
|
|
const operations = normalizeOperations(rawOperations)
|
|
const { material, employee } = await loadAuthorizedContext(materialId, actor)
|
|
const groupEdits = operations.filter((operation) => operation.kind === 'group_images')
|
|
const groups = groupTaskDetailImageEdits(operations, material.assets)
|
|
const uploadIds = operations.flatMap((item) => {
|
|
if (item.kind === 'group_images') return item.uploadIds
|
|
return 'uploadId' in item ? [item.uploadId] : []
|
|
})
|
|
if (new Set(uploadIds).size !== uploadIds.length) {
|
|
throw new TaskDetailChangeApprovalError('同一次变更不能重复使用同一个上传文件')
|
|
}
|
|
if (uploadIds.length > TASK_DETAIL_UPLOAD_LIMITS.maxTotalImageCount) {
|
|
throw new TaskDetailChangeApprovalError(`一次最多上传 ${TASK_DETAIL_UPLOAD_LIMITS.maxTotalImageCount} 张原图`)
|
|
}
|
|
try {
|
|
for (const edit of groupEdits) assertTaskDetailGroupSelection(edit.group, edit.assetIds, material.assets)
|
|
for (const group of Array.from(groups.keys())) {
|
|
await assertTaskDetailImageEditReady({
|
|
materialId,
|
|
operatorUserId: actor.id,
|
|
operatorRole: actor.role,
|
|
group,
|
|
})
|
|
}
|
|
} catch (error) {
|
|
if (!(error instanceof TaskDetailAssetEditError)) throw error
|
|
const explanation = error.message.replace(/请删除后重新上传。?/g, '请联系管理员核对原图与预览图的对应关系。')
|
|
throw new TaskDetailChangeApprovalError(`图片变更暂不能提交:${explanation}`, error.statusCode)
|
|
}
|
|
const isPlatformAdmin = actor.isSuperAdmin || isSystemAdmin(actor.role)
|
|
const subAdminDingtalkUserIds = !isPlatformAdmin && employee ? await loadSubAdminDingtalkUserIds() : []
|
|
const dingtalkUserId = clean(employee?.dingtalkUserId)
|
|
const isActiveSubAdmin = employee?.employmentState === 'ACTIVE'
|
|
&& dingtalkUserId !== null
|
|
&& subAdminDingtalkUserIds.includes(dingtalkUserId)
|
|
if (isPlatformAdmin || isActiveSubAdmin) {
|
|
const data = await batchEditTaskDetailImages({
|
|
materialId,
|
|
operatorUserId: actor.id,
|
|
operatorRole: actor.role,
|
|
...(groupEdits.length > 0 || groups.size > 1 ? { expectedMaterialUpdatedAt: material.updatedAt } : {}),
|
|
operations: operations.map((operation) => {
|
|
if (operation.kind !== 'group_images') return operation
|
|
const { uploadIds, ...rest } = operation
|
|
return { ...rest, files: uploadIds.map((uploadId) => ({ uploadId })) }
|
|
}),
|
|
})
|
|
return { mode: 'applied' as const, data }
|
|
}
|
|
if (!employee) {
|
|
throw new TaskDetailChangeApprovalError('当前账号没有匹配花名册,暂不能提交主管审批', 403)
|
|
}
|
|
const manager = await resolveManager(employee, subAdminDingtalkUserIds)
|
|
const requestId = randomUUID()
|
|
const expiresAt = new Date(Date.now() + APPROVAL_WINDOW_MS)
|
|
const request = await withTaskDetailStagedUploads({
|
|
requestId,
|
|
ownerUserId: actor.id,
|
|
uploadIds,
|
|
enforceImageLimits: true,
|
|
commit: async (staged) => {
|
|
const storedOperations = storeOperations(operations, groups, staged)
|
|
return prisma.taskDetailChangeRequest.create({
|
|
data: {
|
|
id: requestId,
|
|
materialId,
|
|
requesterUserId: actor.id,
|
|
requesterEmployeeId: employee.employeeProfileId,
|
|
requesterName: employee.displayName,
|
|
requesterSnapshot: employeeSnapshot(employee) as any,
|
|
requesterPositionLevel: employee.positionLevel,
|
|
requesterLevelType: employee.levelType,
|
|
requesterIsLeadership: employee.isLeadership,
|
|
managerEmployeeId: manager.employee.id,
|
|
managerUserId: manager.userId,
|
|
managerDingtalkUserId: manager.dingtalkUserId,
|
|
managerName: manager.name,
|
|
managerSnapshot: {
|
|
id: manager.employee.id,
|
|
dingtalkUserId: manager.employee.dingtalkUserId,
|
|
jobNumber: manager.employee.jobNumber,
|
|
displayName: manager.name,
|
|
departmentPath: manager.employee.departmentPath,
|
|
positionLevel: manager.employee.positionLevel,
|
|
} as any,
|
|
status: REQUEST_STATUS.PENDING,
|
|
operations: storedOperations as any,
|
|
baseMaterialUpdatedAt: material.updatedAt,
|
|
expiresAt,
|
|
},
|
|
include: { material: { select: { taskNo: true, productModel: true } } },
|
|
})
|
|
},
|
|
})
|
|
|
|
await queueManagerNotification({
|
|
...request,
|
|
operations,
|
|
expiresAt,
|
|
}).catch((error) => {
|
|
console.error('[task-detail-change] 审批单已创建,但主管通知排队失败:', error)
|
|
})
|
|
return {
|
|
mode: 'pending_approval' as const,
|
|
request: {
|
|
id: request.id,
|
|
status: request.status,
|
|
managerName: request.managerName,
|
|
expiresAt: request.expiresAt,
|
|
},
|
|
}
|
|
}
|
|
|
|
async function loadRequestForActor(id: string, actor: Actor) {
|
|
const [request, employee] = await Promise.all([
|
|
prisma.taskDetailChangeRequest.findUnique({
|
|
where: { id },
|
|
include: {
|
|
material: { select: { id: true, taskNo: true, productModel: true, materialType: true, updatedAt: true, deletedAt: true, ...TASK_DETAIL_PRIVACY_SELECT } },
|
|
requesterEmployee: { select: { dingtalkUserId: true } },
|
|
},
|
|
}),
|
|
getEmployeeByUserId(actor.id),
|
|
])
|
|
if (!request) throw new TaskDetailChangeApprovalError('图片变更申请不存在', 404)
|
|
if (!(await canAccessMaterialPrivacy(request.material, actor.id))) {
|
|
throw new TaskDetailChangeApprovalError('无权限访问该私密素材', 403)
|
|
}
|
|
const managerIsEmployed = Boolean(employee && isRosterEmployed(employee.employmentState))
|
|
const canDecide = Boolean(
|
|
managerIsEmployed
|
|
&& employee?.dingtalkUserId
|
|
&& employee.dingtalkUserId === request.managerDingtalkUserId,
|
|
)
|
|
const canView = canDecide
|
|
|| request.requesterUserId === actor.id
|
|
|| actor.isSuperAdmin
|
|
|| isSystemAdmin(String(actor.role || ''))
|
|
if (!canView) throw new TaskDetailChangeApprovalError('您无权查看这条图片变更申请', 403)
|
|
return { request, employee, canDecide }
|
|
}
|
|
|
|
export async function getTaskDetailChangeRequest(id: string, actor: Actor) {
|
|
const { request, canDecide } = await loadRequestForActor(id, actor)
|
|
const operations = (Array.isArray(request.operations) ? request.operations : []) as StoredTaskDetailChangeOperation[]
|
|
return {
|
|
id: request.id,
|
|
status: request.status,
|
|
requesterName: request.requesterName,
|
|
managerName: request.managerName,
|
|
material: request.material,
|
|
imagePreviews: hasCurrentTaskDetailReviewImages(request)
|
|
? operations.map((operation) => {
|
|
const sources = getTaskDetailReviewSources(operation)
|
|
return { before: sources.before.length, after: sources.after.length }
|
|
})
|
|
: [],
|
|
operations: operations.map((item) => {
|
|
if (item.kind === 'group_images') {
|
|
return {
|
|
kind: item.kind,
|
|
group: item.group,
|
|
affectedCount: item.assetIds.length,
|
|
fileNames: item.stagedFiles.map((file) => file.fileName),
|
|
}
|
|
}
|
|
if ('stagedFile' in item) {
|
|
const { stagedFile, ...operation } = item
|
|
return { ...operation, fileName: stagedFile.fileName }
|
|
}
|
|
return { ...item, fileName: null }
|
|
}),
|
|
submittedAt: request.submittedAt,
|
|
expiresAt: request.expiresAt,
|
|
decidedAt: request.decidedAt,
|
|
appliedAt: request.appliedAt,
|
|
applyError: request.applyError,
|
|
canDecide: canDecide && request.status === REQUEST_STATUS.PENDING && request.expiresAt > new Date(),
|
|
}
|
|
}
|
|
|
|
export async function getTaskDetailChangePreview(id: string, actor: Actor, input: {
|
|
operationIndex: number
|
|
side: TaskDetailReviewSide
|
|
index: number
|
|
}) {
|
|
const { request } = await loadRequestForActor(id, actor)
|
|
const operations = Array.isArray(request.operations) ? request.operations as StoredTaskDetailChangeOperation[] : []
|
|
const operation = operations[input.operationIndex]
|
|
if (!operation || !hasCurrentTaskDetailReviewImages(request)) {
|
|
throw new TaskDetailChangeApprovalError('图片预览已不可用,请刷新审批页', 404)
|
|
}
|
|
const image = await readTaskDetailReviewImage({
|
|
requestId: request.id, materialId: request.materialId, operation, side: input.side, index: input.index,
|
|
})
|
|
if (!image) throw new TaskDetailChangeApprovalError('图片预览不存在或暂不可读', 404)
|
|
return image
|
|
}
|
|
|
|
export async function listTaskDetailChanges(materialId: string, actor: Actor) {
|
|
const material = await prisma.material.findFirst({
|
|
where: { id: materialId, deletedAt: null },
|
|
select: TASK_DETAIL_PRIVACY_SELECT,
|
|
})
|
|
if (!material || !(await canAccessMaterialPrivacy(material, actor.id))) {
|
|
throw new TaskDetailChangeApprovalError('无权限访问该素材', 403)
|
|
}
|
|
const employee = await getEmployeeByUserId(actor.id)
|
|
const privileged = Boolean(actor.isSuperAdmin || isSystemAdmin(String(actor.role || '')))
|
|
const rows = await prisma.taskDetailChangeRequest.findMany({
|
|
where: {
|
|
materialId,
|
|
...(privileged
|
|
? {}
|
|
: {
|
|
OR: [
|
|
{ requesterUserId: actor.id },
|
|
...(employee?.dingtalkUserId
|
|
? [{ managerDingtalkUserId: employee.dingtalkUserId }]
|
|
: []),
|
|
],
|
|
}),
|
|
},
|
|
orderBy: { createdAt: 'desc' },
|
|
take: 10,
|
|
select: {
|
|
id: true,
|
|
status: true,
|
|
requesterName: true,
|
|
managerName: true,
|
|
operations: true,
|
|
submittedAt: true,
|
|
expiresAt: true,
|
|
decidedAt: true,
|
|
appliedAt: true,
|
|
applyError: true,
|
|
},
|
|
})
|
|
return rows.map((row) => ({
|
|
...row,
|
|
operationCount: Array.isArray(row.operations) ? row.operations.length : 0,
|
|
operations: undefined,
|
|
}))
|
|
}
|
|
|
|
export async function decideTaskDetailChange(
|
|
id: string,
|
|
actor: Actor,
|
|
decision: 'approve' | 'reject',
|
|
) {
|
|
const { request, canDecide } = await loadRequestForActor(id, actor)
|
|
if (!canDecide) throw new TaskDetailChangeApprovalError('只有申请人的直属主管可以确认', 403)
|
|
const now = new Date()
|
|
if (request.status !== REQUEST_STATUS.PENDING) {
|
|
throw new TaskDetailChangeApprovalError('该申请已经处理,不能重复确认', 409)
|
|
}
|
|
if (request.expiresAt <= now) {
|
|
const expired = await prisma.taskDetailChangeRequest.updateMany({
|
|
where: { id, status: REQUEST_STATUS.PENDING },
|
|
data: { status: REQUEST_STATUS.EXPIRED, decidedAt: now },
|
|
})
|
|
if (expired.count === 1) {
|
|
await cleanupTaskDetailChangeStaging(id).catch(() => undefined)
|
|
const final = await prisma.taskDetailChangeRequest.findUnique({
|
|
where: { id },
|
|
include: {
|
|
material: { select: { taskNo: true, productModel: true } },
|
|
requesterEmployee: { select: { dingtalkUserId: true } },
|
|
},
|
|
})
|
|
if (final) {
|
|
await queueRequesterResultNotification(final, '因超过 24 小时未确认而自动否决').catch(() => undefined)
|
|
}
|
|
}
|
|
throw new TaskDetailChangeApprovalError('该申请已超时并自动否决', 409)
|
|
}
|
|
|
|
if (decision === 'reject') {
|
|
const updated = await prisma.taskDetailChangeRequest.updateMany({
|
|
where: { id, status: REQUEST_STATUS.PENDING, expiresAt: { gt: now } },
|
|
data: {
|
|
status: REQUEST_STATUS.REJECTED,
|
|
decidedAt: now,
|
|
decidedByUserId: actor.id,
|
|
},
|
|
})
|
|
if (updated.count !== 1) throw new TaskDetailChangeApprovalError('申请状态已变化,请刷新', 409)
|
|
await cleanupTaskDetailChangeStaging(id).catch(() => undefined)
|
|
const final = await prisma.taskDetailChangeRequest.findUnique({
|
|
where: { id },
|
|
include: {
|
|
material: { select: { taskNo: true, productModel: true } },
|
|
requesterEmployee: { select: { dingtalkUserId: true } },
|
|
},
|
|
})
|
|
if (final) await queueRequesterResultNotification(final, '被主管否决').catch(() => undefined)
|
|
return { status: REQUEST_STATUS.REJECTED }
|
|
}
|
|
|
|
const claimed = await prisma.taskDetailChangeRequest.updateMany({
|
|
where: { id, status: REQUEST_STATUS.PENDING, expiresAt: { gt: now } },
|
|
data: {
|
|
status: REQUEST_STATUS.APPLYING,
|
|
decidedAt: now,
|
|
decidedByUserId: actor.id,
|
|
},
|
|
})
|
|
if (claimed.count !== 1) throw new TaskDetailChangeApprovalError('申请状态已变化,请刷新', 409)
|
|
|
|
try {
|
|
const material = await prisma.material.findUnique({
|
|
where: { id: request.materialId },
|
|
select: { updatedAt: true },
|
|
})
|
|
if (!material || material.updatedAt.getTime() !== request.baseMaterialUpdatedAt.getTime()) {
|
|
throw new TaskDetailChangeApprovalError('正式素材在审批期间已更新,本申请已停止应用', 409)
|
|
}
|
|
const operations = applyOperations(
|
|
id,
|
|
(Array.isArray(request.operations) ? request.operations : []) as StoredTaskDetailChangeOperation[],
|
|
)
|
|
await batchEditTaskDetailImages({
|
|
materialId: request.materialId,
|
|
operatorUserId: request.requesterUserId,
|
|
expectedMaterialUpdatedAt: request.baseMaterialUpdatedAt,
|
|
approvalRequestId: id,
|
|
operations: operations as any,
|
|
})
|
|
await cleanupTaskDetailChangeStaging(id).catch(() => undefined)
|
|
const final = await prisma.taskDetailChangeRequest.findUnique({
|
|
where: { id },
|
|
include: {
|
|
material: { select: { taskNo: true, productModel: true } },
|
|
requesterEmployee: { select: { dingtalkUserId: true } },
|
|
},
|
|
})
|
|
if (final) await queueRequesterResultNotification(final, '由直属主管批准并生效').catch(() => undefined)
|
|
return { status: REQUEST_STATUS.APPLIED }
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : '批准后应用图片变更失败'
|
|
await prisma.taskDetailChangeRequest.update({
|
|
where: { id },
|
|
data: { status: REQUEST_STATUS.APPLY_FAILED, applyError: message },
|
|
})
|
|
await cleanupTaskDetailChangeStaging(id).catch(() => undefined)
|
|
const final = await prisma.taskDetailChangeRequest.findUnique({
|
|
where: { id },
|
|
include: {
|
|
material: { select: { taskNo: true, productModel: true } },
|
|
requesterEmployee: { select: { dingtalkUserId: true } },
|
|
},
|
|
})
|
|
if (final) {
|
|
await queueRequesterResultNotification(final, '在批准后应用失败,请重新提交').catch(() => undefined)
|
|
}
|
|
if (error instanceof TaskDetailAssetEditError) {
|
|
throw new TaskDetailChangeApprovalError(message, error.statusCode)
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function expireDueTaskDetailChanges(now = new Date()) {
|
|
const due = await prisma.taskDetailChangeRequest.findMany({
|
|
where: { status: REQUEST_STATUS.PENDING, expiresAt: { lte: now } },
|
|
select: { id: true },
|
|
orderBy: { expiresAt: 'asc' },
|
|
take: 50,
|
|
})
|
|
let expired = 0
|
|
for (const row of due) {
|
|
const changed = await prisma.taskDetailChangeRequest.updateMany({
|
|
where: { id: row.id, status: REQUEST_STATUS.PENDING, expiresAt: { lte: now } },
|
|
data: { status: REQUEST_STATUS.EXPIRED, decidedAt: now },
|
|
})
|
|
if (changed.count !== 1) continue
|
|
expired += 1
|
|
await cleanupTaskDetailChangeStaging(row.id).catch(() => undefined)
|
|
const final = await prisma.taskDetailChangeRequest.findUnique({
|
|
where: { id: row.id },
|
|
include: {
|
|
material: { select: { taskNo: true, productModel: true } },
|
|
requesterEmployee: { select: { dingtalkUserId: true } },
|
|
},
|
|
})
|
|
if (final) await queueRequesterResultNotification(final, '因超过 24 小时未确认而自动否决').catch(() => undefined)
|
|
}
|
|
return expired
|
|
}
|
|
|
|
/**
|
|
* 补偿“审批单已持久化、通知 outbox 落库前进程退出/数据库瞬时失败”的窄窗口。
|
|
* 唯一键保证多实例并发扫描也只会生成一条待发送通知。
|
|
*/
|
|
export async function recoverMissingTaskDetailChangeRequestNotifications() {
|
|
const rows = await prisma.taskDetailChangeRequest.findMany({
|
|
where: {
|
|
status: REQUEST_STATUS.PENDING,
|
|
expiresAt: { gt: new Date() },
|
|
notifications: {
|
|
none: { eventType: 'task_detail_change.requested' },
|
|
},
|
|
},
|
|
orderBy: { createdAt: 'asc' },
|
|
take: 20,
|
|
include: {
|
|
material: { select: { taskNo: true, productModel: true } },
|
|
},
|
|
})
|
|
let queued = 0
|
|
for (const request of rows) {
|
|
await queueManagerNotification({
|
|
id: request.id,
|
|
managerUserId: request.managerUserId,
|
|
managerDingtalkUserId: request.managerDingtalkUserId,
|
|
managerName: request.managerName,
|
|
requesterName: request.requesterName,
|
|
material: request.material,
|
|
operations: Array.isArray(request.operations) ? request.operations : [],
|
|
expiresAt: request.expiresAt,
|
|
})
|
|
queued += 1
|
|
}
|
|
return queued
|
|
}
|
|
|
|
/**
|
|
* 正式素材更新与 request=applied 在同一数据库事务提交;因此长时间停留在
|
|
* applying 只可能是进程中断或处理超时。先转失败会让迟到事务因状态不匹配而回滚。
|
|
*/
|
|
export async function recoverStaleApplyingTaskDetailChanges(now = new Date()) {
|
|
const staleBefore = new Date(now.getTime() - STALE_APPLYING_MS)
|
|
const rows = await prisma.taskDetailChangeRequest.findMany({
|
|
where: { status: REQUEST_STATUS.APPLYING, updatedAt: { lte: staleBefore } },
|
|
select: { id: true },
|
|
orderBy: { updatedAt: 'asc' },
|
|
take: 50,
|
|
})
|
|
let failed = 0
|
|
for (const row of rows) {
|
|
const changed = await prisma.taskDetailChangeRequest.updateMany({
|
|
where: {
|
|
id: row.id,
|
|
status: REQUEST_STATUS.APPLYING,
|
|
updatedAt: { lte: staleBefore },
|
|
},
|
|
data: {
|
|
status: REQUEST_STATUS.APPLY_FAILED,
|
|
applyError: '批准后的图片应用进程中断或超时,请重新提交变更',
|
|
},
|
|
})
|
|
if (changed.count !== 1) continue
|
|
failed += 1
|
|
await cleanupTaskDetailChangeStaging(row.id).catch(() => undefined)
|
|
const final = await prisma.taskDetailChangeRequest.findUnique({
|
|
where: { id: row.id },
|
|
include: {
|
|
material: { select: { taskNo: true, productModel: true } },
|
|
requesterEmployee: { select: { dingtalkUserId: true } },
|
|
},
|
|
})
|
|
if (final) {
|
|
await queueRequesterResultNotification(final, '因应用进程中断或超时而失败,请重新提交').catch(() => undefined)
|
|
}
|
|
}
|
|
return failed
|
|
}
|