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:
Paul Rastoin
2025-10-29 15:07:08 +01:00
committed by GitHub
parent 28c6edfa1f
commit c19a799973
34 changed files with 1281 additions and 577 deletions
@@ -13,11 +13,14 @@ 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';
import { CreateViewFieldInput } from 'src/engine/metadata-modules/view-field/dtos/inputs/create-view-field.input';
import { UpdateViewFieldInput } from 'src/engine/metadata-modules/view-field/dtos/inputs/update-view-field.input';
import { ViewFieldDTO } from 'src/engine/metadata-modules/view-field/dtos/view-field.dto';
import { ViewFieldEntity } from 'src/engine/metadata-modules/view-field/entities/view-field.entity';
import {
generateViewFieldExceptionMessage,
@@ -27,13 +30,18 @@ import {
ViewFieldExceptionMessageKey,
} from 'src/engine/metadata-modules/view-field/exceptions/view-field.exception';
import { ViewFieldRestApiExceptionFilter } from 'src/engine/metadata-modules/view-field/filters/view-field-rest-api-exception.filter';
import { ViewFieldV2Service } from 'src/engine/metadata-modules/view-field/services/view-field-v2.service';
import { ViewFieldService } from 'src/engine/metadata-modules/view-field/services/view-field.service';
@Controller('rest/metadata/viewFields')
@UseGuards(WorkspaceAuthGuard)
@UseFilters(ViewFieldRestApiExceptionFilter)
export class ViewFieldController {
constructor(private readonly viewFieldService: ViewFieldService) {}
constructor(
private readonly viewFieldService: ViewFieldService,
private readonly viewFieldV2Service: ViewFieldV2Service,
private readonly featureFlagService: FeatureFlagService,
) {}
@Get()
async findMany(
@@ -77,25 +85,79 @@ export class ViewFieldController {
@Param('id') id: string,
@Body() input: UpdateViewFieldInput['update'],
@AuthWorkspace() workspace: WorkspaceEntity,
): Promise<ViewFieldEntity> {
): Promise<ViewFieldDTO> {
const isWorkspaceMigrationV2Enabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
workspace.id,
);
if (isWorkspaceMigrationV2Enabled) {
return await this.viewFieldV2Service.updateOne({
updateViewFieldInput: { id, update: input },
workspaceId: workspace.id,
});
}
const updatedViewField = await this.viewFieldService.update(
id,
workspace.id,
input,
);
return updatedViewField;
// Convert ViewFieldEntity to ViewFieldDTO for consistency
return {
id: updatedViewField.id,
fieldMetadataId: updatedViewField.fieldMetadataId,
isVisible: updatedViewField.isVisible,
size: updatedViewField.size,
position: updatedViewField.position,
aggregateOperation: updatedViewField.aggregateOperation,
viewId: updatedViewField.viewId,
workspaceId: updatedViewField.workspaceId,
createdAt: updatedViewField.createdAt,
updatedAt: updatedViewField.updatedAt,
deletedAt: updatedViewField.deletedAt,
};
}
@Post()
async create(
@Body() input: CreateViewFieldInput,
@AuthWorkspace() workspace: WorkspaceEntity,
): Promise<ViewFieldEntity> {
return this.viewFieldService.create({
): Promise<ViewFieldDTO> {
const isWorkspaceMigrationV2Enabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
workspace.id,
);
if (isWorkspaceMigrationV2Enabled) {
return await this.viewFieldV2Service.createOne({
createViewFieldInput: input,
workspaceId: workspace.id,
});
}
const createdViewField = await this.viewFieldService.create({
...input,
workspaceId: workspace.id,
});
// Convert ViewFieldEntity to ViewFieldDTO for consistency
return {
id: createdViewField.id,
fieldMetadataId: createdViewField.fieldMetadataId,
isVisible: createdViewField.isVisible,
size: createdViewField.size,
position: createdViewField.position,
aggregateOperation: createdViewField.aggregateOperation,
viewId: createdViewField.viewId,
workspaceId: createdViewField.workspaceId,
createdAt: createdViewField.createdAt,
updatedAt: createdViewField.updatedAt,
deletedAt: createdViewField.deletedAt,
};
}
@Delete(':id')
@@ -103,6 +165,21 @@ export class ViewFieldController {
@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 deletedViewField = await this.viewFieldV2Service.deleteOne({
deleteViewFieldInput: { id },
workspaceId: workspace.id,
});
return { success: isDefined(deletedViewField) };
}
const deletedViewField = await this.viewFieldService.delete(
id,
workspace.id,
@@ -2,47 +2,91 @@ 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 {
ViewFieldException,
ViewFieldExceptionCode,
} from 'src/engine/metadata-modules/view-field/exceptions/view-field.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(ViewFieldException)
@Injectable()
@Catch(ViewFieldException, WorkspaceMigrationBuilderExceptionV2)
export class ViewFieldRestApiExceptionFilter implements ExceptionFilter {
constructor(
private readonly httpExceptionHandlerService: HttpExceptionHandlerService,
private readonly i18nService: I18nService,
) {}
catch(exception: ViewFieldException, host: ArgumentsHost) {
catch(
exception: ViewFieldException | WorkspaceMigrationBuilderExceptionV2,
host: ArgumentsHost,
) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
switch (exception.code) {
case ViewFieldExceptionCode.VIEW_FIELD_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewFieldExceptionCode.INVALID_VIEW_FIELD_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 ViewFieldException) {
switch (exception.code) {
case ViewFieldExceptionCode.VIEW_FIELD_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewFieldExceptionCode.INVALID_VIEW_FIELD_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,
);
}
}
// Fallback for any other exception type
const unknownException = new UnknownException(
'Internal server error',
'INTERNAL_ERROR',
);
return this.httpExceptionHandlerService.handleError(
unknownException as CustomException,
response,
500,
);
}
}
@@ -2,47 +2,91 @@ 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 {
ViewFilterGroupException,
ViewFilterGroupExceptionCode,
} from 'src/engine/metadata-modules/view-filter-group/exceptions/view-filter-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(ViewFilterGroupException)
@Injectable()
@Catch(ViewFilterGroupException, WorkspaceMigrationBuilderExceptionV2)
export class ViewFilterGroupRestApiExceptionFilter implements ExceptionFilter {
constructor(
private readonly httpExceptionHandlerService: HttpExceptionHandlerService,
private readonly i18nService: I18nService,
) {}
catch(exception: ViewFilterGroupException, host: ArgumentsHost) {
catch(
exception: ViewFilterGroupException | WorkspaceMigrationBuilderExceptionV2,
host: ArgumentsHost,
) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
switch (exception.code) {
case ViewFilterGroupExceptionCode.VIEW_FILTER_GROUP_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewFilterGroupExceptionCode.INVALID_VIEW_FILTER_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 ViewFilterGroupException) {
switch (exception.code) {
case ViewFilterGroupExceptionCode.VIEW_FILTER_GROUP_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewFilterGroupExceptionCode.INVALID_VIEW_FILTER_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,
);
}
}
// Fallback for any other exception type
const unknownException = new UnknownException(
'Internal server error',
'INTERNAL_ERROR',
);
return this.httpExceptionHandlerService.handleError(
unknownException as CustomException,
response,
500,
);
}
}
@@ -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 {
ViewFilterExceptionMessageKey,
} from 'src/engine/metadata-modules/view-filter/exceptions/view-filter.exception';
import { ViewFilterRestApiExceptionFilter } from 'src/engine/metadata-modules/view-filter/filters/view-filter-rest-api-exception.filter';
import { ViewFilterV2Service } from 'src/engine/metadata-modules/view-filter/services/view-filter-v2.service';
import { ViewFilterService } from 'src/engine/metadata-modules/view-filter/services/view-filter.service';
@Controller('rest/metadata/viewFilters')
@UseGuards(WorkspaceAuthGuard)
@UseFilters(ViewFilterRestApiExceptionFilter)
export class ViewFilterController {
constructor(private readonly viewFilterService: ViewFilterService) {}
constructor(
private readonly viewFilterService: ViewFilterService,
private readonly viewFilterV2Service: ViewFilterV2Service,
private readonly featureFlagService: FeatureFlagService,
) {}
@Get()
async findMany(
@@ -77,6 +84,19 @@ export class ViewFilterController {
@Body() input: CreateViewFilterInput,
@AuthWorkspace() workspace: WorkspaceEntity,
): Promise<ViewFilterDTO> {
const isWorkspaceMigrationV2Enabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
workspace.id,
);
if (isWorkspaceMigrationV2Enabled) {
return await this.viewFilterV2Service.createOne({
createViewFilterInput: input,
workspaceId: workspace.id,
});
}
return this.viewFilterService.create({
...input,
workspaceId: workspace.id,
@@ -94,6 +114,19 @@ export class ViewFilterController {
update: input.update ?? input,
};
const isWorkspaceMigrationV2Enabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
workspace.id,
);
if (isWorkspaceMigrationV2Enabled) {
return await this.viewFilterV2Service.updateOne({
updateViewFilterInput: updateInput,
workspaceId: workspace.id,
});
}
const updatedViewFilter = await this.viewFilterService.update(
updateInput.id,
workspace.id,
@@ -108,6 +141,21 @@ export class ViewFilterController {
@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 deletedViewFilter = await this.viewFilterV2Service.deleteOne({
deleteViewFilterInput: { id },
workspaceId: workspace.id,
});
return { success: isDefined(deletedViewFilter) };
}
const deletedViewFilter = await this.viewFilterService.delete(
id,
workspace.id,
@@ -2,47 +2,91 @@ 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 {
ViewFilterException,
ViewFilterExceptionCode,
} from 'src/engine/metadata-modules/view-filter/exceptions/view-filter.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(ViewFilterException)
@Injectable()
@Catch(ViewFilterException, WorkspaceMigrationBuilderExceptionV2)
export class ViewFilterRestApiExceptionFilter implements ExceptionFilter {
constructor(
private readonly httpExceptionHandlerService: HttpExceptionHandlerService,
private readonly i18nService: I18nService,
) {}
catch(exception: ViewFilterException, host: ArgumentsHost) {
catch(
exception: ViewFilterException | WorkspaceMigrationBuilderExceptionV2,
host: ArgumentsHost,
) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
switch (exception.code) {
case ViewFilterExceptionCode.VIEW_FILTER_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewFilterExceptionCode.INVALID_VIEW_FILTER_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 ViewFilterException) {
switch (exception.code) {
case ViewFilterExceptionCode.VIEW_FILTER_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewFilterExceptionCode.INVALID_VIEW_FILTER_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,
);
}
}
// Fallback for any other exception type
const unknownException = new UnknownException(
'Internal server error',
'INTERNAL_ERROR',
);
return this.httpExceptionHandlerService.handleError(
unknownException as CustomException,
response,
500,
);
}
}
@@ -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,
@@ -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`;
}
}
};
@@ -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,
);
}
}
@@ -2,47 +2,91 @@ 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 {
ViewSortException,
ViewSortExceptionCode,
} from 'src/engine/metadata-modules/view-sort/exceptions/view-sort.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(ViewSortException)
@Injectable()
@Catch(ViewSortException, WorkspaceMigrationBuilderExceptionV2)
export class ViewSortRestApiExceptionFilter implements ExceptionFilter {
constructor(
private readonly httpExceptionHandlerService: HttpExceptionHandlerService,
private readonly i18nService: I18nService,
) {}
catch(exception: ViewSortException, host: ArgumentsHost) {
catch(
exception: ViewSortException | WorkspaceMigrationBuilderExceptionV2,
host: ArgumentsHost,
) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
switch (exception.code) {
case ViewSortExceptionCode.VIEW_SORT_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewSortExceptionCode.INVALID_VIEW_SORT_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 ViewSortException) {
switch (exception.code) {
case ViewSortExceptionCode.VIEW_SORT_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewSortExceptionCode.INVALID_VIEW_SORT_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,
);
}
}
// Fallback for any other exception type
const unknownException = new UnknownException(
'Internal server error',
'INTERNAL_ERROR',
);
return this.httpExceptionHandlerService.handleError(
unknownException as CustomException,
response,
500,
);
}
}
@@ -14,6 +14,8 @@ import {
import { type APP_LOCALES } from 'twenty-shared/translations';
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 { I18nService } from 'src/engine/core-modules/i18n/i18n.service';
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
@@ -31,6 +33,7 @@ import {
ViewExceptionMessageKey,
} from 'src/engine/metadata-modules/view/exceptions/view.exception';
import { ViewRestApiExceptionFilter } from 'src/engine/metadata-modules/view/filters/view-rest-api-exception.filter';
import { ViewV2Service } from 'src/engine/metadata-modules/view/services/view-v2.service';
import { ViewService } from 'src/engine/metadata-modules/view/services/view.service';
import { WorkspaceMetadataCacheService } from 'src/engine/metadata-modules/workspace-metadata-cache/services/workspace-metadata-cache.service';
@@ -40,6 +43,8 @@ import { WorkspaceMetadataCacheService } from 'src/engine/metadata-modules/works
export class ViewController {
constructor(
private readonly viewService: ViewService,
private readonly viewV2Service: ViewV2Service,
private readonly featureFlagService: FeatureFlagService,
private readonly workspaceMetadataCacheService: WorkspaceMetadataCacheService,
private readonly i18nService: I18nService,
) {}
@@ -98,10 +103,25 @@ export class ViewController {
@AuthWorkspace() workspace: WorkspaceEntity,
@RequestLocale() locale?: keyof typeof APP_LOCALES,
): Promise<ViewDTO> {
const view = await this.viewService.create({
...input,
workspaceId: workspace.id,
});
const isWorkspaceMigrationV2Enabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
workspace.id,
);
let view: ViewDTO;
if (isWorkspaceMigrationV2Enabled) {
view = await this.viewV2Service.createOne({
createViewInput: input,
workspaceId: workspace.id,
});
} else {
view = await this.viewService.create({
...input,
workspaceId: workspace.id,
});
}
const processedViews = await this.processViewsWithTemplates(
[view],
@@ -119,7 +139,25 @@ export class ViewController {
@RequestLocale() locale: keyof typeof APP_LOCALES | undefined,
@AuthWorkspace() workspace: WorkspaceEntity,
): Promise<ViewDTO> {
const updatedView = await this.viewService.update(id, workspace.id, input);
const isWorkspaceMigrationV2Enabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
workspace.id,
);
let updatedView: ViewDTO;
if (isWorkspaceMigrationV2Enabled) {
updatedView = await this.viewV2Service.updateOne({
updateViewInput: {
...input,
id,
},
workspaceId: workspace.id,
});
} else {
updatedView = await this.viewService.update(id, workspace.id, input);
}
const processedViews = await this.processViewsWithTemplates(
[updatedView],
@@ -135,7 +173,22 @@ export class ViewController {
@Param('id') id: string,
@AuthWorkspace() workspace: WorkspaceEntity,
): Promise<{ success: boolean }> {
const deletedView = await this.viewService.delete(id, workspace.id);
const isWorkspaceMigrationV2Enabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_WORKSPACE_MIGRATION_V2_ENABLED,
workspace.id,
);
let deletedView: ViewDTO | null;
if (isWorkspaceMigrationV2Enabled) {
deletedView = await this.viewV2Service.deleteOne({
deleteViewInput: { id },
workspaceId: workspace.id,
});
} else {
deletedView = await this.viewService.delete(id, workspace.id);
}
return { success: isDefined(deletedView) };
}
@@ -2,47 +2,91 @@ 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 {
ViewException,
ViewExceptionCode,
} from 'src/engine/metadata-modules/view/exceptions/view.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(ViewException)
@Injectable()
@Catch(ViewException, WorkspaceMigrationBuilderExceptionV2)
export class ViewRestApiExceptionFilter implements ExceptionFilter {
constructor(
private readonly httpExceptionHandlerService: HttpExceptionHandlerService,
private readonly i18nService: I18nService,
) {}
catch(exception: ViewException, host: ArgumentsHost) {
catch(
exception: ViewException | WorkspaceMigrationBuilderExceptionV2,
host: ArgumentsHost,
) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
switch (exception.code) {
case ViewExceptionCode.VIEW_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewExceptionCode.INVALID_VIEW_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 ViewException) {
switch (exception.code) {
case ViewExceptionCode.VIEW_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception as CustomException,
response,
404,
);
case ViewExceptionCode.INVALID_VIEW_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,
);
}
}
// Fallback for any other exception type
const unknownException = new UnknownException(
'Internal server error',
'INTERNAL_ERROR',
);
return this.httpExceptionHandlerService.handleError(
unknownException as CustomException,
response,
500,
);
}
}