Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration with dialogflow #3

Closed
wants to merge 11 commits into from
Closed
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"start": "nodemon dist/server.js",
"start": "nodemon --ignore dialogflow/ dist/server.js",
"dev:server": "ts-node-dev --respawn --transpile-only --ignore node_modules src/server.ts",
"pretest": "NODE_ENV=test sequelize db:migrate && NODE_ENV=test sequelize db:seed:all",
"test": "NODE_ENV=test jest",
Expand All @@ -15,8 +15,10 @@
"author": "",
"license": "MIT",
"dependencies": {
"@google-cloud/dialogflow": "^4.6.0",
"@sentry/node": "^5.29.2",
"@types/pino": "^6.3.4",
"actions-on-google": "^3.0.0",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
Expand Down
36 changes: 36 additions & 0 deletions backend/src/controllers/ContactController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
import AppError from "../errors/AppError";
import GetContactService from "../services/ContactServices/GetContactService";
import ToggleUseQueuesContactService from "../services/ContactServices/ToggleUseQueuesContactService";
import ToggleUseDialogflowContactService from "../services/ContactServices/ToggleUseDialogflowContactService";

type IndexQuery = {
searchParam: string;
Expand Down Expand Up @@ -144,6 +146,40 @@ export const update = async (
return res.status(200).json(contact);
};

export const toggleUseQueue = async (
req: Request,
res: Response
): Promise<Response> => {
const { contactId } = req.params;

const contact = await ToggleUseQueuesContactService({ contactId });

const io = getIO();
io.emit("contact", {
action: "update",
contact
});

return res.status(200).json(contact);
};

export const toggleUseDialogflow = async (
req: Request,
res: Response
): Promise<Response> => {
const { contactId } = req.params;

const contact = await ToggleUseDialogflowContactService({ contactId });

const io = getIO();
io.emit("contact", {
action: "update",
contact
});

return res.status(200).json(contact);
};

export const remove = async (
req: Request,
res: Response
Expand Down
2 changes: 2 additions & 0 deletions backend/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Queue from "../models/Queue";
import WhatsappQueue from "../models/WhatsappQueue";
import UserQueue from "../models/UserQueue";
import QuickAnswer from "../models/QuickAnswer";
import Dialogflow from "../models/Dialogflow";

// eslint-disable-next-line
const dbConfig = require("../config/database");
Expand All @@ -25,6 +26,7 @@ const models = [
Whatsapp,
ContactCustomField,
Setting,
Dialogflow,
Queue,
WhatsappQueue,
UserQueue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.createTable("Dialogflows", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
projectName: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
jsonContent: {
type: DataTypes.TEXT,
allowNull: false,
},
language: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false
}
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.dropTable("Dialogflows");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Queues", "dialogflowId", {
type: DataTypes.INTEGER,
references: { model: "Dialogflows", key: "id" },
onUpdate: "CASCADE",
onDelete: "SET NULL"
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Queues", "dialogflowId");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Contacts", "useDialogflow", {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Contacts", "useDialogflow");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Contacts", "useQueues", {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Contacts", "useQueues");
}
};
8 changes: 8 additions & 0 deletions backend/src/models/Contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class Contact extends Model<Contact> {
@Column
isGroup: boolean;

@Default(true)
@Column
useQueues: boolean;

@Default(true)
@Column
useDialogflow: boolean;

@CreatedAt
createdAt: Date;

Expand Down
45 changes: 45 additions & 0 deletions backend/src/models/Dialogflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
Table,
Column,
CreatedAt,
UpdatedAt,
Model,
DataType,
PrimaryKey,
HasMany,
AutoIncrement
} from "sequelize-typescript";
import Queue from "./Queue";

@Table
class Dialogflow extends Model<Dialogflow> {
@PrimaryKey
@AutoIncrement
@Column
id: number;

@Column(DataType.TEXT)
name: string;

@Column(DataType.TEXT)
projectName: string;

@Column(DataType.TEXT)
jsonContent: string;

@Column(DataType.TEXT)
language: string;

@CreatedAt
@Column(DataType.DATE(6))
createdAt: Date;

@UpdatedAt
@Column(DataType.DATE(6))
updatedAt: Date;

@HasMany(() => Queue)
queues: Queue[]
}

export default Dialogflow;
12 changes: 11 additions & 1 deletion backend/src/models/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import {
AutoIncrement,
AllowNull,
Unique,
BelongsToMany
BelongsToMany,
BelongsTo,
ForeignKey
} from "sequelize-typescript";
import User from "./User";
import UserQueue from "./UserQueue";

import Whatsapp from "./Whatsapp";
import WhatsappQueue from "./WhatsappQueue";
import Dialogflow from "./Dialogflow";

@Table
class Queue extends Model<Queue> {
Expand All @@ -36,6 +39,13 @@ class Queue extends Model<Queue> {
@Column
greetingMessage: string;

@ForeignKey(() => Dialogflow)
@Column
dialogflowId: number;

@BelongsTo(() => Dialogflow)
dialogflow: Dialogflow;

@CreatedAt
createdAt: Date;

Expand Down
4 changes: 4 additions & 0 deletions backend/src/routes/contactRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ contactRoutes.post("/contact", isAuth, ContactController.getContact);

contactRoutes.put("/contacts/:contactId", isAuth, ContactController.update);

contactRoutes.put("/contacts/toggleUseQueues/:contactId", isAuth, ContactController.toggleUseQueue);

contactRoutes.put("/contacts/toggleUseDialogflow/:contactId", isAuth, ContactController.toggleUseDialogflow);

contactRoutes.delete("/contacts/:contactId", isAuth, ContactController.remove);

export default contactRoutes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import AppError from "../../errors/AppError";
import Contact from "../../models/Contact";

interface Request {
contactId: string;
}

const ToggleUseDialogflowContactService = async ({
contactId
}: Request): Promise<Contact> => {
const contact = await Contact.findOne({
where: { id: contactId },
attributes: ["id", "useDialogflow"]
});

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

const useDialogflow = contact.useDialogflow ? false : true;

await contact.update({
useDialogflow
});

await contact.reload({
attributes: ["id", "name", "number", "email", "profilePicUrl", "useQueues", "useDialogflow"],
include: ["extraInfo"]
});

return contact;
};

export default ToggleUseDialogflowContactService;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import AppError from "../../errors/AppError";
import Contact from "../../models/Contact";

interface Request {
contactId: string;
}

const ToggleUseQueuesContactService = async ({
contactId
}: Request): Promise<Contact> => {
const contact = await Contact.findOne({
where: { id: contactId },
attributes: ["id", "useQueues"]
});

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

const useQueues = contact.useQueues ? false : true;

await contact.update({
useQueues
});

await contact.reload({
attributes: ["id", "name", "number", "email", "profilePicUrl", "useQueues", "useDialogflow"],
include: ["extraInfo"]
});

return contact;
};

export default ToggleUseQueuesContactService;
4 changes: 2 additions & 2 deletions backend/src/services/ContactServices/UpdateContactService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const UpdateContactService = async ({

const contact = await Contact.findOne({
where: { id: contactId },
attributes: ["id", "name", "number", "email", "profilePicUrl"],
attributes: ["id", "name", "number", "email", "profilePicUrl", "useQueues", "useDialogflow"],
include: ["extraInfo"]
});

Expand Down Expand Up @@ -60,7 +60,7 @@ const UpdateContactService = async ({
});

await contact.reload({
attributes: ["id", "name", "number", "email", "profilePicUrl"],
attributes: ["id", "name", "number", "email", "profilePicUrl", "useQueues", "useDialogflow"],
include: ["extraInfo"]
});

Expand Down
Loading