Skip to content

Commit

Permalink
[Cases] Split attachment types for versioned http apis (#155440)
Browse files Browse the repository at this point in the history
This PR separates the http API io-ts types from the types that are used
in the cases service layer to interact with the saved object client.
This PR is specifically for the attachments it only affects the types
used when interacting with the saved object client and doesn't touch the
transformation logic yet.

Issue: #153726
  • Loading branch information
jonathan-buttner authored Apr 26, 2023
1 parent 6aea93f commit 9f9eb79
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 127 deletions.
48 changes: 48 additions & 0 deletions x-pack/plugins/cases/server/common/types/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 { JsonValue } from '@kbn/utility-types';
import type { User } from './user';

interface AttachmentCommonPersistedAttributes {
created_at: string;
created_by: User;
owner: string;
pushed_at: string | null;
pushed_by: User | null;
updated_at: string | null;
updated_by: User | null;
}

export interface AttachmentRequestAttributes {
type: string;
alertId?: string | string[];
index?: string | string[];
rule?: {
id: string | null;
name: string | null;
};
comment?: string;
actions?: {
targets: Array<{
hostname: string;
endpointId: string;
}>;
type: string;
};
externalReferenceMetadata?: Record<string, JsonValue> | null;
externalReferenceAttachmentTypeId?: string;
externalReferenceStorage?: {
type: string;
soType?: string;
};
persistableStateAttachmentState?: Record<string, JsonValue>;
persistableStateAttachmentTypeId?: string;
}

export type AttachmentPersistedAttributes = AttachmentRequestAttributes &
AttachmentCommonPersistedAttributes;
14 changes: 0 additions & 14 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 Down Expand Up @@ -254,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<CommentRequest>
): context is CommentRequestExternalReferenceSOType => {
return (
context.type === CommentType.externalReference &&
context.externalReferenceStorage?.type === ExternalReferenceStorageType.savedObject
);
};

