From a57b8efda4aedbe1722d2e2d0f109ce25bf901b0 Mon Sep 17 00:00:00 2001 From: GuillaumeMgz Date: Fri, 30 Aug 2024 15:56:24 +0200 Subject: [PATCH] (PC-31577)[PRO] fix: Dont translate searched text in offers search input. --- pro/src/utils/__specs__/translate.spec.ts | 137 ++++++++++++++++++++++ pro/src/utils/translate.ts | 10 +- 2 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 pro/src/utils/__specs__/translate.spec.ts diff --git a/pro/src/utils/__specs__/translate.spec.ts b/pro/src/utils/__specs__/translate.spec.ts new file mode 100644 index 00000000000..e5257ddf4e4 --- /dev/null +++ b/pro/src/utils/__specs__/translate.spec.ts @@ -0,0 +1,137 @@ +import { + CollectiveOfferDisplayedStatus, + EacFormat, + OfferStatus, +} from 'apiClient/v1' +import { Audience } from 'core/shared/types' +import { + translateApiParamsToQueryParams, + translateQueryParamsToApiParams, +} from 'utils/translate' + +describe('translate', () => { + it('should translate between query params to api params for collective offers', () => { + expect( + translateQueryParamsToApiParams( + { + 'nom-ou-isbn': 'input de recherche', + structure: '883', + lieu: '891', + format: 'Concert', + 'periode-evenement-debut': '2024-08-08', + 'periode-evenement-fin': '2024-08-24', + statut: ['en-attente', 'active'], + }, + Audience.COLLECTIVE + ) + ).toEqual( + expect.objectContaining({ + nameOrIsbn: 'input de recherche', + offererId: '883', + venueId: '891', + format: 'Concert', + periodBeginningDate: '2024-08-08', + periodEndingDate: '2024-08-24', + status: ['PENDING', 'ACTIVE'], + }) + ) + + expect( + translateApiParamsToQueryParams( + { + nameOrIsbn: 'input de recherche', + offererId: '883', + venueId: '891', + format: EacFormat.CONCERT, + periodBeginningDate: '2024-08-08', + periodEndingDate: '2024-08-24', + status: [ + CollectiveOfferDisplayedStatus.PENDING, + CollectiveOfferDisplayedStatus.ACTIVE, + ], + }, + Audience.COLLECTIVE + ) + ).toEqual( + expect.objectContaining({ + 'nom-ou-isbn': 'input de recherche', + structure: '883', + lieu: '891', + format: 'Concert', + 'periode-evenement-debut': '2024-08-08', + 'periode-evenement-fin': '2024-08-24', + statut: ['en-attente', 'active'], + }) + ) + }) + + it('should translate query params to api params for individual offers', () => { + expect( + translateQueryParamsToApiParams( + { + 'nom-ou-isbn': 'input de recherche', + structure: '883', + lieu: '891', + 'periode-evenement-debut': '2024-08-08', + 'periode-evenement-fin': '2024-08-24', + statut: 'draft', + }, + Audience.INDIVIDUAL + ) + ).toEqual( + expect.objectContaining({ + nameOrIsbn: 'input de recherche', + offererId: '883', + venueId: '891', + periodBeginningDate: '2024-08-08', + periodEndingDate: '2024-08-24', + status: 'DRAFT', + }) + ) + + expect( + translateApiParamsToQueryParams( + { + nameOrIsbn: 'input de recherche', + offererId: '883', + venueId: '891', + periodBeginningDate: '2024-08-08', + periodEndingDate: '2024-08-24', + status: OfferStatus.DRAFT, + }, + Audience.INDIVIDUAL + ) + ).toEqual( + expect.objectContaining({ + 'nom-ou-isbn': 'input de recherche', + structure: '883', + lieu: '891', + 'periode-evenement-debut': '2024-08-08', + 'periode-evenement-fin': '2024-08-24', + statut: 'draft', + }) + ) + }) + + it('should not translate user inputs for collective offers', () => { + expect( + translateQueryParamsToApiParams( + { + 'nom-ou-isbn': 'en-attente', + }, + Audience.COLLECTIVE + ) + ).toEqual(expect.objectContaining({ nameOrIsbn: 'en-attente' })) + }) + + it('should not translate user inputs for individual offers', () => { + expect( + translateQueryParamsToApiParams( + { + 'nom-ou-isbn': 'remboursements', + }, + Audience.INDIVIDUAL + ) + ).toEqual(expect.objectContaining({ nameOrIsbn: 'remboursements' })) + }) +}) diff --git a/pro/src/utils/translate.ts b/pro/src/utils/translate.ts index 77e874ed31b..eb3f35a7d5d 100644 --- a/pro/src/utils/translate.ts +++ b/pro/src/utils/translate.ts @@ -8,13 +8,17 @@ const translateObjectKeysAndValues = ( originalObject: Record, translationsMap: Record ) => { + const nonTranlatedKeys = ['nom-ou-isbn', 'nameOrIsbn'] // These keys should not have their values translated since it's user inputs + const result: Record = {} Object.entries(originalObject).forEach(([originalKey, originalValue]) => { const translatedKey = translationsMap[originalKey] ?? originalKey - const translatedValue = Object.keys(translationsMap).includes(originalValue) - ? translationsMap[originalValue] - : originalValue + const translatedValue = + Object.keys(translationsMap).includes(originalValue) && + !nonTranlatedKeys.includes(originalKey) + ? translationsMap[originalValue] + : originalValue result[translatedKey] = Array.isArray(translatedValue) ? translatedValue.map((value) => translationsMap[value])