import Position from "../../models/Position";
import AppError from "../../errors/AppError";
import AssociateQuickAnswerQueue from "../QueueService/AssociateQuickAnswerQueue";

interface PositionData {
  name: string;
}

interface Request {
  positionData: PositionData;
  positionId: string;
}

const UpdatePositionService = async ({
  positionData,
  positionId
}: Request): Promise<Position> => {
  const { name } = positionData;

  const position = await Position.findOne({
    where: { id: positionId }
  });

  if (!position) {
    throw new AppError("ERR_NO_POSITION_FOUND", 404);
  }
  await position.update({
    name
  });

  await position.reload({
    attributes: ["id", "name"]
  });

  return position;
};

export default UpdatePositionService;
