import { omit } from 'lodash';
import { NextRequest, NextResponse } from 'next/server';

import { validationErrorHandler } from '../../middlewares/error-handler.middleware';

const routeHandler = async (req: NextRequest) => {
  const bonusTokenData = req.cookies.get('corporate__token');

  if (!bonusTokenData) {
    return NextResponse.json(
      { error: 'User does not have access' },
      { status: 401 },
    );
  }

  const parsedData = JSON.parse(bonusTokenData.value);

  return new Response(JSON.stringify(omit(parsedData, 'token')), {
    headers: { 'Content-Type': 'application/json' },
  });
};

export const GET = validationErrorHandler(routeHandler);
