Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.
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
6 changes: 5 additions & 1 deletion src/apolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ const apolloServer = new ApolloServer({
scopeBrandIds = brandIds;
}

if (!user.isOwner) {
if (!user.isOwner && scopeBrandIds.length) {
// Select non-existent or empty arrays too
scopeBrandIds.push(null);
scopeBrandIds.push([]);

brandIdSelector = { _id: { $in: scopeBrandIds } };
commonQuerySelector = { scopeBrandIds: { $in: scopeBrandIds } };
commonQuerySelectorElk = { terms: { scopeBrandIds } };
Expand Down
34 changes: 33 additions & 1 deletion src/data/resolvers/mutations/conversations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as strip from 'strip';
import * as _ from 'underscore';
import { ConversationMessages, Conversations, Customers, Integrations } from '../../../db/models';
import { ConversationMessages, Conversations, Customers, Integrations, Users } from '../../../db/models';
import Messages from '../../../db/models/ConversationMessages';
import {
CONVERSATION_STATUSES,
Expand Down Expand Up @@ -320,6 +320,21 @@ const conversationMutations = {

await sendNotifications({ user, conversations, type: NOTIFICATION_TYPES.CONVERSATION_ASSIGNEE_CHANGE });

// Add bot message and update conversation
let assignedUser = await Users.getUser(assignedUserId);

for (const conversation of conversations) {
let message = await ConversationMessages.addMessage({
conversationId: conversation._id,
content: `${assignedUser.details?.shortName || assignedUser.email} has been assigned to this conversation`,
fromBot: true,
});

graphqlPubsub.publish('conversationClientMessageInserted', {
conversationClientMessageInserted: message,
});
}

return conversations;
},

Expand All @@ -339,6 +354,23 @@ const conversationMutations = {
// notify graphl subscription
publishConversationsChanged(_ids, 'assigneeChanged');

// Add bot message and update conversation
for (const conversation of oldConversations) {
if (!conversation.assignedUserId) continue;

let unAssignedUser = await Users.getUser(conversation.assignedUserId);
let message = await ConversationMessages.addMessage({
conversationId: conversation._id,
content: `${unAssignedUser.details?.shortName ||
unAssignedUser.email} has been deassigned from this conversation`,
fromBot: true,
});

graphqlPubsub.publish('conversationClientMessageInserted', {
conversationClientMessageInserted: message,
});
}

return updatedConversations;
},

Expand Down
11 changes: 11 additions & 0 deletions src/data/resolvers/queries/conversationQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Channels, Integrations } from '../../../db/models';
import { CONVERSATION_STATUSES } from '../../../db/models/definitions/constants';
import { fixDate } from '../../utils';

const { USE_CHAT_RESTRICTIONS } = process.env;

interface IIn {
$in: string[];
}
Expand All @@ -29,6 +31,7 @@ export interface IListArgs {

interface IUserArgs {
_id: string;
isOwner?: boolean;
starredConversationIds?: string[];
}

Expand Down Expand Up @@ -69,16 +72,24 @@ export default class Builder {
statusFilter = this.statusFilter([CONVERSATION_STATUSES.CLOSED]);
}

let assignedUserQuery: any = {};

if (USE_CHAT_RESTRICTIONS === 'true' && !this.user.isOwner) {
assignedUserQuery.assignedUserId = { $in: [this.user._id, null] };
}

return {
...statusFilter,
// exclude engage messages if customer did not reply
$or: [
{
userId: { $exists: true },
messageCount: { $gt: 1 },
...assignedUserQuery,
},
{
userId: { $exists: false },
...assignedUserQuery,
},
],
};
Expand Down
1 change: 1 addition & 0 deletions src/data/resolvers/queries/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const conversationQueries = {
// initiate query builder
const qb = new QueryBuilder(params, {
_id: user._id,
isOwner: user.isOwner,
starredConversationIds: user.starredConversationIds,
});

Expand Down