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

[1/n]: Migrate deleteOne Rest API to use TwentyORM directly #9784

Merged
merged 14 commits into from
Jan 31, 2025

Conversation

pacyL2K19
Copy link
Contributor

@pacyL2K19 pacyL2K19 commented Jan 22, 2025

This PR

Copy link
Contributor

@greptile-apps greptile-apps 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 Summary

This PR introduces direct TwentyORM integration for the DELETE REST API endpoint, replacing the previous GraphQL-based implementation with a more efficient direct database access approach.

  • Added new RestApiCoreServiceV2 in /packages/twenty-server/src/engine/api/rest/core/rest-api-core-v2.service.ts for direct TwentyORM operations
  • Implemented RestCoreMiddleware in /packages/twenty-server/src/engine/middlewares/rest-core-middleware.ts for authentication and metadata validation
  • Created CommonMiddlewareOperationsService in /packages/twenty-server/src/engine/middlewares/common/common.service.ts to share middleware logic between REST and GraphQL
  • Modified RestApiCoreController to use RestApiCoreServiceV2 for DELETE operations while maintaining backward compatibility for other endpoints
  • Added utility function extractObjectIdFromPath in /packages/twenty-server/src/engine/api/rest/core/utils/extract-id-from-path.utils.ts for UUID validation

12 file(s) reviewed, 14 comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Contributor

@martmull martmull left a comment

Choose a reason for hiding this comment

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

Hey, very nice code and strategy. Like the way you factorized middlewares and the new rest-api-core-v2 service is a great seed for the migration
I put some comments about code standard we have at twenty and some simplification suggestions. Feel free to comment. Thank you

@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch from 32469c4 to 7a2a2a6 Compare January 22, 2025 15:18
@prastoin prastoin self-requested a review January 22, 2025 15:36
Copy link
Contributor

@prastoin prastoin left a comment

Choose a reason for hiding this comment

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

Hello there ! Hope you're doing great
GG for this great PR
I've just reviewed the integrations tests left few comments, no big deal !
About to review main logic afterwards !
If I can provide any further information of course let me know !

@pacyL2K19
Copy link
Contributor Author

Hello there ! Hope you're doing great GG for this great PR I've left few comments, no big deal ! If I can provide any further information of course let me know !

Thank you for the review @prastoin, great suggestions
I'll be following up on this asap tomorrow morning

Copy link
Contributor

@prastoin prastoin left a comment

Choose a reason for hiding this comment

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

Second review on the logic this time !
Indeed there're some very neat refactor out there, good scoping !
Sorry for the long review, I'm available whenever you are tomorrow for peer review session if you wish to !
Please let me know, have a great evening

@FelixMalfait
Copy link
Member

Nice!

@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch 2 times, most recently from 904f938 to 40a828e Compare January 23, 2025 10:38
@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch from 40a828e to aa4be76 Compare January 24, 2025 08:34
@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch 2 times, most recently from 681f336 to 7518708 Compare January 27, 2025 06:53
@pacyL2K19 pacyL2K19 requested a review from martmull January 27, 2025 07:15
@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch from 90b8558 to 13e9bc9 Compare January 27, 2025 15:57
@pacyL2K19 pacyL2K19 requested a review from martmull January 27, 2025 15:57
Copy link
Contributor

@prastoin prastoin left a comment

Choose a reason for hiding this comment

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

Great work ! nearly there left few nitpicks nitpicks and so on

@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch from 13e9bc9 to 779034f Compare January 28, 2025 13:28
Copy link
Contributor

@martmull martmull left a comment

Choose a reason for hiding this comment

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

We are very close 😍

@prastoin
Copy link
Contributor

@greptileai trigger

Copy link
Contributor

@greptile-apps greptile-apps 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 Summary

This PR continues the migration of REST API endpoints to use TwentyORM directly, focusing on middleware and error handling improvements.

  • Added comprehensive middleware architecture in /packages/twenty-server/src/engine/middlewares/middleware.service.ts with centralized authentication and error handling
  • Introduced RestApiExceptionFilter in /packages/twenty-server/src/engine/api/rest/rest-api-exception.filter.ts for consistent error handling across REST endpoints
  • Added integration tests in /packages/twenty-server/test/integration/rest/suites/rest-api-core-delete.integration-spec.ts covering authentication and error cases
  • Configured selective middleware application in app.module.ts using MIGRATED_REST_METHODS constant for gradual migration
  • Improved request data binding with new utility in /packages/twenty-server/src/engine/middlewares/utils/bind-data-to-request-object.utils.ts

18 file(s) reviewed, 18 comment(s)
Edit PR Review Bot Settings | Greptile

