Skip to content

feat: Add OpenAPI Support to Engagement Dashboard API #35882

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

Draft
wants to merge 29 commits into
base: develop
Choose a base branch
from

Conversation

ahmed-n-abdeltwab
Copy link

@ahmed-n-abdeltwab ahmed-n-abdeltwab commented Apr 27, 2025

Description:
This PR integrates OpenAPI support into the Rocket.Chat API, migrate of Rocket.Chat API endpoints to the new OpenAPI pattern. The update includes improved API documentation, enhanced type safety, and response validation using AJV.

Key Changes:

  • Implement the OpenAPI pattern with better route handling.
  • Added AJV-based schema validation for API responses to ensure type safety.
  • Improved the code structure using chained route methods for better maintainability and readability.
  • No breaking changes

Issue Reference:
Relates to #34983, part of the ongoing OpenAPI integration effort.

Testing:

  • Validated that the endpoints return correctly formatted responses.
  • Verified AJV schema validation for the responses.
  • Ensured that the API documentation matches the updated response schema.
  • No breaking changes

Endpoints Migration:

Looking forward to your feedback! 🚀


This pull request introduces OpenAPI support to the Engagement Dashboard API within the Rocket.Chat repository. The changes focus on enhancing the API's structure and validation processes. Specifically, the TypeScript types for API responses have been refactored by introducing a new helper type, ResultForStatus, to improve maintainability. Additionally, several API endpoints in the Engagement Dashboard's users section have been updated to utilize the API.v1.get method along with ajv for schema validation of request queries and responses. This transition from the older API.v1.addRoute structure ensures more consistent API contract definitions and validation.

ahmed-n-abdeltwab and others added 21 commits April 2, 2025 06:36
Replaced nested ternary operators with a cleaner lookup-based approach
for better readability and maintainability. No functional changes.

Related to RocketChat#34983, but does not fully resolve it.
Replaced any type with unknown for the avoid of using any. No functional changes.
Replaced implicit any fallbacks with explicit unknown types.
No functional changes.
Related to RocketChat#34983, but does not fully resolve it.
Replaced nested ternary operators with a cleaner lookup-based approach
for better readability and maintainability. No functional changes.

Related to RocketChat#34983, but does not fully resolve it.
…eadability

fix: incorrect typing of the response types
Copy link
Contributor

dionisio-bot bot commented Apr 27, 2025

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

Copy link

changeset-bot bot commented Apr 27, 2025

⚠️ No Changeset found

Latest commit: 097c03e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

kodus-ai bot commented Apr 27, 2025

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


ahmed-n-abdeltwab seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@ahmed-n-abdeltwab ahmed-n-abdeltwab marked this pull request as draft April 27, 2025 14:18
@ahmed-n-abdeltwab
Copy link
Author

📊 Add OpenAPI Support to Engagement Dashboard API

I made this PR a draft because the test results returned 0 passing, 12 pending


✅ Test Results

$ yarn testapi -f 'Engagement Dashboard'    


  [Engagement Dashboard]
    [/engagement-dashboard/channels/list]
      - should fail if user does not have the view-engagement-dashboard permission
      - should fail if start param is not a valid date
      - should fail if end param is not a valid date
      - should fail if start param is not provided
      - should fail if end param is not provided
      - should succesfuly return results
      - should not return empty rooms when the hideRoomsWithNoActivity param is provided
      - should correctly count messages in an empty room
      - should correctly count messages diff compared to last week when the hideRoomsWithNoActivity param is provided and there are messages in a room
      - should correctly count messages diff compared to last week when there are messages in a room
      - should correctly count messages from last week and diff when moving to the next week and providing the hideRoomsWithNoActivity param
      - should correctly count messages from last week and diff when moving to the next week


  0 passing (981ms)
  12 pending

Copy link

kodus-ai bot commented Apr 27, 2025

Kody Review Complete

Great news! 🎉
No issues were found that match your current review configurations.

Keep up the excellent work! 🚀

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

Copy link

