From ca7288cbddcb48e35d5687598a8c70f4dfd3a898 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 19 Oct 2022 23:34:23 -0400 Subject: [PATCH] fix(striker-ui-api): simplify build ID condition --- striker-ui-api/src/lib/buildCondition.ts | 33 +++++++++++++++++++ striker-ui-api/src/lib/buildIDCondition.ts | 18 ---------- .../src/lib/request_handlers/host/getHost.ts | 22 ++++++++----- .../host/getHostConnection.ts | 4 +-- 4 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 striker-ui-api/src/lib/buildCondition.ts delete mode 100644 striker-ui-api/src/lib/buildIDCondition.ts diff --git a/striker-ui-api/src/lib/buildCondition.ts b/striker-ui-api/src/lib/buildCondition.ts new file mode 100644 index 00000000..5bafdd65 --- /dev/null +++ b/striker-ui-api/src/lib/buildCondition.ts @@ -0,0 +1,33 @@ +import join from './join'; +import { sanitizeQS } from './sanitizeQS'; + +const buildIDCondition = ( + ids: Parameters[0], + conditionPrefix: string, + { + onFallback = () => '', + beforeReturn = (result) => + result ? `${conditionPrefix} IN (${result})` : onFallback(), + }: Pick & { onFallback?: () => string } = {}, +) => + join(ids, { + beforeReturn, + elementWrapper: "'", + separator: ', ', + }) as string; + +export const buildQSIDCondition = ( + ids: unknown, + conditionPrefix: string, + { onFallback = () => '' }: { onFallback?: () => string }, +): { after: string; before: string[] } => { + const before = sanitizeQS(ids, { isForSQL: true, returnType: 'string[]' }); + const after = buildIDCondition(before, conditionPrefix, { onFallback }); + + return { after, before }; +}; + +export const buildParamIDCondition = ( + ids: string[] | '*' = '*', + conditionPrefix: string, +) => (ids[0] === '*' ? '' : buildIDCondition(ids, conditionPrefix)); diff --git a/striker-ui-api/src/lib/buildIDCondition.ts b/striker-ui-api/src/lib/buildIDCondition.ts deleted file mode 100644 index 31a5482f..00000000 --- a/striker-ui-api/src/lib/buildIDCondition.ts +++ /dev/null @@ -1,18 +0,0 @@ -import join from './join'; -import { sanitizeQS } from './sanitizeQS'; - -export const buildIDCondition = ( - ids: unknown, - field: string, - { onFallback = () => '' }: { onFallback?: () => string }, -): { after: string; before: string[] } => { - const before = sanitizeQS(ids, { isForSQL: true, returnType: 'string[]' }); - const after = join(before, { - beforeReturn: (toReturn) => - toReturn ? `${field} IN (${toReturn})` : onFallback.call(null), - elementWrapper: "'", - separator: ', ', - }) as string; - - return { after, before }; -}; diff --git a/striker-ui-api/src/lib/request_handlers/host/getHost.ts b/striker-ui-api/src/lib/request_handlers/host/getHost.ts index d008fb71..a4b90a8d 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHost.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHost.ts @@ -1,20 +1,24 @@ import { getLocalHostUUID } from '../../accessModule'; import buildGetRequestHandler from '../buildGetRequestHandler'; -import { buildIDCondition } from '../../buildIDCondition'; +import { buildQSIDCondition } from '../../buildCondition'; export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { const { hostUUIDs } = request.query; const hostUUIDField = 'hos.host_uuid'; - const { after: condHostUUIDs } = buildIDCondition(hostUUIDs, hostUUIDField, { - onFallback: () => { - try { - return `${hostUUIDField} = '${getLocalHostUUID()}'`; - } catch (subError) { - throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`); - } + const { after: condHostUUIDs } = buildQSIDCondition( + hostUUIDs, + hostUUIDField, + { + onFallback: () => { + try { + return `${hostUUIDField} = '${getLocalHostUUID()}'`; + } catch (subError) { + throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`); + } + }, }, - }); + ); process.stdout.write(`condHostUUIDs=[${condHostUUIDs}]`); diff --git a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts index 298de21f..0bd0f2c9 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts @@ -1,5 +1,5 @@ import { getAnvilData, getLocalHostUUID } from '../../accessModule'; -import { buildIDCondition } from '../../buildIDCondition'; +import { buildQSIDCondition } from '../../buildCondition'; import buildGetRequestHandler from '../buildGetRequestHandler'; const buildHostConnections = ( @@ -59,7 +59,7 @@ export const getHostConnection = buildGetRequestHandler( const hostUUIDField = 'ip_add.ip_address_host_uuid'; const { after: condHostUUIDs, before: beforeBuildIDCond } = - buildIDCondition(rawHostUUIDs, hostUUIDField, { + buildQSIDCondition(rawHostUUIDs, hostUUIDField, { onFallback: () => `${hostUUIDField} = '${localHostUUID}'`, }); const hostUUIDs =