Skip to content

Commit

Permalink
fix(viber): fixed subscription issue
Browse files Browse the repository at this point in the history
  • Loading branch information
soyombo-baterdene committed Aug 29, 2023
1 parent 3a601f2 commit 940116d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-viber-api/src/viber/api.ts
Expand Up @@ -36,7 +36,7 @@ export class ViberAPI {

async registerWebhook(): Promise<any> {
// used for local testing
const localDomain: string = 'https://a2de-202-21-104-34.jp.ngrok.io';
const localDomain: string = 'https://8e09-103-212-118-66.ngrok-free.app';

const domain: string = getEnv({ name: 'DOMAIN', subdomain: this.subdomain })
? getEnv({ name: 'DOMAIN', subdomain: this.subdomain }) + '/gateway'
Expand Down
85 changes: 56 additions & 29 deletions packages/plugin-viber-api/src/viber/messageListen.ts
Expand Up @@ -3,7 +3,7 @@ import {
Conversations,
ConversationMessages,
IConversation,
IConversationMessages
Integrations
} from '../models';
import { sendInboxMessage } from '../messageBroker';
import { graphqlPubsub } from '../configs';
Expand Down Expand Up @@ -50,6 +50,21 @@ const messageListen = async (
integrationId
});

const viberIntegration = await Integrations.findOne({
inboxId: integrationId
});
const integration = await sendInboxMessage({
subdomain,
action: 'integrations.findOne',
data: { _id: viberIntegration.erxesApiId },
isRPC: true,
defaultValue: null
});

if (!integration) {
throw new Error('Integration not found');
}

if (!conversation) {
try {
conversation = await Conversations.create({
Expand All @@ -65,31 +80,27 @@ const messageListen = async (
: e
);
}
}

try {
const apiConversationResponse = await sendInboxMessage({
subdomain,
action: 'integrations.receive',
data: {
action: 'create-or-update-conversation',
payload: JSON.stringify({
customerId: customer.contactsId,
integrationId,
content: message.message.text || '',
attachments: null,
conversationId: conversation.erxesApiId,
updatedAt: message.timestamp
})
},
isRPC: true
});

conversation.erxesApiId = apiConversationResponse._id;

await conversation.save();
} catch (e) {
throw new Error(e);
}
try {
await sendInboxMessage({
subdomain,
action: 'integrations.receive',
data: {
action: 'create-or-update-conversation',
payload: JSON.stringify({
customerId: customer.contactsId,
integrationId,
content: message.message.text || '',
conversationId: conversation.erxesApiId,
createdAt: message.timestamp
})
},
isRPC: true,
defaultValue: null
});
} catch (e) {
console.error(e);
}

try {
Expand All @@ -106,9 +117,7 @@ const messageListen = async (
messageObj.attachments = [{ type: 'image', url: message.message.media }];
}

const conversationMessage: IConversationMessages = await ConversationMessages.create(
messageObj
);
const conversationMessage = await ConversationMessages.create(messageObj);

await sendInboxMessage({
subdomain,
Expand All @@ -119,11 +128,29 @@ const messageListen = async (
}
});

let channelMemberIds: string[] = [];

const channels = await sendInboxMessage({
subdomain,
action: 'channels.find',
data: {
integrationIds: { $in: [integration._id] }
},
isRPC: true
});

for (const channel of channels) {
channelMemberIds = [...channelMemberIds, ...(channel.memberIds || [])];
}

graphqlPubsub.publish('conversationClientMessageInserted', {
conversationClientMessageInserted: {
...conversationMessage.toObject(),
conversationId: conversation.erxesApiId
}
},
conversation,
integration,
channelMemberIds
});

graphqlPubsub.publish('conversationMessageInserted', {
Expand Down

0 comments on commit 940116d

Please sign in to comment.