enabled socket events on contact controller

This commit is contained in:
canove
2020-09-20 11:47:46 -03:00
parent a879bb95ca
commit 23d16679b6
2 changed files with 20 additions and 18 deletions

View File

@@ -1,4 +1,5 @@
import { Request, Response } from "express";
import { getIO } from "../libs/socket";
import ListContactsService from "../services/ContactServices/ListContactsService";
import CreateContactService from "../services/ContactServices/CreateContactService";
@@ -6,7 +7,6 @@ import ShowContactService from "../services/ContactServices/ShowContactService";
import UpdateContactService from "../services/ContactServices/UpdateContactService";
import DeleteContactService from "../services/ContactServices/DeleteContactService";
// const { getIO } = require("../libs/socket");
// const { getWbot } = require("../libs/wbot");
type IndexQuery = {
@@ -57,7 +57,6 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
// }
// const wbot = getWbot(defaultWhatsapp);
// const io = getIO();
// try {
// const isValidNumber = await wbot.isRegisteredUser(
@@ -86,10 +85,11 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
// }
// );
// io.emit("contact", {
// action: "create",
// contact: contact
// });
const io = getIO();
io.emit("contact", {
action: "create",
contact
});
return res.status(200).json(contact);
};
@@ -112,11 +112,11 @@ export const update = async (
const contact = await UpdateContactService({ contactData, contactId });
// const io = getIO();
// io.emit("contact", {
// action: "update",
// contact: contact
// });
const io = getIO();
io.emit("contact", {
action: "update",
contact
});
return res.status(200).json(contact);
};
@@ -129,11 +129,11 @@ export const remove = async (
await DeleteContactService(contactId);
// const io = getIO();
// io.emit("contact", {
// action: "delete",
// contactId: contactId
// });
const io = getIO();
io.emit("contact", {
action: "delete",
contactId
});
return res.status(200).json({ message: "Contact deleted" });
};

View File

@@ -16,8 +16,8 @@ interface Request {
const CreateContactService = async ({
name,
number,
email,
extraInfo
email = "",
extraInfo = []
}: Request): Promise<Contact> => {
const numberExists = await Contact.findOne({
where: { number }
@@ -27,6 +27,8 @@ const CreateContactService = async ({
throw new AppError("A contact with this number already exists.");
}
console.log("extra", extraInfo);
const contact = await Contact.create(
{
name,