From 8d026c5fa628d739fb78017d767aa2b60940f268 Mon Sep 17 00:00:00 2001 From: Gerard Soldevila Date: Thu, 3 Nov 2022 11:06:22 +0100 Subject: [PATCH] Address PR remarks --- .../notifications/server/config/config.ts | 20 +++++++++++-- .../server/config/connectors_email_config.ts | 30 ------------------- .../services/connectors_email_service.ts | 16 ++++++---- .../connectors_email_service_provider.ts | 11 +++++-- 4 files changed, 36 insertions(+), 41 deletions(-) delete mode 100644 x-pack/plugins/notifications/server/config/connectors_email_config.ts diff --git a/x-pack/plugins/notifications/server/config/config.ts b/x-pack/plugins/notifications/server/config/config.ts index f2967b6f59fd83..f2dc570adabe98 100644 --- a/x-pack/plugins/notifications/server/config/config.ts +++ b/x-pack/plugins/notifications/server/config/config.ts @@ -5,11 +5,25 @@ * 2.0. */ +import { schema, type TypeOf } from '@kbn/config-schema'; import type { PluginConfigDescriptor } from '@kbn/core/server'; -import { configSchema, type ConnectorsEmailConfigType } from './connectors_email_config'; -export type NotificationsConfigType = ConnectorsEmailConfigType; +export const configSchema = schema.object( + { + connectors: schema.maybe( + schema.object({ + default: schema.maybe( + schema.object({ + email: schema.maybe(schema.string()), + }) + ), + }) + ), + }, + { defaultValue: {} } +); +export type NotificationsConfigType = TypeOf; -export const config: PluginConfigDescriptor = { +export const config: PluginConfigDescriptor = { schema: configSchema, }; diff --git a/x-pack/plugins/notifications/server/config/connectors_email_config.ts b/x-pack/plugins/notifications/server/config/connectors_email_config.ts deleted file mode 100644 index 3bd6ead9451b06..00000000000000 --- a/x-pack/plugins/notifications/server/config/connectors_email_config.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { schema, type TypeOf } from '@kbn/config-schema'; -import type { PluginConfigDescriptor } from '@kbn/core/server'; - -export const configSchema = schema.object( - { - connectors: schema.maybe( - schema.object({ - default: schema.maybe( - schema.object({ - email: schema.maybe(schema.string()), - }) - ), - }) - ), - }, - { defaultValue: {} } -); - -export type ConnectorsEmailConfigType = TypeOf; - -export const config: PluginConfigDescriptor = { - schema: configSchema, -}; diff --git a/x-pack/plugins/notifications/server/services/connectors_email_service.ts b/x-pack/plugins/notifications/server/services/connectors_email_service.ts index 267e12df5c6f59..4647a16ccbec11 100755 --- a/x-pack/plugins/notifications/server/services/connectors_email_service.ts +++ b/x-pack/plugins/notifications/server/services/connectors_email_service.ts @@ -16,6 +16,16 @@ export class ConnectorsEmailService implements EmailService { ) {} async sendPlainTextEmail(params: PlainTextEmail): Promise { + const relatedSavedObjects = params.context?.relatedObjects?.length + ? params.context!.relatedObjects!.map(({ id, type, spaceId }) => { + return { + id, + type, + namespace: spaceId, + }; + }) + : undefined; + const actions = params.to.map((to) => ({ id: this.connectorId, params: { @@ -23,11 +33,7 @@ export class ConnectorsEmailService implements EmailService { subject: params.subject, message: params.message, }, - ...(params.context?.relatedObjects?.length && { - relatedSavedObjects: params.context!.relatedObjects!.map( - ({ id, type, spaceId: namespace }) => ({ id, type, namespace }) - ), - }), + relatedSavedObjects, })); return await this.actionsClient.bulkEnqueueExecution(this.requesterId, actions); } diff --git a/x-pack/plugins/notifications/server/services/connectors_email_service_provider.ts b/x-pack/plugins/notifications/server/services/connectors_email_service_provider.ts index d0666138164744..f034116eb701ce 100755 --- a/x-pack/plugins/notifications/server/services/connectors_email_service_provider.ts +++ b/x-pack/plugins/notifications/server/services/connectors_email_service_provider.ts @@ -14,6 +14,8 @@ import { LicensedEmailService } from './licensed_email_service'; import { ConnectorsEmailService } from './connectors_email_service'; import { PLUGIN_ID } from '../../common'; +const MINIMUM_LICENSE = 'platinum'; + export interface EmailServiceSetupDeps { actions?: PluginSetupContract; licensing?: LicensingPluginSetup; @@ -54,6 +56,7 @@ export class EmailServiceProvider } this.setupSuccessful = true; + this.setupError = ''; } public start(plugins: EmailServiceStartDeps): EmailServiceStart { @@ -68,7 +71,7 @@ export class EmailServiceProvider email = new LicensedEmailService( new ConnectorsEmailService(PLUGIN_ID, emailConnector, unsecuredActionsClient), licensing.license$, - 'platinum', + MINIMUM_LICENSE, this.logger ); } catch (err) { @@ -79,8 +82,10 @@ export class EmailServiceProvider return { isEmailServiceAvailable: () => !!email, getEmailService: () => { - if (email) return email; - throw new Error(this.setupError); + if (!email) { + throw new Error(this.setupError); + } + return email; }, }; }