From 039d755e89478b56fa7e9eb28240ea0d68d10ad9 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 3 May 2023 15:09:10 -0400 Subject: [PATCH] refactor(striker-ui-api): reduce repeat in get all servers --- .../lib/request_handlers/server/getServer.ts | 55 ++++++++++--------- striker-ui-api/src/types/ApiServer.d.ts | 2 + 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/striker-ui-api/src/lib/request_handlers/server/getServer.ts b/striker-ui-api/src/lib/request_handlers/server/getServer.ts index 516e73ed..a786c366 100644 --- a/striker-ui-api/src/lib/request_handlers/server/getServer.ts +++ b/striker-ui-api/src/lib/request_handlers/server/getServer.ts @@ -1,4 +1,7 @@ +import { DELETED } from '../../consts'; + import buildGetRequestHandler from '../buildGetRequestHandler'; +import { buildQueryResultReducer } from '../../buildQueryResultModifier'; import join from '../../join'; import { sanitize } from '../../sanitize'; import { stdoutVar } from '../../shell'; @@ -17,31 +20,33 @@ export const getServer = buildGetRequestHandler( stdoutVar({ condAnvilUUIDs }); if (buildQueryOptions) { - buildQueryOptions.afterQueryReturn = (queryStdout) => { - let result = queryStdout; - - if (queryStdout instanceof Array) { - result = queryStdout.map( - ([ - serverUUID, - serverName, - serverState, - serverHostUUID, - anvilUUID, - anvilName, - ]) => ({ - serverHostUUID, - serverName, - serverState, - serverUUID, - anvilUUID, - anvilName, - }), - ); - } + buildQueryOptions.afterQueryReturn = buildQueryResultReducer< + ServerOverview[] + >( + ( + previous, + [ + serverUUID, + serverName, + serverState, + serverHostUUID, + anvilUUID, + anvilName, + ], + ) => { + previous.push({ + anvilName, + anvilUUID, + serverHostUUID, + serverName, + serverState, + serverUUID, + }); - return result; - }; + return previous; + }, + [], + ); } return ` @@ -55,7 +60,7 @@ export const getServer = buildGetRequestHandler( FROM servers AS ser JOIN anvils AS anv ON ser.server_anvil_uuid = anv.anvil_uuid - WHERE ser.server_state != 'DELETED' + WHERE ser.server_state != '${DELETED}' ${condAnvilUUIDs};`; }, ); diff --git a/striker-ui-api/src/types/ApiServer.d.ts b/striker-ui-api/src/types/ApiServer.d.ts index 59fa3a73..ece4a217 100644 --- a/striker-ui-api/src/types/ApiServer.d.ts +++ b/striker-ui-api/src/types/ApiServer.d.ts @@ -1,4 +1,6 @@ type ServerOverview = { + anvilName: string; + anvilUUID: string; serverHostUUID: string; serverName: string; serverState: string;