The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Stop a manifest without translations from deleting an application's stored locales (#24408)
Félix Malfaite0e15ee51abe
diff --git a/packages/twenty-docs/developers/extend/apps/translations/overview.mdx b/packages/twenty-docs/developers/extend/apps/translations/overview.mdxindex 05cd71197d7..60083f9d031 100644--- a/packages/twenty-docs/developers/extend/apps/translations/overview.mdx+++ b/packages/twenty-docs/developers/extend/apps/translations/overview.mdx@@ -126,14 +126,11 @@ each string against its catalog, falling back to the source when a translationis missing. Switching language in the host re-renders `Trans` and`useTranslate().t` strings live.-Because catalogs are compiled at build time, updating a translation means-re-running `twenty dev:build` (and redeploying), the same as any other change.--<Note>-Translations are compiled by `twenty dev:build` (and `twenty apply`). The-continuous `twenty dev` watch shows source strings, so test localized output-with a one-off build.-</Note>+Manifest labels are compiled on every sync — `twenty dev:build`, `twenty apply`+and the continuous `twenty dev` watch alike — so editing a locale file and+syncing is enough to see the new text. Front component catalogs are baked into+each component bundle at build time, so changing one of those still means+re-running `twenty dev:build` (and redeploying).`Trans` text children may span multiple lines — whitespace is collapsed thesame way JSX collapses it, so both of these extract to the key `Welcome back`:diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/minimal-app/__integration__/app-dev/expected-manifest.ts b/packages/twenty-sdk/src/cli/__tests__/apps/minimal-app/__integration__/app-dev/expected-manifest.tsindex e589b603b9e..d8492cf8bf6 100644--- a/packages/twenty-sdk/src/cli/__tests__/apps/minimal-app/__integration__/app-dev/expected-manifest.ts+++ b/packages/twenty-sdk/src/cli/__tests__/apps/minimal-app/__integration__/app-dev/expected-manifest.ts@@ -16,6 +16,7 @@ export const EXPECTED_MANIFEST: Manifest = {},permissionFlags: [],skills: [],+ translations: {},agents: [],publicAssets: [],indexes: [],diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/rich-app/__integration__/app-dev/expected-manifest.ts b/packages/twenty-sdk/src/cli/__tests__/apps/rich-app/__integration__/app-dev/expected-manifest.tsindex 825bc55342c..a9be15a8500 100644--- a/packages/twenty-sdk/src/cli/__tests__/apps/rich-app/__integration__/app-dev/expected-manifest.ts+++ b/packages/twenty-sdk/src/cli/__tests__/apps/rich-app/__integration__/app-dev/expected-manifest.ts@@ -59,6 +59,7 @@ export const EXPECTED_MANIFEST: Manifest = {},],skills: [],+ translations: {},agents: [],application: {applicationVariables: {diff --git a/packages/twenty-sdk/src/cli/operations/build.ts b/packages/twenty-sdk/src/cli/operations/build.tsindex cbab2227b45..aa692c4e273 100644--- a/packages/twenty-sdk/src/cli/operations/build.ts+++ b/packages/twenty-sdk/src/cli/operations/build.ts@@ -98,10 +98,7 @@ const innerAppBuild = async (builtFileInfos: buildResult.builtFileInfos,});- await writeManifestToOutput(- appPath,- translations ? { ...updatedManifest, translations } : updatedManifest,- );+ await writeManifestToOutput(appPath, { ...updatedManifest, translations });const outputDir = path.join(appPath, '.twenty', 'output');diff --git a/packages/twenty-sdk/src/cli/operations/dev-once.ts b/packages/twenty-sdk/src/cli/operations/dev-once.tsindex 3ca7358a77f..f668416c24e 100644--- a/packages/twenty-sdk/src/cli/operations/dev-once.ts+++ b/packages/twenty-sdk/src/cli/operations/dev-once.ts@@ -13,6 +13,7 @@ import { promptForReauthentication } from '@/cli/utilities/auth/reauth-helper';import { buildApplication } from '@/cli/utilities/build/common/build-application';import { runTypecheck } from '@/cli/utilities/build/common/typecheck-plugin';import { buildAndValidateManifest } from '@/cli/utilities/build/manifest/build-and-validate-manifest';+import { compileApplicationTranslations } from '@/cli/utilities/translations/compile-application-translations';import { manifestUpdateChecksums } from '@/cli/utilities/build/manifest/manifest-update-checksums';import { writeManifestToOutput } from '@/cli/utilities/build/manifest/manifest-writer';import { ClientService } from '@/cli/utilities/client/client-service';@@ -202,10 +203,15 @@ const innerAppDevOnce = async (};}- const manifest: Manifest = manifestUpdateChecksums({- manifest: manifestResult.manifest,- builtFileInfos: buildResult.builtFileInfos,- });+ const translations = await compileApplicationTranslations(appPath);++ const manifest: Manifest = {+ ...manifestUpdateChecksums({+ manifest: manifestResult.manifest,+ builtFileInfos: buildResult.builtFileInfos,+ }),+ translations,+ };await writeManifestToOutput(appPath, manifest);diff --git a/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/steps/sync-application-orchestrator-step.ts b/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/steps/sync-application-orchestrator-step.tsindex 75f5aa51697..da73891a130 100644--- a/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/steps/sync-application-orchestrator-step.ts+++ b/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/steps/sync-application-orchestrator-step.ts@@ -1,6 +1,7 @@import { type ApiService } from '@/cli/utilities/api/api-service';import { manifestUpdateChecksums } from '@/cli/utilities/build/manifest/manifest-update-checksums';import { writeManifestToOutput } from '@/cli/utilities/build/manifest/manifest-writer';+import { compileApplicationTranslations } from '@/cli/utilities/translations/compile-application-translations';import {type OrchestratorState,type OrchestratorStateBuiltFileInfo,@@ -69,10 +70,13 @@ export class SyncApplicationOrchestratorStep {const events: OrchestratorStateStepEvent[] = [];- const manifest = manifestUpdateChecksums({- manifest: input.manifest,- builtFileInfos: input.builtFileInfos,- });+ const manifest = {+ ...manifestUpdateChecksums({+ manifest: input.manifest,+ builtFileInfos: input.builtFileInfos,+ }),+ translations: await compileApplicationTranslations(input.appPath),+ };events.push({ message: 'Manifest checksums set', status: 'info' });diff --git a/packages/twenty-sdk/src/cli/utilities/translations/__tests__/application-translations.spec.ts b/packages/twenty-sdk/src/cli/utilities/translations/__tests__/application-translations.spec.tsindex 256970c4c0d..3acb9b2bc0c 100644--- a/packages/twenty-sdk/src/cli/utilities/translations/__tests__/application-translations.spec.ts+++ b/packages/twenty-sdk/src/cli/utilities/translations/__tests__/application-translations.spec.ts@@ -154,9 +154,9 @@ describe('compileApplicationTranslations', () => {});});- it('returns undefined when there is no locales directory', async () => {+ it('returns no locales when there is no locales directory', async () => {const appPath = await mkdtemp(join(tmpdir(), 'twenty-translations-empty-'));- expect(await compileApplicationTranslations(appPath)).toBeUndefined();+ expect(await compileApplicationTranslations(appPath)).toEqual({});});});diff --git a/packages/twenty-sdk/src/cli/utilities/translations/compile-application-translations.ts b/packages/twenty-sdk/src/cli/utilities/translations/compile-application-translations.tsindex 912225989cc..308bcb458c5 100644--- a/packages/twenty-sdk/src/cli/utilities/translations/compile-application-translations.ts+++ b/packages/twenty-sdk/src/cli/utilities/translations/compile-application-translations.ts@@ -16,11 +16,11 @@ const isSupportedLocale = (locale: string): locale is AppLocale =>export const compileApplicationTranslations = async (appPath: string,-): Promise<TranslationsManifest | undefined> => {+): Promise<TranslationsManifest> => {const localesDir = path.join(appPath, LOCALES_DIR);if (!(await pathExists(localesDir))) {- return undefined;+ return {};}const localeFiles = (await readdir(localesDir)).filter((entry) =>@@ -61,9 +61,5 @@ export const compileApplicationTranslations = async (}}- if (Object.keys(translations).length === 0) {- return undefined;- }-return translations as TranslationsManifest;};diff --git a/packages/twenty-server/src/engine/core-modules/application/application-translation/__tests__/application-translation-sync.service.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-translation/__tests__/application-translation-sync.service.spec.tsnew file mode 100644index 00000000000..8d908595bec--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/application-translation/__tests__/application-translation-sync.service.spec.ts@@ -0,0 +1,75 @@+import { Test, type TestingModule } from '@nestjs/testing';+import { getRepositoryToken } from '@nestjs/typeorm';++import { ApplicationTranslationCacheService } from 'src/engine/core-modules/application/application-translation/application-translation-cache.service';+import { ApplicationTranslationSyncService } from 'src/engine/core-modules/application/application-translation/application-translation-sync.service';+import { ApplicationTranslationEntity } from 'src/engine/core-modules/application/application-translation/application-translation.entity';++const APPLICATION_REGISTRATION_ID = '20202020-0000-0000-0000-000000000001';++describe('ApplicationTranslationSyncService', () => {+ let service: ApplicationTranslationSyncService;++ const repository = {+ find: jest.fn(),+ update: jest.fn(),+ insert: jest.fn(),+ softDelete: jest.fn(),+ };+ const cacheService = { invalidate: jest.fn() };++ beforeEach(async () => {+ jest.clearAllMocks();+ repository.find.mockResolvedValue([]);++ const module: TestingModule = await Test.createTestingModule({+ providers: [+ ApplicationTranslationSyncService,+ {+ provide: getRepositoryToken(ApplicationTranslationEntity),+ useValue: repository,+ },+ {+ provide: ApplicationTranslationCacheService,+ useValue: cacheService,+ },+ ],+ }).compile();++ service = module.get(ApplicationTranslationSyncService);+ });++ it('leaves stored translations alone when the manifest carries none', async () => {+ await service.syncFromManifest({+ applicationRegistrationId: APPLICATION_REGISTRATION_ID,+ translations: undefined,+ });++ expect(repository.find).toHaveBeenCalledTimes(0);+ expect(repository.update).toHaveBeenCalledTimes(0);+ expect(repository.insert).toHaveBeenCalledTimes(0);+ expect(repository.softDelete).toHaveBeenCalledTimes(0);+ expect(cacheService.invalidate).toHaveBeenCalledTimes(0);+ });++ it('applies the manifest when it declares translations', async () => {+ repository.find.mockResolvedValue([+ { id: 'row-de', locale: 'de-DE', deletedAt: null },+ ]);++ await service.syncFromManifest({+ applicationRegistrationId: APPLICATION_REGISTRATION_ID,+ translations: { 'fr-FR': { abc123: 'Entreprise' } },+ });++ expect(repository.insert).toHaveBeenCalledTimes(1);+ expect(repository.insert).toHaveBeenCalledWith({+ applicationRegistrationId: APPLICATION_REGISTRATION_ID,+ locale: 'fr-FR',+ messages: { abc123: 'Entreprise' },+ });+ expect(repository.softDelete).toHaveBeenCalledTimes(1);+ expect(repository.softDelete).toHaveBeenCalledWith(['row-de']);+ expect(cacheService.invalidate).toHaveBeenCalledTimes(1);+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/application/application-translation/application-translation-sync.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-translation/application-translation-sync.service.tsindex 5d6f7d7a0ff..de9c4e7a88d 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-translation/application-translation-sync.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-translation/application-translation-sync.service.ts@@ -8,6 +8,7 @@ import { Repository } from 'typeorm';import { ApplicationTranslationCacheService } from 'src/engine/core-modules/application/application-translation/application-translation-cache.service';import { ApplicationTranslationEntity } from 'src/engine/core-modules/application/application-translation/application-translation.entity';+import { computeApplicationTranslationSyncPlan } from 'src/engine/core-modules/application/application-translation/utils/compute-application-translation-sync-plan.util';@Injectable()export class ApplicationTranslationSyncService {@@ -26,66 +27,40 @@ export class ApplicationTranslationSyncService {applicationRegistrationId: string;translations: TranslationsManifest | undefined;}): Promise<void> {+ // Absence says nothing about translations, so it must not prune: this+ // table is cross-workspace, and a sync from a toolchain that does not+ // compile them would drop the locales an app published, everywhere.+ if (!isDefined(translations)) {+ return;+ }+const existingRows = await this.applicationTranslationRepository.find({where: { applicationRegistrationId },withDeleted: true,});- const existingRowByLocale = new Map<- keyof typeof APP_LOCALES,- ApplicationTranslationEntity- >();-- for (const row of existingRows) {- const currentRow = existingRowByLocale.get(row.locale);-- const shouldPreferRow =- !isDefined(currentRow) ||- (isDefined(currentRow.deletedAt) && !isDefined(row.deletedAt));-- if (shouldPreferRow) {- existingRowByLocale.set(row.locale, row);- }- }-- const manifestLocales = new Set<keyof typeof APP_LOCALES>();- const upsertPromises: Promise<unknown>[] = [];-- for (const [locale, messages] of Object.entries(translations ?? {}) as [- keyof typeof APP_LOCALES,- Record<string, string>,- ][]) {- manifestLocales.add(locale);-- const existingRow = existingRowByLocale.get(locale);-- if (isDefined(existingRow)) {- upsertPromises.push(- this.applicationTranslationRepository.update(existingRow.id, {- messages,- deletedAt: null,- }),- );- } else {- upsertPromises.push(- this.applicationTranslationRepository.insert({- applicationRegistrationId,- locale,- messages,- }),- );- }- }-- await Promise.all(upsertPromises);-- const rowsToSoftDelete = existingRows.filter(- (row) => !manifestLocales.has(row.locale) && !isDefined(row.deletedAt),- );-- if (rowsToSoftDelete.length > 0) {+ const { rowsToUpdate, rowsToInsert, rowIdsToSoftDelete } =+ computeApplicationTranslationSyncPlan({ existingRows, translations });++ await Promise.all([+ ...rowsToUpdate.map(({ id, messages }) =>+ this.applicationTranslationRepository.update(id, {+ messages,+ deletedAt: null,+ }),+ ),+ ...rowsToInsert.map(({ locale, messages }) =>+ this.applicationTranslationRepository.insert({+ applicationRegistrationId,+ locale,+ messages,+ }),+ ),+ ]);++ if (rowIdsToSoftDelete.length > 0) {await this.applicationTranslationRepository.softDelete(- rowsToSoftDelete.map((row) => row.id),+ rowIdsToSoftDelete,);}diff --git a/packages/twenty-server/src/engine/core-modules/application/application-translation/utils/__tests__/compute-application-translation-sync-plan.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-translation/utils/__tests__/compute-application-translation-sync-plan.util.spec.tsnew file mode 100644index 00000000000..a38caf66096--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/application-translation/utils/__tests__/compute-application-translation-sync-plan.util.spec.ts@@ -0,0 +1,82 @@+import { computeApplicationTranslationSyncPlan } from 'src/engine/core-modules/application/application-translation/utils/compute-application-translation-sync-plan.util';++describe('computeApplicationTranslationSyncPlan', () => {+ it('inserts the locales a first sync brings', () => {+ expect(+ computeApplicationTranslationSyncPlan({+ existingRows: [],+ translations: { 'fr-FR': { abc123: 'Entreprise' } },+ }),+ ).toEqual({+ rowsToUpdate: [],+ rowsToInsert: [{ locale: 'fr-FR', messages: { abc123: 'Entreprise' } }],+ rowIdsToSoftDelete: [],+ });+ });++ it('updates a locale it already stores', () => {+ expect(+ computeApplicationTranslationSyncPlan({+ existingRows: [{ id: 'row-fr', locale: 'fr-FR', deletedAt: null }],+ translations: { 'fr-FR': { abc123: 'Société' } },+ }),+ ).toEqual({+ rowsToUpdate: [{ id: 'row-fr', messages: { abc123: 'Société' } }],+ rowsToInsert: [],+ rowIdsToSoftDelete: [],+ });+ });++ it('prunes only the locales the manifest dropped', () => {+ const plan = computeApplicationTranslationSyncPlan({+ existingRows: [+ { id: 'row-fr', locale: 'fr-FR', deletedAt: null },+ { id: 'row-de', locale: 'de-DE', deletedAt: null },+ ],+ translations: { 'fr-FR': { abc123: 'Entreprise' } },+ });++ expect(plan.rowIdsToSoftDelete).toEqual(['row-de']);+ expect(plan.rowsToUpdate).toEqual([+ { id: 'row-fr', messages: { abc123: 'Entreprise' } },+ ]);+ });++ it('prunes every locale when the manifest declares none', () => {+ expect(+ computeApplicationTranslationSyncPlan({+ existingRows: [+ { id: 'row-fr', locale: 'fr-FR', deletedAt: null },+ { id: 'row-de', locale: 'de-DE', deletedAt: null },+ ],+ translations: {},+ }).rowIdsToSoftDelete,+ ).toEqual(['row-fr', 'row-de']);+ });++ it('leaves an already soft-deleted row out of the prune list', () => {+ expect(+ computeApplicationTranslationSyncPlan({+ existingRows: [+ { id: 'row-fr', locale: 'fr-FR', deletedAt: new Date() },+ ],+ translations: {},+ }).rowIdsToSoftDelete,+ ).toEqual([]);+ });++ it('revives a soft-deleted locale instead of inserting a duplicate', () => {+ expect(+ computeApplicationTranslationSyncPlan({+ existingRows: [+ { id: 'row-fr-old', locale: 'fr-FR', deletedAt: new Date() },+ ],+ translations: { 'fr-FR': { abc123: 'Entreprise' } },+ }),+ ).toEqual({+ rowsToUpdate: [{ id: 'row-fr-old', messages: { abc123: 'Entreprise' } }],+ rowsToInsert: [],+ rowIdsToSoftDelete: [],+ });+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/application/application-translation/utils/compute-application-translation-sync-plan.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-translation/utils/compute-application-translation-sync-plan.util.tsnew file mode 100644index 00000000000..eb876d13dbe--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/application-translation/utils/compute-application-translation-sync-plan.util.ts@@ -0,0 +1,77 @@+import { type TranslationsManifest } from 'twenty-shared/application';+import { type APP_LOCALES } from 'twenty-shared/translations';+import { isDefined } from 'twenty-shared/utils';++type StoredTranslationRow = {+ id: string;+ locale: keyof typeof APP_LOCALES;+ deletedAt: Date | null;+};++export type ApplicationTranslationSyncPlan = {+ rowsToUpdate: { id: string; messages: Record<string, string> }[];+ rowsToInsert: {+ locale: keyof typeof APP_LOCALES;+ messages: Record<string, string>;+ }[];+ rowIdsToSoftDelete: string[];+};++// A soft-deleted row is preferred over none, so a locale that comes back is+// revived rather than duplicated.+const pickRowByLocale = (+ existingRows: StoredTranslationRow[],+): Map<keyof typeof APP_LOCALES, StoredTranslationRow> => {+ const rowByLocale = new Map<keyof typeof APP_LOCALES, StoredTranslationRow>();++ for (const row of existingRows) {+ const currentRow = rowByLocale.get(row.locale);++ const shouldPreferRow =+ !isDefined(currentRow) ||+ (isDefined(currentRow.deletedAt) && !isDefined(row.deletedAt));++ if (shouldPreferRow) {+ rowByLocale.set(row.locale, row);+ }+ }++ return rowByLocale;+};++export const computeApplicationTranslationSyncPlan = ({+ existingRows,+ translations,+}: {+ existingRows: StoredTranslationRow[];+ translations: TranslationsManifest;+}): ApplicationTranslationSyncPlan => {+ const rowByLocale = pickRowByLocale(existingRows);+ const manifestEntries = Object.entries(translations) as [+ keyof typeof APP_LOCALES,+ Record<string, string>,+ ][];+ const manifestLocales = new Set(manifestEntries.map(([locale]) => locale));++ const rowsToUpdate = manifestEntries+ .map(([locale, messages]) => {+ const existingRow = rowByLocale.get(locale);++ return isDefined(existingRow)+ ? { id: existingRow.id, messages }+ : undefined;+ })+ .filter(isDefined);++ const rowsToInsert = manifestEntries+ .filter(([locale]) => !isDefined(rowByLocale.get(locale)))+ .map(([locale, messages]) => ({ locale, messages }));++ const rowIdsToSoftDelete = existingRows+ .filter(+ (row) => !manifestLocales.has(row.locale) && !isDefined(row.deletedAt),+ )+ .map((row) => row.id);++ return { rowsToUpdate, rowsToInsert, rowIdsToSoftDelete };+};