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

import { deleteTicketsByContactsCache } from '../../helpers/TicketCache'
import {deleteContactsByIdCache} from '../../helpers/ContactsCache'

const DeleteContactService = async (id: string): Promise<void> => {
  const contact = await Contact.findOne({
    where: { id }
  });

  if (!contact) {
    throw new AppError("ERR_NO_CONTACT_FOUND", 404);
  }

  // TEST DEL
  await deleteTicketsByContactsCache(contact.number)
  await deleteContactsByIdCache(contact.id)
  // 

  await contact.destroy();
};

export default DeleteContactService;
