From e4e4e7c6766048fa9c04271d5b737e386dcbadaf Mon Sep 17 00:00:00 2001 From: Jan Monschke Date: Thu, 26 Sep 2024 14:17:05 +0200 Subject: [PATCH] Migrate more types --- .../output/kibana.serverless.staging.yaml | 31 +++++--- oas_docs/output/kibana.staging.yaml | 31 +++++--- .../get_timelines/get_timelines_route.gen.ts | 10 +-- .../get_timelines_route.schema.yaml | 5 -- .../common/api/timeline/model/api.ts | 45 ++--------- .../api/timeline/model/components.gen.ts | 34 ++++++-- .../api/timeline/model/components.schema.yaml | 38 +++++++-- ...imeline_api_2023_10_31.bundled.schema.yaml | 30 ++++--- ...imeline_api_2023_10_31.bundled.schema.yaml | 30 ++++--- .../public/timelines/containers/api.ts | 12 +-- .../timeline/saved_object/timelines/index.ts | 79 ++++++++++--------- .../timelines/pick_saved_timeline.ts | 6 +- 12 files changed, 189 insertions(+), 162 deletions(-) diff --git a/oas_docs/output/kibana.serverless.staging.yaml b/oas_docs/output/kibana.serverless.staging.yaml index fc5d2a4e629f8..326d38b61519e 100644 --- a/oas_docs/output/kibana.serverless.staging.yaml +++ b/oas_docs/output/kibana.serverless.staging.yaml @@ -16962,11 +16962,6 @@ paths: required: - timeline - totalCount - - defaultTimelineCount - - templateTimelineCount - - favoriteCount - - elasticTemplateTimelineCount - - customTemplateTimelineCount description: Indicates that the (template) Timelines were found and returned. '400': content: @@ -31598,6 +31593,18 @@ components: updatedBy: nullable: true type: string + Security_Timeline_API_SavedTimelineWithSavedObjectId: + allOf: + - $ref: '#/components/schemas/Security_Timeline_API_SavedTimeline' + - type: object + properties: + savedObjectId: + type: string + version: + type: string + required: + - savedObjectId + - version Security_Timeline_API_SerializedFilterQueryResult: type: object properties: @@ -31647,6 +31654,8 @@ components: Security_Timeline_API_TimelineResponse: allOf: - $ref: '#/components/schemas/Security_Timeline_API_SavedTimeline' + - $ref: >- + #/components/schemas/Security_Timeline_API_SavedTimelineWithSavedObjectId - type: object properties: eventIdToNoteIds: @@ -31674,13 +31683,6 @@ components: $ref: '#/components/schemas/Security_Timeline_API_PinnedEvent' nullable: true type: array - savedObjectId: - type: string - version: - type: string - required: - - savedObjectId - - version Security_Timeline_API_TimelineSavedToReturnObject: allOf: - $ref: '#/components/schemas/Security_Timeline_API_SavedTimeline' @@ -31689,22 +31691,27 @@ components: eventIdToNoteIds: items: $ref: '#/components/schemas/Security_Timeline_API_Note' + nullable: true type: array noteIds: items: type: string + nullable: true type: array notes: items: $ref: '#/components/schemas/Security_Timeline_API_Note' + nullable: true type: array pinnedEventIds: items: type: string + nullable: true type: array pinnedEventsSaveObject: items: $ref: '#/components/schemas/Security_Timeline_API_PinnedEvent' + nullable: true type: array savedObjectId: type: string diff --git a/oas_docs/output/kibana.staging.yaml b/oas_docs/output/kibana.staging.yaml index 46851940678ed..74770826b33ac 100644 --- a/oas_docs/output/kibana.staging.yaml +++ b/oas_docs/output/kibana.staging.yaml @@ -21051,11 +21051,6 @@ paths: required: - timeline - totalCount - - defaultTimelineCount - - templateTimelineCount - - favoriteCount - - elasticTemplateTimelineCount - - customTemplateTimelineCount description: Indicates that the (template) Timelines were found and returned. '400': content: @@ -39607,6 +39602,18 @@ components: updatedBy: nullable: true type: string + Security_Timeline_API_SavedTimelineWithSavedObjectId: + allOf: + - $ref: '#/components/schemas/Security_Timeline_API_SavedTimeline' + - type: object + properties: + savedObjectId: + type: string + version: + type: string + required: + - savedObjectId + - version Security_Timeline_API_SerializedFilterQueryResult: type: object properties: @@ -39656,6 +39663,8 @@ components: Security_Timeline_API_TimelineResponse: allOf: - $ref: '#/components/schemas/Security_Timeline_API_SavedTimeline' + - $ref: >- + #/components/schemas/Security_Timeline_API_SavedTimelineWithSavedObjectId - type: object properties: eventIdToNoteIds: @@ -39683,13 +39692,6 @@ components: $ref: '#/components/schemas/Security_Timeline_API_PinnedEvent' nullable: true type: array - savedObjectId: - type: string - version: - type: string - required: - - savedObjectId - - version Security_Timeline_API_TimelineSavedToReturnObject: allOf: - $ref: '#/components/schemas/Security_Timeline_API_SavedTimeline' @@ -39698,22 +39700,27 @@ components: eventIdToNoteIds: items: $ref: '#/components/schemas/Security_Timeline_API_Note' + nullable: true type: array noteIds: items: type: string + nullable: true type: array notes: items: $ref: '#/components/schemas/Security_Timeline_API_Note' + nullable: true type: array pinnedEventIds: items: type: string + nullable: true type: array pinnedEventsSaveObject: items: $ref: '#/components/schemas/Security_Timeline_API_PinnedEvent' + nullable: true type: array savedObjectId: type: string diff --git a/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.gen.ts b/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.gen.ts index e2828beb876c3..714746875599b 100644 --- a/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.gen.ts +++ b/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.gen.ts @@ -43,9 +43,9 @@ export type GetTimelinesResponse = z.infer; export const GetTimelinesResponse = z.object({ timeline: z.array(TimelineResponse), totalCount: z.number(), - defaultTimelineCount: z.number(), - templateTimelineCount: z.number(), - favoriteCount: z.number(), - elasticTemplateTimelineCount: z.number(), - customTemplateTimelineCount: z.number(), + defaultTimelineCount: z.number().optional(), + templateTimelineCount: z.number().optional(), + favoriteCount: z.number().optional(), + elasticTemplateTimelineCount: z.number().optional(), + customTemplateTimelineCount: z.number().optional(), }); diff --git a/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.schema.yaml b/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.schema.yaml index 73afeac39bba3..7d83f60af3e02 100644 --- a/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.schema.yaml +++ b/x-pack/plugins/security_solution/common/api/timeline/get_timelines/get_timelines_route.schema.yaml @@ -78,11 +78,6 @@ paths: required: [ timeline, totalCount, - defaultTimelineCount, - templateTimelineCount, - favoriteCount, - elasticTemplateTimelineCount, - customTemplateTimelineCount, ] properties: timeline: diff --git a/x-pack/plugins/security_solution/common/api/timeline/model/api.ts b/x-pack/plugins/security_solution/common/api/timeline/model/api.ts index cd83969f16f65..7ad3052b7ad6f 100644 --- a/x-pack/plugins/security_solution/common/api/timeline/model/api.ts +++ b/x-pack/plugins/security_solution/common/api/timeline/model/api.ts @@ -27,11 +27,14 @@ import { RowRendererId, RowRendererIdEnum, SavedTimeline, + SavedTimelineWithSavedObjectId, SortDirection, SortFieldTimeline, SortFieldTimelineEnum, TemplateTimelineType, TemplateTimelineTypeEnum, + TimelineErrorResponse, + TimelineResponse, TimelineSavedToReturnObject, TimelineStatus, TimelineStatusEnum, @@ -54,10 +57,13 @@ export { RowRendererId, RowRendererIdEnum, SavedTimeline, + SavedTimelineWithSavedObjectId, SortDirection, SortFieldTimeline, SortFieldTimelineEnum, TemplateTimelineType, + TimelineErrorResponse, + TimelineResponse, TemplateTimelineTypeEnum, TimelineSavedToReturnObject, TimelineStatus, @@ -325,16 +331,6 @@ export const SavedTimelineRuntimeType = runtimeTypes.partial({ savedSearchId: unionWithNullType(runtimeTypes.string), }); -export type SavedTimelineWithSavedObjectId = SavedTimeline & { - savedObjectId?: string | null; -}; - -/** - * This type represents a timeline type stored in a saved object that does not include any fields that reference - * other saved objects. - */ -export type TimelineWithoutExternalRefs = Omit; - export const TimelineSavedToReturnObjectRuntimeType = runtimeTypes.intersection([ SavedTimelineRuntimeType, runtimeTypes.type({ @@ -354,26 +350,6 @@ export type TimelineSavedObject = runtimeTypes.TypeOf< typeof TimelineSavedToReturnObjectRuntimeType >; -const responseTimelines = runtimeTypes.type({ - timeline: runtimeTypes.array(TimelineSavedToReturnObjectRuntimeType), - totalCount: runtimeTypes.number, -}); - -export type ResponseTimelines = runtimeTypes.TypeOf; - -export const TimelineErrorResponseType = runtimeTypes.union([ - runtimeTypes.type({ - status_code: runtimeTypes.number, - message: runtimeTypes.string, - }), - runtimeTypes.type({ - statusCode: runtimeTypes.number, - message: runtimeTypes.string, - }), -]); - -export type TimelineErrorResponse = runtimeTypes.TypeOf; - /** * Import/export timelines */ @@ -533,12 +509,3 @@ export interface SortTimeline { sortField: SortFieldTimeline; sortOrder: SortDirection; } - -export interface GetAllTimelineVariables { - pageInfo: PageInfoTimeline; - search?: Maybe; - sort?: Maybe; - onlyUserFavorite?: Maybe; - timelineType?: Maybe; - status?: Maybe; -} diff --git a/x-pack/plugins/security_solution/common/api/timeline/model/components.gen.ts b/x-pack/plugins/security_solution/common/api/timeline/model/components.gen.ts index 1b1290195afbc..c8d966aad9934 100644 --- a/x-pack/plugins/security_solution/common/api/timeline/model/components.gen.ts +++ b/x-pack/plugins/security_solution/common/api/timeline/model/components.gen.ts @@ -215,6 +215,14 @@ export const SavedTimeline = z.object({ updatedBy: z.string().nullable().optional(), }); +export type SavedTimelineWithSavedObjectId = z.infer; +export const SavedTimelineWithSavedObjectId = SavedTimeline.merge( + z.object({ + savedObjectId: z.string(), + version: z.string(), + }) +); + export type BareNote = z.infer; export const BareNote = z.object({ eventId: z.string().nullable().optional(), @@ -253,15 +261,13 @@ export const PinnedEvent = BarePinnedEvent.merge( ); export type TimelineResponse = z.infer; -export const TimelineResponse = SavedTimeline.merge( +export const TimelineResponse = SavedTimeline.merge(SavedTimelineWithSavedObjectId).merge( z.object({ eventIdToNoteIds: z.array(Note).nullable().optional(), notes: z.array(Note).nullable().optional(), noteIds: z.array(z.string()).nullable().optional(), pinnedEventIds: z.array(z.string()).nullable().optional(), pinnedEventsSaveObject: z.array(PinnedEvent).nullable().optional(), - savedObjectId: z.string(), - version: z.string(), }) ); @@ -270,11 +276,11 @@ export const TimelineSavedToReturnObject = SavedTimeline.merge( z.object({ savedObjectId: z.string(), version: z.string(), - eventIdToNoteIds: z.array(Note).optional(), - notes: z.array(Note).optional(), - noteIds: z.array(z.string()).optional(), - pinnedEventIds: z.array(z.string()).optional(), - pinnedEventsSaveObject: z.array(PinnedEvent).optional(), + eventIdToNoteIds: z.array(Note).nullable().optional(), + notes: z.array(Note).nullable().optional(), + noteIds: z.array(z.string()).nullable().optional(), + pinnedEventIds: z.array(z.string()).nullable().optional(), + pinnedEventsSaveObject: z.array(PinnedEvent).nullable().optional(), }) ); @@ -396,6 +402,18 @@ export const ImportTimelineResult = z.object({ .optional(), }); +export type TimelineErrorResponse = z.infer; +export const TimelineErrorResponse = z.union([ + z.object({ + message: z.string(), + status_code: z.number(), + }), + z.object({ + message: z.string(), + statusCode: z.number(), + }), +]); + export type ExportedTimelines = z.infer; export const ExportedTimelines = SavedTimeline.merge( z.object({ diff --git a/x-pack/plugins/security_solution/common/api/timeline/model/components.schema.yaml b/x-pack/plugins/security_solution/common/api/timeline/model/components.schema.yaml index eeed806a1dd18..1335062586e06 100644 --- a/x-pack/plugins/security_solution/common/api/timeline/model/components.schema.yaml +++ b/x-pack/plugins/security_solution/common/api/timeline/model/components.schema.yaml @@ -153,11 +153,21 @@ components: updatedBy: type: string nullable: true - TimelineResponse: + SavedTimelineWithSavedObjectId: allOf: - $ref: '#/components/schemas/SavedTimeline' - type: object required: [savedObjectId, version] + properties: + savedObjectId: + type: string + version: + type: string + TimelineResponse: + allOf: + - $ref: '#/components/schemas/SavedTimeline' + - $ref: '#/components/schemas/SavedTimelineWithSavedObjectId' + - type: object properties: eventIdToNoteIds: type: array @@ -184,10 +194,6 @@ components: nullable: true items: $ref: '#/components/schemas/PinnedEvent' - savedObjectId: - type: string - version: - type: string ResolvedTimeline: type: object required: [timeline, outcome] @@ -662,22 +668,27 @@ components: type: string eventIdToNoteIds: type: array + nullable: true items: $ref: '#/components/schemas/Note' notes: type: array + nullable: true items: $ref: '#/components/schemas/Note' noteIds: type: array + nullable: true items: type: string pinnedEventIds: type: array + nullable: true items: type: string pinnedEventsSaveObject: type: array + nullable: true items: $ref: '#/components/schemas/PinnedEvent' ImportTimelineResult: @@ -705,6 +716,23 @@ components: type: string status_code: type: number + TimelineErrorResponse: + oneOf: + - type: object + required: [message, status_code] + properties: + message: + type: string + status_code: + type: number + - type: object + required: [message, statusCode] + properties: + message: + type: string + statusCode: + type: number + ExportedTimelines: allOf: - $ref: '#/components/schemas/SavedTimeline' diff --git a/x-pack/plugins/security_solution/docs/openapi/ess/security_solution_timeline_api_2023_10_31.bundled.schema.yaml b/x-pack/plugins/security_solution/docs/openapi/ess/security_solution_timeline_api_2023_10_31.bundled.schema.yaml index 40fd9869d88e2..b8db839779fb6 100644 --- a/x-pack/plugins/security_solution/docs/openapi/ess/security_solution_timeline_api_2023_10_31.bundled.schema.yaml +++ b/x-pack/plugins/security_solution/docs/openapi/ess/security_solution_timeline_api_2023_10_31.bundled.schema.yaml @@ -892,11 +892,6 @@ paths: required: - timeline - totalCount - - defaultTimelineCount - - templateTimelineCount - - favoriteCount - - elasticTemplateTimelineCount - - customTemplateTimelineCount description: Indicates that the (template) Timelines were found and returned. '400': content: @@ -1499,6 +1494,18 @@ components: updatedBy: nullable: true type: string + SavedTimelineWithSavedObjectId: + allOf: + - $ref: '#/components/schemas/SavedTimeline' + - type: object + properties: + savedObjectId: + type: string + version: + type: string + required: + - savedObjectId + - version SerializedFilterQueryResult: type: object properties: @@ -1548,6 +1555,7 @@ components: TimelineResponse: allOf: - $ref: '#/components/schemas/SavedTimeline' + - $ref: '#/components/schemas/SavedTimelineWithSavedObjectId' - type: object properties: eventIdToNoteIds: @@ -1575,13 +1583,6 @@ components: $ref: '#/components/schemas/PinnedEvent' nullable: true type: array - savedObjectId: - type: string - version: - type: string - required: - - savedObjectId - - version TimelineSavedToReturnObject: allOf: - $ref: '#/components/schemas/SavedTimeline' @@ -1590,22 +1591,27 @@ components: eventIdToNoteIds: items: $ref: '#/components/schemas/Note' + nullable: true type: array noteIds: items: type: string + nullable: true type: array notes: items: $ref: '#/components/schemas/Note' + nullable: true type: array pinnedEventIds: items: type: string + nullable: true type: array pinnedEventsSaveObject: items: $ref: '#/components/schemas/PinnedEvent' + nullable: true type: array savedObjectId: type: string diff --git a/x-pack/plugins/security_solution/docs/openapi/serverless/security_solution_timeline_api_2023_10_31.bundled.schema.yaml b/x-pack/plugins/security_solution/docs/openapi/serverless/security_solution_timeline_api_2023_10_31.bundled.schema.yaml index f4a7a13a41775..f42cab0247072 100644 --- a/x-pack/plugins/security_solution/docs/openapi/serverless/security_solution_timeline_api_2023_10_31.bundled.schema.yaml +++ b/x-pack/plugins/security_solution/docs/openapi/serverless/security_solution_timeline_api_2023_10_31.bundled.schema.yaml @@ -892,11 +892,6 @@ paths: required: - timeline - totalCount - - defaultTimelineCount - - templateTimelineCount - - favoriteCount - - elasticTemplateTimelineCount - - customTemplateTimelineCount description: Indicates that the (template) Timelines were found and returned. '400': content: @@ -1499,6 +1494,18 @@ components: updatedBy: nullable: true type: string + SavedTimelineWithSavedObjectId: + allOf: + - $ref: '#/components/schemas/SavedTimeline' + - type: object + properties: + savedObjectId: + type: string + version: + type: string + required: + - savedObjectId + - version SerializedFilterQueryResult: type: object properties: @@ -1548,6 +1555,7 @@ components: TimelineResponse: allOf: - $ref: '#/components/schemas/SavedTimeline' + - $ref: '#/components/schemas/SavedTimelineWithSavedObjectId' - type: object properties: eventIdToNoteIds: @@ -1575,13 +1583,6 @@ components: $ref: '#/components/schemas/PinnedEvent' nullable: true type: array - savedObjectId: - type: string - version: - type: string - required: - - savedObjectId - - version TimelineSavedToReturnObject: allOf: - $ref: '#/components/schemas/SavedTimeline' @@ -1590,22 +1591,27 @@ components: eventIdToNoteIds: items: $ref: '#/components/schemas/Note' + nullable: true type: array noteIds: items: type: string + nullable: true type: array notes: items: $ref: '#/components/schemas/Note' + nullable: true type: array pinnedEventIds: items: type: string + nullable: true type: array pinnedEventsSaveObject: items: $ref: '#/components/schemas/PinnedEvent' + nullable: true type: array savedObjectId: type: string diff --git a/x-pack/plugins/security_solution/public/timelines/containers/api.ts b/x-pack/plugins/security_solution/public/timelines/containers/api.ts index 186cebcb6331d..52befd485471d 100644 --- a/x-pack/plugins/security_solution/public/timelines/containers/api.ts +++ b/x-pack/plugins/security_solution/public/timelines/containers/api.ts @@ -5,17 +5,12 @@ * 2.0. */ -import { fold } from 'fp-ts/lib/Either'; -import { identity } from 'fp-ts/lib/function'; -import { pipe } from 'fp-ts/lib/pipeable'; import { isEmpty } from 'lodash'; -import { throwErrors } from '@kbn/cases-plugin/common'; import type { SavedSearch } from '@kbn/saved-search-plugin/common'; import type { CleanDraftTimelinesResponse, - TimelineErrorResponse, // todo TimelineType, PatchTimelineResponse, CreateTimelinesResponse, @@ -25,8 +20,8 @@ import type { } from '../../../common/api/timeline'; import { ImportTimelineResult, + TimelineErrorResponse, TimelineStatusEnum, - TimelineErrorResponseType, // todo PersistFavoriteRouteResponse, TimelineTypeEnum, GetTimelineResponse, @@ -89,10 +84,7 @@ const decodeGetTimelinesResponse = (respTimeline: GetTimelinesResponse) => parseOrThrow(GetTimelinesResponse)(respTimeline); const decodeTimelineErrorResponse = (respTimeline?: TimelineErrorResponse) => - pipe( - TimelineErrorResponseType.decode(respTimeline), - fold(throwErrors(createToasterPlainError), identity) - ); + parseOrThrow(TimelineErrorResponse)(respTimeline); const decodePrepackedTimelineResponse = (respTimeline?: ImportTimelineResult) => parseOrThrow(ImportTimelineResult)(respTimeline); diff --git a/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/index.ts b/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/index.ts index 1ff380319744f..cbde4fb2aaacf 100644 --- a/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/index.ts +++ b/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/index.ts @@ -22,17 +22,14 @@ import type { GetTimelinesResponse, ExportTimelineNotFoundError, PageInfoTimeline, - ResponseTimelines, FavoriteTimelineResponse, - ResponseTimeline, SortTimeline, - TimelineResult, + TimelineResponse, TimelineType, TimelineStatus, ResolvedTimeline, - TimelineSavedObject, SavedTimeline, - TimelineWithoutExternalRefs, + SavedTimelineWithSavedObjectId, } from '../../../../../common/api/timeline'; import { TimelineStatusEnum, TimelineTypeEnum } from '../../../../../common/api/timeline'; import type { SavedObjectTimelineWithoutExternalRefs } from '../../../../../common/types/timeline/saved_object'; @@ -49,11 +46,13 @@ import { timelineFieldsMigrator } from './field_migrator'; export { pickSavedTimeline } from './pick_saved_timeline'; export { convertSavedObjectToSavedTimeline } from './convert_saved_object_to_savedtimeline'; +type TimelineWithoutExternalRefs = Omit; + export const getTimeline = async ( request: FrameworkRequest, timelineId: string, timelineType: TimelineType | null = TimelineTypeEnum.default -): Promise => { +): Promise => { let timelineIdToUse = timelineId; try { if (timelineType === TimelineTypeEnum.template) { @@ -77,7 +76,7 @@ export const getTimeline = async ( export const getTimelineOrNull = async ( frameworkRequest: FrameworkRequest, savedObjectId: string -): Promise => { +): Promise => { let timeline = null; try { timeline = await getTimeline(frameworkRequest, savedObjectId); @@ -90,22 +89,18 @@ export const resolveTimelineOrNull = async ( frameworkRequest: FrameworkRequest, savedObjectId: string ): Promise => { - let resolvedTimeline = null; try { - resolvedTimeline = await resolveSavedTimeline(frameworkRequest, savedObjectId); - // eslint-disable-next-line no-empty - } catch (e) {} - return resolvedTimeline; - // } + const resolvedTimeline = await resolveSavedTimeline(frameworkRequest, savedObjectId); + return resolvedTimeline; + } catch (e) { + return null; + } }; export const getTimelineByTemplateTimelineId = async ( request: FrameworkRequest, templateTimelineId: string -): Promise<{ - totalCount: number; - timeline: TimelineSavedObject[]; -}> => { +): Promise => { const options: SavedObjectsFindOptions = { type: timelineSavedObjectType, filter: `siem-ui-timeline.attributes.templateTimelineId: "${templateTimelineId}"`, @@ -117,7 +112,7 @@ export const getTimelineByTemplateTimelineId = async ( export const getTimelineTemplateOrNull = async ( frameworkRequest: FrameworkRequest, templateTimelineId: string -): Promise => { +): Promise => { let templateTimeline = null; try { templateTimeline = await getTimelineByTemplateTimelineId(frameworkRequest, templateTimelineId); @@ -190,10 +185,7 @@ export const getExistingPrepackagedTimelines = async ( request: FrameworkRequest, countsOnly?: boolean, pageInfo?: PageInfoTimeline -): Promise<{ - totalCount: number; - timeline: TimelineSavedObject[]; -}> => { +): Promise => { const queryPageInfo = countsOnly && pageInfo == null ? { @@ -291,7 +283,7 @@ export const getAllTimeline = async ( export const getDraftTimeline = async ( request: FrameworkRequest, timelineType: TimelineType | null -): Promise => { +): Promise => { const filter = combineFilters([ getTimelineTypeFilter(timelineType ?? null, TimelineStatusEnum.draft), getTimelinesCreatedAndUpdatedByCurrentUser({ request }), @@ -385,13 +377,19 @@ export const persistFavorite = async ( } }; +interface InternalTimelineResponse { + code: number; + message: string; + timeline: TimelineResponse; +} + export const persistTimeline = async ( request: FrameworkRequest, timelineId: string | null, version: string | null, timeline: SavedTimeline, isImmutable?: boolean -): Promise => { +): Promise => { const savedObjectsClient = (await request.context.core).savedObjects.client; const userInfo = isImmutable ? ({ username: 'Elastic' } as AuthenticatedUser) : request.user; try { @@ -414,7 +412,7 @@ export const persistTimeline = async ( timeline: await getSavedTimeline(request, timelineId), }; } else if (getOr(null, 'output.statusCode', err) === 403) { - const timelineToReturn: TimelineResult = { + const timelineToReturn: TimelineResponse = { ...timeline, savedObjectId: '', version: '', @@ -439,7 +437,7 @@ export const createTimeline = async ({ timeline: SavedTimeline; savedObjectsClient: SavedObjectsClientContract; userInfo: AuthenticatedUser | null; -}) => { +}): Promise => { const { transformedFields: migratedAttributes, references } = timelineFieldsMigrator.extractFieldsToReferences({ data: pickSavedTimeline(timelineId, timeline, userInfo), @@ -479,7 +477,7 @@ const updateTimeline = async ({ savedObjectsClient: SavedObjectsClientContract; userInfo: AuthenticatedUser | null; version: string | null; -}) => { +}): Promise => { const rawTimelineSavedObject = await savedObjectsClient.get( timelineSavedObjectType, @@ -516,11 +514,12 @@ export const updatePartialSavedTimeline = async ( timelineId ); - const { transformedFields, references } = - timelineFieldsMigrator.extractFieldsToReferences({ - data: timeline, - existingReferences: currentSavedTimeline.references, - }); + const { transformedFields, references } = timelineFieldsMigrator.extractFieldsToReferences< + Omit + >({ + data: timeline, + existingReferences: currentSavedTimeline.references, + }); const timelineUpdateAttributes = pickSavedTimeline( null, @@ -588,7 +587,7 @@ export const copyTimeline = async ( request: FrameworkRequest, timeline: SavedTimeline, timelineId: string -): Promise => { +): Promise => { const savedObjectsClient = (await request.context.core).savedObjects.client; // Fetch all objects that need to be copied @@ -658,7 +657,10 @@ const resolveBasicSavedTimeline = async (request: FrameworkRequest, timelineId: }; }; -const resolveSavedTimeline = async (request: FrameworkRequest, timelineId: string) => { +const resolveSavedTimeline = async ( + request: FrameworkRequest, + timelineId: string +): Promise => { const userName = request.user?.username ?? UNAUTHENTICATED_USER; const { resolvedTimelineSavedObject, ...resolveAttributes } = await resolveBasicSavedTimeline( @@ -673,7 +675,6 @@ const resolveSavedTimeline = async (request: FrameworkRequest, timelineId: strin ]); const [notes, pinnedEvents, timeline] = timelineWithNotesAndPinnedEvents; - return { timeline: timelineWithReduxProperties(notes, pinnedEvents, timeline, userName), ...resolveAttributes, @@ -742,9 +743,9 @@ export const convertStringToBase64 = (text: string): string => Buffer.from(text) export const timelineWithReduxProperties = ( notes: Note[], pinnedEvents: PinnedEvent[], - timeline: TimelineSavedObject, + timeline: TimelineResponse, userName: string -): TimelineSavedObject => ({ +): TimelineResponse => ({ ...timeline, favorite: timeline.favorite != null && userName != null @@ -789,7 +790,7 @@ export const getSelectedTimelines = async ( ); const timelineObjects: { - timelines: TimelineSavedObject[]; + timelines: TimelineResponse[]; errors: ExportTimelineNotFoundError[]; } = savedObjects.saved_objects.reduce( (acc, savedObject) => { @@ -805,7 +806,7 @@ export const getSelectedTimelines = async ( return { errors: [...acc.errors, savedObject.error], timelines: acc.timelines }; }, { - timelines: [] as TimelineSavedObject[], + timelines: [] as TimelineResponse[], errors: [] as ExportTimelineNotFoundError[], } ); diff --git a/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/pick_saved_timeline.ts b/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/pick_saved_timeline.ts index 50b57bf96bd5b..221e088bdab02 100644 --- a/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/pick_saved_timeline.ts +++ b/x-pack/plugins/security_solution/server/lib/timeline/saved_object/timelines/pick_saved_timeline.ts @@ -9,14 +9,14 @@ import { isEmpty } from 'lodash/fp'; import type { AuthenticatedUser } from '@kbn/security-plugin/common'; import { getUserDisplayName } from '@kbn/user-profile-components'; import { UNAUTHENTICATED_USER } from '../../../../../common/constants'; -import type { SavedTimelineWithSavedObjectId } from '../../../../../common/api/timeline'; +import type { SavedTimeline } from '../../../../../common/api/timeline'; import { TimelineTypeEnum, TimelineStatusEnum } from '../../../../../common/api/timeline'; export const pickSavedTimeline = ( timelineId: string | null, - savedTimeline: SavedTimelineWithSavedObjectId, + savedTimeline: SavedTimeline, userInfo: AuthenticatedUser | null -): SavedTimelineWithSavedObjectId => { +): SavedTimeline => { const dateNow = new Date().valueOf(); if (timelineId == null) {