Skip to content

Commit ac0f0b7

Browse files
fix(remoteitems): fix direct request of remote items (#192)
* fix(remoteitems): fix direct request of remote items (#192) When requesting Remote items directly (opposed to indirectly as reference of another requested local item), those remote items were not correctly handled. This led to empty responses, even though the item itself was correctly fetched and put --------- Co-authored-by: Emre Neumann <[email protected]>
1 parent 7e1533f commit ac0f0b7

File tree

9 files changed

+117
-13
lines changed

9 files changed

+117
-13
lines changed

integrationtests/FSXAProxyApi.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FSXAContentMode,
66
FSXAProxyApi,
77
LogLevel,
8+
Image,
89
Page,
910
QueryBuilderQuery,
1011
Reference,
@@ -488,6 +489,24 @@ describe('FSXAProxyAPI', () => {
488489
remotePageRefReference.value?.remoteProject
489490
)
490491
})
492+
it('api returns remote element if directly requested', async function () {
493+
const remoteMedia = createMediaPicture(undefined, "de_DE")
494+
495+
await caasClient.addDocToCollection({
496+
...remoteMedia,
497+
locale: {
498+
identifier: 'de',
499+
country: 'DE',
500+
language: 'de',
501+
},
502+
})
503+
const res: Image = await proxyAPI.fetchElement({
504+
id: remoteMedia.identifier,
505+
locale: 'de_DE',
506+
})
507+
expect(res).toBeDefined
508+
expect(res.id).toBe(remoteMedia.identifier)
509+
})
491510
})
492511

