Fix view rest api in v2 (#15398)
# Introduction Initially wanted to reactive the test introduced in https://github.com/twentyhq/twenty/pull/15393, that was failing because of direct data source access removing all views ( even seeded one ) which was making the test fail While doing so discovered a lot of issue with the rest API: - Rest api wasn't consuming the v2 at all - Rest api wasn't prepared to handle v2 exceptions - Rest api did not handled unknown exceptions ( timeout ) Refactored the cleanup of each test to follow black box pattern and avoid test leakage
This commit is contained in:
+54
-1
@@ -13,6 +13,8 @@ import {
|
||||
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
|
||||
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
|
||||
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
|
||||
import { WorkspaceAuthGuard } from 'src/engine/guards/workspace-auth.guard';
|
||||
@@ -27,13 +29,18 @@ import {
|
||||
ViewGroupExceptionMessageKey,
|
||||
} from 'src/engine/metadata-modules/view-group/exceptions/view-group.exception';
|
||||
import { ViewGroupRestApiExceptionFilter } from 'src/engine/metadata-modules/view-group/filters/view-group-rest-api-exception.filter';
|
||||
import { ViewGroupV2Service } from 'src/engine/metadata-modules/view-group/services/view-group-v2.service';
|
||||
import { ViewGroupService } from 'src/engine/metadata-modules/view-group/services/view-group.service';
|
||||
|
||||
@Controller('rest/metadata/viewGroups')
|
||||
@UseGuards(WorkspaceAuthGuard)
|
||||
@UseFilters(ViewGroupRestApiExceptionFilter)
|
||||
export class ViewGroupController {
|
||||
constructor(private readonly viewGroupService: ViewGroupService) {}
|
||||
constructor(
|
||||
private readonly viewGroupService: ViewGroupService,
|
||||
private readonly viewGroupV2Service: ViewGroupV2Service,
|
||||
private readonly featureFlagService: FeatureFlagService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
async findMany(
|
||||
@@ -77,6 +84,19 @@ export class ViewGroupController {
|
||||
@Body() input: CreateViewGroupInput,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<ViewGroupDTO> {
|
||||
const isWorkspaceMigrationV2Enabled =
|
||||
await this.featureFlagService.isFeatureEnabled(
|
||||
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
|
||||
workspace.id,
|
||||
);
|
||||
|
||||
if (isWorkspaceMigrationV2Enabled) {
|
||||
return await this.viewGroupV2Service.createOne({
|
||||
createViewGroupInput: input,
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
}
|
||||
|
||||
return this.viewGroupService.create({
|
||||
...input,
|
||||
workspaceId: workspace.id,
|
||||
@@ -89,6 +109,24 @@ export class ViewGroupController {
|
||||
@Body() input: UpdateViewGroupInput,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<ViewGroupDTO> {
|
||||
const isWorkspaceMigrationV2Enabled =
|
||||
await this.featureFlagService.isFeatureEnabled(
|
||||
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
|
||||
workspace.id,
|
||||
);
|
||||
|
||||
if (isWorkspaceMigrationV2Enabled) {
|
||||
const updateInput = {
|
||||
id,
|
||||
update: input.update ?? input,
|
||||
};
|
||||
|
||||
return await this.viewGroupV2Service.updateOne({
|
||||
updateViewGroupInput: updateInput,
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
}
|
||||
|
||||
const updatedViewGroup = await this.viewGroupService.update(
|
||||
id,
|
||||
workspace.id,
|
||||
@@ -103,6 +141,21 @@ export class ViewGroupController {
|
||||
@Param('id') id: string,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<{ success: boolean }> {
|
||||
const isWorkspaceMigrationV2Enabled =
|
||||
await this.featureFlagService.isFeatureEnabled(
|
||||
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
|
||||
workspace.id,
|
||||
);
|
||||
|
||||
if (isWorkspaceMigrationV2Enabled) {
|
||||
const deletedViewGroup = await this.viewGroupV2Service.deleteOne({
|
||||
deleteViewGroupInput: { id },
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
|
||||
return { success: isDefined(deletedViewGroup) };
|
||||
}
|
||||
|
||||
const deletedViewGroup = await this.viewGroupService.delete(
|
||||
id,
|
||||
workspace.id,
|
||||
|
||||
+5
-3
@@ -1,6 +1,5 @@
|
||||
import { msg } from '@lingui/core/macro';
|
||||
import { type MessageDescriptor } from '@lingui/core';
|
||||
import { assertUnreachable } from 'twenty-shared/utils';
|
||||
import { msg } from '@lingui/core/macro';
|
||||
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
@@ -44,7 +43,7 @@ export const generateViewGroupExceptionMessage = (
|
||||
case ViewGroupExceptionMessageKey.FIELD_METADATA_ID_REQUIRED:
|
||||
return 'FieldMetadataId is required';
|
||||
default:
|
||||
assertUnreachable(key);
|
||||
return 'unknown';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -58,5 +57,8 @@ export const generateViewGroupUserFriendlyExceptionMessage = (
|
||||
return msg`ViewId is required to create a view group.`;
|
||||
case ViewGroupExceptionMessageKey.FIELD_METADATA_ID_REQUIRED:
|
||||
return msg`FieldMetadataId is required to create a view group.`;
|
||||
default: {
|
||||
return msg`unknown`;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+65
-22
@@ -2,47 +2,90 @@ import {
|
||||
type ArgumentsHost,
|
||||
Catch,
|
||||
type ExceptionFilter,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { type Response } from 'express';
|
||||
import { SOURCE_LOCALE } from 'twenty-shared/translations';
|
||||
|
||||
import { HttpExceptionHandlerService } from 'src/engine/core-modules/exception-handler/http-exception-handler.service';
|
||||
import { I18nService } from 'src/engine/core-modules/i18n/i18n.service';
|
||||
import {
|
||||
ViewGroupException,
|
||||
ViewGroupExceptionCode,
|
||||
} from 'src/engine/metadata-modules/view-group/exceptions/view-group.exception';
|
||||
import { type CustomException } from 'src/utils/custom-exception';
|
||||
import { WorkspaceMigrationBuilderExceptionV2 } from 'src/engine/workspace-manager/workspace-migration-v2/exceptions/workspace-migration-builder-exception-v2';
|
||||
import { fromWorkspaceMigrationBuilderExceptionToMetadataValidationResponseError } from 'src/engine/workspace-manager/workspace-migration-v2/interceptors/utils/from-workspace-migration-builder-exception-to-metadata-validation-response-error.util';
|
||||
import {
|
||||
type CustomException,
|
||||
UnknownException,
|
||||
} from 'src/utils/custom-exception';
|
||||
|
||||
@Catch(ViewGroupException)
|
||||
@Injectable()
|
||||
@Catch(ViewGroupException, WorkspaceMigrationBuilderExceptionV2)
|
||||
export class ViewGroupRestApiExceptionFilter implements ExceptionFilter {
|
||||
constructor(
|
||||
private readonly httpExceptionHandlerService: HttpExceptionHandlerService,
|
||||
private readonly i18nService: I18nService,
|
||||
) {}
|
||||
|
||||
catch(exception: ViewGroupException, host: ArgumentsHost) {
|
||||
catch(
|
||||
exception: ViewGroupException | WorkspaceMigrationBuilderExceptionV2,
|
||||
host: ArgumentsHost,
|
||||
) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
|
||||
switch (exception.code) {
|
||||
case ViewGroupExceptionCode.VIEW_GROUP_NOT_FOUND:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
404,
|
||||
);
|
||||
case ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
);
|
||||
default:
|
||||
// TODO: change to 500 when we have input validation
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
if (exception instanceof WorkspaceMigrationBuilderExceptionV2) {
|
||||
const i18n = this.i18nService.getI18nInstance(SOURCE_LOCALE);
|
||||
const { errors, summary } =
|
||||
fromWorkspaceMigrationBuilderExceptionToMetadataValidationResponseError(
|
||||
exception,
|
||||
i18n,
|
||||
);
|
||||
|
||||
return response.status(400).json({
|
||||
statusCode: 400,
|
||||
error: 'METADATA_VALIDATION_ERROR',
|
||||
message: exception.message || 'Validation failed',
|
||||
errors,
|
||||
summary,
|
||||
});
|
||||
}
|
||||
|
||||
if (exception instanceof ViewGroupException) {
|
||||
switch (exception.code) {
|
||||
case ViewGroupExceptionCode.VIEW_GROUP_NOT_FOUND:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
404,
|
||||
);
|
||||
case ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA:
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
);
|
||||
default:
|
||||
// TODO: change to 500 when we have input validation
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
exception as CustomException,
|
||||
response,
|
||||
400,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const unknownException = new UnknownException(
|
||||
'Internal server error',
|
||||
'INTERNAL_ERROR',
|
||||
);
|
||||
|
||||
return this.httpExceptionHandlerService.handleError(
|
||||
unknownException as CustomException,
|
||||
response,
|
||||
500,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user