Skip to content

Commit

Permalink
fix(Facebook): refactor Facebook
Browse files Browse the repository at this point in the history
  • Loading branch information
uuganaa1007 committed Feb 23, 2024
1 parent a8fc2e0 commit 5df6447
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 80 deletions.
137 changes: 60 additions & 77 deletions packages/plugin-facebook-api/src/handleFacebookMessage.ts
Expand Up @@ -14,7 +14,6 @@ export const handleFacebookMessage = async (
) => {
const { action, payload } = msg;
const doc = JSON.parse(payload || '{}');

if (doc.internal) {
const conversation = await models.Conversations.getConversation({
erxesApiId: doc.conversationId,
Expand All @@ -37,14 +36,10 @@ export const handleFacebookMessage = async (
extraInfo,
userId,
} = doc;

let strippedContent = strip(content);

strippedContent = strippedContent.replace(/&/g, '&');

const commentConversationResult = await models.CommentConversation.findOne({
erxesApiId: conversationId,
});

const post = await models.PostConversations.findOne({
$or: [
{ erxesApiId: conversationId },
Expand All @@ -55,83 +50,71 @@ export const handleFacebookMessage = async (
},
],
});

if (!commentConversationResult) {
throw new Error('comment not found');
}
if (!post) {
throw new Error('Post not found');
}
if (commentConversationResult) {
const { recipientId, comment_id, senderId } = commentConversationResult;
await models.CommentConversationReply.create({
recipientId: recipientId,
senderId: senderId,
attachments: attachments,
userId: userId,
createdAt: new Date(Date.now()),
content: strippedContent,
parentId: comment_id,
});
let strippedContent = strip(content);

let attachment: {
url?: string;
type?: string;
payload?: { url: string };
} = {};

if (attachments && attachments.length > 0) {
attachment = {
type: 'file',
payload: {
url: attachments[0].url,
},
};
}
strippedContent = strippedContent.replace(/&/g, '&');

let data = {
message: strippedContent,
attachment_url: attachment.payload ? attachment.payload.url : undefined,
const { recipientId, comment_id, senderId } = commentConversationResult;
await models.CommentConversationReply.create({
recipientId: recipientId,
senderId: senderId,
attachments: attachments,
userId: userId,
createdAt: new Date(Date.now()),
content: strippedContent,
parentId: comment_id,
});

let attachment: {
url?: string;
type?: string;
payload?: { url: string };
} = {};

if (attachments && attachments.length > 0) {
attachment = {
type: 'file',
payload: {
url: attachments[0].url,
},
};
const id = commentConversationResult
? commentConversationResult.comment_id
: post.postId;

if (commentConversationResult && commentConversationResult.comment_id) {
data = {
message: ` @[${commentConversationResult.senderId}] ${strippedContent}`,
attachment_url: attachment.url,
};
}
try {
const inboxConversation = await sendInboxMessage({
isRPC: true,
subdomain,
action: 'conversations.findOne',
data: { query: { _id: conversationId } },
});
await sendReply(
models,
`${id}/comments`,
data,
recipientId,
inboxConversation && inboxConversation.integrationId,
);

sendInboxMessage({
action: 'sendNotifications',
isRPC: false,
subdomain,
data: {
userId,
conversations: [inboxConversation],
type: 'conversationStateChange',
mobile: true,
messageContent: content,
},
});

return { status: 'success' };
} catch (e) {
throw new Error(e.message);
}
}
let data = {
message: strippedContent,
attachment_url: attachment.payload ? attachment.payload.url : undefined,
};
const id = commentConversationResult
? commentConversationResult.comment_id
: post.postId;
if (commentConversationResult && commentConversationResult.comment_id) {
data = {
message: ` @[${commentConversationResult.senderId}] ${strippedContent}`,
attachment_url: attachment.url,
};
}
try {
const inboxConversation = await sendInboxMessage({
isRPC: true,
subdomain,
action: 'conversations.findOne',
data: { query: { _id: conversationId } },
});
await sendReply(
models,
`${id}/comments`,
data,
recipientId,
inboxConversation && inboxConversation.integrationId,
);
return { status: 'success' };
} catch (e) {
throw new Error(e.message);
}
}
if (action === 'reply-messenger') {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-facebook-api/src/store.ts
Expand Up @@ -263,7 +263,7 @@ export const getOrCreateComment = async (
const mainConversation = await models.CommentConversation.findOne({
comment_id: commentParams.comment_id,
});
const subConversation = await models.CommentConversation.findOne({
const parentConversation = await models.CommentConversation.findOne({
comment_id: commentParams.parent_id,
});
const replyConversation = await models.CommentConversationReply.findOne({
Expand Down Expand Up @@ -301,11 +301,11 @@ export const getOrCreateComment = async (
customerId: customer.erxesApiId,
parentId: commentParams.parent_id,
};
if (subConversation) {
if (parentConversation) {
await models.CommentConversationReply.create({
...doc,
});
} else if (mainConversation === null && subConversation === null) {
} else {
await models.CommentConversation.create({
...doc,
});
Expand Down

0 comments on commit 5df6447

Please sign in to comment.