From 86f4ca369f566e22fb71f09a305c7b03c0faddbe Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 7 Sep 2023 09:10:16 +0200 Subject: [PATCH] fix(orchestration): Fix remote joiner filtering fields (#4970) * fix(orchestration): Fix remote joiner filtering fields * cleanup test --- .../src/__tests__/joiner/remote-joiner.ts | 145 ++++++++++++++++++ .../orchestration/src/joiner/remote-joiner.ts | 19 ++- 2 files changed, 161 insertions(+), 3 deletions(-) diff --git a/packages/orchestration/src/__tests__/joiner/remote-joiner.ts b/packages/orchestration/src/__tests__/joiner/remote-joiner.ts index b36ef4cc48..93acc27667 100644 --- a/packages/orchestration/src/__tests__/joiner/remote-joiner.ts +++ b/packages/orchestration/src/__tests__/joiner/remote-joiner.ts @@ -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", diff --git a/packages/orchestration/src/joiner/remote-joiner.ts b/packages/orchestration/src/joiner/remote-joiner.ts index 038e1e4781..14ba840f72 100644 --- a/packages/orchestration/src/joiner/remote-joiner.ts +++ b/packages/orchestration/src/joiner/remote-joiner.ts @@ -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[] {