@martmull
Copy link
Contributor

martmull commented Jan 28, 2025

Test Main PacyBranch
Wrong ID Send API Request
{"statusCode": 400,"messages": ["Value '-20202020-3ec3-4fe3-8997-b76aa0bfa408' is not a valid UUID"],"error": "Bad Request"}
id -20202020-0713-40a5-8216-82802401d33e
invalid input syntax for type uuid: "-20202020-0713-40a5-8216-82802401d33e"
Wrong Token Send API Request errors
Missing Record Send API Request
• Parameters
Could not find any entity of type "company" matching: {"where": {"id": "20202020-0713-40a5-8216-82802401d33f"}}
Valid Id of Existing Record Send API Request Pasted Graphic 16
Valid Id of Not Existing Record Send API Request Send API Request
Could not find any entity of type "company" matching: {"where": {"id": "20202020-0713-40a5-8216-82802401d33e"}}

@martmull
Copy link
Contributor

martmull commented Jan 28, 2025

@pacyL2K19
Here are the behavior differences between old delete endpoint and the new one -> #9784 (comment)
What we should do:

  • return only data.deleteCompany.id (and not all the record) when delete succeed
  • return a 401 status error when token invalid (and not a 500 error) (maybe an update of packages/twenty-server/src/engine/api/rest/rest-api-exception.filter.ts)
  • format the error data like this
{
"statusCode": 400,
"messages": [<messages>],
"error": "Bad Request"
}

We probably need to pass the change in writeRestResponseOnExceptionCaught method
image

Also, I think all those cases should be tested if possible

@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch from 779034f to 5c596d7 Compare January 29, 2025 16:27
@pacyL2K19
Copy link
Contributor Author

pacyL2K19 commented Jan 29, 2025

@pacyL2K19 Here are the behavior differences between old delete endpoint and the new one -> #9784 (comment) What we should do:

  • return only data.deleteCompany.id (and not all the record) when delete succeed
  • return a 401 status error when token invalid (and not a 500 error) (maybe an update of packages/twenty-server/src/engine/api/rest/rest-api-exception.filter.ts)
  • format the error data like this
{
"statusCode": 400,
"messages": [<messages>],
"error": "Bad Request"
}

We probably need to pass the change in writeRestResponseOnExceptionCaught method image

Also, I think all those cases should be tested if possible

I'm on this

@pacyL2K19 pacyL2K19 force-pushed the feat/rest-api-on-twenty-orm branch from 5c596d7 to f2252b6 Compare January 31, 2025 13:03
@pacyL2K19 pacyL2K19 requested a review from martmull January 31, 2025 13:04
@pacyL2K19
Copy link
Contributor Author

@pacyL2K19 Here are the behavior differences between old delete endpoint and the new one -> #9784 (comment) What we should do:

  • return only data.deleteCompany.id (and not all the record) when delete succeed
  • return a 401 status error when token invalid (and not a 500 error) (maybe an update of packages/twenty-server/src/engine/api/rest/rest-api-exception.filter.ts)
  • format the error data like this
{
"statusCode": 400,
"messages": [<messages>],
"error": "Bad Request"
}

We probably need to pass the change in writeRestResponseOnExceptionCaught method image
Also, I think all those cases should be tested if possible

I'm on this

This is ready for another review @martmull
Screenshot 2025-01-31 at 14 18 19

@pacyL2K19 pacyL2K19 requested a review from prastoin January 31, 2025 13:06
Copy link
Contributor

@martmull martmull left a comment

Choose a reason for hiding this comment

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

Very nice, thank you for your outstanding work! I believe we have an excellent first PR to kickstart the migration

@martmull martmull merged commit 66296a4 into twentyhq:main Jan 31, 2025
30 checks passed
Copy link
Contributor

Fails
🚫

node failed.

Log

�[31mError: �[39m SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
�[90m    at parseJSONFromBytes (node:internal/deps/undici/undici:5591:19)�[39m
�[90m    at successSteps (node:internal/deps/undici/undici:5562:27)�[39m
�[90m    at fullyReadBody (node:internal/deps/undici/undici:1665:9)�[39m
�[90m    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)�[39m
�[90m    at async specConsumeBody (node:internal/deps/undici/undici:5571:7)�[39m
danger-results://tmp/danger-results-903b417b.json

Generated by 🚫 dangerJS against f2252b6

@prastoin
Copy link
Contributor

prastoin commented Jan 31, 2025

Congrats @pacyL2K19 ! :) Thanks for the great code and work !

@pacyL2K19 pacyL2K19 deleted the feat/rest-api-on-twenty-orm branch February 10, 2025 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants