From b20ed4c8dbce1da91e7ce4f5104e64a9dbd37213 Mon Sep 17 00:00:00 2001 From: canove Date: Thu, 23 Jul 2020 17:15:47 -0300 Subject: [PATCH] Create custom field table and assocs --- backend/src/controllers/ContactController.js | 17 ++++++++ backend/src/database/index.js | 3 +- ...723200315-create-contacts-custom-fields.js | 41 +++++++++++++++++++ backend/src/models/Contact.js | 4 ++ backend/src/models/ContactCustomField.js | 26 ++++++++++++ backend/src/routes/contacts.js | 4 ++ frontend/src/pages/Contacts/ContactModal.js | 29 ++++++++++++- frontend/src/pages/Contacts/ContactsList.js | 15 ++++++- 8 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 backend/src/database/migrations/20200723200315-create-contacts-custom-fields.js create mode 100644 backend/src/models/ContactCustomField.js diff --git a/backend/src/controllers/ContactController.js b/backend/src/controllers/ContactController.js index 1e5d363..5d6bca6 100644 --- a/backend/src/controllers/ContactController.js +++ b/backend/src/controllers/ContactController.js @@ -1,6 +1,8 @@ const Sequelize = require("sequelize"); const Contact = require("../models/Contact"); +const ContactCustomField = require("../models/ContactCustomField"); + // const Message = require("../models/Message"); // const Sequelize = require("sequelize"); // const { getIO } = require("../libs/socket"); @@ -50,3 +52,18 @@ exports.store = async (req, res) => { res.status(200).json({ number, name }); }; + +exports.show = async (req, res) => { + const { contactId } = req.params; + + const { id, name, number, extraInfo } = await Contact.findByPk(contactId, { + include: [{ model: ContactCustomField, as: "extraInfo" }], + }); + + res.status(200).json({ + id, + name, + number, + extraInfo, + }); +}; diff --git a/backend/src/database/index.js b/backend/src/database/index.js index 74c6d1d..4f2eef4 100644 --- a/backend/src/database/index.js +++ b/backend/src/database/index.js @@ -6,8 +6,9 @@ const Contact = require("../models/Contact"); const Ticket = require("../models/Ticket"); const Message = require("../models/Message"); const Whatsapp = require("../models/Whatsapp"); +const ContactCustomField = require("../models/ContactCustomField"); -const models = [User, Contact, Ticket, Message, Whatsapp]; +const models = [User, Contact, Ticket, Message, Whatsapp, ContactCustomField]; class Database { constructor() { diff --git a/backend/src/database/migrations/20200723200315-create-contacts-custom-fields.js b/backend/src/database/migrations/20200723200315-create-contacts-custom-fields.js new file mode 100644 index 0000000..d990ad3 --- /dev/null +++ b/backend/src/database/migrations/20200723200315-create-contacts-custom-fields.js @@ -0,0 +1,41 @@ +"use strict"; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable("ContactCustomFields", { + id: { + type: Sequelize.INTEGER, + autoIncrement: true, + primaryKey: true, + allowNull: false, + }, + name: { + type: Sequelize.STRING, + allowNull: false, + }, + value: { + type: Sequelize.STRING, + allowNull: false, + }, + contactId: { + type: Sequelize.INTEGER, + references: { model: "Contacts", key: "id" }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + allowNull: false, + }, + createdAt: { + type: Sequelize.DATE, + allowNull: false, + }, + updatedAt: { + type: Sequelize.DATE, + allowNull: false, + }, + }); + }, + + down: queryInterface => { + return queryInterface.dropTable("ContactCustomFields"); + }, +}; diff --git a/backend/src/models/Contact.js b/backend/src/models/Contact.js index aa6609b..79ea4dd 100644 --- a/backend/src/models/Contact.js +++ b/backend/src/models/Contact.js @@ -18,6 +18,10 @@ class Contact extends Sequelize.Model { static associate(models) { this.hasMany(models.Ticket, { foreignKey: "contactId" }); + this.hasMany(models.ContactCustomField, { + foreignKey: "contactId", + as: "extraInfo", + }); } } diff --git a/backend/src/models/ContactCustomField.js b/backend/src/models/ContactCustomField.js new file mode 100644 index 0000000..24c6a74 --- /dev/null +++ b/backend/src/models/ContactCustomField.js @@ -0,0 +1,26 @@ +const Sequelize = require("sequelize"); + +class ContactCustomField extends Sequelize.Model { + static init(sequelize) { + super.init( + { + name: { type: Sequelize.STRING }, + value: { type: Sequelize.STRING }, + }, + { + sequelize, + } + ); + + return this; + } + + static associate(models) { + this.belongsTo(models.Contact, { + foreignKey: "contactId", + as: "extraInfo", + }); + } +} + +module.exports = ContactCustomField; diff --git a/backend/src/routes/contacts.js b/backend/src/routes/contacts.js index ef4719c..57eb3c2 100644 --- a/backend/src/routes/contacts.js +++ b/backend/src/routes/contacts.js @@ -7,6 +7,10 @@ const routes = express.Router(); routes.get("/contacts", isAuth, ContactController.index); +routes.get("/contacts/:contactId", isAuth, ContactController.show); + routes.post("/contacts", isAuth, ContactController.store); +// routes.put("/contacts/contactId", isAuth, ContactController.index); + module.exports = routes; diff --git a/frontend/src/pages/Contacts/ContactModal.js b/frontend/src/pages/Contacts/ContactModal.js index f70c4dd..c77ed15 100644 --- a/frontend/src/pages/Contacts/ContactModal.js +++ b/frontend/src/pages/Contacts/ContactModal.js @@ -15,6 +15,8 @@ import DeleteOutlineIcon from "@material-ui/icons/DeleteOutline"; import { makeStyles } from "@material-ui/core/styles"; +import api from "../../util/api"; + const useStyles = makeStyles(theme => ({ root: { display: "flex", @@ -55,12 +57,34 @@ const AddContactModal = ({ }, ], }); + + useEffect(() => { + const fetchContact = async () => { + if (!contactId) return; + const res = await api.get(`/contacts/${contactId}`); + setContact(res.data); + }; + + fetchContact(); + }, [contactId]); + const handleClose = () => { setModalOpen(false); + setContact({ + id: "", + name: "", + number: "", + email: "", + extraInfo: [ + { + id: "", + name: "", + value: "", + }, + ], + }); }; - useEffect(() => {}, [contactId]); - return (
{ setTimeout(() => { alert(JSON.stringify(values, null, 2)); diff --git a/frontend/src/pages/Contacts/ContactsList.js b/frontend/src/pages/Contacts/ContactsList.js index 358b6d9..64f7cf3 100644 --- a/frontend/src/pages/Contacts/ContactsList.js +++ b/frontend/src/pages/Contacts/ContactsList.js @@ -75,8 +75,9 @@ const Contacts = () => { const [count, setCount] = useState(0); const [searchParam, setSearchParam] = useState(""); const [contacts, setContacts] = useState([]); + const [selectedContactId, setSelectedContactId] = useState(null); - const [modalOpen, setModalOpen] = useState(true); + const [modalOpen, setModalOpen] = useState(false); useEffect(() => { setLoading(true); @@ -113,6 +114,7 @@ const Contacts = () => { }; const handleClickOpen = () => { + setSelectedContactId(null); setModalOpen(true); }; @@ -120,6 +122,11 @@ const Contacts = () => { setModalOpen(false); }; + const hadleEditContact = contactId => { + setSelectedContactId(contactId); + setModalOpen(true); + }; + return ( { onClose={handleClose} setModalOpen={setModalOpen} aria-labelledby="form-dialog-title" + contactId={selectedContactId} >
@@ -176,7 +184,10 @@ const Contacts = () => { {contact.number} {contact.updatedAt} - + hadleEditContact(contact.id)} + >