From fdef0dbbaf62cc47598e62464c9e0970303026dd Mon Sep 17 00:00:00 2001 From: Narmandakh Enkhtuvshin <37796969+Enkhtuvshin0513@users.noreply.github.com> Date: Mon, 16 Dec 2019 17:53:44 +0800 Subject: [PATCH] erxes/erxes-integrations#69 --- src/data/schema/user.ts | 2 ++ src/db/models/definitions/users.ts | 2 ++ src/middlewares/integrationsApiMiddleware.ts | 12 ++++++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/data/schema/user.ts b/src/data/schema/user.ts index 0e8125214..82433590f 100644 --- a/src/data/schema/user.ts +++ b/src/data/schema/user.ts @@ -6,6 +6,7 @@ export const types = ` position: String location: String description: String + operatorPhone: String } input UserLinks { @@ -35,6 +36,7 @@ export const types = ` position: String location: String description: String + operatorPhone: String } type UserLinksType { diff --git a/src/db/models/definitions/users.ts b/src/db/models/definitions/users.ts index b20feca60..150badc73 100644 --- a/src/db/models/definitions/users.ts +++ b/src/db/models/definitions/users.ts @@ -15,6 +15,7 @@ export interface IDetail { position?: string; location?: string; description?: string; + operatorPhone?: string; } export interface IDetailDocument extends IDetail, Document {} @@ -76,6 +77,7 @@ const detailSchema = new Schema( position: field({ type: String, label: 'Position' }), location: field({ type: String, optional: true, label: 'Location' }), description: field({ type: String, optional: true, label: 'Description' }), + operatorPhone: field({ type: String, optional: true, label: 'Company phone' }), }, { _id: false }, ); diff --git a/src/middlewares/integrationsApiMiddleware.ts b/src/middlewares/integrationsApiMiddleware.ts index ec12c02c5..043aed469 100644 --- a/src/middlewares/integrationsApiMiddleware.ts +++ b/src/middlewares/integrationsApiMiddleware.ts @@ -1,4 +1,4 @@ -import { ConversationMessages, Conversations, Customers, Integrations } from '../db/models'; +import { ConversationMessages, Conversations, Customers, Integrations, Users } from '../db/models'; import { CONVERSATION_STATUSES } from '../db/models/definitions/constants'; import { graphqlPubsub } from '../pubsub'; @@ -48,14 +48,18 @@ const integrationsApiMiddleware = async (req, res) => { } if (action === 'create-or-update-conversation') { - if (doc.conversationId) { - const { conversationId, content } = doc; + const { conversationId, content, owner } = doc; + const user = await Users.findOne({ 'details.operatorPhone': owner }); + const assignedUserId = user ? user._id : ''; - await Conversations.updateConversation(conversationId, { content }); + if (conversationId) { + await Conversations.updateConversation(conversationId, { content, assignedUserId }); return res.json({ _id: conversationId }); } + doc.assignedUserId = assignedUserId; + const conversation = await Conversations.createConversation(doc); return res.json({ _id: conversation._id });