feat(striker-ui-api): add /alert-override handlers

main
Tsu-ba-me 11 months ago
parent 08c0d09d08
commit e64b9dbf91
  1. 35
      striker-ui-api/src/lib/request_handlers/alert-override/createAlertOverride.ts
  2. 22
      striker-ui-api/src/lib/request_handlers/alert-override/deleteAlertOverride.ts
  3. 69
      striker-ui-api/src/lib/request_handlers/alert-override/getAlertOverride.ts
  4. 76
      striker-ui-api/src/lib/request_handlers/alert-override/getAlertOverrideDetail.ts
  5. 45
      striker-ui-api/src/lib/request_handlers/alert-override/getAlertOverrideRequestBody.ts
  6. 5
      striker-ui-api/src/lib/request_handlers/alert-override/index.ts
  7. 38
      striker-ui-api/src/lib/request_handlers/alert-override/updateAlertOverride.ts
  8. 21
      striker-ui-api/src/types/ApiAlertOverride.d.ts

@ -0,0 +1,35 @@
import { RequestHandler } from 'express';
import { execManageAlerts } from '../../execManageAlerts';
import { getAlertOverrideRequestBody } from './getAlertOverrideRequestBody';
import { stderr, stdout } from '../../shell';
export const createAlertOverride: RequestHandler<
undefined,
undefined,
AlertOverrideRequestBody
> = (request, response) => {
const { body: rBody = {} } = request;
stdout(`Begin creating alert override.`);
let body: AlertOverrideRequestBody;
try {
body = getAlertOverrideRequestBody(rBody);
} catch (error) {
stderr(`Failed to process alert override input; CAUSE: ${error}`);
return response.status(400).send();
}
try {
execManageAlerts('alert-overrides', 'add', { body });
} catch (error) {
stderr(`Failed to create alert override; CAUSE: ${error}`);
return response.status(500).send();
}
return response.status(201).send();
};

@ -0,0 +1,22 @@
import { RequestHandler } from 'express';
import { execManageAlerts } from '../../execManageAlerts';
import { stderr } from '../../shell';
export const deleteAlertOverride: RequestHandler<
AlertOverrideParamsDictionary
> = (request, response) => {
const {
params: { uuid },
} = request;
try {
execManageAlerts('alert-overrides', 'delete', { uuid });
} catch (error) {
stderr(`Failed to delete alert override: CAUSE: ${error}`);
return response.status(500).send();
}
return response.status(204).send();
};

@ -0,0 +1,69 @@
import { RequestHandler } from 'express';
import { DELETED } from '../../consts';
import buildGetRequestHandler from '../buildGetRequestHandler';
import { buildQueryResultReducer } from '../../buildQueryResultModifier';
import { getShortHostName } from '../../disassembleHostName';
export const getAlertOverride: RequestHandler = buildGetRequestHandler(
(request, options) => {
const query = `
SELECT
a.alert_override_uuid,
a.alert_override_alert_level,
b.recipient_uuid,
b.recipient_name,
c.host_uuid,
c.host_name,
c.host_type
FROM alert_overrides AS a
JOIN recipients AS b
ON a.alert_override_recipient_uuid = b.recipient_uuid
JOIN hosts AS c
ON a.alert_override_host_uuid = c.host_uuid
WHERE a.alert_override_alert_level != -1
AND b.recipient_name != '${DELETED}'
ORDER BY b.recipient_name ASC;`;
const afterQueryReturn: QueryResultModifierFunction =
buildQueryResultReducer<AlertOverrideOverviewList>(
(
previous,
[
uuid,
level,
mailRecipientUuid,
mailRecipientName,
hostUuid,
hostName,
hostType,
],
) => {
previous[uuid] = {
host: {
hostName,
hostType,
hostUUID: hostUuid,
shortHostName: getShortHostName(hostName),
},
level: Number(level),
mailRecipient: {
name: mailRecipientName,
uuid: mailRecipientUuid,
},
uuid,
};
return previous;
},
{},
);
if (options) {
options.afterQueryReturn = afterQueryReturn;
}
return query;
},
);

