Skip to content

Commit

Permalink
Using transformed attributes type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed May 1, 2023
1 parent 15ac201 commit d271d41
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/cases/server/common/types/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { JsonValue } from '@kbn/utility-types';
import type { CommentAttributes } from '../../../common/api';
import type { User } from './user';

interface AttachmentCommonPersistedAttributes {
Expand Down Expand Up @@ -46,3 +47,5 @@ export interface AttachmentRequestAttributes {

export type AttachmentPersistedAttributes = AttachmentRequestAttributes &
AttachmentCommonPersistedAttributes;

export type AttachmentTransformedAttributes = CommentAttributes;
18 changes: 11 additions & 7 deletions x-pack/plugins/cases/server/services/attachments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -155,7 +157,7 @@ export class AttachmentService {
references,
id,
refresh,
}: CreateAttachmentArgs): Promise<SavedObject<AttachmentAttributes>> {
}: CreateAttachmentArgs): Promise<SavedObject<AttachmentTransformedAttributes>> {
try {
this.context.log.debug(`Attempting to POST a new comment`);

Expand Down Expand Up @@ -190,7 +192,7 @@ export class AttachmentService {
public async bulkCreate({
attachments,
refresh,
}: BulkCreateAttachments): Promise<SavedObjectsBulkResponse<AttachmentAttributes>> {
}: BulkCreateAttachments): Promise<SavedObjectsBulkResponse<AttachmentTransformedAttributes>> {
try {
this.context.log.debug(`Attempting to bulk create attachments`);
const res =
Expand Down Expand Up @@ -231,7 +233,7 @@ export class AttachmentService {
attachmentId,
updatedAttributes,
options,
}: UpdateAttachmentArgs): Promise<SavedObjectsUpdateResponse<AttachmentAttributes>> {
}: UpdateAttachmentArgs): Promise<SavedObjectsUpdateResponse<AttachmentTransformedAttributes>> {
try {
this.context.log.debug(`Attempting to UPDATE comment ${attachmentId}`);

Expand Down Expand Up @@ -278,7 +280,9 @@ export class AttachmentService {
public async bulkUpdate({
comments,
refresh,
}: BulkUpdateAttachmentArgs): Promise<SavedObjectsBulkUpdateResponse<AttachmentAttributes>> {
}: BulkUpdateAttachmentArgs): Promise<
SavedObjectsBulkUpdateResponse<AttachmentTransformedAttributes>
> {
try {
this.context.log.debug(
`Attempting to UPDATE comments ${comments.map((c) => c.attachmentId).join(', ')}`
Expand Down Expand Up @@ -336,7 +340,7 @@ export class AttachmentService {
options,
}: {
options?: SavedObjectFindOptionsKueryNode;
}): Promise<SavedObjectsFindResponse<AttachmentAttributes>> {
}): Promise<SavedObjectsFindResponse<AttachmentTransformedAttributes>> {
try {
this.context.log.debug(`Attempting to find comments`);
const res =
Expand Down
19 changes: 9 additions & 10 deletions x-pack/plugins/cases/server/services/attachments/operations/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
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,
MAX_ALERTS_PER_CASE,
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,
Expand All @@ -42,7 +41,7 @@ export class AttachmentGetter {

public async bulkGet(
attachmentIds: string[]
): Promise<BulkOptionalAttributes<AttachmentAttributes>> {
): Promise<BulkOptionalAttributes<AttachmentTransformedAttributes>> {
try {
this.context.log.debug(
`Attempting to retrieve attachments with ids: ${attachmentIds.join()}`
Expand Down Expand Up @@ -192,7 +191,7 @@ export class AttachmentGetter {

public async get({
attachmentId,
}: GetAttachmentArgs): Promise<SavedObject<AttachmentAttributes>> {
}: GetAttachmentArgs): Promise<SavedObject<AttachmentTransformedAttributes>> {
try {
this.context.log.debug(`Attempting to GET attachment ${attachmentId}`);
const res = await this.context.unsecuredSavedObjectsClient.get<AttachmentPersistedAttributes>(
Expand Down Expand Up @@ -302,7 +301,7 @@ export class AttachmentGetter {
}: {
caseId: string;
fileIds: string[];
}): Promise<Array<SavedObject<AttachmentAttributes>>> {
}): Promise<Array<SavedObject<AttachmentTransformedAttributes>>> {
try {
this.context.log.debug('Attempting to find file attachments');

Expand Down Expand Up @@ -331,7 +330,7 @@ export class AttachmentGetter {
}
);

const foundAttachments: Array<SavedObject<AttachmentAttributes>> = [];
const foundAttachments: Array<SavedObject<AttachmentTransformedAttributes>> = [];

for await (const attachmentSavedObjects of finder.find()) {
foundAttachments.push(
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/cases/server/services/cases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -66,6 +66,7 @@ import type {
PatchCaseArgs,
PatchCasesArgs,
} from './types';
import type { AttachmentTransformedAttributes } from '../../common/types/attachments';

export class CasesService {
private readonly log: Logger;
Expand Down Expand Up @@ -343,7 +344,7 @@ export class CasesService {
private async getAllComments({
id,
options,
}: FindCommentsArgs): Promise<SavedObjectsFindResponse<CommentAttributes>> {
}: FindCommentsArgs): Promise<SavedObjectsFindResponse<AttachmentTransformedAttributes>> {
try {
this.log.debug(`Attempting to GET all comments internal for id ${JSON.stringify(id)}`);
if (options?.page !== undefined || options?.perPage !== undefined) {
Expand Down Expand Up @@ -378,7 +379,7 @@ export class CasesService {
public async getAllCaseComments({
id,
options,
}: FindCaseCommentsArgs): Promise<SavedObjectsFindResponse<CommentAttributes>> {
}: FindCaseCommentsArgs): Promise<SavedObjectsFindResponse<AttachmentTransformedAttributes>> {
try {
const refs = this.asArray(id).map((caseID) => ({ type: CASE_SAVED_OBJECT, id: caseID }));
if (refs.length <= 0) {
Expand Down
17 changes: 9 additions & 8 deletions x-pack/plugins/cases/server/services/so_references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -56,11 +57,11 @@ type OptionalAttributes<T> = PartialField<SavedObject<T>, 'attributes'>;
export const injectAttachmentAttributesAndHandleErrors = (
savedObject: OptionalAttributes<AttachmentPersistedAttributes>,
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry
): OptionalAttributes<CommentAttributes> => {
): OptionalAttributes<AttachmentTransformedAttributes> => {
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<CommentAttributes>;
return savedObject as OptionalAttributes<AttachmentTransformedAttributes>;
}

return injectAttachmentSOAttributesFromRefs(savedObject, persistableStateAttachmentTypeRegistry);
Expand All @@ -73,9 +74,9 @@ const hasAttributes = <T>(savedObject: OptionalAttributes<T>): savedObject is Sa
export const injectAttachmentSOAttributesFromRefs = (
savedObject: SavedObject<AttachmentPersistedAttributes>,
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry
): SavedObject<CommentAttributes> => {
): SavedObject<AttachmentTransformedAttributes> => {
const soExtractor = getAttachmentSOExtractor(savedObject.attributes);
const so = soExtractor.populateFieldsFromReferences<CommentAttributes>(savedObject);
const so = soExtractor.populateFieldsFromReferences<AttachmentTransformedAttributes>(savedObject);
const injectedAttributes = injectPersistableReferencesToSO(so.attributes, so.references, {
persistableStateAttachmentTypeRegistry,
});
Expand All @@ -87,9 +88,9 @@ export const injectAttachmentSOAttributesFromRefsForPatch = (
updatedAttributes: CommentPatchAttributes,
savedObject: SavedObjectsUpdateResponse<AttachmentPersistedAttributes>,
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry
): SavedObjectsUpdateResponse<CommentAttributes> => {
): SavedObjectsUpdateResponse<AttachmentTransformedAttributes> => {
const soExtractor = getAttachmentSOExtractor(savedObject.attributes);
const so = soExtractor.populateFieldsFromReferencesForPatch<CommentAttributes>({
const so = soExtractor.populateFieldsFromReferencesForPatch<AttachmentTransformedAttributes>({
dataBeforeRequest: updatedAttributes,
dataReturnedFromRequest: savedObject,
});
Expand All @@ -105,7 +106,7 @@ export const injectAttachmentSOAttributesFromRefsForPatch = (
return {
...so,
attributes: { ...so.attributes, ...injectedAttributes },
} as SavedObjectsUpdateResponse<CommentAttributes>;
} as SavedObjectsUpdateResponse<AttachmentTransformedAttributes>;
};

interface ExtractionResults {
Expand Down

0 comments on commit d271d41

Please sign in to comment.