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

Unassign from Agent if they are deactivated when closing conversation #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 17 additions & 4 deletions close-conversations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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];

Expand All @@ -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
Expand Down