Migrate fields of deprecated type LINK to type LINKS (#6332)
Closes #5909. Adding a command to migrate fields of type Link to fields of type Links, including their data.
This commit is contained in:
@ -68,7 +68,7 @@ export class CompanyWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: COMPANY_STANDARD_FIELD_IDS.linkedinLink,
|
||||
type: FieldMetadataType.LINK,
|
||||
type: FieldMetadataType.LINKS,
|
||||
label: 'Linkedin',
|
||||
description: 'The company Linkedin account',
|
||||
icon: 'IconBrandLinkedin',
|
||||
@ -78,7 +78,7 @@ export class CompanyWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: COMPANY_STANDARD_FIELD_IDS.xLink,
|
||||
type: FieldMetadataType.LINK,
|
||||
type: FieldMetadataType.LINKS,
|
||||
label: 'X',
|
||||
description: 'The company Twitter/X account',
|
||||
icon: 'IconBrandX',
|
||||
|
||||
@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
|
||||
|
||||
import { CalendarModule } from 'src/modules/calendar/calendar.module';
|
||||
import { MessagingModule } from 'src/modules/messaging/messaging.module';
|
||||
import { ViewModule } from 'src/modules/view/view.module';
|
||||
|
||||
@Module({
|
||||
imports: [MessagingModule, CalendarModule],
|
||||
imports: [MessagingModule, CalendarModule, ViewModule],
|
||||
providers: [],
|
||||
exports: [],
|
||||
})
|
||||
|
||||
@ -56,7 +56,7 @@ export class PersonWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: PERSON_STANDARD_FIELD_IDS.linkedinLink,
|
||||
type: FieldMetadataType.LINK,
|
||||
type: FieldMetadataType.LINKS,
|
||||
label: 'Linkedin',
|
||||
description: 'Contact’s Linkedin account',
|
||||
icon: 'IconBrandLinkedin',
|
||||
@ -66,7 +66,7 @@ export class PersonWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: PERSON_STANDARD_FIELD_IDS.xLink,
|
||||
type: FieldMetadataType.LINK,
|
||||
type: FieldMetadataType.LINKS,
|
||||
label: 'X',
|
||||
description: 'Contact’s X/Twitter account',
|
||||
icon: 'IconBrandX',
|
||||
|
||||
@ -0,0 +1,99 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
import { isDefined } from 'class-validator';
|
||||
import isEmpty from 'lodash.isempty';
|
||||
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { ViewFieldWorkspaceEntity } from 'src/modules/view/standard-objects/view-field.workspace-entity';
|
||||
|
||||
@Injectable()
|
||||
export class ViewService {
|
||||
private readonly logger = new Logger(ViewService.name);
|
||||
constructor(
|
||||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
) {}
|
||||
|
||||
async addFieldToViews({
|
||||
workspaceId,
|
||||
fieldId,
|
||||
viewsIds,
|
||||
positions,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
fieldId: string;
|
||||
viewsIds: string[];
|
||||
positions?: {
|
||||
[key: string]: number;
|
||||
}[];
|
||||
}) {
|
||||
const viewFieldRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace(
|
||||
workspaceId,
|
||||
ViewFieldWorkspaceEntity,
|
||||
);
|
||||
|
||||
for (const viewId of viewsIds) {
|
||||
const position = positions?.[viewId];
|
||||
const newFieldInThisView = await viewFieldRepository.findBy({
|
||||
fieldMetadataId: fieldId,
|
||||
viewId: viewId as string,
|
||||
isVisible: true,
|
||||
});
|
||||
|
||||
if (!isEmpty(newFieldInThisView)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.logger.log(
|
||||
`Adding new field ${fieldId} to view ${viewId} for workspace ${workspaceId}...`,
|
||||
);
|
||||
const newViewField = viewFieldRepository.create({
|
||||
viewId: viewId,
|
||||
fieldMetadataId: fieldId,
|
||||
isVisible: true,
|
||||
...(isDefined(position) && { position: position }),
|
||||
});
|
||||
|
||||
await viewFieldRepository.save(newViewField);
|
||||
this.logger.log(
|
||||
`New field successfully added to view ${viewId} for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async removeFieldFromViews({
|
||||
workspaceId,
|
||||
fieldId,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
fieldId: string;
|
||||
}) {
|
||||
const viewFieldRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace(
|
||||
workspaceId,
|
||||
ViewFieldWorkspaceEntity,
|
||||
);
|
||||
const viewsWithField = await viewFieldRepository.find({
|
||||
where: {
|
||||
fieldMetadataId: fieldId,
|
||||
isVisible: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const viewWithField of viewsWithField) {
|
||||
const viewId = viewWithField.viewId;
|
||||
|
||||
this.logger.log(
|
||||
`Removing field ${fieldId} from view ${viewId} for workspace ${workspaceId}...`,
|
||||
);
|
||||
await viewFieldRepository.delete({
|
||||
viewId: viewWithField.viewId as string,
|
||||
fieldMetadataId: fieldId,
|
||||
});
|
||||
|
||||
this.logger.log(
|
||||
`Field ${fieldId} successfully removed from view ${viewId} for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
packages/twenty-server/src/modules/view/view.module.ts
Normal file
10
packages/twenty-server/src/modules/view/view.module.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { ViewService } from 'src/modules/view/services/view.service';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
providers: [ViewService],
|
||||
exports: [ViewService],
|
||||
})
|
||||
export class ViewModule {}
|
||||
Reference in New Issue
Block a user