From d271d41816d1549b0db8cbeba721d8893143c04e Mon Sep 17 00:00:00 2001 From: Jonathan Buttner Date: Mon, 1 May 2023 15:05:03 -0400 Subject: [PATCH] Using transformed attributes type --- .../cases/server/common/types/attachments.ts | 3 +++ .../server/services/attachments/index.ts | 18 +++++++++++------- .../services/attachments/operations/get.ts | 19 +++++++++---------- .../cases/server/services/cases/index.ts | 7 ++++--- .../cases/server/services/so_references.ts | 17 +++++++++-------- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/x-pack/plugins/cases/server/common/types/attachments.ts b/x-pack/plugins/cases/server/common/types/attachments.ts index 55b9990c5fb12..02c84925ef531 100644 --- a/x-pack/plugins/cases/server/common/types/attachments.ts +++ b/x-pack/plugins/cases/server/common/types/attachments.ts @@ -6,6 +6,7 @@ */ import type { JsonValue } from '@kbn/utility-types'; +import type { CommentAttributes } from '../../../common/api'; import type { User } from './user'; interface AttachmentCommonPersistedAttributes { @@ -46,3 +47,5 @@ export interface AttachmentRequestAttributes { export type AttachmentPersistedAttributes = AttachmentRequestAttributes & AttachmentCommonPersistedAttributes; + +export type AttachmentTransformedAttributes = CommentAttributes; diff --git a/x-pack/plugins/cases/server/services/attachments/index.ts b/x-pack/plugins/cases/server/services/attachments/index.ts index e76b93ce0e5b9..2e8788a4021ba 100644 --- a/x-pack/plugins/cases/server/services/attachments/index.ts +++ b/x-pack/plugins/cases/server/services/attachments/index.ts @@ -14,7 +14,6 @@ import type { } from '@kbn/core/server'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import type { CommentAttributes as AttachmentAttributes } from '../../../common/api'; import { CommentType } from '../../../common/api'; import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../../common/constants'; import { buildFilter, combineFilters } from '../../client/utils'; @@ -38,7 +37,10 @@ import type { UpdateAttachmentArgs, } from './types'; import { AttachmentGetter } from './operations/get'; -import type { AttachmentPersistedAttributes } from '../../common/types/attachments'; +import type { + AttachmentPersistedAttributes, + AttachmentTransformedAttributes, +} from '../../common/types/attachments'; export class AttachmentService { private readonly _getter: AttachmentGetter; @@ -155,7 +157,7 @@ export class AttachmentService { references, id, refresh, - }: CreateAttachmentArgs): Promise> { + }: CreateAttachmentArgs): Promise> { try { this.context.log.debug(`Attempting to POST a new comment`); @@ -190,7 +192,7 @@ export class AttachmentService { public async bulkCreate({ attachments, refresh, - }: BulkCreateAttachments): Promise> { + }: BulkCreateAttachments): Promise> { try { this.context.log.debug(`Attempting to bulk create attachments`); const res = @@ -231,7 +233,7 @@ export class AttachmentService { attachmentId, updatedAttributes, options, - }: UpdateAttachmentArgs): Promise> { + }: UpdateAttachmentArgs): Promise> { try { this.context.log.debug(`Attempting to UPDATE comment ${attachmentId}`); @@ -278,7 +280,9 @@ export class AttachmentService { public async bulkUpdate({ comments, refresh, - }: BulkUpdateAttachmentArgs): Promise> { + }: BulkUpdateAttachmentArgs): Promise< + SavedObjectsBulkUpdateResponse + > { try { this.context.log.debug( `Attempting to UPDATE comments ${comments.map((c) => c.attachmentId).join(', ')}` @@ -336,7 +340,7 @@ export class AttachmentService { options, }: { options?: SavedObjectFindOptionsKueryNode; - }): Promise> { + }): Promise> { try { this.context.log.debug(`Attempting to find comments`); const res = diff --git a/x-pack/plugins/cases/server/services/attachments/operations/get.ts b/x-pack/plugins/cases/server/services/attachments/operations/get.ts index 4aa5c65ebdcc1..bac3e5fd40731 100644 --- a/x-pack/plugins/cases/server/services/attachments/operations/get.ts +++ b/x-pack/plugins/cases/server/services/attachments/operations/get.ts @@ -8,7 +8,10 @@ import type { SavedObject } from '@kbn/core/server'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { FILE_SO_TYPE } from '@kbn/files-plugin/common'; -import type { AttachmentPersistedAttributes } from '../../../common/types/attachments'; +import type { + AttachmentPersistedAttributes, + AttachmentTransformedAttributes, +} from '../../../common/types/attachments'; import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT, @@ -16,11 +19,7 @@ import { MAX_DOCS_PER_PAGE, } from '../../../../common/constants'; import { buildFilter, combineFilters } from '../../../client/utils'; -import type { - AttachmentTotals, - AttributesTypeAlerts, - CommentAttributes as AttachmentAttributes, -} from '../../../../common/api'; +import type { AttachmentTotals, AttributesTypeAlerts } from '../../../../common/api'; import { CommentType } from '../../../../common/api'; import type { AlertIdsAggsResult, @@ -42,7 +41,7 @@ export class AttachmentGetter { public async bulkGet( attachmentIds: string[] - ): Promise> { + ): Promise> { try { this.context.log.debug( `Attempting to retrieve attachments with ids: ${attachmentIds.join()}` @@ -192,7 +191,7 @@ export class AttachmentGetter { public async get({ attachmentId, - }: GetAttachmentArgs): Promise> { + }: GetAttachmentArgs): Promise> { try { this.context.log.debug(`Attempting to GET attachment ${attachmentId}`); const res = await this.context.unsecuredSavedObjectsClient.get( @@ -302,7 +301,7 @@ export class AttachmentGetter { }: { caseId: string; fileIds: string[]; - }): Promise>> { + }): Promise>> { try { this.context.log.debug('Attempting to find file attachments'); @@ -331,7 +330,7 @@ export class AttachmentGetter { } ); - const foundAttachments: Array> = []; + const foundAttachments: Array> = []; for await (const attachmentSavedObjects of finder.find()) { foundAttachments.push( diff --git a/x-pack/plugins/cases/server/services/cases/index.ts b/x-pack/plugins/cases/server/services/cases/index.ts index 93c6d610d5539..42fd0f03a25e2 100644 --- a/x-pack/plugins/cases/server/services/cases/index.ts +++ b/x-pack/plugins/cases/server/services/cases/index.ts @@ -27,7 +27,7 @@ import { CASE_SAVED_OBJECT, MAX_DOCS_PER_PAGE, } from '../../../common/constants'; -import type { Case, CommentAttributes, User, CaseStatuses } from '../../../common/api'; +import type { Case, User, CaseStatuses } from '../../../common/api'; import { caseStatuses } from '../../../common/api'; import type { SavedObjectFindOptionsKueryNode } from '../../common/types'; import { defaultSortField, flattenCaseSavedObject } from '../../common/utils'; @@ -66,6 +66,7 @@ import type { PatchCaseArgs, PatchCasesArgs, } from './types'; +import type { AttachmentTransformedAttributes } from '../../common/types/attachments'; export class CasesService { private readonly log: Logger; @@ -343,7 +344,7 @@ export class CasesService { private async getAllComments({ id, options, - }: FindCommentsArgs): Promise> { + }: FindCommentsArgs): Promise> { try { this.log.debug(`Attempting to GET all comments internal for id ${JSON.stringify(id)}`); if (options?.page !== undefined || options?.perPage !== undefined) { @@ -378,7 +379,7 @@ export class CasesService { public async getAllCaseComments({ id, options, - }: FindCaseCommentsArgs): Promise> { + }: FindCaseCommentsArgs): Promise> { try { const refs = this.asArray(id).map((caseID) => ({ type: CASE_SAVED_OBJECT, id: caseID })); if (refs.length <= 0) { diff --git a/x-pack/plugins/cases/server/services/so_references.ts b/x-pack/plugins/cases/server/services/so_references.ts index 8a3cc747c752a..f3d9261d2d375 100644 --- a/x-pack/plugins/cases/server/services/so_references.ts +++ b/x-pack/plugins/cases/server/services/so_references.ts @@ -12,8 +12,8 @@ import type { } from '@kbn/core/server'; import { isEqual, uniqWith } from 'lodash'; import type { - CommentAttributesNoSO, CommentAttributes, + CommentAttributesNoSO, CommentPatchAttributes, } from '../../common/api'; import type { PersistableStateAttachmentTypeRegistry } from '../attachment_framework/persistable_state_registry'; @@ -25,6 +25,7 @@ import { EXTERNAL_REFERENCE_REF_NAME } from '../common/constants'; import type { AttachmentPersistedAttributes, AttachmentRequestAttributes, + AttachmentTransformedAttributes, } from '../common/types/attachments'; import { isCommentRequestTypeExternalReferenceSO } from './type_guards'; import type { PartialField } from '../types'; @@ -56,11 +57,11 @@ type OptionalAttributes = PartialField, 'attributes'>; export const injectAttachmentAttributesAndHandleErrors = ( savedObject: OptionalAttributes, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): OptionalAttributes => { +): OptionalAttributes => { if (!hasAttributes(savedObject)) { // we don't actually have an attributes field here so the type doesn't matter, this cast is to get the types to stop // complaining though - return savedObject as OptionalAttributes; + return savedObject as OptionalAttributes; } return injectAttachmentSOAttributesFromRefs(savedObject, persistableStateAttachmentTypeRegistry); @@ -73,9 +74,9 @@ const hasAttributes = (savedObject: OptionalAttributes): savedObject is Sa export const injectAttachmentSOAttributesFromRefs = ( savedObject: SavedObject, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): SavedObject => { +): SavedObject => { const soExtractor = getAttachmentSOExtractor(savedObject.attributes); - const so = soExtractor.populateFieldsFromReferences(savedObject); + const so = soExtractor.populateFieldsFromReferences(savedObject); const injectedAttributes = injectPersistableReferencesToSO(so.attributes, so.references, { persistableStateAttachmentTypeRegistry, }); @@ -87,9 +88,9 @@ export const injectAttachmentSOAttributesFromRefsForPatch = ( updatedAttributes: CommentPatchAttributes, savedObject: SavedObjectsUpdateResponse, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): SavedObjectsUpdateResponse => { +): SavedObjectsUpdateResponse => { const soExtractor = getAttachmentSOExtractor(savedObject.attributes); - const so = soExtractor.populateFieldsFromReferencesForPatch({ + const so = soExtractor.populateFieldsFromReferencesForPatch({ dataBeforeRequest: updatedAttributes, dataReturnedFromRequest: savedObject, }); @@ -105,7 +106,7 @@ export const injectAttachmentSOAttributesFromRefsForPatch = ( return { ...so, attributes: { ...so.attributes, ...injectedAttributes }, - } as SavedObjectsUpdateResponse; + } as SavedObjectsUpdateResponse; }; interface ExtractionResults {