Skip to content

Commit

Permalink
(PC-31577)[PRO] fix: Dont translate searched text in offers search in…
Browse files Browse the repository at this point in the history
…put.
  • Loading branch information
GuillaumeMgz committed Sep 3, 2024
1 parent 824b3b6 commit a57b8ef
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 3 deletions.
137 changes: 137 additions & 0 deletions pro/src/utils/__specs__/translate.spec.ts
Original file line number Diff line number Diff line change
@@ -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' }))
})
})
10 changes: 7 additions & 3 deletions pro/src/utils/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ const translateObjectKeysAndValues = (
originalObject: Record<string, any>,
translationsMap: Record<string, string>
) => {
const nonTranlatedKeys = ['nom-ou-isbn', 'nameOrIsbn'] // These keys should not have their values translated since it's user inputs

const result: Record<string, string> = {}
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])
Expand Down

0 comments on commit a57b8ef

Please sign in to comment.