493512
describe('fetchByFilter', () => {

src/modules/CaaSMapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ export class CaaSMapper {
10361036

10371037
// find resolved refs and puzzle them back together
10381038
const mappedItems = findResolvedReferencesByIds(
1039-
items.map((item) => getItemId(item)),
1039+
items.map((item) => getItemId(item, remoteProjectId)),
10401040
this.resolvedReferences
10411041
)
10421042

src/modules/FSXAProxyApi.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('FSXAProxyAPI', () => {
1313
beforeEach(() => {
1414
fetchMock.resetMocks()
1515
id = Faker.datatype.uuid()
16-
locale = Faker.locale
16+
locale = `${Faker.locale}_${Faker.locale.toUpperCase()}`
1717
})
1818
describe('The initialization', () => {
1919
it('should throw an error if the BASEURL is empty', () => {

src/modules/FSXAProxyApi.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ export class FSXAProxyApi implements FSXAApi {
148148
mappedItems = denormalizeResolvedReferences(
149149
mappedItems,
150150
referenceMap,
151-
resolvedReferences
151+
resolvedReferences,
152+
remoteProject
152153
)
153154

154155
return mappedItems[0] as unknown as T
@@ -251,7 +252,8 @@ export class FSXAProxyApi implements FSXAApi {
251252
items = denormalizeResolvedReferences(
252253
items as (CaasApi_Item | MappedCaasItem)[],
253254
referenceMap!,
254-
resolvedReferences!
255+
resolvedReferences!,
256+
remoteProject
255257
)
256258

257259
return { page, pagesize, totalPages, size, items } as FetchResponse

src/modules/FSXARemoteApi.spec.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
import { generateRandomConfig } from '../testutils/generateRandomConfig'
1111

1212
import 'jest-fetch-mock'
13-
import { createDataEntry } from '../testutils'
13+
import {
14+
createDataEntry,
15+
createMediaPicture,
16+
createMediaPictureReference,
17+
} from '../testutils'
18+
import { getMappedMediaPicture } from '../testutils/getMappedMediaPicture'
1419
require('jest-fetch-mock').enableFetchMocks()
1520

1621
describe('FSXARemoteAPI', () => {
@@ -556,7 +561,7 @@ describe('FSXARemoteAPI', () => {
556561
})
557562
describe('fetchByFilter', () => {
558563
let remoteApi: FSXARemoteApi
559-
let config: any
564+
let config: ReturnType<typeof generateRandomConfig>
560565
let filters: QueryBuilderQuery[]
561566
let filterValue: string
562567
let filterField: string
@@ -765,6 +770,55 @@ describe('FSXARemoteAPI', () => {
765770
items,
766771
})
767772
})
773+
it('should return items when fetching remote items', async () => {
774+
const mainMedia = createMediaPicture(
775+
undefined,
776+
config.remotes.remote.locale
777+
)
778+
const referencedMedia = createMediaPicture(
779+
undefined,
780+
config.remotes.remote.locale
781+
)
782+
783+
mainMedia.metaFormData.fsRef = createMediaPictureReference(
784+
referencedMedia._id
785+
)
786+
787+
const firstResponse = { _embedded: { 'rh:doc': [mainMedia] } }
788+
const secondResponse = { _embedded: { 'rh:doc': [referencedMedia] } }
789+
790+
fetchMock
791+
.mockResponseOnce(JSON.stringify(firstResponse))
792+
.mockResponseOnce(JSON.stringify(secondResponse))
793+
794+
const actualRequest = await remoteApi.fetchByFilter({
795+
filters,
796+
locale: 'de_DE',
797+
remoteProject: config.remotes.remote.id,
798+
})
799+
800+
const mappedMainMedia = getMappedMediaPicture(
801+
mainMedia,
802+
config.remotes.remote.locale,
803+
config.remotes.remote.id
804+
)
805+
const mappedReferencedMedia = getMappedMediaPicture(
806+
referencedMedia,
807+
config.remotes.remote.locale,
808+
config.remotes.remote.id
809+
)
810+
811+
mappedMainMedia.meta.fsRef = mappedReferencedMedia
812+
813+
expect(actualRequest).toBeDefined()
814+
expect(actualRequest).toStrictEqual({
815+
page: 1,
816+
pagesize: 30,
817+
size: undefined,
818+
totalPages: undefined,
819+
items: [mappedMainMedia],
820+
})
821+
})
768822
})
769823
describe('fetchNavigation', () => {
770824
let remoteApi: FSXARemoteApi

src/modules/FSXARemoteApi.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,11 @@ export class FSXARemoteApi implements FSXAApi {
460460
remoteProject && this.remotes
461461
? this.remotes[remoteProject]?.locale
462462
: locale
463+
464+
const remoteProjectId = remoteProject
465+
? this.remotes[remoteProject]?.id
466+
: undefined
467+
463468
const {
464469
items,
465470
referenceMap = {},
@@ -473,7 +478,7 @@ export class FSXARemoteApi implements FSXAApi {
473478
},
474479
],
475480
additionalParams,
476-
remoteProject,
481+
remoteProject: remoteProjectId,
477482
fetchOptions,
478483
filterContext,
479484
normalized: true,
@@ -489,7 +494,8 @@ export class FSXARemoteApi implements FSXAApi {
489494
: denormalizeResolvedReferences(
490495
items as (MappedCaasItem | CaasApi_Item)[],
491496
referenceMap,
492-
resolvedReferences
497+
resolvedReferences,
498+
remoteProjectId
493499
)[0]
494500
}
495501

@@ -661,7 +667,8 @@ export class FSXARemoteApi implements FSXAApi {
661667
: denormalizeResolvedReferences(
662668
mappedItems,
663669
referenceMap,
664-
resolvedReferences
670+
resolvedReferences,
671+
remoteProjectId
665672
),
666673
...(normalized && { referenceMap }),
667674
...(normalized && { resolvedReferences }),

src/modules/MappingUtils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ const imageMapForceResolution = ({
7272
const denormalizeResolvedReferences = (
7373
mappedItems: (CaasApi_Item | MappedCaasItem)[],
7474
referenceMap: ReferencedItemsInfo,
75-
resolvedReferences: ResolvedReferencesInfo
75+
resolvedReferences: ResolvedReferencesInfo,
76+
remoteProjectId?: string
7677
) => {
7778
if (!referenceMap || Object.keys(referenceMap).length === 0)
7879
return mappedItems
@@ -104,7 +105,7 @@ const denormalizeResolvedReferences = (
104105
// update mappedItems
105106
const queriedIds = mappedItems
106107
.filter((item) => !!item)
107-
.map((item) => getItemId(item))
108+
.map((item) => getItemId(item, remoteProjectId))
108109

109110
return findResolvedReferencesByIds(queriedIds, resolvedReferences)
110111
}

src/testutils/generateRandomConfig.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ export const generateRandomConfig = () => {
1212
? FSXAContentMode.PREVIEW
1313
: FSXAContentMode.RELEASE
1414
const REMOTES = {
15-
remote: { id: Faker.datatype.uuid(), locale: Faker.locale },
16-
secondRemote: { id: Faker.datatype.uuid(), locale: Faker.locale },
15+
remote: {
16+
id: Faker.datatype.uuid(),
17+
locale: `${Faker.locale}_${Faker.locale.toUpperCase()}`,
18+
},
19+
secondRemote: {
20+
id: Faker.datatype.uuid(),
21+
locale: `${Faker.locale}_${Faker.locale.toUpperCase()}`,
22+
},
1723
}
1824

1925
return {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { CaaSApi_Media_Picture, Image } from '../types'
2+
3+
export const getMappedMediaPicture = (
4+
caasMedia: CaaSApi_Media_Picture,
5+
locale: string,
6+
remoteProjectId?: string
7+
): Image => ({
8+
type: "Image",
9+
id: caasMedia.identifier,
10+
previewId: `${caasMedia.identifier}.${locale}`,
11+
meta: {},
12+
description: caasMedia.description,
13+
resolutions: {},
14+
remoteProjectId
15+
})

0 commit comments

Comments
 (0)