fix(materials): open approval notifications in desktop browsers

This commit is contained in:
WuYongJie 2026-11-27 15:50:39 +08:00
parent fc273c69f5
commit 705d1e28b1
2 changed files with 68 additions and 1 deletions

View File

@ -0,0 +1,54 @@
import { headers } from 'next/headers'
import { redirect } from 'next/navigation'
import { getServerSession } from 'next-auth'
import DingtalkEntryPage from './page'
import DingtalkEntryClient from './DingtalkEntryClient'
jest.mock('next/headers', () => ({ headers: jest.fn() }))
jest.mock('next/navigation', () => ({
redirect: jest.fn((path: string) => { throw new Error(path) }),
}))
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
const approvalPath = '/task-detail-change-requests/request-1'
function setUserAgent(userAgent: string) {
jest.mocked(headers).mockReturnValue(new Headers({ 'user-agent': userAgent }))
}
describe('DingtalkEntryPage', () => {
beforeEach(() => {
jest.clearAllMocks()
setUserAgent('Mozilla/5.0 Chrome/130.0')
jest.mocked(getServerSession).mockResolvedValue(null)
})
it('takes a signed-out browser to login with the original approval path', async () => {
await expect(DingtalkEntryPage({ searchParams: { redirect: approvalPath } })).rejects.toThrow('/login?redirect=')
expect(redirect).toHaveBeenCalledWith(`/login?redirect=${encodeURIComponent(approvalPath)}`)
})
it('takes a signed-in browser directly to the approval', async () => {
jest.mocked(getServerSession).mockResolvedValue({ user: { name: '伯言' }, expires: '2026-11-28' })
await expect(DingtalkEntryPage({ searchParams: { redirect: approvalPath } })).rejects.toThrow(approvalPath)
expect(redirect).toHaveBeenCalledWith(approvalPath)
})
it('uses the in-app login flow for an approval opened inside DingTalk', async () => {
setUserAgent('Mozilla/5.0 DingTalk/8.0')
const page = await DingtalkEntryPage({ searchParams: { redirect: approvalPath, corpId: 'ding-corp' } })
expect(page.type).toBe(DingtalkEntryClient)
expect(page.props.redirectPath).toBe(approvalPath)
expect(getServerSession).not.toHaveBeenCalled()
})
it('keeps performance surveys on their existing notification entry', async () => {
const surveyPath = '/performance-survey/survey/survey-1'
const page = await DingtalkEntryPage({ searchParams: { redirect: surveyPath, corpId: 'ding-corp' } })
expect(page.type).toBe(DingtalkEntryClient)
expect(page.props.redirectPath).toBe(surveyPath)
expect(redirect).not.toHaveBeenCalled()
})
})

View File

@ -1,3 +1,8 @@
import { headers } from 'next/headers'
import { redirect } from 'next/navigation'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { getConfiguredDingtalkCorpId, normalizeDingtalkCorpId } from '@/lib/dingtalk-corp'
import { normalizeInternalRedirectPath } from '@/lib/safe-redirect'
import DingtalkEntryClient from './DingtalkEntryClient'
@ -13,8 +18,16 @@ type DingtalkEntryPageProps = {
}
}
export default function DingtalkEntryPage({ searchParams }: DingtalkEntryPageProps) {
export default async function DingtalkEntryPage({ searchParams }: DingtalkEntryPageProps) {
const redirectPath = normalizeInternalRedirectPath(searchParams?.redirect, '/performance-survey')
if (
redirectPath.startsWith('/task-detail-change-requests/')
&& !/DingTalk/i.test(headers().get('user-agent') || '')
) {
const session = await getServerSession(authOptions)
redirect(session?.user ? redirectPath : `/login?redirect=${encodeURIComponent(redirectPath)}`)
}
const corpId =
normalizeDingtalkCorpId(searchParams?.corpId || searchParams?.corpid) ||
getConfiguredDingtalkCorpId()