All files / pages/api welcome.ts

0% Statements 0/18
0% Branches 0/12
0% Functions 0/3
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                                                               
import { NextApiRequest, NextApiResponse } from 'next'
import { getServerSession } from 'next-auth'
import { authOptions } from './auth/[...nextauth]'
import { getInboxPref } from '../../lib/inbox-preferences'
 
// TODO: Improve user experience by making this similar to login screen?
export default async function welcome(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  const session = await getServerSession(req, res, authOptions)
  const name = session?.user.name
  const spid = name ? name.split('|')[1] : ''
 
  const locale = req.query['locale'] ? req.query['locale'].toString() : '' // user input, potentially unsafe
  const safeLocale = locale === 'en' ? 'en' : 'fr'
 
  res.setHeader('Content-Type', 'text/html; charset=utf-8')
  try {
    const resp = await getInboxPref(spid)
    const noNotificationPref = resp.subscribedEvents.length === 0
    const redirectDestination = noNotificationPref
      ? getResUrl(safeLocale)
      : getDashboardUrl(safeLocale)
    res.redirect(redirectDestination)
  } catch {
    res.redirect(getDashboardUrl(safeLocale))
  }
}
 
function getResUrl(locale: string) {
  return locale === 'en'
    ? '/inbox-now-available'
    : '/boite-reception-disponibles'
}
 
function getDashboardUrl(locale: string) {
  return locale === 'en' ? '/my-dashboard' : '/mon-tableau-de-bord'
}