xplorer-io / xplorers-api

API for all things Xplorers
MIT License
1 stars 0 forks source link

Project Refactor #4

Open ArunaAcharya opened 1 month ago

ArunaAcharya commented 1 month ago

I have implemented a feature-based architecture for this project. This approach organises the codebase around features or domains rather than technical layers. Each feature or domain is encapsulated with its own directory, which includes relevant controllers, modules, routes and tests. Details: Feature Organisation: This project is divided into feature-specific directories such as authentication, event, accolades etc. Each of these directories contains: Controllers: Each controller is responsible for handling the logic associated with a specific router. This separation ensures that each controllers deals with a single route or a set of related routes, simplifying maintenance and understanding of the code.
Models: Data models and interaction with external services Routes: routing definitions for the feature Tests: Unit and integration tests specific to the feature Error Handling: Added a centralised error handling middleware to manage errors consistently across the application. To ensure that the error handling middleware is triggered properly, all errors should be passed to it using the next function in controllers. Centralised Routing: index.ts: This file acts as the common router, aggregating and managing routes for various features. It consolidates routes from different feature modules into a single point of entry for routing. Application entry Point: server.ts: This file is the main application entry point. It initialises the Express application, set up middleware and starts the server. it also imports the common router from index.ts and applies it to the Express app.

For the existing fetchSlackUser functionality, I have refactored the code into three distinct files for better organization and clarity: fetchSlackUserCtrl.ts: Controller responsible for fetching Slack user details. fetchSlackUserStatusCtrl.ts: Controller dedicated to fetching the status of a Slack user. fetchSlackUser.ts: Module that handles the actual communication with the Slack API to retrieve user information.

image

tested against google cloud api gateway:

image

references: https://roro88.medium.com/how-to-create-a-simple-rest-api-using-typescript-and-nodejs-324b80f0122d https://expressjs.com/en/guide/writing-middleware.html

Summary by CodeRabbit

Release Notes

boltdynamics commented 1 month ago

Have you deployed the API in Google Cloud and verified the functionality works as expected? Can you also include these test outputs (screenshots) in the Pull Request description please πŸ™‡

boltdynamics commented 1 month ago

@20was @sonam-serchan @ArunaAcharya I see that you are trying to implement some sort of framework or a template - defining routes and modules. What is this process or framework called? What are we basing this off?

ArunaAcharya commented 1 month ago

@20was @sonam-serchan @ArunaAcharya I see that you are trying to implement some sort of framework or a template - defining routes and modules. What is this process or framework called? What are we basing this off?

I was following a template inspired my this article. This guide provides a structure for creating REST APIs with TypeScript and Node.js, which we could adapt for our project. Feel free to suggest better approach though 😁

coderabbitai[bot] commented 3 weeks ago

Walkthrough

The changes introduce new functionality for handling Slack user information in an Express application. This includes the addition of controllers for fetching user details and status, a service for interacting with the Slack API, and a custom error handling mechanism. The project structure has been modularized by separating routes and controllers into distinct files, enhancing organization. Additionally, a new .gitignore entry has been added, and the package.json has been updated with new dependencies and scripts.

Changes

Files Change Summary
.gitignore Added launch.json to the list of ignored files.
package.json Updated scripts to include a new dev script using nodemon, added dependencies: morgan, nodemon, zod, and dev dependencies: @types/morgan, ts-node.
src/authentication/controllers/... Introduced getSlackUserDetail and checkSlackUserStatus controller functions for fetching Slack user details and status, respectively.
src/authentication/models/... Added new TypeScript interfaces and types for Slack user data in slackUserInterface.ts, and defined a validation schema in slackUserSchema.ts.
src/authentication/routes/... Created authenticationRouter.ts to define routes for Slack user operations.
src/authentication/services/... Introduced fetchSlackUser.ts service to interact with the Slack API for user information retrieval.
src/errors/customErrorType.ts Added CustomError class for structured error handling.
src/events/controllers/getEvents.ts Introduced a simple getEvents function to handle event requests.
src/events/routes/eventRoutes.ts Created an Express router for handling /events GET requests.
src/index.ts Restructured routing logic to use modular route imports instead of direct route definitions.
src/middleware/errorMiddleware.ts Added error handling middleware to manage errors in the Express application.
src/server.ts Established an Express server setup with middleware and route handling.
src/slackUser.ts Deleted file that previously handled Slack user interactions, as functionality has been modularized and refactored.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Router
    participant Controller
    participant Service

    Client->>Server: GET /slack/user
    Server->>Router: Route request
    Router->>Controller: getSlackUserDetail()
    Controller->>Service: fetchSlackUser(params)
    Service-->>Controller: User data
    Controller-->>Router: Respond with user data
    Router-->>Server: Send response to Client
sequenceDiagram
    participant Client
    participant Server
    participant Router
    participant Controller
    participant Service

    Client->>Server: GET /slack/user/status
    Server->>Router: Route request
    Router->>Controller: checkSlackUserStatus()
    Controller->>Service: fetchSlackUser(email)
    Service-->>Controller: User status
    Controller-->>Router: Respond with user status
    Router-->>Server: Send response to Client

Poem

🐰 In the burrow where code does hop,
New features bloom, and never stop.
With Slack users fetched, and errors tamed,
Our app now dances, forever acclaimed!
So let us cheer, with a joyful shout,
For the changes made, we’re leaping about! πŸŽ‰


πŸ“œ Recent review details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
πŸ“₯ Commits Files that changed from the base of the PR and between 7774a635bd1f766608aca7b5a350e30d0ce3e0d6 and f600f9f344880ba20292924aadf8186c0dd8f958.
β›” Files ignored due to path filters (1) * `pnpm-lock.yaml` is excluded by `!**/pnpm-lock.yaml`
πŸ“’ Files selected for processing (12) * package.json (1 hunks) * src/authentication/controllers/fetchSlackUser.ts (1 hunks) * src/authentication/controllers/fetchSlackUserStatus.ts (1 hunks) * src/authentication/models/slackUserInterface.ts (1 hunks) * src/authentication/models/slackUserSchema.ts (1 hunks) * src/authentication/routes/authenticationRouter.ts (1 hunks) * src/authentication/services/fetchSlackUser.ts (1 hunks) * src/errors/customErrorType.ts (1 hunks) * src/events/routes/eventRoutes.ts (1 hunks) * src/index.ts (1 hunks) * src/middleware/errorMiddleware.ts (1 hunks) * src/server.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (12) * package.json * src/authentication/controllers/fetchSlackUser.ts * src/authentication/controllers/fetchSlackUserStatus.ts * src/authentication/models/slackUserInterface.ts * src/authentication/models/slackUserSchema.ts * src/authentication/routes/authenticationRouter.ts * src/authentication/services/fetchSlackUser.ts * src/errors/customErrorType.ts * src/events/routes/eventRoutes.ts * src/index.ts * src/middleware/errorMiddleware.ts * src/server.ts
--- Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
❀️ Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
πŸͺ§ Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit , please review it.` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (Invoked using PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. ### Other keywords and placeholders - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. - Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description. - Add `@coderabbitai` anywhere in the PR title to generate the title automatically. ### CodeRabbit Configuration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
20was commented 2 weeks ago

Great work @ArunaAcharya