import { createParamDecorator, ExecutionContext } from "@nestjs/common";
import { UserDto } from "src/identity-and-account/users-management/user.dto";

export interface AuthenticatedRequest extends Request {
    user: UserDto;
}

export const Access = createParamDecorator<keyof UserDto | undefined>(
    (data, ctx: ExecutionContext) => {
        const request = ctx.switchToHttp().getRequest<AuthenticatedRequest>();
        const user: UserDto = request.user;
        return data ? user?.[data] : user;
    },
);