fix(orchestration): Fix remote joiner filtering fields (#4970)

* fix(orchestration): Fix remote joiner filtering fields

* cleanup test
This commit is contained in:
Adrien de Peretti
2023-09-07 09:10:16 +02:00
committed by GitHub
parent 66bd9a835c
commit 86f4ca369f
2 changed files with 161 additions and 3 deletions

View File

@@ -40,13 +40,158 @@ const fetchServiceDataCallback = async (
describe("RemoteJoiner", () => {
let joiner: RemoteJoiner
beforeAll(() => {
joiner = new RemoteJoiner(serviceConfigs, fetchServiceDataCallback)
})
beforeEach(() => {
jest.clearAllMocks()
})
it("should filter the fields and attach the values correctly", () => {
const data = {
id: "prod_01H1PN579TJ707BRK938E2ME2N",
title: "7468915",
handle: "7468915",
subtitle: null,
description: null,
collection_id: null,
collection: null,
type_id: "ptyp_01GX66TMARS55DBNYE31DDT8ZV",
type: {
id: "ptyp_01GX66TMARS55DBNYE31DDT8ZV",
value: "test-type-1",
},
options: [
{
id: "opt_01H1PN57AQE8G3FK365EYNH917",
title: "4108194",
product_id: "prod_01H1PN579TJ707BRK938E2ME2N",
product: "prod_01H1PN579TJ707BRK938E2ME2N",
values: [
{
id: "optval_01H1PN57EAMXYFRGSJJJE9P0TJ",
value: "4108194",
option_id: "opt_01H1PN57AQE8G3FK365EYNH917",
option: "opt_01H1PN57AQE8G3FK365EYNH917",
variant_id: "variant_01H1PN57E99TMZAGNEZBSS3FM3",
variant: "variant_01H1PN57E99TMZAGNEZBSS3FM3",
},
],
},
],
variants: [
{
id: "variant_01H1PN57E99TMZAGNEZBSS3FM3",
product_id: "prod_01H1PN579TJ707BRK938E2ME2N",
product: "prod_01H1PN579TJ707BRK938E2ME2N",
options: [
{
id: "optval_01H1PN57EAMXYFRGSJJJE9P0TJ",
value: "4108194",
option_id: "opt_01H1PN57AQE8G3FK365EYNH917",
option: "opt_01H1PN57AQE8G3FK365EYNH917",
variant_id: "variant_01H1PN57E99TMZAGNEZBSS3FM3",
variant: "variant_01H1PN57E99TMZAGNEZBSS3FM3",
},
],
},
],
tags: [],
images: [],
}
const fields = [
"id",
"title",
"subtitle",
"description",
"handle",
"images",
"tags",
"type",
"collection",
"options",
"variants_id",
]
const expands = {
collection: {
fields: ["id", "title", "handle"],
},
images: {
fields: ["url"],
},
options: {
fields: ["title", "values"],
expands: {
values: {
fields: ["id", "value"],
},
},
},
tags: {
fields: ["value"],
},
type: {
fields: ["value"],
},
variants: {
fields: ["id", "options"],
expands: {
options: {
fields: ["id", "value"],
},
},
},
}
const filteredFields = (RemoteJoiner as any).filterFields(
data,
fields,
expands
)
expect(filteredFields).toEqual(
expect.objectContaining({
id: "prod_01H1PN579TJ707BRK938E2ME2N",
title: "7468915",
subtitle: null,
description: null,
handle: "7468915",
images: [],
tags: [],
type: {
value: "test-type-1",
},
collection: null,
options: [
{
title: "4108194",
values: [
{
id: "optval_01H1PN57EAMXYFRGSJJJE9P0TJ",
value: "4108194",
},
],
},
],
variants: [
{
id: "variant_01H1PN57E99TMZAGNEZBSS3FM3",
options: [
{
id: "optval_01H1PN57EAMXYFRGSJJJE9P0TJ",
value: "4108194",
},
],
},
],
})
)
})
it("Simple query of a service, its id and no fields specified", async () => {
const query = {
service: "user",

View File

@@ -36,7 +36,12 @@ export class RemoteJoiner {
}
const filteredData = fields.reduce((acc: any, field: string) => {
acc[field] = data?.[field]
const fieldValue = data?.[field]
if (isDefined(fieldValue)) {
acc[field] = data?.[field]
}
return acc
}, {})
@@ -49,17 +54,25 @@ export class RemoteJoiner {
RemoteJoiner.filterFields(item, expand.fields, expand.expands)
)
} else {
filteredData[key] = RemoteJoiner.filterFields(
const filteredFields = RemoteJoiner.filterFields(
data[key],
expand.fields,
expand.expands
)
if (isDefined(filteredFields)) {
filteredData[key] = RemoteJoiner.filterFields(
data[key],
expand.fields,
expand.expands
)
}
}
}
}
}
return filteredData
return (Object.keys(filteredData).length && filteredData) || undefined
}
private static getNestedItems(items: any[], property: string): any[] {