From ac1197afe1595ee34ed7f589fd70c0dbfad3f47b Mon Sep 17 00:00:00 2001 From: ad-elias Date: Sun, 17 Nov 2024 15:18:02 +0100 Subject: [PATCH] Fix secondary links default value and types (#8532) Fixes broken companies view --- .../api/__mocks__/object-metadata-item.mock.ts | 2 +- .../calendar/dtos/timeline-calendar-event.dto.ts | 2 +- .../composite-types/links.composite-type.ts | 7 ++++++- .../field-metadata/dtos/default-value.input.ts | 14 +++++++++++--- .../field-metadata/utils/generate-default-value.ts | 2 +- .../services/create-company.service.ts | 12 ++++-------- .../services/create-contact.service.ts | 2 +- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/twenty-server/src/engine/api/__mocks__/object-metadata-item.mock.ts b/packages/twenty-server/src/engine/api/__mocks__/object-metadata-item.mock.ts index cc23b7e26a..77f1a24e44 100644 --- a/packages/twenty-server/src/engine/api/__mocks__/object-metadata-item.mock.ts +++ b/packages/twenty-server/src/engine/api/__mocks__/object-metadata-item.mock.ts @@ -93,7 +93,7 @@ const fieldLinksMock = { type: FieldMetadataType.LINKS, isNullable: false, defaultValue: [ - { primaryLinkLabel: '', primaryLinkUrl: '', secondaryLinks: {} }, + { primaryLinkLabel: '', primaryLinkUrl: '', secondaryLinks: [] }, ], }; diff --git a/packages/twenty-server/src/engine/core-modules/calendar/dtos/timeline-calendar-event.dto.ts b/packages/twenty-server/src/engine/core-modules/calendar/dtos/timeline-calendar-event.dto.ts index 967ca31e59..ebc869a029 100644 --- a/packages/twenty-server/src/engine/core-modules/calendar/dtos/timeline-calendar-event.dto.ts +++ b/packages/twenty-server/src/engine/core-modules/calendar/dtos/timeline-calendar-event.dto.ts @@ -22,7 +22,7 @@ export class LinksMetadata { primaryLinkUrl: string; @Field(() => [LinkMetadata], { nullable: true }) - secondaryLinks: object | null; + secondaryLinks: LinkMetadata[] | null; } @ObjectType('TimelineCalendarEvent') diff --git a/packages/twenty-server/src/engine/metadata-modules/field-metadata/composite-types/links.composite-type.ts b/packages/twenty-server/src/engine/metadata-modules/field-metadata/composite-types/links.composite-type.ts index eedfeb4da0..b2d5cfd6ba 100644 --- a/packages/twenty-server/src/engine/metadata-modules/field-metadata/composite-types/links.composite-type.ts +++ b/packages/twenty-server/src/engine/metadata-modules/field-metadata/composite-types/links.composite-type.ts @@ -27,8 +27,13 @@ export const linksCompositeType: CompositeType = { ], }; +export type LinkMetadata = { + label: string; + url: string; +}; + export type LinksMetadata = { primaryLinkLabel: string; primaryLinkUrl: string; - secondaryLinks: object | null; + secondaryLinks: LinkMetadata[] | null; }; diff --git a/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/default-value.input.ts b/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/default-value.input.ts index 679a68f713..99bf6e07fe 100644 --- a/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/default-value.input.ts +++ b/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/default-value.input.ts @@ -31,7 +31,7 @@ export class FieldMetadataDefaultValueString { export class FieldMetadataDefaultValueRawJson { @ValidateIf((_object, value) => value !== null) - @IsObject() + @IsObject() // TODO: Should this also allow arrays? value: object | null; } @@ -137,6 +137,14 @@ export class FieldMetadataDefaultValueAddress { addressLng: number | null; } +class LinkMetadata { + @IsString() + label: string; + + @IsString() + url: string; +} + export class FieldMetadataDefaultValueLinks { @ValidateIf((_object, value) => value !== null) @IsQuotedString() @@ -147,8 +155,8 @@ export class FieldMetadataDefaultValueLinks { primaryLinkUrl: string | null; @ValidateIf((_object, value) => value !== null) - @IsObject() - secondaryLinks: object | null; + @IsArray() + secondaryLinks: LinkMetadata[] | null; } export class FieldMetadataDefaultActor { diff --git a/packages/twenty-server/src/engine/metadata-modules/field-metadata/utils/generate-default-value.ts b/packages/twenty-server/src/engine/metadata-modules/field-metadata/utils/generate-default-value.ts index 848250a2eb..ac3642814c 100644 --- a/packages/twenty-server/src/engine/metadata-modules/field-metadata/utils/generate-default-value.ts +++ b/packages/twenty-server/src/engine/metadata-modules/field-metadata/utils/generate-default-value.ts @@ -38,7 +38,7 @@ export function generateDefaultValue( return { primaryLinkLabel: "''", primaryLinkUrl: "''", - secondaryLinks: [], + secondaryLinks: "'[]'", }; case FieldMetadataType.PHONES: return { diff --git a/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts b/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts index bc3c71c9aa..06bfef81c8 100644 --- a/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts +++ b/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import axios, { AxiosInstance } from 'axios'; import uniqBy from 'lodash.uniqby'; -import { EntityManager, ILike } from 'typeorm'; +import { DeepPartial, EntityManager, ILike } from 'typeorm'; import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type'; import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository'; @@ -90,11 +90,7 @@ export class CreateCompanyService { ); // Create new companies - const createdCompanies = await companyRepository.save( - newCompaniesData, - undefined, - transactionManager, - ); + const createdCompanies = await companyRepository.save(newCompaniesData); const createdCompanyIdsMap = this.createCompanyMap(createdCompanies); return { @@ -157,10 +153,10 @@ export class CreateCompanyService { }; } - private createCompanyMap(companies: CompanyWorkspaceEntity[]) { + private createCompanyMap(companies: DeepPartial[]) { return companies.reduce( (acc, company) => { - if (!company.domainName) { + if (!company.domainName?.primaryLinkUrl || !company.id) { return acc; } const key = extractDomainFromLink(company.domainName.primaryLinkUrl); diff --git a/packages/twenty-server/src/modules/contact-creation-manager/services/create-contact.service.ts b/packages/twenty-server/src/modules/contact-creation-manager/services/create-contact.service.ts index 400573ddda..6f2dc9e88b 100644 --- a/packages/twenty-server/src/modules/contact-creation-manager/services/create-contact.service.ts +++ b/packages/twenty-server/src/modules/contact-creation-manager/services/create-contact.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; -import { EntityManager } from 'typeorm'; +import { DeepPartial, EntityManager } from 'typeorm'; import { v4 } from 'uuid'; import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';