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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.5.2",
"version": "1.5.3",
"main": "index.ts",
"license": "BUSL-1.1",
"scripts": {
Expand Down
23 changes: 10 additions & 13 deletions src/resolvers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ module.exports = {
async bulkUpdateAssignee(_obj, { input }, { factories, user, ...context }) {
const { projectId, eventIds, assignee } = input;
const { validEventIds, invalidEventIds } = parseBulkEventIds(eventIds);
let assigneeData = null;

if (validEventIds.length === 0) {
return {
Expand All @@ -322,6 +323,8 @@ module.exports = {
throw new UserInputError('assignee not found');
}

assigneeData = userExists;

const project = await factories.projectsFactory.findById(projectId);
const workspace = await factories.workspacesFactory.findById(project.workspaceId);
const assigneeExistsInWorkspace = await workspace.getMemberInfo(assignee);
Expand All @@ -338,19 +341,13 @@ module.exports = {
};

if (assignee && resultWithInvalid.updatedEventIds.length > 0) {
factories.usersFactory.dataLoaders.userById.load(assignee)
.then((assigneeData) => {
fireAndForgetAssigneeNotifications({
assigneeData,
eventIds: resultWithInvalid.updatedEventIds,
projectId,
assigneeId: assignee,
whoAssignedId: user.id,
});
})
.catch((error) => {
console.error('Failed to load assignee data for bulk notifications', error);
});
fireAndForgetAssigneeNotifications({
assigneeData,
eventIds: resultWithInvalid.updatedEventIds,
projectId,
assigneeId: assignee,
whoAssignedId: user.id,
});
}

return resultWithInvalid;
Expand Down
20 changes: 17 additions & 3 deletions src/resolvers/helpers/bulkEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function fireAndForgetAssigneeNotifications({
assigneeId,
whoAssignedId,
}) {
if (!assigneeData) {
console.error('Failed to enqueue assignee notifications: assignee data is empty');

return;
}

Promise.allSettled(eventIds.map(eventId => sendPersonalNotification(assigneeData, {
type: 'assignee',
payload: {
Expand All @@ -28,9 +34,17 @@ function fireAndForgetAssigneeNotifications({
whoAssignedId,
eventId,
},
}))).catch((error) => {
console.error('Failed to enqueue assignee notifications', error);
});
})))
.then((results) => {
const failedResults = results.filter(result => result.status === 'rejected');

if (failedResults.length > 0) {
console.error('Failed to enqueue assignee notifications', failedResults);
}
})
.catch((error) => {
console.error('Failed to enqueue assignee notifications', error);
});
}

/**
Expand Down
86 changes: 86 additions & 0 deletions test/resolvers/bulk-events-helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import '../../src/env-test';

jest.mock('../../src/utils/personalNotifications', () => ({
__esModule: true,
default: jest.fn().mockResolvedValue(undefined),
}));

import sendPersonalNotification from '../../src/utils/personalNotifications';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { fireAndForgetAssigneeNotifications } = require('../../src/resolvers/helpers/bulkEvents') as {
fireAndForgetAssigneeNotifications: (args: {
assigneeData: Record<string, unknown> | null;
eventIds: string[];
projectId: string;
assigneeId: string;
whoAssignedId: string;
}) => void;
};

describe('fireAndForgetAssigneeNotifications', () => {
let consoleErrorSpy: jest.SpyInstance;

beforeEach(() => {
jest.clearAllMocks();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
});

afterEach(() => {
consoleErrorSpy.mockRestore();
});

it('should enqueue personal notification for each event id', async () => {
fireAndForgetAssigneeNotifications({
assigneeData: { id: 'assignee-1', email: 'assignee@hawk.so' },
eventIds: [ 'e-1', 'e-2' ],
projectId: 'p-1',
assigneeId: 'assignee-1',
whoAssignedId: 'u-1',
});

await Promise.resolve();

expect(sendPersonalNotification).toHaveBeenCalledTimes(2);
expect(sendPersonalNotification).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ id: 'assignee-1' }),
{
type: 'assignee',
payload: {
assigneeId: 'assignee-1',
projectId: 'p-1',
whoAssignedId: 'u-1',
eventId: 'e-1',
},
}
);
expect(sendPersonalNotification).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ id: 'assignee-1' }),
{
type: 'assignee',
payload: {
assigneeId: 'assignee-1',
projectId: 'p-1',
whoAssignedId: 'u-1',
eventId: 'e-2',
},
}
);
});

it('should not call personal notifications when assignee data is empty', () => {
fireAndForgetAssigneeNotifications({
assigneeData: null,
eventIds: [ 'e-1' ],
projectId: 'p-1',
assigneeId: 'assignee-1',
whoAssignedId: 'u-1',
});

expect(sendPersonalNotification).not.toHaveBeenCalled();
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to enqueue assignee notifications: assignee data is empty'
);
});
});
14 changes: 14 additions & 0 deletions test/resolvers/event-bulk-update-assignee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jest.mock('../../src/resolvers/helpers/eventsFactory', () => ({
}));

import getEventsFactory from '../../src/resolvers/helpers/eventsFactory';
import sendPersonalNotification from '../../src/utils/personalNotifications';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const eventResolvers = require('../../src/resolvers/event') as {
EventsMutations: {
Expand Down Expand Up @@ -90,6 +91,19 @@ describe('EventsMutations.bulkUpdateAssignee', () => {
[ '507f1f77bcf86cd799439011' ],
'assignee-1'
);
expect(sendPersonalNotification).toHaveBeenCalledTimes(1);
expect(sendPersonalNotification).toHaveBeenCalledWith(
expect.objectContaining({ id: 'assignee-1' }),
expect.objectContaining({
type: 'assignee',
payload: expect.objectContaining({
assigneeId: 'assignee-1',
projectId: 'p1',
whoAssignedId: 'u1',
eventId: '507f1f77bcf86cd799439011',
}),
})
);
});

it('should validate ids on resolver level and merge invalid ids into failedEventIds', async () => {
Expand Down