Skip to content

Commit

Permalink
Create persistable state attachment type
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 29, 2022
1 parent 0f515de commit ec1abfb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
31 changes: 30 additions & 1 deletion x-pack/plugins/cases/common/api/cases/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum CommentType {
alert = 'alert',
actions = 'actions',
externalReference = 'externalReference',
persistableState = 'persistableState',
}

export enum IsolateHostActionType {
Expand Down Expand Up @@ -101,6 +102,13 @@ export const ExternalReferenceSORt = rt.type({
export const ExternalReferenceRt = rt.union([ExternalReferenceNoSORt, ExternalReferenceSORt]);
export const ExternalReferenceWithoutSORefsRt = ExternalReferenceNoSORt;

export const PersistableStateAttachmentRt = rt.type({
type: rt.literal(CommentType.persistableState),
owner: rt.string,
persistableStateAttachmentTypeId: rt.string,
persistableStateAttachmentState: rt.record(rt.string, jsonValueRt),
});

const AttributesTypeUserRt = rt.intersection([ContextTypeUserRt, CommentAttributesBasicRt]);
const AttributesTypeAlertsRt = rt.intersection([AlertCommentRequestRt, CommentAttributesBasicRt]);
const AttributesTypeActionsRt = rt.intersection([
Expand All @@ -118,18 +126,25 @@ const AttributesTypeExternalReferenceWithoutSORefsRt = rt.intersection([
CommentAttributesBasicRt,
]);

const AttributesTypePersistableStateRt = rt.intersection([
PersistableStateAttachmentRt,
CommentAttributesBasicRt,
]);

const CommentAttributesRt = rt.union([
AttributesTypeUserRt,
AttributesTypeAlertsRt,
AttributesTypeActionsRt,
AttributesTypeExternalReferenceRt,
AttributesTypePersistableStateRt,
]);

const CommentAttributesWithoutSORefsRt = rt.union([
AttributesTypeUserRt,
AttributesTypeAlertsRt,
AttributesTypeActionsRt,
AttributesTypeExternalReferenceWithoutSORefsRt,
AttributesTypePersistableStateRt,
]);

export const CommentRequestRt = rt.union([
Expand All @@ -138,6 +153,7 @@ export const CommentRequestRt = rt.union([
ActionsCommentRequestRt,
ExternalReferenceNoSORt,
ExternalReferenceSORt,
PersistableStateAttachmentRt,
]);

export const CommentResponseRt = rt.intersection([
Expand Down Expand Up @@ -180,6 +196,14 @@ export const CommentResponseTypeExternalReferenceRt = rt.intersection([
}),
]);

export const CommentResponseTypePersistableStateRt = rt.intersection([
AttributesTypePersistableStateRt,
rt.type({
id: rt.string,
version: rt.string,
}),
]);

export const AllCommentsResponseRT = rt.array(CommentResponseRt);

export const CommentPatchRequestRt = rt.intersection([
Expand All @@ -204,6 +228,7 @@ export const CommentPatchAttributesRt = rt.intersection([
rt.partial(ActionsCommentRequestRt.props),
rt.partial(ExternalReferenceNoSORt.props),
rt.partial(ExternalReferenceSORt.props),
rt.partial(PersistableStateAttachmentRt.props),
]),
rt.partial(CommentAttributesBasicRt.props),
]);
Expand Down Expand Up @@ -234,10 +259,13 @@ export type BulkCreateCommentRequest = rt.TypeOf<typeof BulkCreateCommentRequest
export type CommentResponse = rt.TypeOf<typeof CommentResponseRt>;
export type CommentResponseUserType = rt.TypeOf<typeof CommentResponseTypeUserRt>;
export type CommentResponseAlertsType = rt.TypeOf<typeof CommentResponseTypeAlertsRt>;
export type CommentResponseActionsType = rt.TypeOf<typeof CommentResponseTypeActionsRt>;
export type CommentResponseTypePersistableState = rt.TypeOf<
typeof CommentResponseTypePersistableStateRt
>;
export type CommentResponseExternalReferenceType = rt.TypeOf<
typeof CommentResponseTypeExternalReferenceRt
>;
export type CommentResponseActionsType = rt.TypeOf<typeof CommentResponseTypeActionsRt>;
export type AllCommentsResponse = rt.TypeOf<typeof AllCommentsResponseRt>;
export type CommentsResponse = rt.TypeOf<typeof CommentsResponseRt>;
export type CommentPatchRequest = rt.TypeOf<typeof CommentPatchRequestRt>;
Expand All @@ -248,3 +276,4 @@ export type CommentRequestActionsType = rt.TypeOf<typeof ActionsCommentRequestRt
export type CommentRequestExternalReferenceType = rt.TypeOf<typeof ExternalReferenceRt>;
export type CommentRequestExternalReferenceSOType = rt.TypeOf<typeof ExternalReferenceSORt>;
export type CommentRequestExternalReferenceNoSOType = rt.TypeOf<typeof ExternalReferenceNoSORt>;
export type CommentRequestPersistableStateType = rt.TypeOf<typeof PersistableStateAttachmentRt>;
7 changes: 7 additions & 0 deletions x-pack/plugins/cases/server/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ExternalReferenceSORt,
CommentRequestExternalReferenceType,
ExternalReferenceWithoutSORefsRt,
PersistableStateAttachmentRt,
} from '../../common/api';
import { combineFilterWithAuthorizationFilter } from '../authorization/utils';
import {
Expand All @@ -37,6 +38,7 @@ import {
isCommentRequestTypeActions,
isCommentRequestTypeExternalReference,
assertUnreachable,
isCommentRequestTypePersistableState,
} from '../common/utils';
import { SavedObjectFindOptionsKueryNode } from '../common/types';

Expand Down Expand Up @@ -91,6 +93,11 @@ export const decodeCommentRequest = (comment: CommentRequest) => {
}
} else if (isCommentRequestTypeExternalReference(comment)) {
decodeExternalReferenceAttachment(comment);
} else if (isCommentRequestTypePersistableState(comment)) {
pipe(
excess(PersistableStateAttachmentRt).decode(comment),
fold(throwErrors(badRequest), identity)
);
} else {
/**
* This assertion ensures that TS will show an error
Expand Down
20 changes: 15 additions & 5 deletions x-pack/plugins/cases/server/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
CommentRequestAlertType,
CommentRequestExternalReferenceSOType,
CommentRequestExternalReferenceType,
CommentRequestPersistableStateType,
CommentRequestUserType,
CommentResponse,
CommentsResponse,
Expand Down Expand Up @@ -210,7 +211,7 @@ export const transformNewComment = ({
};

/**
* A type narrowing function for user comments. Exporting so integration tests can use it.
* A type narrowing function for user comments.
*/
export const isCommentRequestTypeUser = (
context: CommentRequest
Expand All @@ -219,7 +220,7 @@ export const isCommentRequestTypeUser = (
};

/**
* A type narrowing function for actions comments. Exporting so integration tests can use it.
* A type narrowing function for actions comments.
*/
export const isCommentRequestTypeActions = (
context: CommentRequest
Expand All @@ -228,7 +229,7 @@ export const isCommentRequestTypeActions = (
};

/**
* A type narrowing function for alert comments. Exporting so integration tests can use it.
* A type narrowing function for alert comments.
*/
export const isCommentRequestTypeAlert = (
context: CommentRequest
Expand All @@ -237,7 +238,7 @@ export const isCommentRequestTypeAlert = (
};

/**
* A type narrowing function for external reference attachments. Exporting so integration tests can use it.
* A type narrowing function for external reference attachments.
*/
export const isCommentRequestTypeExternalReference = (
context: CommentRequest
Expand All @@ -246,7 +247,7 @@ export const isCommentRequestTypeExternalReference = (
};

/**
* A type narrowing function for external reference so attachments. Exporting so integration tests can use it.
* A type narrowing function for external reference so attachments.
*/
export const isCommentRequestTypeExternalReferenceSO = (
context: Partial<CommentRequest>
Expand All @@ -257,6 +258,15 @@ export const isCommentRequestTypeExternalReferenceSO = (
);
};

/**
* A type narrowing function for persistable state attachments.
*/
export const isCommentRequestTypePersistableState = (
context: CommentRequest
): context is CommentRequestPersistableStateType => {
return context.type === CommentType.persistableState;
};

/**
* Adds the ids and indices to a map of statuses
*/
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/cases/server/saved_object_types/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ export const createCaseCommentSavedObjectType = ({
type: 'object',
enabled: false,
},
persistableStateAttachmentTypeId: {
type: 'keyword',
},
persistableStateAttachmentState: {
dynamic: false,
type: 'object',
enabled: false,
},
pushed_at: {
type: 'date',
},
Expand Down

0 comments on commit ec1abfb

Please sign in to comment.