66 lines
3.0 KiB
TypeScript
66 lines
3.0 KiB
TypeScript
import { isSystemAdmin } from '@/lib/department-permission'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { getPublicAppOrigin } from '@/lib/public-app-url'
|
|
import { submitYouyidianNotification } from '@/lib/youyidian-notification-center'
|
|
import { DesktopClientError } from './contract.mjs'
|
|
import { feedbackIdSchema, parseDesktopFeedback } from './feedback-contract'
|
|
|
|
export async function submitDesktopFeedback(ownerId: string, raw: string) {
|
|
const data = parseDesktopFeedback(raw)
|
|
const { feedbackId } = data
|
|
const { count } = await prisma.desktopClientFeedback.createMany({
|
|
data: { ...data, ownerId },
|
|
skipDuplicates: true,
|
|
})
|
|
const record = await prisma.desktopClientFeedback.findFirst({
|
|
where: { feedbackId, ownerId },
|
|
select: { feedbackId: true, status: true, createdAt: true },
|
|
})
|
|
if (!record) throw new DesktopClientError('反馈编号不可用', 409, 'feedback_id_conflict')
|
|
|
|
const detailPath = `/feedback/${feedbackId}`
|
|
if (count === 1) {
|
|
try {
|
|
const recipientUserId = process.env.YYD_DESKTOP_FEEDBACK_RECIPIENT_USER_ID?.trim()
|
|
if (!recipientUserId) throw new Error('Missing feedback recipient')
|
|
const result = await submitYouyidianNotification({
|
|
idempotencyKey: `desktop-feedback:${feedbackId}`,
|
|
recipientUserId,
|
|
title: '小壹问题反馈',
|
|
content: `收到一条小壹客户端问题反馈。反馈编号 ${feedbackId}`,
|
|
actionTitle: '查看反馈',
|
|
actionUrl: `${getPublicAppOrigin()}${detailPath}`,
|
|
})
|
|
if (result.status === 'failed') throw new Error('Feedback notification failed')
|
|
} catch {
|
|
// Errors from the external service can include request details. Only log the feedback ID.
|
|
console.warn('[desktop-feedback] 通知投递失败', { feedbackId })
|
|
}
|
|
}
|
|
return { feedback: { feedbackId, status: record.status, createdAt: record.createdAt, detailPath } }
|
|
}
|
|
|
|
export async function loadDesktopFeedback(id: string, viewerId: string) {
|
|
const parsedId = feedbackIdSchema.safeParse(id)
|
|
if (!parsedId.success) return { status: 404 } as const
|
|
|
|
const viewer = await prisma.user.findUnique({
|
|
where: { id: viewerId },
|
|
select: { role: true, isSuperAdmin: true, isActive: true, status: true, archivedAt: true },
|
|
})
|
|
if (!viewer?.isActive || viewer.status !== 'active' || viewer.archivedAt) return { status: 403 } as const
|
|
|
|
const feedbackId = parsedId.data
|
|
const canReadAll = viewer.isSuperAdmin || isSystemAdmin(viewer.role)
|
|
const feedback = await prisma.desktopClientFeedback.findFirst({
|
|
where: { feedbackId, ...(canReadAll ? {} : { ownerId: viewerId }) },
|
|
include: { owner: { select: { realName: true, username: true } } },
|
|
})
|
|
if (feedback) return { status: 200, feedback } as const
|
|
// For an ordinary viewer, only check existence so a 403 never loads another user's snapshot.
|
|
const exists = !canReadAll && await prisma.desktopClientFeedback.findUnique({
|
|
where: { feedbackId }, select: { feedbackId: true },
|
|
})
|
|
return exists ? { status: 403 } as const : { status: 404 } as const
|
|
}
|