fix(striker-ui-api): simplify build ID condition

main
Tsu-ba-me 2 years ago
parent 58ef1c04c7
commit ca7288cbdd
  1. 33
      striker-ui-api/src/lib/buildCondition.ts
  2. 18
      striker-ui-api/src/lib/buildIDCondition.ts
  3. 22
      striker-ui-api/src/lib/request_handlers/host/getHost.ts
  4. 4
      striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts

@ -0,0 +1,33 @@
import join from './join';
import { sanitizeQS } from './sanitizeQS';
const buildIDCondition = (
ids: Parameters<JoinFunction>[0],
conditionPrefix: string,
{
onFallback = () => '',
beforeReturn = (result) =>
result ? `${conditionPrefix} IN (${result})` : onFallback(),
}: Pick<JoinOptions, 'beforeReturn'> & { 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));

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

@ -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}]`);

@ -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 =

Loading…
Cancel
Save