kodus-ai bot commented Apr 28, 2025

Kody Review Complete

Great news! 🎉
No issues were found that match your current review configurations.

Keep up the excellent work! 🚀

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

@ahmed-n-abdeltwab
Copy link
Author

ahmed-n-abdeltwab commented Apr 28, 2025

📊 PR Summary

  1. refactor: Rename response schemas for clarity and consistency
  2. fix: Ensure additionalProperties is set to false for error response schemas
  3. fix: Add missing descriptions for error response schemas

✅ Test Results

$ yarn testapi -f 'Engagement Dashboard'    

[Engagement Dashboard]
    [/engagement-dashboard/channels/list]
      - should fail if user does not have the view-engagement-dashboard permission
      - should fail if start param is not a valid date
      - should fail if end param is not a valid date
      - should fail if start param is not provided
      - should fail if end param is not provided
      - should succesfuly return results
      - should not return empty rooms when the hideRoomsWithNoActivity param is provided
      - should correctly count messages in an empty room
      - should correctly count messages diff compared to last week when the hideRoomsWithNoActivity param is provided and there are messages in a room
      - should correctly count messages diff compared to last week when there are messages in a room
      - should correctly count messages from last week and diff when moving to the next week and providing the hideRoomsWithNoActivity param
      - should correctly count messages from last week and diff when moving to the next week


  0 passing (745ms)
  12 pending

GitHub Copilot Review

Your code is well-structured and adheres to TypeScript and API design best practices. However, here are some suggestions for improvement:

1. Error Handling

  • While you have schemas for 400, 401, and 403 responses, consider adding a 500 response schema for unexpected server errors. This will make the API more robust and user-friendly.

2. Code Duplication

  • There is some duplication in the query and response schemas across the .get methods. Consider extracting common schemas (e.g., userActivityQuerySchema, commonBadRequestErrorSchema) into a shared module or variable to reduce redundancy.

3. Type Safety

  • The ajv.compile calls are well-typed, but ensure that the types (IQuery and others) are reused consistently across the codebase to avoid discrepancies.

4. Consistency in API Endpoints

  • Some endpoints start with /v1/engagement-dashboard/users/ while others omit the leading /. Ensure consistency in endpoint definitions.

5. Documentation

  • While the schemas include descriptions, consider adding JSDoc comments for each API endpoint to provide additional context for developers.

6. Performance

  • If the find* functions (e.g., findWeeklyUsersRegisteredData) involve database queries, ensure they are optimized with proper indexing and pagination where applicable.

7. Validation

  • The check function from meteor/check is used for runtime validation. Since you are already using ajv for schema validation, consider consolidating validation logic to avoid redundancy.

8. Logging

  • Add logging for critical operations (e.g., database queries, errors) to help with debugging and monitoring.

9. Security

  • Ensure that sensitive data (e.g., user IDs) is not exposed unnecessarily in the API responses. For example, in the usersList property, consider whether exposing raw user IDs is necessary.

10. Testing

  • Ensure that you have unit and integration tests for all API endpoints to verify their behavior under various scenarios (e.g., valid/invalid inputs, edge cases).

Example Refactor for Common Schemas

You can extract common schemas into a shared variable:

const userCountSchema = {
	type: 'integer',
	description: 'Number of users',
};

const dateSchema = {
	type: 'string',
	format: 'date-time',
	description: 'Date in ISO format',
};

const commonResponseSchema = {
	type: 'object',
	properties: {
		success: { type: 'boolean' },
	},
	required: ['success'],
	additionalProperties: false,
};

Then reuse these schemas in your API definitions.

Final Thoughts

Your code is already in good shape, but implementing these suggestions will improve maintainability, performance, and developer experience. Let me know if you'd like help with specific refactoring!

Copy link

kodus-ai bot commented Apr 28, 2025

Kody Review Complete

Great news! 🎉
No issues were found that match your current review configurations.

Keep up the excellent work! 🚀

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

Copy link

kodus-ai bot commented Apr 28, 2025

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

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.

2 participants