Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,27 @@ describe('CaseCommentModel', () => {
})
).rejects.toThrow();
});

it('throws if trying to add a unified (v2) event to a closed case', async () => {
clientArgs.services.caseService.getCase.mockResolvedValue(closedCase);

const modelForClosedCase = await CaseCommentModel.create(closedCase.id, clientArgs);

const unifiedEventComment = {
type: SECURITY_EVENT_ATTACHMENT_TYPE,
owner: SECURITY_SOLUTION_OWNER,
attachmentId: 'event-id-1',
metadata: { index: 'idx-1' },
};

await expect(
modelForClosedCase.createComment({
id: 'comment-1',
commentReq: unifiedEventComment as never,
createdDate,
})
).rejects.toThrow('Event cannot be attached to a closed case');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ import {
isAlertAttachmentType,
isEventAttachmentType,
} from '../../../common/utils/attachments';
import type {
AlertAttachmentPayload,
AttachmentAttributes,
Case,
EventAttachmentPayload,
UserCommentAttachmentPayload,
import type { AttachmentAttributes, Case UserCommentAttachmentPayload,
Comment thread
christineweng marked this conversation as resolved.
Outdated
} from '../../../common/types/domain';
import {
CaseRt,
Expand Down Expand Up @@ -489,13 +484,6 @@ export class CaseCommentModel {
return dedupedAttachments;
}

private getAttachmentsByType<
T extends AttachmentType,
R = T extends AttachmentType.event ? AlertAttachmentPayload[] : EventAttachmentPayload[]
>(attachments: AttachmentRequestV2[], attachmentType: T): R {
return attachments.filter((attachment) => attachment.type === attachmentType) as R;
}

private async validateCreateCommentRequest(req: Array<AttachmentRequestV2>) {
if (this.caseInfo.attributes.status === CaseStatuses.closed) {
const hasAlertsInRequest = req.some((a) => isAlertAttachmentType(a.type));
Expand All @@ -504,8 +492,9 @@ export class CaseCommentModel {
throw Boom.badRequest('Alert cannot be attached to a closed case');
}

const eventAttachments = this.getAttachmentsByType(req, AttachmentType.event);
const hasEventsInRequest = eventAttachments.length > 0;
// `isEventAttachmentType` matches both the legacy `event` type and the unified
// `security.event` type — a type-only match here would miss unified events.
const hasEventsInRequest = req.some((a) => isEventAttachmentType(a.type));

if (hasEventsInRequest) {
throw Boom.badRequest('Event cannot be attached to a closed case');
Expand Down
Loading