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 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'; import { stdoutVar } from './shell';
@ -45,7 +45,7 @@ export const execManageAlerts = (
body?: Record<string, unknown>; body?: Record<string, unknown>;
uuid?: string; uuid?: string;
} = {}, } = {},
) => { ): { uuid?: string } => {
const shallow = { ...body }; const shallow = { ...body };
if (uuid) { if (uuid) {
@ -69,13 +69,17 @@ export const execManageAlerts = (
stdoutVar({ commandArgs }, 'Manage alerts with args: '); stdoutVar({ commandArgs }, 'Manage alerts with args: ');
let result: SpawnSyncReturns<string>;
try { try {
const { error, signal, status, stderr, stdout } = spawnSync( result = spawnSync(
SERVER_PATHS.usr.sbin['anvil-manage-alerts'].self, SERVER_PATHS.usr.sbin['anvil-manage-alerts'].self,
commandArgs, commandArgs,
{ encoding: 'utf-8', timeout: 10000 }, { encoding: 'utf-8', timeout: 10000 },
); );
const { error, signal, status, stderr, stdout } = result;
stdoutVar( stdoutVar(
{ error, signal, status, stderr, stdout }, { error, signal, status, stderr, stdout },
'Manage alerts returned: ', 'Manage alerts returned: ',
@ -95,4 +99,8 @@ export const execManageAlerts = (
} catch (error) { } catch (error) {
throw new Error(`Failed to complete manage alerts; CAUSE: ${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< export const createMailRecipient: RequestHandler<
undefined, undefined,
undefined, MailRecipientResponseBody | undefined,
MailRecipientRequestBody MailRecipientRequestBody
> = (request, response) => { > = (request, response) => {
const { body: rBody = {} } = request; const { body: rBody = {} } = request;
stdout('Begin creating alert recipient.'); stdout('Begin creating alert recipient.');
let body: MailRecipientRequestBody; let reqBody: MailRecipientRequestBody;
try { try {
body = getMailRecipientRequestBody(rBody); reqBody = getMailRecipientRequestBody(rBody);
} catch (error) { } catch (error) {
stderr(`Failed to process alert recipient input; CAUSE: ${error}`); stderr(`Failed to process alert recipient input; CAUSE: ${error}`);
return response.status(400).send(); return response.status(400).send();
} }
let resBody: MailRecipientResponseBody | undefined;
try { try {
execManageAlerts('recipients', 'add', { body }); const { uuid = '' } = execManageAlerts('recipients', 'add', {
body: reqBody,
});
resBody = { uuid };
} catch (error) { } catch (error) {
stderr(`Failed to create alert recipient; CAUSE: ${error}`); stderr(`Failed to create alert recipient; CAUSE: ${error}`);
return response.status(500).send(); 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 MailRecipientRequestBody = Omit<MailRecipientDetail, 'uuid'>;
type MailRecipientResponseBody = Pick<MailRecipientDetail, 'uuid'>;

Loading…
Cancel
Save