From 49fdc9cad41d57431f573bec38bba00cba22d0cc Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 24 Mar 2022 13:34:03 -0400 Subject: [PATCH] fix(striker-ui-api): hoist and enhance join logic --- striker-ui-api/src/lib/join.ts | 15 +++++++++++++++ .../files/buildQueryFilesDetail.ts | 15 +++++++++------ striker-ui-api/src/types/JoinOptions.d.ts | 5 +++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 striker-ui-api/src/lib/join.ts create mode 100644 striker-ui-api/src/types/JoinOptions.d.ts diff --git a/striker-ui-api/src/lib/join.ts b/striker-ui-api/src/lib/join.ts new file mode 100644 index 00000000..1bc1fcfb --- /dev/null +++ b/striker-ui-api/src/lib/join.ts @@ -0,0 +1,15 @@ +const join = ( + elements: string[] | undefined, + { beforeReturn, elementWrapper = '', separator = '' }: JoinOptions, +) => { + const joinSeparator = `${elementWrapper}${separator}${elementWrapper}`; + + const toReturn = + elements instanceof Array && elements.length > 0 + ? `${elementWrapper}${elements.join(joinSeparator)}${elementWrapper}` + : undefined; + + return typeof beforeReturn === 'function' ? beforeReturn(toReturn) : toReturn; +}; + +export default join; diff --git a/striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts b/striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts index 2e21d764..76181a78 100644 --- a/striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts +++ b/striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts @@ -1,11 +1,14 @@ -const buildQueryFilesDetail = ({ filesUUID }: { filesUUID?: string[] }) => { - let condFilesUUID = ''; +import join from '../../join'; - if (filesUUID instanceof Array && filesUUID.length > 0) { - const catFilesUUID = `'${filesUUID.join("', '")}'`; +const buildQueryFilesDetail = ({ filesUUID }: { filesUUID?: string[] }) => { + const condFilesUUID = join(filesUUID, { + beforeReturn: (toReturn) => + toReturn ? `AND fil.file_uuid IN (${toReturn})` : '', + elementWrapper: "'", + separator: ', ', + }); - condFilesUUID = `AND fil.file_uuid IN (${catFilesUUID})`; - } + console.log(`condFilesUUID=[${condFilesUUID}]`); return ` SELECT diff --git a/striker-ui-api/src/types/JoinOptions.d.ts b/striker-ui-api/src/types/JoinOptions.d.ts new file mode 100644 index 00000000..efbdda7d --- /dev/null +++ b/striker-ui-api/src/types/JoinOptions.d.ts @@ -0,0 +1,5 @@ +type JoinOptions = { + beforeReturn?: (toReturn?: string) => string; + elementWrapper?: string; + separator?: string; +};