@ -0,0 +1,76 @@
import { RequestHandler } from 'express';
import { DELETED } from '../../consts';
import buildGetRequestHandler from '../buildGetRequestHandler';
import { buildQueryResultModifier } from '../../buildQueryResultModifier';
import { getShortHostName } from '../../disassembleHostName';
import { sanitize } from '../../sanitize';
export const getAlertOverrideDetail: RequestHandler<AlertOverrideParamsDictionary> =
buildGetRequestHandler((request, options) => {
const {
params: { uuid: rUuid },
} = request;
const uuid = sanitize(rUuid, 'string', { modifierType: 'sql' });
const query = `
SELECT
a.alert_override_uuid,
a.alert_override_alert_level,
b.recipient_uuid,
b.recipient_name,
c.host_uuid,
c.host_name,
c.host_type
FROM alert_overrides AS a
JOIN recipients AS b
ON a.alert_override_recipient_uuid = b.recipient_uuid
JOIN hosts AS c
ON a.alert_override_host_uuid = c.host_uuid
WHERE a.alert_override_alert_level != -1
AND b.recipient_name != '${DELETED}'
AND a.alert_override_uuid = '${uuid}'
ORDER BY b.recipient_name ASC;`;
const afterQueryReturn: QueryResultModifierFunction =
buildQueryResultModifier<AlertOverrideDetail | undefined>((rows) => {
if (!rows.length) {
return undefined;
}
const {
0: [
uuid,
level,
mailRecipientUuid,
mailRecipientName,
hostUuid,
hostName,
hostType,
],
} = rows;
return {
host: {
hostName,
hostType,
hostUUID: hostUuid,
shortHostName: getShortHostName(hostName),
},
level: Number(level),
mailRecipient: {
name: mailRecipientName,
uuid: mailRecipientUuid,
},
uuid,
};
});
if (options) {
options.afterQueryReturn = afterQueryReturn;
}
return query;
});

@ -0,0 +1,45 @@
import assert from 'assert';
import { REP_UUID } from '../../consts';
import { sanitize } from '../../sanitize';
export const getAlertOverrideRequestBody = (
body: Partial<AlertOverrideRequestBody>,
uuid?: string,
): AlertOverrideRequestBody => {
const {
hostUuid: rHostUuid,
level: rLevel,
mailRecipientUuid: rMailRecipientUuid,
} = body;
const hostUuid = sanitize(rHostUuid, 'string');
const level = sanitize(rLevel, 'number');
const mailRecipientUuid = sanitize(rMailRecipientUuid, 'string');
if (uuid) {
assert(REP_UUID.test(uuid), `Expected valid UUIDv4; got [${uuid}]`);
}
assert(
REP_UUID.test(hostUuid),
`Expected valid host UUIDv4; got [${hostUuid}]`,
);
assert(
Number.isSafeInteger(level),
`Expected level to be an integer; got [${level}]`,
);
assert(
REP_UUID.test(mailRecipientUuid),
`Expected valid mail recipient UUIDv4; got [${mailRecipientUuid}]`,
);
return {
hostUuid,
level,
mailRecipientUuid,
};
};

@ -0,0 +1,5 @@
export * from './createAlertOverride';
export * from './deleteAlertOverride';
export * from './getAlertOverride';
export * from './getAlertOverrideDetail';
export * from './updateAlertOverride';

@ -0,0 +1,38 @@
import { RequestHandler } from 'express';
import { execManageAlerts } from '../../execManageAlerts';
import { getAlertOverrideRequestBody } from './getAlertOverrideRequestBody';
import { stderr, stdout } from '../../shell';
export const updateAlertOverride: RequestHandler<
AlertOverrideParamsDictionary,
undefined,
AlertOverrideRequestBody
> = (request, response) => {
const {
body: rBody = {},
params: { uuid },
} = request;
stdout('Begin updating alert override.');
let body: AlertOverrideRequestBody;
try {
body = getAlertOverrideRequestBody(rBody, uuid);
} catch (error) {
stderr(`Failed to process alert override input; CAUSE: ${error}`);
return response.status(400).send();
}
try {
execManageAlerts('alert-overrides', 'edit', { body, uuid });
} catch (error) {
stderr(`Failed to update alert override; CAUSE: ${error}`);
return response.status(500).send();
}
return response.status(200).send();
};

@ -0,0 +1,21 @@
type AlertOverrideOverview = {
level: number;
host: HostOverview;
mailRecipient: MailRecipientOverview;
uuid: string;
};
type AlertOverrideDetail = AlertOverrideOverview;
type AlertOverrideOverviewList = {
[uuid: string]: AlertOverrideOverview;
};
type AlertOverrideParamsDictionary = {
uuid: string;
};
type AlertOverrideRequestBody = Pick<AlertOverrideDetail, 'level'> & {
hostUuid: string;
mailRecipientUuid: string;
};
Loading…
Cancel
Save