mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
Create custom field table and assocs
This commit is contained in:
@@ -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,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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");
|
||||
},
|
||||
};
|
||||
@@ -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",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
backend/src/models/ContactCustomField.js
Normal file
26
backend/src/models/ContactCustomField.js
Normal file
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user