fix(orchestration): Fix remote joiner filtering fields (#4970)
* fix(orchestration): Fix remote joiner filtering fields * cleanup test
This commit is contained in:
1 parent
66bd9a835c
commit
86f4ca369f
2 files changed
+159
-1
No files matched your search
@@ -40,13 +40,158 @@ const fetchServiceDataCallback = async (
|
|||||||
|
|
||||||
describe("RemoteJoiner", () => {
|
describe("RemoteJoiner", () => {
|
||||||
let joiner: RemoteJoiner
|
let joiner: RemoteJoiner
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
joiner = new RemoteJoiner(serviceConfigs, fetchServiceDataCallback)
|
joiner = new RemoteJoiner(serviceConfigs, fetchServiceDataCallback)
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks()
|
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 () => {
|
it("Simple query of a service, its id and no fields specified", async () => {
|
||||||
const query = {
|
const query = {
|
||||||
service: "user",
|
service: "user",
|
||||||
|
|||||||
@@ -36,7 +36,12 @@ export class RemoteJoiner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const filteredData = fields.reduce((acc: any, field: string) => {
|
const filteredData = fields.reduce((acc: any, field: string) => {
|
||||||
|
const fieldValue = data?.[field]
|
||||||
|
|
||||||
|
if (isDefined(fieldValue)) {
|
||||||
acc[field] = data?.[field]
|
acc[field] = data?.[field]
|
||||||
|
}
|
||||||
|
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
@@ -49,6 +54,13 @@ export class RemoteJoiner {
|
|||||||
RemoteJoiner.filterFields(item, expand.fields, expand.expands)
|
RemoteJoiner.filterFields(item, expand.fields, expand.expands)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
const filteredFields = RemoteJoiner.filterFields(
|
||||||
|
data[key],
|
||||||
|
expand.fields,
|
||||||
|
expand.expands
|
||||||
|
)
|
||||||
|
|
||||||
|
if (isDefined(filteredFields)) {
|
||||||
filteredData[key] = RemoteJoiner.filterFields(
|
filteredData[key] = RemoteJoiner.filterFields(
|
||||||
data[key],
|
data[key],
|
||||||
expand.fields,
|
expand.fields,
|
||||||
@@ -58,8 +70,9 @@ export class RemoteJoiner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return filteredData
|
return (Object.keys(filteredData).length && filteredData) || undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getNestedItems(items: any[], property: string): any[] {
|
private static getNestedItems(items: any[], property: string): any[] {
|
||||||
|
|||||||
Reference in new issue
Block a user