import AppError from "../../errors/AppError";
import SchedulingNotify from "../../models/SchedulingNotify";


interface SchedulingData {
  name?: string
}

interface Request {
  schedulingData: SchedulingData
  schedulingDataId: string
}

const UpdateSchedulingNotify = async ({
  schedulingData,
  schedulingDataId

}: Request): Promise<SchedulingNotify> => {

  try {

    const { name } = schedulingData;

    const updateScheduling = await SchedulingNotify.findOne({
      where: { id: schedulingDataId },
      attributes: ["id", "name"]
    });

    if (!updateScheduling) {
      //console.log('NOT FOUND SCHEDULING NOTIFY')
      throw new AppError("ERR_NO_SCHEDULING_NOTIFY_FOUND", 404);
    }
    await updateScheduling.update({ name });

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

    return updateScheduling;

  } catch (error: any) {
    console.error('===> Error on CreateSchedulingNotifyService.ts file: \n', error)
    throw new AppError(error.message);
  }

};

export default UpdateSchedulingNotify;
