fix(striker-ui-api): respond with created mail recipient uuid

main
Tsu-ba-me 11 months ago
parent 6a47a4c9be
commit 462a4ae999
  1. 16
      striker-ui-api/src/lib/execManageAlerts.ts
  2. 16
      striker-ui-api/src/lib/request_handlers/mail-recipient/createMailRecipient.ts
  3. 2
      striker-ui-api/src/types/ApiMailRecipient.d.ts

@ -1,7 +1,7 @@
import assert from 'assert';
import { spawnSync } from 'child_process';
import { SpawnSyncReturns, spawnSync } from 'child_process';
import { SERVER_PATHS } from './consts';
import { P_UUID, SERVER_PATHS } from './consts';
import { stdoutVar } from './shell';
@ -45,7 +45,7 @@ export const execManageAlerts = (
body?: Record<string, unknown>;
uuid?: string;
} = {},
) => {
): { uuid?: string } => {
const shallow = { ...body };
if (uuid) {
@ -69,13 +69,17 @@ export const execManageAlerts = (
stdoutVar({ commandArgs }, 'Manage alerts with args: ');
let result: SpawnSyncReturns<string>;
try {
const { error, signal, status, stderr, stdout } = spawnSync(
result = spawnSync(
SERVER_PATHS.usr.sbin['anvil-manage-alerts'].self,
commandArgs,
{ encoding: 'utf-8', timeout: 10000 },
);
const { error, signal, status, stderr, stdout } = result;
stdoutVar(
{ error, signal, status, stderr, stdout },
'Manage alerts returned: ',
@ -95,4 +99,8 @@ export const execManageAlerts = (
} catch (error) {
throw new Error(`Failed to complete manage alerts; CAUSE: ${error}`);
}
return {
uuid: result.stdout.match(new RegExp(P_UUID))?.[0],
};
};

@ -6,30 +6,36 @@ import { stderr, stdout } from '../../shell';
export const createMailRecipient: RequestHandler<
undefined,
undefined,
MailRecipientResponseBody | undefined,
MailRecipientRequestBody
> = (request, response) => {
const { body: rBody = {} } = request;
stdout('Begin creating alert recipient.');
let body: MailRecipientRequestBody;
let reqBody: MailRecipientRequestBody;
try {
body = getMailRecipientRequestBody(rBody);
reqBody = getMailRecipientRequestBody(rBody);
} catch (error) {
stderr(`Failed to process alert recipient input; CAUSE: ${error}`);
return response.status(400).send();
}
let resBody: MailRecipientResponseBody | undefined;
try {
execManageAlerts('recipients', 'add', { body });
const { uuid = '' } = execManageAlerts('recipients', 'add', {
body: reqBody,
});
resBody = { uuid };
} catch (error) {
stderr(`Failed to create alert recipient; CAUSE: ${error}`);
return response.status(500).send();
}
return response.status(201).send();
return response.status(201).send(resBody);
};

@ -18,3 +18,5 @@ type MailRecipientParamsDictionary = {
};
type MailRecipientRequestBody = Omit<MailRecipientDetail, 'uuid'>;
type MailRecipientResponseBody = Pick<MailRecipientDetail, 'uuid'>;

Loading…
Cancel
Save