diff --git a/README.md b/README.md index c99bd59..edffab8 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ This script adds a topic to conversations in Gladly, and then closes them utiliz The script accomplishes this by doing the following: - Retrieves the conversation from Gladly using the [Get Conversation API](https://developer.gladly.com/rest/#operation/getConversation) - Adds a topic to the conversation, as defined in the CSV file, using the [Add Topic API](https://developer.gladly.com/rest/#operation/addTopicToConversation) -- Closes the conversation, assigning it to the conversationId and agentId values the conversation is currently assigned to using the [Update Conversation API](https://developer.gladly.com/rest/#operation/patchConversation) +- Closes the conversation, assigning it to the conversationId and agentId values the conversation is currently assigned to using the [Update Conversation API](https://developer.gladly.com/rest/#operation/patchConversation). If Agent is deactivated (meaning that they cannot be found via the [LIST Agents](https://developer.gladly.com/rest/#operation/getAgents) API call anymore), the conversation will be assigned to the inbox and Agent will be unassigned. ### How to use script diff --git a/close-conversations/index.js b/close-conversations/index.js index 864f44d..6bdb81c 100644 --- a/close-conversations/index.js +++ b/close-conversations/index.js @@ -2,7 +2,7 @@ require('dotenv').config(); const csv = require('csvtojson'); const slugid = require('slugid'); -const { getConversation, updateConversation, addTopics } = require('../util/api'); +const { getConversation, updateConversation, addTopics, listAgents } = require('../util/api'); //Keep note of rate limits: https://developer.gladly.com/rest/#section/Default-Rate-Limit const queue = require('queue'); @@ -11,8 +11,14 @@ let q = new queue({ }); //Load CSV file -csv().fromFile(`${__dirname}/sample-close-conversations.csv`) -.then((rows) => { +Promise.all([ + csv().fromFile(`${__dirname}/sample-close-conversations.csv`), + listAgents() +]) +.then((promiseResults) => { + let rows = promiseResults[0]; + let agents = promiseResults[1]; + for(let rowIdx in rows) { let row = rows[rowIdx]; @@ -27,10 +33,17 @@ csv().fromFile(`${__dirname}/sample-close-conversations.csv`) "topicIds": [ row['topicId'] ] }) .then(() => { + let isValidAgent = false; + for(let agent in agents) { + if(agent.id == thisConversation.agentId) { + isValidAgent = true; + } + } + updateConversation(thisConversation.id, { "assignee": { "inboxId": thisConversation.inboxId, //stay assigned to the same inbox the conversation is currently in - "agentId": thisConversation.agentId //stay assigned to the same agent the conversation is currently assigned to + "agentId": isValidAgent ? thisConversation.agentId : null //stay assigned to the same agent the conversation is currently assigned to, unless agent is deactivated }, "status": { "value": "CLOSED", //close the conversation