Skip to content

Commit

Permalink
fixed types
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Apr 2, 2021
1 parent 3c2b78d commit 4f07713
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 59 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/alerting/server/alerts_client/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export interface CreateOptions<Params extends AlertTypeParams> {
| 'mutedInstanceIds'
| 'actions'
| 'executionStatus'
> & { actions: AlertAction[] };
> & { actions: NormalizedAlertAction[] };
options?: {
id?: string;
migrationVersion?: Record<string, string>;
Expand All @@ -163,7 +163,7 @@ export interface UpdateOptions<Params extends AlertTypeParams> {
name: string;
tags: string[];
schedule: IntervalSchedule;
actions: AlertAction[];
actions: NormalizedAlertAction[];
params: Params;
throttle: string | null;
notifyWhen: AlertNotifyWhenType | null;
Expand Down
15 changes: 0 additions & 15 deletions x-pack/plugins/alerting/server/alerts_client/tests/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,20 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
{
group: 'default',
id: '1',
params: {
foo: true,
},
actionTypeId: 'test',
},
{
group: 'default',
id: '2',
params: {
foo: true,
},
actionTypeId: 'test2',
},
],
},
Expand Down Expand Up @@ -438,7 +435,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -608,7 +604,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -732,7 +727,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -862,7 +856,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -971,23 +964,20 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
{
group: 'default',
id: '1',
params: {
foo: true,
},
actionTypeId: 'test',
},
{
group: 'default',
id: '2',
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -1036,7 +1026,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -1159,7 +1148,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -1192,7 +1180,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -1230,7 +1217,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down Expand Up @@ -1267,7 +1253,6 @@ describe('update()', () => {
params: {
foo: true,
},
actionTypeId: 'test',
},
],
},
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/alerting/server/routes/create_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ describe('createRuleRoute', () => {
group: mockedAlert.actions[0].group,
id: mockedAlert.actions[0].id,
params: mockedAlert.actions[0].params,
connector_type_id: mockedAlert.actions[0].actionTypeId,
},
],
};
Expand Down Expand Up @@ -132,7 +131,6 @@ describe('createRuleRoute', () => {
"data": Object {
"actions": Array [
Object {
"actionTypeId": "test",
"group": "default",
"id": "2",
"params": Object {
Expand Down Expand Up @@ -205,7 +203,6 @@ describe('createRuleRoute', () => {
"data": Object {
"actions": Array [
Object {
"actionTypeId": "test",
"group": "default",
"id": "2",
"params": Object {
Expand Down
22 changes: 4 additions & 18 deletions x-pack/plugins/alerting/server/routes/create_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
AlertingRequestHandlerContext,
BASE_ALERTING_API_PATH,
AlertNotifyWhenType,
AlertAction,
} from '../types';

export const bodySchema = schema.object({
Expand All @@ -41,31 +40,20 @@ export const bodySchema = schema.object({
group: schema.string(),
id: schema.string(),
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }),
connector_type_id: schema.string(),
}),
{ defaultValue: [] }
),
notify_when: schema.string({ validate: validateNotifyWhenType }),
});

const rewriteBodyReqActions: RewriteRequestCase<AlertAction> = ({
connector_type_id: actionTypeId,
...rest
}) => ({
actionTypeId,
...rest,
});

const rewriteBodyReq: RewriteRequestCase<CreateOptions<AlertTypeParams>['data']> = ({
rule_type_id: alertTypeId,
notify_when: notifyWhen,
actions,
...rest
}) => ({
...rest,
alertTypeId,
notifyWhen,
actions: actions.map((action) => rewriteBodyReqActions(action)),
});
const rewriteBodyRes: RewriteResponseCase<SanitizedAlert<AlertTypeParams>> = ({
actions,
Expand Down Expand Up @@ -130,12 +118,10 @@ export const createRuleRoute = (
try {
const createdRule: SanitizedAlert<AlertTypeParams> = await alertsClient.create<AlertTypeParams>(
{
data: {
...rewriteBodyReq({
...rule,
notify_when: rule.notify_when as AlertNotifyWhenType,
}),
},
data: rewriteBodyReq({
...rule,
notify_when: rule.notify_when as AlertNotifyWhenType,
}),
options: { id: params?.id },
}
);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/routes/legacy/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const bodySchema = schema.object({
schema.object({
group: schema.string(),
id: schema.string(),
actionTypeId: schema.string(),
actionTypeId: schema.maybe(schema.string()),
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }),
}),
{ defaultValue: [] }
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/routes/legacy/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const bodySchema = schema.object({
group: schema.string(),
id: schema.string(),
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }),
actionTypeId: schema.string(),
actionTypeId: schema.maybe(schema.string()),
}),
{ defaultValue: [] }
),
Expand Down
11 changes: 4 additions & 7 deletions x-pack/plugins/alerting/server/routes/update_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ describe('updateRuleRoute', () => {
group: mockedAlert.actions[0].group,
id: mockedAlert.actions[0].id,
params: mockedAlert.actions[0].params,
connector_type_id: mockedAlert.actions[0].actionTypeId,
},
],
};
Expand Down Expand Up @@ -110,7 +109,6 @@ describe('updateRuleRoute', () => {
"data": Object {
"actions": Array [
Object {
"actionTypeId": "test",
"group": "default",
"id": "2",
"params": Object {
Expand Down Expand Up @@ -205,11 +203,10 @@ describe('updateRuleRoute', () => {

alertsClient.update.mockRejectedValue(new AlertTypeDisabledError('Fail', 'license_invalid'));

const [context, req, res] = mockHandlerArguments(
{ alertsClient },
{ params: {}, body: updateRequest },
['ok', 'forbidden']
);
const [context, req, res] = mockHandlerArguments({ alertsClient }, { params: {}, body: {} }, [
'ok',
'forbidden',
]);

await handler(context, req, res);

Expand Down
14 changes: 2 additions & 12 deletions x-pack/plugins/alerting/server/routes/update_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { schema } from '@kbn/config-schema';
import { IRouter } from 'kibana/server';
import { ILicenseState, AlertTypeDisabledError, validateDurationSchema } from '../lib';
import { AlertAction, AlertNotifyWhenType } from '../../common';
import { AlertNotifyWhenType } from '../../common';
import { UpdateOptions } from '../alerts_client';
import {
verifyAccessAndContext,
Expand Down Expand Up @@ -41,29 +41,19 @@ const bodySchema = schema.object({
group: schema.string(),
id: schema.string(),
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }),
connector_type_id: schema.string(),
}),
{ defaultValue: [] }
),
notify_when: schema.string({ validate: validateNotifyWhenType }),
});

const rewriteBodyReqActions: RewriteRequestCase<AlertAction> = ({
connector_type_id: actionTypeId,
...rest
}) => ({
actionTypeId,
...rest,
});

const rewriteBodyReq: RewriteRequestCase<UpdateOptions<AlertTypeParams>> = (result) => {
const { notify_when: notifyWhen, actions, ...rest } = result.data;
const { notify_when: notifyWhen, ...rest } = result.data;
return {
...result,
data: {
...rest,
notifyWhen,
actions: actions.map((action) => rewriteBodyReqActions(action)),
},
};
};
Expand Down

0 comments on commit 4f07713

Please sign in to comment.