-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1/n]: Migrate
deleteOne
Rest API to use TwentyORM directly (#9784)
# This PR - Addressing #3644 - Migrates the `DELETE /rest/*` endpoint to use TwentyORM - Factorizes common middleware logic into a common module --------- Co-authored-by: martmull <martmull@hotmail.fr>
- Loading branch information
Showing
22 changed files
with
548 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/twenty-server/src/engine/api/rest/core/rest-api-core-v2.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
|
||
import { Request } from 'express'; | ||
import { capitalize } from 'twenty-shared'; | ||
|
||
import { CoreQueryBuilderFactory } from 'src/engine/api/rest/core/query-builder/core-query-builder.factory'; | ||
import { parseCorePath } from 'src/engine/api/rest/core/query-builder/utils/path-parsers/parse-core-path.utils'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
|
||
@Injectable() | ||
export class RestApiCoreServiceV2 { | ||
constructor( | ||
private readonly coreQueryBuilderFactory: CoreQueryBuilderFactory, | ||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
) {} | ||
|
||
async delete(request: Request) { | ||
const { workspace } = request; | ||
const { object: parsedObject, id: recordId } = parseCorePath(request); | ||
|
||
const objectMetadata = await this.coreQueryBuilderFactory.getObjectMetadata( | ||
request, | ||
parsedObject, | ||
); | ||
|
||
if (!objectMetadata) { | ||
throw new BadRequestException('Object metadata not found'); | ||
} | ||
|
||
if (!recordId) { | ||
throw new BadRequestException('Record ID not found'); | ||
} | ||
|
||
const objectMetadataNameSingular = | ||
objectMetadata.objectMetadataItem.nameSingular; | ||
|
||
if (!workspace?.id) { | ||
throw new BadRequestException('Workspace not found'); | ||
} | ||
|
||
const repository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace( | ||
workspace.id, | ||
objectMetadataNameSingular, | ||
); | ||
|
||
const recordToDelete = await repository.findOneOrFail({ | ||
where: { | ||
id: recordId, | ||
}, | ||
}); | ||
|
||
await repository.delete(recordId); | ||
|
||
return this.formatResult('delete', objectMetadataNameSingular, { | ||
id: recordToDelete.id, | ||
}); | ||
} | ||
|
||
private formatResult<T>( | ||
operation: 'delete' | 'create' | 'update' | 'find', | ||
objectNameSingular: string, | ||
data: T, | ||
) { | ||
const result = { | ||
data: { | ||
[operation + capitalize(objectNameSingular)]: data, | ||
}, | ||
}; | ||
|
||
return result; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/twenty-server/src/engine/api/rest/rest-api-exception.filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; | ||
|
||
import { Response } from 'express'; | ||
|
||
import { HttpExceptionHandlerService } from 'src/engine/core-modules/exception-handler/http-exception-handler.service'; | ||
import { CustomException } from 'src/utils/custom-exception'; | ||
|
||
@Catch() | ||
export class RestApiExceptionFilter implements ExceptionFilter { | ||
constructor( | ||
private readonly httpExceptionHandlerService: HttpExceptionHandlerService, | ||
) {} | ||
|
||
catch(exception: unknown, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
|
||
return this.httpExceptionHandlerService.handleError( | ||
exception as CustomException, | ||
response, | ||
400, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/twenty-server/src/engine/middlewares/constants/default-error-message.constant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const INTERNAL_SERVER_ERROR = 'Internal server error'; |
19 changes: 19 additions & 0 deletions
19
...twenty-server/src/engine/middlewares/constants/excluded-middleware-operations.constant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const EXCLUDED_MIDDLEWARE_OPERATIONS = [ | ||
'GetClientConfig', | ||
'GetWorkspaceFromInviteHash', | ||
'Track', | ||
'CheckUserExists', | ||
'GetLoginTokenFromCredentials', | ||
'GetAuthTokensFromLoginToken', | ||
'GetLoginTokenFromEmailVerificationToken', | ||
'ResendEmailVerificationToken', | ||
'SignUp', | ||
'RenewToken', | ||
'EmailPasswordResetLink', | ||
'ValidatePasswordResetToken', | ||
'UpdatePasswordViaResetToken', | ||
'IntrospectionQuery', | ||
'ExchangeAuthorizationCode', | ||
'GetAuthorizationUrl', | ||
'GetPublicWorkspaceDataBySubdomain', | ||
] as const; |
Oops, something went wrong.