Files
twenty/packages/twenty-server/src/modules/view/services/view-group-sync.service.ts
T
Raphaël Bosi 593b064448 Create resolvers and controllers for core views (#13624)
Created:
- Services
- Resolvers
- Controllers
- Tests for services
- Integration tests for GraphQL and Rest

Updated the Rest API playground

Added new feature flag `IS_CORE_VIEW_ENABLED`

Updated `viewFilter` `operand` and `view` `type` to be enums rather than
strings and generated migration file.

Closes https://github.com/twentyhq/core-team-issues/issues/1259

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
2025-08-07 18:45:04 +02:00

113 lines
3.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { isDefined } from 'twenty-shared/utils';
import { Repository } from 'typeorm';
import { ObjectRecordDiff } from 'src/engine/core-modules/event-emitter/types/object-record-diff';
import { ViewGroup } from 'src/engine/core-modules/view/entities/view-group.entity';
import { ViewGroupWorkspaceEntity } from 'src/modules/view/standard-objects/view-group.workspace-entity';
@Injectable()
export class ViewGroupSyncService {
constructor(
@InjectRepository(ViewGroup, 'core')
private readonly coreViewGroupRepository: Repository<ViewGroup>,
) {}
private parseUpdateDataFromDiff(
diff: Partial<ObjectRecordDiff<ViewGroupWorkspaceEntity>>,
): Partial<ViewGroup> {
const updateData: Record<string, unknown> = {};
for (const key of Object.keys(diff)) {
const diffValue = diff[key as keyof ViewGroupWorkspaceEntity];
if (isDefined(diffValue)) {
updateData[key] = diffValue.after;
}
}
return updateData as Partial<ViewGroup>;
}
public async createCoreViewGroup(
workspaceId: string,
workspaceViewGroup: ViewGroupWorkspaceEntity,
): Promise<void> {
if (!workspaceViewGroup.viewId) {
return;
}
const coreViewGroup: Partial<ViewGroup> = {
id: workspaceViewGroup.id,
fieldMetadataId: workspaceViewGroup.fieldMetadataId,
viewId: workspaceViewGroup.viewId,
fieldValue: workspaceViewGroup.fieldValue,
isVisible: workspaceViewGroup.isVisible,
position: workspaceViewGroup.position,
workspaceId,
createdAt: new Date(workspaceViewGroup.createdAt),
updatedAt: new Date(workspaceViewGroup.updatedAt),
deletedAt: workspaceViewGroup.deletedAt
? new Date(workspaceViewGroup.deletedAt)
: null,
};
await this.coreViewGroupRepository.save(coreViewGroup);
}
public async updateCoreViewGroup(
workspaceId: string,
workspaceViewGroup: ViewGroupWorkspaceEntity,
diff?: Partial<ObjectRecordDiff<ViewGroupWorkspaceEntity>>,
): Promise<void> {
if (!workspaceViewGroup.viewId) {
return;
}
if (!diff || Object.keys(diff).length === 0) {
return;
}
const updateData = this.parseUpdateDataFromDiff(diff);
if (Object.keys(updateData).length > 0) {
await this.coreViewGroupRepository.update(
{ id: workspaceViewGroup.id, workspaceId },
updateData,
);
}
}
public async deleteCoreViewGroup(
workspaceId: string,
workspaceViewGroup: Pick<ViewGroupWorkspaceEntity, 'id'>,
): Promise<void> {
await this.coreViewGroupRepository.softDelete({
id: workspaceViewGroup.id,
workspaceId,
});
}
public async destroyCoreViewGroup(
workspaceId: string,
workspaceViewGroup: Pick<ViewGroupWorkspaceEntity, 'id'>,
): Promise<void> {
await this.coreViewGroupRepository.delete({
id: workspaceViewGroup.id,
workspaceId,
});
}
public async restoreCoreViewGroup(
workspaceId: string,
workspaceViewGroup: Pick<ViewGroupWorkspaceEntity, 'id'>,
): Promise<void> {
await this.coreViewGroupRepository.restore({
id: workspaceViewGroup.id,
workspaceId,
});
}
}