[3/n]: Migrate the PUT rest/* and PATCH rest/* to use TwentyORM (#10002)

# This PR

- Is addressing #3644 
- Migrates the PUT and PATCH rest/* endpoints to use twentyORM directly
- Adds integration tests
This commit is contained in:
P A C · 先生
2025-02-04 18:25:02 +02:00
committed by GitHub
parent 7dfb9dd77f
commit 5be22413c9
5 changed files with 144 additions and 5 deletions

View File

@ -61,19 +61,21 @@ export class RestApiCoreController {
}
@Patch()
@UseFilters(RestApiExceptionFilter)
async handleApiPatch(@Req() request: Request, @Res() res: Response) {
const result = await this.restApiCoreService.update(request);
const result = await this.restApiCoreServiceV2.update(request);
res.status(200).send(cleanGraphQLResponse(result.data.data));
res.status(200).send(result);
}
// This endpoint is not documented in the OpenAPI schema.
// We keep it to avoid a breaking change since it initially used PUT instead of PATCH,
// and because the PUT verb is often used as a PATCH.
@Put()
@UseFilters(RestApiExceptionFilter)
async handleApiPut(@Req() request: Request, @Res() res: Response) {
const result = await this.restApiCoreService.update(request);
const result = await this.restApiCoreServiceV2.update(request);
res.status(200).send(cleanGraphQLResponse(result.data.data));
res.status(200).send(result);
}
}

View File

@ -48,6 +48,32 @@ export class RestApiCoreServiceV2 {
);
}
async update(request: Request) {
const { id: recordId } = parseCorePath(request);
if (!recordId) {
throw new BadRequestException('Record ID not found');
}
const { objectMetadataNameSingular, repository } =
await this.getRepositoryAndMetadataOrFail(request);
const recordToUpdate = await repository.findOneOrFail({
where: { id: recordId },
});
const updatedRecord = await repository.save({
...recordToUpdate,
...request.body,
});
return this.formatResult(
'update',
objectMetadataNameSingular,
updatedRecord,
);
}
private formatResult<T>(
operation: 'delete' | 'create' | 'update' | 'find',
objectNameSingular: string,