Skip to content

Commit

Permalink
Addressing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Apr 26, 2023
1 parent 01bcd62 commit dcf9277
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 37 deletions.
14 changes: 12 additions & 2 deletions x-pack/plugins/cases/server/common/types/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ export interface AttachmentRequestAttributes {
type: string;
alertId?: string | string[];
index?: string | string[];
rule?: Record<string, unknown>;
rule?: {
id: string | null;
name: string | null;
};
comment?: string;
actions?: Record<string, unknown>;
actions?: {
targets: Array<{
hostname: string;
endpointId: string;
}>;
type: string;
};
externalReferenceMetadata?: Record<string, unknown> | null;
externalReferenceAttachmentTypeId?: string;
externalReferenceStorage?: {
type: string;
soType?: string;
};
persistableStateAttachmentState?: Record<string, unknown>;
persistableStateAttachmentTypeId?: string;
}

export type AttachmentPersistedAttributes = AttachmentRequestAttributes &
Expand Down
15 changes: 0 additions & 15 deletions x-pack/plugins/cases/server/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import type {
CommentRequest,
CommentRequestActionsType,
CommentRequestAlertType,
CommentRequestExternalReferenceSOType,
CommentRequestUserType,
CommentResponse,
CommentsResponse,
Expand All @@ -45,7 +44,6 @@ import {
CaseStatuses,
CommentType,
ConnectorTypes,
ExternalReferenceStorageType,
ExternalReferenceSORt,
FileAttachmentMetadataRt,
} from '../../common/api';
Expand All @@ -55,7 +53,6 @@ import {
getLensVisualizations,
} from '../../common/utils/markdown_plugins/utils';
import { dedupAssignees } from '../client/cases/utils';
import type { AttachmentRequestAttributes } from './types/attachments';
import type { CaseSavedObjectTransformed, CaseTransformedAttributes } from './types/case';

/**
Expand Down Expand Up @@ -255,18 +252,6 @@ export const isCommentRequestTypeAlert = (
return context.type === CommentType.alert;
};

/**
* A type narrowing function for external reference so attachments.
*/
export const isCommentRequestTypeExternalReferenceSO = (
context: Partial<AttachmentRequestAttributes>
): context is CommentRequestExternalReferenceSOType => {
return (
context.type === CommentType.externalReference &&
context.externalReferenceStorage?.type === ExternalReferenceStorageType.savedObject
);
};

/**
* A type narrowing function for file attachments.
*/
Expand Down
14 changes: 4 additions & 10 deletions x-pack/plugins/cases/server/services/attachments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import type {
} from '@kbn/core/server';

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type {
CommentAttributes as AttachmentAttributes,
CommentAttributesWithoutRefs as AttachmentAttributesWithoutRefs,
} from '../../../common/api';
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';
Expand Down Expand Up @@ -102,10 +99,7 @@ export class AttachmentService {

const combinedFilter = combineFilters([attachmentFilter, filter]);

const response = await this.context.unsecuredSavedObjectsClient.find<
AttachmentAttributes,
Agg
>({
const response = await this.context.unsecuredSavedObjectsClient.find<unknown, Agg>({
type: CASE_COMMENT_SAVED_OBJECT,
hasReference: { type: CASE_SAVED_OBJECT, id: caseId },
page: 1,
Expand Down Expand Up @@ -254,7 +248,7 @@ export class AttachmentService {
const shouldUpdateRefs = extractedReferences.length > 0 || didDeleteOperation;

const res =
await this.context.unsecuredSavedObjectsClient.update<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.update<AttachmentPersistedAttributes>(
CASE_COMMENT_SAVED_OBJECT,
attachmentId,
extractedAttributes,
Expand Down Expand Up @@ -291,7 +285,7 @@ export class AttachmentService {
);

const res =
await this.context.unsecuredSavedObjectsClient.bulkUpdate<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.bulkUpdate<AttachmentPersistedAttributes>(
comments.map((c) => {
const {
attributes: extractedAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export class AttachmentGetter {
`Attempting to retrieve attachments associated with cases: [${caseIds}]`
);

const finder = this.context.unsecuredSavedObjectsClient.createPointInTimeFinder({
// We are intentionally not adding the type here because we only want to interact with the id and this function
// should not use the attributes
const finder = this.context.unsecuredSavedObjectsClient.createPointInTimeFinder<unknown>({
type: CASE_COMMENT_SAVED_OBJECT,
hasReference: caseIds.map((id) => ({ id, type: CASE_SAVED_OBJECT })),
sortField: 'created_at',
Expand Down
23 changes: 15 additions & 8 deletions x-pack/plugins/cases/server/services/so_references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type {
CommentAttributesNoSO,
CommentAttributes,
CommentPatchAttributes,
CommentAttributesWithoutRefs,
} from '../../common/api';
import type { PersistableStateAttachmentTypeRegistry } from '../attachment_framework/persistable_state_registry';
import {
Expand All @@ -27,11 +26,13 @@ import type {
AttachmentPersistedAttributes,
AttachmentRequestAttributes,
} from '../common/types/attachments';
import { isCommentRequestTypeExternalReferenceSO } from '../common/utils';
import { isCommentRequestTypeExternalReferenceSO } from './type_guards';
import type { PartialField } from '../types';
import { SOReferenceExtractor } from './so_reference_extractor';

export const getAttachmentSOExtractor = (attachment: Partial<AttachmentRequestAttributes>) => {
export const getAttachmentSOExtractor = (
attachment: Partial<AttachmentRequestAttributes>
): SOReferenceExtractor => {
const fieldsToExtract = [];

if (isCommentRequestTypeExternalReferenceSO(attachment)) {
Expand Down Expand Up @@ -72,7 +73,7 @@ const hasAttributes = <T>(savedObject: OptionalAttributes<T>): savedObject is Sa
export const injectAttachmentSOAttributesFromRefs = (
savedObject: SavedObject<AttachmentPersistedAttributes>,
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry
) => {
): SavedObject<CommentAttributes> => {
const soExtractor = getAttachmentSOExtractor(savedObject.attributes);
const so = soExtractor.populateFieldsFromReferences<CommentAttributes>(savedObject);
const injectedAttributes = injectPersistableReferencesToSO(so.attributes, so.references, {
Expand All @@ -84,9 +85,9 @@ export const injectAttachmentSOAttributesFromRefs = (

export const injectAttachmentSOAttributesFromRefsForPatch = (
updatedAttributes: CommentPatchAttributes,
savedObject: SavedObjectsUpdateResponse<CommentAttributesWithoutRefs>,
savedObject: SavedObjectsUpdateResponse<AttachmentPersistedAttributes>,
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry
) => {
): SavedObjectsUpdateResponse<CommentAttributes> => {
const soExtractor = getAttachmentSOExtractor(savedObject.attributes);
const so = soExtractor.populateFieldsFromReferencesForPatch<CommentAttributes>({
dataBeforeRequest: updatedAttributes,
Expand All @@ -107,11 +108,17 @@ export const injectAttachmentSOAttributesFromRefsForPatch = (
} as SavedObjectsUpdateResponse<CommentAttributes>;
};

interface ExtractionResults {
attributes: AttachmentPersistedAttributes;
references: SavedObjectReference[];
didDeleteOperation: boolean;
}

export const extractAttachmentSORefsFromAttributes = (
attributes: CommentAttributes | CommentPatchAttributes,
references: SavedObjectReference[],
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry
) => {
): ExtractionResults => {
const soExtractor = getAttachmentSOExtractor(attributes);

const {
Expand All @@ -135,5 +142,5 @@ export const extractAttachmentSORefsFromAttributes = (
};
};

export const getUniqueReferences = (references: SavedObjectReference[]) =>
export const getUniqueReferences = (references: SavedObjectReference[]): SavedObjectReference[] =>
uniqWith(references, isEqual);
22 changes: 22 additions & 0 deletions x-pack/plugins/cases/server/services/type_guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 type { CommentRequestExternalReferenceSOType } from '../../common/api';
import { CommentType, ExternalReferenceStorageType } from '../../common/api';
import type { AttachmentRequestAttributes } from '../common/types/attachments';

/**
* A type narrowing function for external reference saved object attachments.
*/
export const isCommentRequestTypeExternalReferenceSO = (
context: Partial<AttachmentRequestAttributes>
): context is CommentRequestExternalReferenceSOType => {
return (
context.type === CommentType.externalReference &&
context.externalReferenceStorage?.type === ExternalReferenceStorageType.savedObject
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
PUSH_CONNECTOR_ID_REFERENCE_NAME,
} from '../../common/constants';
import { findConnectorIdReference } from '../transform';
import { isCommentRequestTypeExternalReferenceSO } from '../../common/utils';
import { isCommentRequestTypeExternalReferenceSO } from '../type_guards';
import type { PersistableStateAttachmentTypeRegistry } from '../../attachment_framework/persistable_state_registry';
import { injectPersistableReferencesToSO } from '../../attachment_framework/so_references';
import { findReferenceId } from '../../common/references';
Expand Down

0 comments on commit dcf9277

Please sign in to comment.