From 56a1b068cc93d90445cbb8dc29a25d5250dd74cb Mon Sep 17 00:00:00 2001 From: Dean Date: Thu, 4 May 2023 11:52:20 +0100 Subject: [PATCH 1/4] Added json parse option for row import attachment values --- packages/server/src/utilities/rowProcessor/map.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/server/src/utilities/rowProcessor/map.ts b/packages/server/src/utilities/rowProcessor/map.ts index 8911d62133..d3572094c6 100644 --- a/packages/server/src/utilities/rowProcessor/map.ts +++ b/packages/server/src/utilities/rowProcessor/map.ts @@ -70,6 +70,17 @@ export const TYPE_TRANSFORM_MAP: any = { "": [], [null]: [], [undefined]: undefined, + parse: (attachments) => { + if(typeof attachments === "string"){ + let result = attachments + try { + result = JSON.parse(attachments) + } catch (e) { + console.error("Could not parse attachments", e) + } + return result; + } + } }, [FieldTypes.BOOLEAN]: { "": null, From a1fdff8df8633e5a9f531be2c47b765118d72c17 Mon Sep 17 00:00:00 2001 From: Dean Date: Thu, 4 May 2023 12:25:30 +0100 Subject: [PATCH 2/4] Linting --- packages/server/src/utilities/rowProcessor/map.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/server/src/utilities/rowProcessor/map.ts b/packages/server/src/utilities/rowProcessor/map.ts index d3572094c6..cbf1c3ae36 100644 --- a/packages/server/src/utilities/rowProcessor/map.ts +++ b/packages/server/src/utilities/rowProcessor/map.ts @@ -70,17 +70,17 @@ export const TYPE_TRANSFORM_MAP: any = { "": [], [null]: [], [undefined]: undefined, - parse: (attachments) => { - if(typeof attachments === "string"){ + parse: attachments => { + if (typeof attachments === "string") { let result = attachments try { result = JSON.parse(attachments) } catch (e) { console.error("Could not parse attachments", e) } - return result; + return result } - } + }, }, [FieldTypes.BOOLEAN]: { "": null, From 5498cc86bdeec01457cb3eb2fba1a3631048b80b Mon Sep 17 00:00:00 2001 From: Dean Date: Thu, 4 May 2023 12:58:45 +0100 Subject: [PATCH 3/4] Refactor to fix tests and added an extra empty scenario --- packages/server/src/api/routes/tests/row.spec.js | 3 +++ packages/server/src/utilities/rowProcessor/map.ts | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.js b/packages/server/src/api/routes/tests/row.spec.js index 5cbb3d04b4..4b835a1fb5 100644 --- a/packages/server/src/api/routes/tests/row.spec.js +++ b/packages/server/src/api/routes/tests/row.spec.js @@ -212,6 +212,7 @@ describe("/rows", () => { attachmentNull: attachment, attachmentUndefined: attachment, attachmentEmpty: attachment, + attachmentEmptyArrayStr: attachment }, }) @@ -239,6 +240,7 @@ describe("/rows", () => { attachmentNull: null, attachmentUndefined: undefined, attachmentEmpty: "", + attachmentEmptyArrayStr: "[]", } const id = (await config.createRow(row))._id @@ -268,6 +270,7 @@ describe("/rows", () => { expect(saved.attachmentNull).toEqual([]) expect(saved.attachmentUndefined).toBe(undefined) expect(saved.attachmentEmpty).toEqual([]) + expect(saved.attachmentEmptyArrayStr).toEqual([]) }) }) diff --git a/packages/server/src/utilities/rowProcessor/map.ts b/packages/server/src/utilities/rowProcessor/map.ts index cbf1c3ae36..669223b302 100644 --- a/packages/server/src/utilities/rowProcessor/map.ts +++ b/packages/server/src/utilities/rowProcessor/map.ts @@ -67,12 +67,14 @@ export const TYPE_TRANSFORM_MAP: any = { }, }, [FieldTypes.ATTACHMENT]: { - "": [], [null]: [], [undefined]: undefined, parse: attachments => { if (typeof attachments === "string") { - let result = attachments + if (attachments === "") { + return [] + } + let result try { result = JSON.parse(attachments) } catch (e) { @@ -80,6 +82,7 @@ export const TYPE_TRANSFORM_MAP: any = { } return result } + return attachments }, }, [FieldTypes.BOOLEAN]: { From b0ff61c9ecb8f9faffdd0136230a7dac792cd06b Mon Sep 17 00:00:00 2001 From: Dean Date: Fri, 5 May 2023 09:24:57 +0100 Subject: [PATCH 4/4] Feedback update. Console.error to logging.logalert --- packages/server/src/utilities/rowProcessor/map.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/server/src/utilities/rowProcessor/map.ts b/packages/server/src/utilities/rowProcessor/map.ts index 669223b302..808b16178d 100644 --- a/packages/server/src/utilities/rowProcessor/map.ts +++ b/packages/server/src/utilities/rowProcessor/map.ts @@ -1,5 +1,6 @@ // @ts-nocheck import { FieldTypes } from "../../constants" +import { logging } from "@budibase/backend-core" /** * A map of how we convert various properties in rows to each other based on the row type. @@ -78,7 +79,7 @@ export const TYPE_TRANSFORM_MAP: any = { try { result = JSON.parse(attachments) } catch (e) { - console.error("Could not parse attachments", e) + logging.logAlert("Could not parse attachments", e) } return result }