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; +};