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

fix:(erxes:error Error occurred during callback inbox:sendNotifications Cannot read properties of undefined (reading '_id')) #5016

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

uuganaa1007
Copy link
Collaborator

ISSUE

Context

Your context here. Additionally, any screenshots. Delete this line.

// Delete the below section once completed

PR Checklist

  • Description is clearly stated under Context section
  • Screenshots and the additional verifications are attached

@uuganaa1007 uuganaa1007 changed the title (reading '_id') error fix:(erxes:error Error occurred during callback inbox:sendNotifications Cannot read properties of undefined (reading '_id')) Feb 23, 2024
Copy link

sonarcloud bot commented Feb 23, 2024

Quality Gate Passed Quality Gate passed

Issues
2 New issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Type: Enhancement

PR Summary: This pull request introduces enhancements to the Facebook plugin's message handling logic, specifically in how it deals with Facebook messages and comments. It adds logic to find a post associated with a conversation and throws an error if the post is not found. Additionally, it refactors how conversation data is spread into response objects, aiming to streamline the process.

Decision: Comment

📝 Type: 'Enhancement' - not supported yet.
  • Sourcery currently only approves 'Typo fix' PRs.
✅ Issue addressed: this change correctly addresses the issue or implements the desired feature.
No details provided.
✅ Small diff: the diff is small enough to approve with confidence.
No details provided.

General suggestions:

  • The PR seems to address the issue it is aimed at, which is likely related to improving error handling and data management within the Facebook plugin. However, the PR description could be more detailed in explaining the context and the specific problem being solved. Providing a clear link to the issue and a thorough explanation would help reviewers understand the changes better.
  • Consider improving error handling by creating a custom error class for more specific error scenarios, such as when a post is not found. This would allow for more granular error management and improve the maintainability of the code.
  • Ensure that the refactoring done, especially with spreading conversation objects, does not inadvertently omit necessary fields or introduce inconsistencies with how data is managed elsewhere in the application.
packages/plugin-facebook-api/src/handleFacebookMessage.ts: The code change increases complexity due to additional database queries and more intricate conditional logic; consider encapsulating new logic into separate functions for improved readability and maintainability.

While reviewing the proposed changes, I noticed that the complexity of the code has increased significantly. This is primarily due to the introduction of an additional database query, more complex conditional logic, and increased cognitive load required to understand the flow of the code. These changes could potentially make the code harder to maintain and debug in the future.

To address these concerns, I suggest encapsulating some of the new logic into separate, well-named functions. This approach can help reduce complexity by abstracting away details, making the main flow of the code more readable and easier to follow. For example, the logic for fetching PostConversations and preparing the data object based on conditions could be moved into their own functions. This not only makes the code cleaner but also improves its maintainability by clearly defining each function's responsibility.

Here's a simplified example of how this could be implemented:

async function findRelevantPost(conversationId, commentConversationResult) {
  if (!commentConversationResult) {
    throw new Error('Comment conversation result not found');
  }
  return await models.PostConversations.findOne({
    $or: [
      { erxesApiId: conversationId },
      { postId: commentConversationResult.postId },
    ],
  });
}

async function prepareData(commentConversationResult, post, strippedContent, attachment) {
  if (commentConversationResult && commentConversationResult.comment_id) {
    return {
      message: ` @[${commentConversationResult.senderId}] ${strippedContent}`,
      attachment_url: attachment.url,
    };
  } else {
    return {
      message: strippedContent,
      attachment_url: attachment.payload ? attachment.payload.url : undefined,
    };
  }
}

By adopting this approach, we can make the code more maintainable and easier to understand, which is crucial for long-term project health.

Thanks for using Sourcery. We offer it for free for open source projects and would be very grateful if you could help us grow. If you like it, would you consider sharing Sourcery on your favourite social media? ✨

Share Sourcery

Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.


if (!post) {
throw new Error('Post not found');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): Throwing a generic Error here might make it harder to handle specific cases of missing posts differently from other types of errors. Consider creating a custom error class for this scenario to improve error handling specificity.

@enkhtuvshinD enkhtuvshinD merged commit a8fc2e0 into dev Feb 23, 2024
3 of 4 checks passed
@enkhtuvshinD enkhtuvshinD deleted the post-receive-fix3 branch February 23, 2024 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants