Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -15,12 +15,30 @@ describe('delete', () => {

beforeEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();

clientArgs.services.attachmentService.getter.get.mockResolvedValue(mockCaseComments[0]);
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it('refreshes when deleting', async () => {
await deleteComment({ caseID: 'mock-id-1', attachmentID: 'mock-comment-1' }, clientArgs);

expect(clientArgs.services.attachmentService.bulkDelete).toHaveBeenCalledWith({
attachmentIds: ['mock-comment-1'],
refresh: true,
});
});

describe('Alerts', () => {
const commentSO = mockCaseComments[0];
const alertsSO = mockCaseComments[3];
clientArgs.services.attachmentService.getter.get.mockResolvedValue(alertsSO);

beforeEach(() => {
clientArgs.services.attachmentService.getter.get.mockResolvedValue(alertsSO);
});

it('delete alerts correctly', async () => {
await deleteComment({ caseID: 'mock-id-4', attachmentID: 'mock-comment-4' }, clientArgs);
Expand All @@ -38,10 +56,41 @@ describe('delete', () => {
expect(clientArgs.services.alertsService.removeCaseIdFromAlerts).not.toHaveBeenCalledWith();
});
});

describe('Attachment stats', () => {
it('updates attachment stats correctly', async () => {
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map([
[
'mock-id-1',
{
userComments: 2,
alerts: 2,
},
],
])
);

await deleteComment({ caseID: 'mock-id-1', attachmentID: 'mock-comment-1' }, clientArgs);

const args = clientArgs.services.caseService.patchCase.mock.calls[0][0];

expect(args.updatedAttributes.total_comments).toEqual(2);
expect(args.updatedAttributes.total_alerts).toEqual(2);
expect(args.updatedAttributes.updated_at).toBeDefined();
expect(args.updatedAttributes.updated_by).toEqual({
email: 'damaged_raccoon@elastic.co',
full_name: 'Damaged Raccoon',
profile_uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0',
username: 'damaged_raccoon',
});
});
});
});

describe('deleteAll', () => {
const clientArgs = createCasesClientMockArgs();

const getAllCaseCommentsResponse = {
saved_objects: mockCaseComments.map((so) => ({ ...so, score: 0 })),
total: mockCaseComments.length,
Expand All @@ -51,13 +100,36 @@ describe('delete', () => {

beforeEach(() => {
jest.clearAllMocks();
});
jest.resetAllMocks();

clientArgs.services.attachmentService.getter.get.mockResolvedValue(mockCaseComments[0]);
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);

describe('Alerts', () => {
clientArgs.services.caseService.getAllCaseComments.mockResolvedValue(
getAllCaseCommentsResponse
);
});

it('refreshes when deleting', async () => {
await deleteAll({ caseID: 'mock-id-1' }, clientArgs);

expect(clientArgs.services.attachmentService.bulkDelete).toHaveBeenCalledWith({
attachmentIds: [
'mock-comment-1',
'mock-comment-2',
'mock-comment-3',
'mock-comment-4',
'mock-comment-5',
'mock-comment-6',
'mock-comment-7',
],
refresh: true,
});
});

describe('Alerts', () => {
it('delete alerts correctly', async () => {
await deleteAll({ caseID: 'mock-id-4' }, clientArgs);

Expand All @@ -82,5 +154,35 @@ describe('delete', () => {
expect(clientArgs.services.alertsService.removeCaseIdFromAlerts).not.toHaveBeenCalledWith();
});
});

describe('Attachment stats', () => {
it('updates attachment stats correctly', async () => {
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map([
[
'mock-id-1',
{
userComments: 0,
alerts: 0,
},
],
])
);

await deleteAll({ caseID: 'mock-id-1' }, clientArgs);

const args = clientArgs.services.caseService.patchCase.mock.calls[0][0];

expect(args.updatedAttributes.total_comments).toEqual(0);
expect(args.updatedAttributes.total_alerts).toEqual(0);
expect(args.updatedAttributes.updated_at).toBeDefined();
expect(args.updatedAttributes.updated_by).toEqual({
email: 'damaged_raccoon@elastic.co',
full_name: 'Damaged Raccoon',
profile_uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0',
username: 'damaged_raccoon',
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ export async function deleteAll(

await attachmentService.bulkDelete({
attachmentIds: comments.saved_objects.map((so) => so.id),
refresh: false,
refresh: true,
});

await updateCaseAttachmentStats({
caseService: clientArgs.services.caseService,
attachmentService: clientArgs.services.attachmentService,
caseId: caseID,
user,
});

await userActionService.creator.bulkCreateAttachmentDeletion({
Expand Down Expand Up @@ -115,7 +122,14 @@ export async function deleteComment(

await attachmentService.bulkDelete({
attachmentIds: [attachmentID],
refresh: false,
refresh: true,
});

await updateCaseAttachmentStats({
caseService: clientArgs.services.caseService,
attachmentService: clientArgs.services.attachmentService,
caseId: caseID,
user,
});

// we only want to store the fields related to the original request of the attachment, not fields like
Expand Down Expand Up @@ -163,3 +177,42 @@ const handleAlerts = async ({ alertsService, attachments, caseId }: HandleAlerts
const alerts = getAlertInfoFromComments(alertAttachments);
await alertsService.removeCaseIdFromAlerts({ alerts, caseId });
};

interface UpdateCaseAttachmentStats {
caseService: CasesClientArgs['services']['caseService'];
attachmentService: CasesClientArgs['services']['attachmentService'];
caseId: string;
user: CasesClientArgs['user'];
}

const updateCaseAttachmentStats = async ({
caseService,
attachmentService,
caseId,
user,
}: UpdateCaseAttachmentStats) => {
const originalCase = await caseService.getCase({
id: caseId,
});

const date = new Date().toISOString();

const attachmentStats = await attachmentService.getter.getCaseAttatchmentStats({
caseIds: [caseId],
});

const totalComments = attachmentStats.get(caseId)?.userComments ?? 0;
const totalAlerts = attachmentStats.get(caseId)?.alerts ?? 0;

await caseService.patchCase({
originalCase,
caseId,
updatedAttributes: {
updated_at: date,
updated_by: user,
total_comments: totalComments,
total_alerts: totalAlerts,
},
refresh: false,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('bulkGet', () => {
unauthorized: [],
});

clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);

beforeEach(() => {
jest.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const bulkGet = async (
operation: Operations.bulkGetCases,
});

const commentTotals = await attachmentService.getter.getCaseCommentStats({
const commentTotals = await attachmentService.getter.getCaseAttatchmentStats({
caseIds: authorizedCases.map((theCase) => theCase.id),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ describe('update', () => {
saved_objects: [{ ...mockCases[0], attributes: { assignees: cases.cases[0].assignees } }],
});

clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it('notifies an assignee', async () => {
Expand Down Expand Up @@ -437,7 +439,9 @@ describe('update', () => {
per_page: 10,
page: 1,
});
clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it(`does not throw error when category is non empty string less than ${MAX_CATEGORY_LENGTH} characters`, async () => {
Expand Down Expand Up @@ -571,7 +575,9 @@ describe('update', () => {
per_page: 10,
page: 1,
});
clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it(`does not throw error when title is non empty string less than ${MAX_TITLE_LENGTH} characters`, async () => {
Expand Down Expand Up @@ -706,7 +712,9 @@ describe('update', () => {
per_page: 10,
page: 1,
});
clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it(`does not throw error when description is non empty string less than ${MAX_DESCRIPTION_LENGTH} characters`, async () => {
Expand Down Expand Up @@ -848,7 +856,7 @@ describe('update', () => {
const caseCommentsStats = new Map();
caseCommentsStats.set(mockCases[0].id, { userComments: 1, alerts: 2 });
caseCommentsStats.set(mockCases[1].id, { userComments: 3, alerts: 4 });
clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
caseCommentsStats
);
});
Expand Down Expand Up @@ -972,7 +980,9 @@ describe('update', () => {
]
`);

expect(clientArgs.services.attachmentService.getter.getCaseCommentStats).toHaveBeenCalledWith(
expect(
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats
).toHaveBeenCalledWith(
expect.objectContaining({
caseIds: [mockCases[0].id, mockCases[1].id],
})
Expand All @@ -992,7 +1002,9 @@ describe('update', () => {
per_page: 10,
page: 1,
});
clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it('does not throw error when tags array is empty', async () => {
Expand Down Expand Up @@ -1197,7 +1209,9 @@ describe('update', () => {
customFields: defaultCustomFieldsConfiguration,
},
]);
clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it('can update customFields', async () => {
Expand Down Expand Up @@ -1587,7 +1601,7 @@ describe('update', () => {

beforeEach(() => {
jest.clearAllMocks();
clientArgsMock.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(
clientArgsMock.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});
Expand Down Expand Up @@ -1807,7 +1821,7 @@ describe('update', () => {
per_page: 10,
page: 1,
});
clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});
Expand Down Expand Up @@ -1915,7 +1929,9 @@ describe('update', () => {
saved_objects: mockCases,
});

clientArgs.services.attachmentService.getter.getCaseCommentStats.mockResolvedValue(new Map());
clientArgs.services.attachmentService.getter.getCaseAttatchmentStats.mockResolvedValue(
new Map()
);
});

it('calculates metrics correctly', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ export const bulkUpdate = async (
alertsService,
});

const commentsMap = await attachmentService.getter.getCaseCommentStats({
const commentsMap = await attachmentService.getter.getCaseAttatchmentStats({
caseIds,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const getCasesByAlertID = async (
return [];
}

const commentStats = await attachmentService.getter.getCaseCommentStats({
const commentStats = await attachmentService.getter.getCaseAttatchmentStats({
caseIds,
});

Expand Down
Loading