Skip to content

Commit

Permalink
Merge pull request #681 from Hexastack/fix/subscriber-handove-event
Browse files Browse the repository at this point in the history
fix: handover/handback lifecycle hook
  • Loading branch information
marrouchi authored Feb 5, 2025
2 parents 2afb813 + a607247 commit a9a86ab
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 16 deletions.
73 changes: 72 additions & 1 deletion api/src/chat/repositories/subscriber.repository.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
Expand Down Expand Up @@ -52,6 +52,7 @@ describe('SubscriberRepository', () => {
let allSubscribers: Subscriber[];
let allAttachments: Attachment[];
let subscribersWithPopulatedFields: SubscriberFull[];
let eventEmitter: EventEmitter2;

beforeAll(async () => {
const module = await Test.createTestingModule({
Expand Down Expand Up @@ -94,6 +95,7 @@ describe('SubscriberRepository', () => {
allUsers.find(({ id }) => subscriber.assignedTo === id) || null,
avatar: allAttachments.find(({ id }) => subscriber.avatar === id) || null,
}));
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
});

afterEach(jest.clearAllMocks);
Expand Down Expand Up @@ -155,4 +157,73 @@ describe('SubscriberRepository', () => {
);
});
});

describe('updateOne', () => {
it('should execute preUpdate hook and emit events on assignedTo change', async () => {
// Arrange: Set up a mock subscriber
const oldSubscriber = {
...subscriberFixtures[0], // Mocked existing subscriber
assignedTo: null,
} as Subscriber;

const updates = { assignedTo: '9'.repeat(24) }; // Change assigned user;

jest
.spyOn(subscriberRepository, 'findOne')
.mockResolvedValue(oldSubscriber);
jest.spyOn(eventEmitter, 'emit');

await subscriberRepository.updateOne(oldSubscriber.id, updates);

expect(eventEmitter.emit).toHaveBeenNthCalledWith(
3,
'hook:subscriber:assign',
expect.anything(),
expect.anything(),
);
expect(eventEmitter.emit).toHaveBeenNthCalledWith(
4,
'hook:analytics:passation',
expect.anything(),
true, // Because assignedTo has changed
);
});

it('should not emit events if assignedTo remains unchanged', async () => {
const oldSubscriber = {
...subscriberFixtures[0],
assignedTo: '8'.repeat(24),
} as Subscriber;

const updates = { assignedTo: '8'.repeat(24) }; // Same user;

jest
.spyOn(subscriberRepository, 'findOne')
.mockResolvedValue(oldSubscriber);
jest.spyOn(eventEmitter, 'emit');

await subscriberRepository.updateOne(oldSubscriber.id, updates);

expect(eventEmitter.emit).not.toHaveBeenCalledWith(
'hook:subscriber:assign',
expect.anything(),
expect.anything(),
);
expect(eventEmitter.emit).not.toHaveBeenCalledWith(
'hook:analytics:passation',
expect.anything(),
expect.anything(),
);
});

it('should throw an error if the subscriber does not exist', async () => {
jest.spyOn(subscriberRepository, 'findOne').mockResolvedValue(null);

await expect(
subscriberRepository.updateOne('0'.repeat(24), {
$set: { assignedTo: 'user-456' },
}),
).rejects.toThrow();
});
});
});
33 changes: 18 additions & 15 deletions api/src/chat/repositories/subscriber.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,29 @@ export class SubscriberRepository extends BaseRepository<
): Promise<void> {
const subscriberUpdates: SubscriberUpdateDto = updates?.['$set'];

const oldSubscriber = await this.findOne(criteria);
if ('assignedTo' in subscriberUpdates) {
// In case of a handover or handback, emit events
const oldSubscriber = await this.findOne(criteria);

if (!oldSubscriber) {
throw new Error('Something went wrong: subscriber does not exist');
}

if (subscriberUpdates.assignedTo !== oldSubscriber?.assignedTo) {
this.eventEmitter.emit(
'hook:subscriber:assign',
subscriberUpdates,
oldSubscriber,
);
if (!oldSubscriber) {
throw new Error('Something went wrong: subscriber does not exist');
}

if (!(subscriberUpdates.assignedTo && oldSubscriber?.assignedTo)) {
if (subscriberUpdates.assignedTo !== oldSubscriber?.assignedTo) {
this.eventEmitter.emit(
'hook:analytics:passation',
'hook:subscriber:assign',
subscriberUpdates,
oldSubscriber,
!!subscriberUpdates?.assignedTo,
);
subscriberUpdates.assignedAt = new Date();

if (!(subscriberUpdates.assignedTo && oldSubscriber?.assignedTo)) {
this.eventEmitter.emit(
'hook:analytics:passation',
oldSubscriber,
!!subscriberUpdates?.assignedTo,
);
subscriberUpdates.assignedAt = new Date();
}
}
}
}
Expand Down

0 comments on commit a9a86ab

Please sign in to comment.