Skip to content

The open alternative to Salesforce, designed for AI.

refactor(rest): move restore to a suffix path and make restore-one reachable (#24544)

Félix Malfait9e4f03e4d4b1
diff --git a/packages/twenty-server/src/engine/api/rest/core/controllers/rest-api-core.controller.ts b/packages/twenty-server/src/engine/api/rest/core/controllers/rest-api-core.controller.ts
index 9d0462a6b3f..d00253dbeea 100644
--- a/packages/twenty-server/src/engine/api/rest/core/controllers/rest-api-core.controller.ts
+++ b/packages/twenty-server/src/engine/api/rest/core/controllers/rest-api-core.controller.ts
@@ -107,7 +107,7 @@ export class RestApiCoreController {
res.status(200).send(result);
}
- @Patch('restore/*path')
+ @Patch('*path/restore')
async handleApiRestore(
@Req() request: AuthenticatedRequest,
@Res() res: Response,
diff --git a/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/__tests__/parse-core-path.utils.spec.ts b/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/__tests__/parse-core-path.utils.spec.ts
index cbb1d24023b..8c1f55d9b12 100644
--- a/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/__tests__/parse-core-path.utils.spec.ts
+++ b/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/__tests__/parse-core-path.utils.spec.ts
@@ -73,4 +73,63 @@ describe('parseCorePath', () => {
id: undefined,
});
});
+ it('should parse object and id from restore one request', () => {
+ const request: any = {
+ method: 'PATCH',
+ path: `/rest/companies/${testUUID}/restore`,
+ };
+
+ expect(parseCorePath(request)).toEqual({
+ object: 'companies',
+ id: testUUID,
+ });
+ });
+
+ it('should parse object from restore many request', () => {
+ const request: any = { method: 'PATCH', path: '/rest/companies/restore' };
+
+ expect(parseCorePath(request)).toEqual({
+ object: 'companies',
+ id: undefined,
+ });
+ });
+
+ it('should throw for malformed uuid in restore one request', () => {
+ const malformedUUID = 'malformed-uuid';
+ const request: any = {
+ method: 'PATCH',
+ path: `/rest/companies/${malformedUUID}/restore`,
+ };
+
+ expect(() => parseCorePath(request)).toThrow(
+ `'${malformedUUID}' is not a valid UUID`,
+ );
+ });
+
+ it('should throw for an over-long restore request', () => {
+ const request: any = {
+ method: 'PATCH',
+ path: `/rest/companies/${testUUID}/toto/restore`,
+ };
+
+ expect(() => parseCorePath(request)).toThrow(
+ `Query path '/rest/companies/${testUUID}/toto/restore' invalid. Valid examples: /rest/companies/id or /rest/companies or /rest/batch/companies`,
+ );
+ });
+
+ // Every method has a wildcard route on this controller, so without the
+ // method check DELETE would read the id as a destroy target.
+ it.each(['DELETE', 'GET', 'POST', 'PUT'])(
+ 'should throw for a restore-shaped path on %s',
+ (method) => {
+ const request: any = {
+ method,
+ path: `/rest/companies/${testUUID}/restore`,
+ };
+
+ expect(() => parseCorePath(request)).toThrow(
+ `Query path '/rest/companies/${testUUID}/restore' invalid. Valid examples: /rest/companies/id or /rest/companies or /rest/batch/companies`,
+ );
+ },
+ );
});
diff --git a/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/parse-core-path.utils.ts b/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/parse-core-path.utils.ts
index fafa0cda87e..d207f93daa7 100644
--- a/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/parse-core-path.utils.ts
+++ b/packages/twenty-server/src/engine/api/rest/input-request-parsers/path-parser-utils/parse-core-path.utils.ts
@@ -8,15 +8,22 @@ export const parseCorePath = (
request: Request,
): { object: string; id?: string } => {
const queryAction = request.path
- .replace(`/${ApiPath.Rest}/`, '')
- .replace(`/${ApiPath.Rest}`, '')
+ .replace(new RegExp(`^/${ApiPath.Rest}`), '')
.split('/')
.filter(Boolean);
- if (
- queryAction.length > 2 ||
- (queryAction.length > 3 && queryAction[0] === 'restore')
- ) {
+ // Restore is the only action on a single record, so /{object}/{id}/restore
+ // is the one path with a third segment. It is mounted on PATCH alone;
+ // allowing the segment on other methods would let a restore-shaped path
+ // through their wildcard routes, where DELETE would read the id as a
+ // destroy target.
+ const isRestoreRequest =
+ request.method === 'PATCH' &&
+ queryAction[queryAction.length - 1] === 'restore';
+
+ const maximumSegmentCount = isRestoreRequest ? 3 : 2;
+
+ if (queryAction.length > maximumSegmentCount) {
throw new BadRequestException(
`Query path '${request.path}' invalid. Valid examples: /${ApiPath.Rest}/companies/id or /${ApiPath.Rest}/companies or /${ApiPath.Rest}/batch/companies`,
);
@@ -44,15 +51,15 @@ export const parseCorePath = (
return { object: queryAction[0] };
}
- if (queryAction[0] === 'restore') {
- const recordId = queryAction[2];
+ if (isRestoreRequest) {
+ const recordId = queryAction.length === 3 ? queryAction[1] : undefined;
if (isDefined(recordId) && !isValidUuid(recordId)) {
throw new BadRequestException(`'${recordId}' is not a valid UUID`);
}
return {
- object: queryAction[1],
+ object: queryAction[0],
id: recordId,
};
}
diff --git a/packages/twenty-server/src/engine/core-modules/open-api/open-api.service.ts b/packages/twenty-server/src/engine/core-modules/open-api/open-api.service.ts
index 46e802acc91..1448774836a 100644
--- a/packages/twenty-server/src/engine/core-modules/open-api/open-api.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/open-api/open-api.service.ts
@@ -124,9 +124,9 @@ export class OpenApiService {
paths[`/${item.namePlural}/{id}`] = computeSingleResultPath(item);
paths[`/${item.namePlural}/duplicates`] =
computeDuplicatesResultPath(item);
- paths[`/restore/${item.namePlural}/{id}`] =
+ paths[`/${item.namePlural}/{id}/restore`] =
computeRestoreOneResultPath(item);
- paths[`/restore/${item.namePlural}`] = computeRestoreManyResultPath(item);
+ paths[`/${item.namePlural}/restore`] = computeRestoreManyResultPath(item);
paths[`/${item.namePlural}/merge`] = computeMergeManyResultPath(item);
paths[`/${item.namePlural}/groupBy`] = computeGroupByResultPath(item);