/**
 * Current User Decorator
 *
 * Extracts current user from request.
 */

import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { CurrentUserData } from '../guards/jwt-auth.guard';

export const CurrentUser = createParamDecorator(
  (data: keyof CurrentUserData | undefined, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    const user = request.user as CurrentUserData;

    if (!user) {
      return undefined;
    }

    return data ? user[data] : user;
  },
);
