Move view in metadata-modules/ and create atomic folder + module for each view entity (#14990)

# Introduction
Preparing view-filter and view-group introduction in v2 core engine
Moving view from `core-modules` to `metadata-modules`

## What happened

### Created dedicated modules for each view entity:
- ViewFieldModule
- ViewFilterModule
- ViewFilterGroupModule
- ViewGroupModule
- ViewSortModule

### Each module is now completely independent with its own:
- Controller
- Resolver
- Service
- Entity

### Created dedicated abstraction metadata module folder for:
- flat-view-field
- flat-view

### Dependencies 
- Eleminated circular dep on ViewModule to all others ones
- Granular import not importing the whole viewModule anymore everywhere


close https://github.com/twentyhq/core-team-issues/issues/1703
This commit is contained in:
Paul Rastoin
2025-10-09 15:18:15 +02:00
committed by GitHub
parent c9a1a110e1
commit 59fbe35a8c
198 changed files with 877 additions and 765 deletions
@@ -0,0 +1,321 @@
import { Test, type TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { type Repository } from 'typeorm';
import { ViewGroupEntity } from 'src/engine/metadata-modules/view-group/entities/view-group.entity';
import {
ViewGroupException,
ViewGroupExceptionCode,
ViewGroupExceptionMessageKey,
generateViewGroupExceptionMessage,
generateViewGroupUserFriendlyExceptionMessage,
} from 'src/engine/metadata-modules/view-group/exceptions/view-group.exception';
import { ViewGroupService } from 'src/engine/metadata-modules/view-group/services/view-group.service';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';
describe('ViewGroupService', () => {
let viewGroupService: ViewGroupService;
let viewGroupRepository: Repository<ViewGroupEntity>;
const mockViewGroup = {
id: 'view-group-id',
fieldMetadataId: 'field-id',
viewId: 'view-id',
workspaceId: 'workspace-id',
fieldValue: 'group-value',
isVisible: true,
position: 0,
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: null,
} as ViewGroupEntity;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ViewGroupService,
{
provide: getRepositoryToken(ViewGroupEntity),
useValue: {
find: jest.fn(),
findOne: jest.fn(),
create: jest.fn(),
save: jest.fn(),
softDelete: jest.fn(),
delete: jest.fn(),
},
},
{
provide: WorkspaceCacheStorageService,
useValue: {
flushGraphQLOperation: jest.fn(),
},
},
],
}).compile();
viewGroupService = module.get<ViewGroupService>(ViewGroupService);
viewGroupRepository = module.get<Repository<ViewGroupEntity>>(
getRepositoryToken(ViewGroupEntity),
);
});
it('should be defined', () => {
expect(viewGroupService).toBeDefined();
});
describe('findByWorkspaceId', () => {
it('should return view groups for a workspace', async () => {
const workspaceId = 'workspace-id';
const expectedViewGroups = [mockViewGroup];
jest
.spyOn(viewGroupRepository, 'find')
.mockResolvedValue(expectedViewGroups);
const result = await viewGroupService.findByWorkspaceId(workspaceId);
expect(viewGroupRepository.find).toHaveBeenCalledWith({
where: {
workspaceId,
deletedAt: expect.anything(),
},
order: { position: 'ASC' },
relations: ['workspace', 'view'],
});
expect(result).toEqual(expectedViewGroups);
});
});
describe('findByViewId', () => {
it('should return view groups for a view', async () => {
const workspaceId = 'workspace-id';
const viewId = 'view-id';
const expectedViewGroups = [mockViewGroup];
jest
.spyOn(viewGroupRepository, 'find')
.mockResolvedValue(expectedViewGroups);
const result = await viewGroupService.findByViewId(workspaceId, viewId);
expect(viewGroupRepository.find).toHaveBeenCalledWith({
where: {
workspaceId,
viewId,
deletedAt: expect.anything(),
},
order: { position: 'ASC' },
relations: ['workspace', 'view'],
});
expect(result).toEqual(expectedViewGroups);
});
});
describe('findById', () => {
it('should return a view group by id', async () => {
const id = 'view-group-id';
const workspaceId = 'workspace-id';
jest
.spyOn(viewGroupRepository, 'findOne')
.mockResolvedValue(mockViewGroup);
const result = await viewGroupService.findById(id, workspaceId);
expect(viewGroupRepository.findOne).toHaveBeenCalledWith({
where: {
id,
workspaceId,
deletedAt: expect.anything(),
},
relations: ['workspace', 'view'],
});
expect(result).toEqual(mockViewGroup);
});
it('should return null when view group is not found', async () => {
const id = 'non-existent-id';
const workspaceId = 'workspace-id';
jest.spyOn(viewGroupRepository, 'findOne').mockResolvedValue(null);
const result = await viewGroupService.findById(id, workspaceId);
expect(result).toBeNull();
});
});
describe('create', () => {
const validViewGroupData = {
fieldMetadataId: 'field-id',
viewId: 'view-id',
workspaceId: 'workspace-id',
fieldValue: 'group-value',
isVisible: true,
position: 0,
};
it('should create a view group successfully', async () => {
jest.spyOn(viewGroupRepository, 'create').mockReturnValue(mockViewGroup);
jest.spyOn(viewGroupRepository, 'save').mockResolvedValue(mockViewGroup);
const result = await viewGroupService.create(validViewGroupData);
expect(viewGroupRepository.create).toHaveBeenCalledWith(
validViewGroupData,
);
expect(viewGroupRepository.save).toHaveBeenCalledWith(mockViewGroup);
expect(result).toEqual(mockViewGroup);
});
it('should throw exception when workspaceId is missing', async () => {
const invalidData = { ...validViewGroupData, workspaceId: undefined };
await expect(viewGroupService.create(invalidData)).rejects.toThrow(
new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.WORKSPACE_ID_REQUIRED,
),
ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA,
{
userFriendlyMessage: generateViewGroupUserFriendlyExceptionMessage(
ViewGroupExceptionMessageKey.WORKSPACE_ID_REQUIRED,
),
},
),
);
});
it('should throw exception when viewId is missing', async () => {
const invalidData = { ...validViewGroupData, viewId: undefined };
await expect(viewGroupService.create(invalidData)).rejects.toThrow(
new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_ID_REQUIRED,
),
ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA,
{
userFriendlyMessage: generateViewGroupUserFriendlyExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_ID_REQUIRED,
),
},
),
);
});
it('should throw exception when fieldMetadataId is missing', async () => {
const invalidData = { ...validViewGroupData, fieldMetadataId: undefined };
await expect(viewGroupService.create(invalidData)).rejects.toThrow(
new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.FIELD_METADATA_ID_REQUIRED,
),
ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA,
{
userFriendlyMessage: generateViewGroupUserFriendlyExceptionMessage(
ViewGroupExceptionMessageKey.FIELD_METADATA_ID_REQUIRED,
),
},
),
);
});
});
describe('update', () => {
it('should update a view group successfully', async () => {
const id = 'view-group-id';
const workspaceId = 'workspace-id';
const updateData = { isVisible: false };
const updatedViewGroup = { ...mockViewGroup, ...updateData };
jest.spyOn(viewGroupService, 'findById').mockResolvedValue(mockViewGroup);
jest
.spyOn(viewGroupRepository, 'save')
.mockResolvedValue(updatedViewGroup);
const result = await viewGroupService.update(id, workspaceId, updateData);
expect(viewGroupService.findById).toHaveBeenCalledWith(id, workspaceId);
expect(viewGroupRepository.save).toHaveBeenCalledWith({
id,
...updateData,
});
expect(result).toEqual({ ...mockViewGroup, ...updatedViewGroup });
});
it('should throw exception when view group is not found', async () => {
const id = 'non-existent-id';
const workspaceId = 'workspace-id';
const updateData = { isVisible: false };
jest.spyOn(viewGroupService, 'findById').mockResolvedValue(null);
await expect(
viewGroupService.update(id, workspaceId, updateData),
).rejects.toThrow(
new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_GROUP_NOT_FOUND,
id,
),
ViewGroupExceptionCode.VIEW_GROUP_NOT_FOUND,
),
);
});
});
describe('delete', () => {
it('should delete a view group successfully', async () => {
const id = 'view-group-id';
const workspaceId = 'workspace-id';
jest.spyOn(viewGroupService, 'findById').mockResolvedValue(mockViewGroup);
jest
.spyOn(viewGroupRepository, 'softDelete')
.mockResolvedValue({} as any);
const result = await viewGroupService.delete(id, workspaceId);
expect(viewGroupService.findById).toHaveBeenCalledWith(id, workspaceId);
expect(viewGroupRepository.softDelete).toHaveBeenCalledWith(id);
expect(result).toEqual(mockViewGroup);
});
it('should throw exception when view group is not found', async () => {
const id = 'non-existent-id';
const workspaceId = 'workspace-id';
jest.spyOn(viewGroupService, 'findById').mockResolvedValue(null);
await expect(viewGroupService.delete(id, workspaceId)).rejects.toThrow(
new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_GROUP_NOT_FOUND,
id,
),
ViewGroupExceptionCode.VIEW_GROUP_NOT_FOUND,
),
);
});
});
describe('destroy', () => {
it('should destroy a view group successfully', async () => {
const id = 'view-group-id';
const workspaceId = 'workspace-id';
jest.spyOn(viewGroupService, 'findById').mockResolvedValue(mockViewGroup);
jest.spyOn(viewGroupRepository, 'delete').mockResolvedValue({} as any);
const result = await viewGroupService.destroy(id, workspaceId);
expect(viewGroupService.findById).toHaveBeenCalledWith(id, workspaceId);
expect(viewGroupRepository.delete).toHaveBeenCalledWith(id);
expect(result).toEqual(true);
});
});
});
@@ -0,0 +1,197 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { isDefined } from 'twenty-shared/utils';
import { IsNull, Repository } from 'typeorm';
import { ViewGroupEntity } from 'src/engine/metadata-modules/view-group/entities/view-group.entity';
import {
ViewGroupException,
ViewGroupExceptionCode,
ViewGroupExceptionMessageKey,
generateViewGroupExceptionMessage,
generateViewGroupUserFriendlyExceptionMessage,
} from 'src/engine/metadata-modules/view-group/exceptions/view-group.exception';
import { FIND_ALL_CORE_VIEWS_GRAPHQL_OPERATION } from 'src/engine/metadata-modules/view/constants/find-all-core-views-graphql-operation.constant';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';
@Injectable()
export class ViewGroupService {
constructor(
@InjectRepository(ViewGroupEntity)
private readonly viewGroupRepository: Repository<ViewGroupEntity>,
private readonly workspaceCacheStorageService: WorkspaceCacheStorageService,
) {}
async findByWorkspaceId(workspaceId: string): Promise<ViewGroupEntity[]> {
return this.viewGroupRepository.find({
where: {
workspaceId,
deletedAt: IsNull(),
},
order: { position: 'ASC' },
relations: ['workspace', 'view'],
});
}
async findByViewId(
workspaceId: string,
viewId: string,
): Promise<ViewGroupEntity[]> {
return this.viewGroupRepository.find({
where: {
workspaceId,
viewId,
deletedAt: IsNull(),
},
order: { position: 'ASC' },
relations: ['workspace', 'view'],
});
}
async findById(
id: string,
workspaceId: string,
): Promise<ViewGroupEntity | null> {
const viewGroup = await this.viewGroupRepository.findOne({
where: {
id,
workspaceId,
deletedAt: IsNull(),
},
relations: ['workspace', 'view'],
});
return viewGroup || null;
}
async create(
viewGroupData: Partial<ViewGroupEntity>,
): Promise<ViewGroupEntity> {
if (!isDefined(viewGroupData.workspaceId)) {
throw new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.WORKSPACE_ID_REQUIRED,
),
ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA,
{
userFriendlyMessage: generateViewGroupUserFriendlyExceptionMessage(
ViewGroupExceptionMessageKey.WORKSPACE_ID_REQUIRED,
),
},
);
}
if (!isDefined(viewGroupData.viewId)) {
throw new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_ID_REQUIRED,
),
ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA,
{
userFriendlyMessage: generateViewGroupUserFriendlyExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_ID_REQUIRED,
),
},
);
}
if (!isDefined(viewGroupData.fieldMetadataId)) {
throw new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.FIELD_METADATA_ID_REQUIRED,
),
ViewGroupExceptionCode.INVALID_VIEW_GROUP_DATA,
{
userFriendlyMessage: generateViewGroupUserFriendlyExceptionMessage(
ViewGroupExceptionMessageKey.FIELD_METADATA_ID_REQUIRED,
),
},
);
}
const viewGroup = this.viewGroupRepository.create(viewGroupData);
await this.flushGraphQLCache(viewGroupData.workspaceId);
const savedViewGroup = await this.viewGroupRepository.save(viewGroup);
await this.flushGraphQLCache(viewGroupData.workspaceId);
return savedViewGroup;
}
async update(
id: string,
workspaceId: string,
updateData: Partial<ViewGroupEntity>,
): Promise<ViewGroupEntity> {
const existingViewGroup = await this.findById(id, workspaceId);
if (!isDefined(existingViewGroup)) {
throw new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_GROUP_NOT_FOUND,
id,
),
ViewGroupExceptionCode.VIEW_GROUP_NOT_FOUND,
);
}
const updatedViewGroup = await this.viewGroupRepository.save({
id,
...updateData,
});
await this.flushGraphQLCache(workspaceId);
return { ...existingViewGroup, ...updatedViewGroup };
}
async delete(id: string, workspaceId: string): Promise<ViewGroupEntity> {
const viewGroup = await this.findById(id, workspaceId);
if (!isDefined(viewGroup)) {
throw new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_GROUP_NOT_FOUND,
id,
),
ViewGroupExceptionCode.VIEW_GROUP_NOT_FOUND,
);
}
await this.viewGroupRepository.softDelete(id);
await this.flushGraphQLCache(workspaceId);
return viewGroup;
}
async destroy(id: string, workspaceId: string): Promise<boolean> {
const viewGroup = await this.findById(id, workspaceId);
if (!isDefined(viewGroup)) {
throw new ViewGroupException(
generateViewGroupExceptionMessage(
ViewGroupExceptionMessageKey.VIEW_GROUP_NOT_FOUND,
id,
),
ViewGroupExceptionCode.VIEW_GROUP_NOT_FOUND,
);
}
await this.viewGroupRepository.delete(id);
await this.flushGraphQLCache(workspaceId);
return true;
}
private async flushGraphQLCache(workspaceId: string): Promise<void> {
await this.workspaceCacheStorageService.flushGraphQLOperation({
operationName: FIND_ALL_CORE_VIEWS_GRAPHQL_OPERATION,
workspaceId,
});
}
}