The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Initialize application variable value at creation instead of in the manifest (#28808)
Paul Rastoin9b2715ec3cba
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest-migration.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest-migration.service.tsindex 5ff6d0cd935..4df7e5f0b4e 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest-migration.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest-migration.service.ts@@ -126,7 +126,6 @@ export class ApplicationManifestMigrationService {ownerFlatApplication,fromAllFlatEntityMaps,now,- workspaceId,});const dependencyAllFlatEntityMaps = getApplicationSubAllFlatEntityMaps({@@ -212,7 +211,6 @@ export class ApplicationManifestMigrationService {ownerFlatApplication,fromAllFlatEntityMaps,now,- workspaceId,});assertConnectionProviderLogicFunctionsAreOwn({diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest.module.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest.module.tsindex ced80c551c6..41908eafc75 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest.module.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-manifest.module.ts@@ -14,7 +14,6 @@ import { ApplicationHealthModule } from 'src/engine/core-modules/application/appimport { ApplicationRegistrationLookupModule } from 'src/engine/core-modules/application/application-registration/application-registration-lookup/application-registration-lookup.module';import { ApplicationRegistrationModule } from 'src/engine/core-modules/application/application-registration/application-registration.module';import { ApplicationTranslationModule } from 'src/engine/core-modules/application/application-translation/application-translation.module';-import { SecretEncryptionModule } from 'src/engine/core-modules/secret-encryption/secret-encryption.module';import { SdkClientModule } from 'src/engine/core-modules/sdk-client/sdk-client.module';import { ConnectedAccountMetadataModule } from 'src/engine/metadata-modules/connected-account/connected-account-metadata.module';import { FrontComponentEntity } from 'src/engine/metadata-modules/front-component/entities/front-component.entity';@@ -33,7 +32,6 @@ import { provideWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspacApplicationRegistrationModule,ApplicationTranslationModule,ConnectedAccountMetadataModule,- SecretEncryptionModule,SdkClientModule,WorkflowVersionCoreModule,WorkspaceCacheModule,diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-application-variable-manifest-to-universal-flat-application-variable.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-application-variable-manifest-to-universal-flat-application-variable.util.tsindex 2c6c7bc0142..db09195d7df 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-application-variable-manifest-to-universal-flat-application-variable.util.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-application-variable-manifest-to-universal-flat-application-variable.util.ts@@ -6,7 +6,6 @@ import {type ApplicationVariableType,} from 'twenty-shared/application';-import { type EncryptedString } from 'src/engine/core-modules/secret-encryption/branded-strings/encrypted-string.type';import { type UniversalFlatApplicationVariable } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-application-variable.type';export const fromApplicationVariableManifestToUniversalFlatApplicationVariable =@@ -15,7 +14,6 @@ export const fromApplicationVariableManifestToUniversalFlatApplicationVariable =universalIdentifier,description,label,- encryptedValue,defaultValue,isSecret,isDeprecated,@@ -30,7 +28,6 @@ export const fromApplicationVariableManifestToUniversalFlatApplicationVariable =universalIdentifier: string;description?: string;label?: string;- encryptedValue: EncryptedString | null;defaultValue: string | null;isSecret?: boolean;isDeprecated?: boolean;@@ -45,7 +42,6 @@ export const fromApplicationVariableManifestToUniversalFlatApplicationVariable =universalIdentifier,applicationUniversalIdentifier,key,- value: encryptedValue,description: description ?? '',label: label ?? '',isSecret: isSecret ?? false,diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/compute-application-manifest-all-universal-flat-entity-maps.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/compute-application-manifest-all-universal-flat-entity-maps.service.tsindex 3e5e5c948db..bd6f47ada67 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/compute-application-manifest-all-universal-flat-entity-maps.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/compute-application-manifest-all-universal-flat-entity-maps.service.ts@@ -48,8 +48,6 @@ import {} from 'src/engine/core-modules/application/application.exception';import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';import { fromAgentManifestToUniversalFlatAgent } from 'src/engine/core-modules/application/utils/from-agent-manifest-to-universal-flat-agent.util';-import { type PlaintextString } from 'src/engine/core-modules/secret-encryption/branded-strings/plaintext-string.type';-import { SecretEncryptionService } from 'src/engine/core-modules/secret-encryption/secret-encryption.service';import { createEmptyAllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-all-flat-entity-maps.constant';import { type AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';import { type UniversalFlatFieldMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-field-metadata.type';@@ -57,22 +55,16 @@ import { addUniversalFlatEntityToUniversalFlatEntityMapsThroughMutationOrThrow }@Injectable()export class ComputeApplicationManifestAllUniversalFlatEntityMapsService {- constructor(- private readonly secretEncryptionService: SecretEncryptionService,- ) {}-compute({manifest,ownerFlatApplication,fromAllFlatEntityMaps,now,- workspaceId,}: {manifest: Manifest;ownerFlatApplication: FlatApplication;fromAllFlatEntityMaps: AllFlatEntityMaps;now: string;- workspaceId: string;}): AllFlatEntityMaps {const allUniversalFlatEntityMaps = createEmptyAllFlatEntityMaps();@@ -626,23 +618,12 @@ export class ComputeApplicationManifestAllUniversalFlatEntityMapsService {): null;- // A user variable's values belong to each member, so it has no- // workspace value: the manifest value is only its default.- const encryptedValue =- applicationVariableManifest.scope === 'USER'- ? null- : this.secretEncryptionService.encryptVersioned(- (defaultValue ?? '') as PlaintextString,- { workspaceId },- );-addUniversalFlatEntityToUniversalFlatEntityMapsThroughMutationOrThrow({universalFlatEntity:fromApplicationVariableManifestToUniversalFlatApplicationVariable({key,universalIdentifier:applicationVariableManifest.universalIdentifier,- encryptedValue,defaultValue,description: applicationVariableManifest.description,label: applicationVariableManifest.label,diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-application-variable.type.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-application-variable.type.tsindex c906694db3e..2494c8cb974 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-application-variable.type.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-application-variable.type.ts@@ -1,7 +1,15 @@import { type ApplicationVariableEntity } from 'src/engine/core-modules/application/application-variable/application-variable.entity';import { type UniversalFlatEntityFrom } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-entity-from.type';-export type UniversalFlatApplicationVariable = UniversalFlatEntityFrom<+type ApplicationVariableUniversalFlatEntity = UniversalFlatEntityFrom<ApplicationVariableEntity,'applicationVariable'>;++// Workspace-owned: manifests never carry it, the create action handler sets it+// Related to https://github.com/twentyhq/core-team-issues/issues/2934+export type UniversalFlatApplicationVariable = Omit<+ ApplicationVariableUniversalFlatEntity,+ 'value'+> &+ Partial<Pick<ApplicationVariableUniversalFlatEntity, 'value'>>;diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/__tests__/validate-application-variable-scope.util.spec.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/__tests__/validate-application-variable-scope.util.spec.tsindex 735e831273b..5b82c265a59 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/__tests__/validate-application-variable-scope.util.spec.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/__tests__/validate-application-variable-scope.util.spec.ts@@ -2,29 +2,15 @@ import { ApplicationVariableEntityExceptionCode } from 'src/engine/core-modules/import { validateApplicationVariableScope } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/validate-application-variable-scope.util';describe('validateApplicationVariableScope', () => {- it('should return no error for a workspace variable with a value', () => {- const errors = validateApplicationVariableScope({- scope: 'WORKSPACE',- value: 'enc:v2:value',- });-- expect(errors).toEqual([]);- });-- it('should return no error for a user variable without a value', () => {- const errors = validateApplicationVariableScope({- scope: 'USER',- value: null,- });-- expect(errors).toEqual([]);- });+ it.each(['WORKSPACE', 'USER'])(+ 'should return no error for the %s scope',+ (scope) => {+ expect(validateApplicationVariableScope({ scope })).toEqual([]);+ },+ );it('should return an error for an unknown scope', () => {- const errors = validateApplicationVariableScope({- scope: 'TEAM',- value: 'enc:v2:value',- });+ const errors = validateApplicationVariableScope({ scope: 'TEAM' });expect(errors).toHaveLength(1);expect(errors[0].code).toBe(@@ -32,28 +18,4 @@ describe('validateApplicationVariableScope', () => {);expect(errors[0].message).toContain('TEAM');});-- it('should return an error for a workspace variable without a value', () => {- const errors = validateApplicationVariableScope({- scope: 'WORKSPACE',- value: null,- });-- expect(errors).toHaveLength(1);- expect(errors[0].code).toBe(- ApplicationVariableEntityExceptionCode.INVALID_APPLICATION_VARIABLE_INPUT,- );- });-- it('should return an error for a user variable with a value', () => {- const errors = validateApplicationVariableScope({- scope: 'USER',- value: 'enc:v2:value',- });-- expect(errors).toHaveLength(1);- expect(errors[0].code).toBe(- ApplicationVariableEntityExceptionCode.INVALID_APPLICATION_VARIABLE_INPUT,- );- });});diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/validate-application-variable-scope.util.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/validate-application-variable-scope.util.tsindex 2c0637e2655..e78554cae28 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/validate-application-variable-scope.util.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-builder/validators/utils/validate-application-variable-scope.util.ts@@ -1,43 +1,23 @@import { msg, t } from '@lingui/core/macro';import { isApplicationVariableScope } from 'twenty-shared/application';-import { isDefined } from 'twenty-shared/utils';import { ApplicationVariableEntityExceptionCode } from 'src/engine/core-modules/application/application-variable/application-variable.exception';import { type FlatEntityValidationError } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/types/failed-flat-entity-validation.type';export const validateApplicationVariableScope = ({scope,- value,}: {scope: string;- value: string | null;}): FlatEntityValidationError<ApplicationVariableEntityExceptionCode>[] => {- const errors: FlatEntityValidationError<ApplicationVariableEntityExceptionCode>[] =- [];+ if (isApplicationVariableScope(scope)) {+ return [];+ }- if (!isApplicationVariableScope(scope)) {- errors.push({+ return [+ {code: ApplicationVariableEntityExceptionCode.INVALID_APPLICATION_VARIABLE_INPUT,message: t`Application variable scope "${scope}" is invalid`,userFriendlyMessage: msg`This application variable scope is not supported.`,- });- }-- if (scope === 'USER' && isDefined(value)) {- errors.push({- code: ApplicationVariableEntityExceptionCode.INVALID_APPLICATION_VARIABLE_INPUT,- message: t`User application variable cannot have a value`,- userFriendlyMessage: msg`A user variable cannot have a value: each member sets their own.`,- });- }-- if (scope === 'WORKSPACE' && !isDefined(value)) {- errors.push({- code: ApplicationVariableEntityExceptionCode.INVALID_APPLICATION_VARIABLE_INPUT,- message: t`Workspace application variable must have a value`,- userFriendlyMessage: msg`A workspace variable must have a value.`,- });- }-- return errors;+ },+ ];};diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/application-variable/services/create-application-variable-action-handler.service.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/application-variable/services/create-application-variable-action-handler.service.tsindex 201027c2540..2bff808f6c7 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/application-variable/services/create-application-variable-action-handler.service.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/application-variable/services/create-application-variable-action-handler.service.ts@@ -4,6 +4,8 @@ import { v4 } from 'uuid';import { WorkspaceMigrationRunnerActionHandler } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/interfaces/workspace-migration-runner-action-handler-service.interface';+import { type PlaintextString } from 'src/engine/core-modules/secret-encryption/branded-strings/plaintext-string.type';+import { SecretEncryptionService } from 'src/engine/core-modules/secret-encryption/secret-encryption.service';import { getUniversalFlatEntityEmptyForeignKeyAggregators } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/utils/reset-universal-flat-entity-foreign-key-aggregators.util';import {FlatCreateApplicationVariableAction,@@ -19,7 +21,9 @@ export class CreateApplicationVariableActionHandlerService extends WorkspaceMigr'create','applicationVariable',) {- constructor() {+ constructor(+ private readonly secretEncryptionService: SecretEncryptionService,+ ) {super();}@@ -33,10 +37,20 @@ export class CreateApplicationVariableActionHandlerService extends WorkspaceMigrmetadataName: 'applicationVariable',});+ const { scope, defaultValue } = action.flatEntity;+return {...action,flatEntity: {...action.flatEntity,+ // A user variable's values belong to each member, so it has no workspace value+ value:+ scope === 'USER'+ ? null+ : this.secretEncryptionService.encryptVersioned(+ (defaultValue ?? '') as PlaintextString,+ { workspaceId },+ ),applicationId: flatApplication.id,id: action.id ?? v4(),workspaceId,diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/workspace-schema-migration-runner-action-handlers.module.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/workspace-schema-migration-runner-action-handlers.module.tsindex ff890ad8830..ad37fbe497d 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/workspace-schema-migration-runner-action-handlers.module.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/action-handlers/workspace-schema-migration-runner-action-handlers.module.ts@@ -121,12 +121,14 @@ import { BuildIndexDeferredActionHandlerService } from 'src/engine/workspace-manimport { DeleteLogicFunctionResourcesDeferredActionHandlerService } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/deferred-action-handlers/services/delete-logic-function-resources-deferred-action-handler.service';import { DeleteWorkflowRunsDeferredActionHandlerWorkspaceService } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/deferred-action-handlers/services/delete-workflow-runs-deferred-action-handler.workspace-service';import { ValidateForeignKeyDeferredActionHandlerService } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/deferred-action-handlers/services/validate-foreign-key-deferred-action-handler.service';+import { SecretEncryptionModule } from 'src/engine/core-modules/secret-encryption/secret-encryption.module';import { WorkflowDeletionModule } from 'src/modules/workflow/workflow-deletion/workflow-deletion.module';@Module({imports: [WorkspaceSchemaManagerModule,MetricsModule,+ SecretEncryptionModule,WorkflowDeletionModule,],providers: [