/**
* A type narrowing function for file attachments.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
} from '@kbn/core/server';
import { mergeSavedObjectMigrationMaps } from '@kbn/core/server';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import type { CommentAttributes } from '../../../common/api';
import { CommentType } from '../../../common/api';
import type { LensMarkdownNode, MarkdownNode } from '../../../common/utils/markdown_plugins/utils';
import {
Expand All @@ -32,6 +31,7 @@ import { GENERATED_ALERT, SUB_CASE_SAVED_OBJECT } from './constants';
import type { PersistableStateAttachmentTypeRegistry } from '../../attachment_framework/persistable_state_registry';
import { getAllPersistableAttachmentMigrations } from './get_all_persistable_attachment_migrations';
import type { PersistableStateAttachmentState } from '../../attachment_framework/types';
import type { AttachmentPersistedAttributes } from '../../common/types/attachments';

interface UnsanitizedComment {
comment: string;
Expand Down Expand Up @@ -71,7 +71,7 @@ export const createCommentsMigrations = (

const persistableStateAttachmentMigrations = mapValues<
MigrateFunctionsObject,
SavedObjectMigrationFn<CommentAttributes>
SavedObjectMigrationFn<AttachmentPersistedAttributes>
>(
getAllPersistableAttachmentMigrations(migrationDeps.persistableStateAttachmentTypeRegistry),
migratePersistableStateAttachments
Expand Down Expand Up @@ -134,8 +134,10 @@ export const createCommentsMigrations = (
};

export const migratePersistableStateAttachments =
(migrate: MigrateFunction): SavedObjectMigrationFn<CommentAttributes, CommentAttributes> =>
(doc: SavedObjectUnsanitizedDoc<CommentAttributes>) => {
(
migrate: MigrateFunction
): SavedObjectMigrationFn<AttachmentPersistedAttributes, AttachmentPersistedAttributes> =>
(doc: SavedObjectUnsanitizedDoc<AttachmentPersistedAttributes>) => {
if (doc.attributes.type !== CommentType.persistableState) {
return doc;
}
Expand Down
78 changes: 19 additions & 59 deletions x-pack/plugins/cases/server/services/attachments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@

import type {
SavedObject,
SavedObjectReference,
SavedObjectsBulkResponse,
SavedObjectsBulkUpdateResponse,
SavedObjectsFindResponse,
SavedObjectsUpdateOptions,
SavedObjectsUpdateResponse,
} from '@kbn/core/server';

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type {
CommentAttributes as AttachmentAttributes,
CommentAttributesWithoutRefs as AttachmentAttributesWithoutRefs,
CommentPatchAttributes as AttachmentPatchAttributes,
} 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 All @@ -32,50 +26,19 @@ import {
injectAttachmentSOAttributesFromRefsForPatch,
} from '../so_references';
import type { SavedObjectFindOptionsKueryNode } from '../../common/types';
import type { IndexRefresh } from '../types';
import type { AttachedToCaseArgs, ServiceContext } from './types';
import type {
AlertsAttachedToCaseArgs,
AttachmentsAttachedToCaseArgs,
BulkCreateAttachments,
BulkUpdateAttachmentArgs,
CountActionsAttachedToCaseArgs,
CreateAttachmentArgs,
DeleteAttachmentArgs,
ServiceContext,
UpdateAttachmentArgs,
} from './types';
import { AttachmentGetter } from './operations/get';

type AlertsAttachedToCaseArgs = AttachedToCaseArgs;

interface AttachmentsAttachedToCaseArgs extends AttachedToCaseArgs {
attachmentType: CommentType;
aggregations: Record<string, estypes.AggregationsAggregationContainer>;
}

interface CountActionsAttachedToCaseArgs extends AttachedToCaseArgs {
aggregations: Record<string, estypes.AggregationsAggregationContainer>;
}

interface DeleteAttachmentArgs extends IndexRefresh {
attachmentIds: string[];
}

interface CreateAttachmentArgs extends IndexRefresh {
attributes: AttachmentAttributes;
references: SavedObjectReference[];
id: string;
}

interface BulkCreateAttachments extends IndexRefresh {
attachments: Array<{
attributes: AttachmentAttributes;
references: SavedObjectReference[];
id: string;
}>;
}

interface UpdateArgs {
attachmentId: string;
updatedAttributes: AttachmentPatchAttributes;
options?: Omit<SavedObjectsUpdateOptions<AttachmentAttributes>, 'upsert'>;
}

export type UpdateAttachmentArgs = UpdateArgs;

interface BulkUpdateAttachmentArgs extends IndexRefresh {
comments: UpdateArgs[];
}
import type { AttachmentPersistedAttributes } from '../../common/types/attachments';

export class AttachmentService {
private readonly _getter: AttachmentGetter;
Expand Down Expand Up @@ -136,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 @@ -207,7 +167,7 @@ export class AttachmentService {
);

const attachment =
await this.context.unsecuredSavedObjectsClient.create<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.create<AttachmentPersistedAttributes>(
CASE_COMMENT_SAVED_OBJECT,
extractedAttributes,
{
Expand All @@ -234,7 +194,7 @@ export class AttachmentService {
try {
this.context.log.debug(`Attempting to bulk create attachments`);
const res =
await this.context.unsecuredSavedObjectsClient.bulkCreate<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.bulkCreate<AttachmentPersistedAttributes>(
attachments.map((attachment) => {
const { attributes: extractedAttributes, references: extractedReferences } =
extractAttachmentSORefsFromAttributes(
Expand Down Expand Up @@ -288,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 @@ -325,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 Expand Up @@ -380,7 +340,7 @@ export class AttachmentService {
try {
this.context.log.debug(`Attempting to find comments`);
const res =
await this.context.unsecuredSavedObjectsClient.find<AttachmentAttributesWithoutRefs>({
await this.context.unsecuredSavedObjectsClient.find<AttachmentPersistedAttributes>({
sortField: defaultSortField,
...options,
type: CASE_COMMENT_SAVED_OBJECT,
Expand Down
Loading

0 comments on commit 9f9eb79

Please sign in to comment.