From c9e27a7e97f4ca432c7bda116c9ac94b044f5c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Bari=C4=87?= Date: Wed, 7 Aug 2024 11:37:22 +0200 Subject: [PATCH] feat: always send join message when channel is changed for source (#2109) - with last change it was not sending if bot was part of slack channel already --- __tests__/integrations.ts | 52 +++++++++++++++++++++++++++++++++++--- src/schema/integrations.ts | 4 +++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/__tests__/integrations.ts b/__tests__/integrations.ts index 1be0a3099..01e463d01 100644 --- a/__tests__/integrations.ts +++ b/__tests__/integrations.ts @@ -32,6 +32,10 @@ import { import { SourceMemberRoles } from '../src/roles'; import { addSeconds } from 'date-fns'; +const slackPostMessage = jest.fn().mockResolvedValue({ + ok: true, +}); + jest.mock('@slack/web-api', () => ({ ...(jest.requireActual('@slack/web-api') as Record), WebClient: function () { @@ -66,9 +70,7 @@ jest.mock('@slack/web-api', () => ({ }), }, chat: { - postMessage: jest.fn().mockResolvedValue({ - ok: true, - }), + postMessage: slackPostMessage, }, }; }, @@ -89,6 +91,7 @@ beforeAll(async () => { beforeEach(async () => { loggedUser = undefined; + jest.clearAllMocks(); }); afterAll(() => disposeGraphQLTesting(state)); @@ -274,6 +277,7 @@ describe('slack integration', () => { ); expect(res.errors).toBeFalsy(); + expect(slackPostMessage).toHaveBeenCalledTimes(1); }); it('should update channel for source', async () => { @@ -322,6 +326,8 @@ describe('slack integration', () => { expect(userSourceIntegrationUpdate).toMatchObject({ channelIds: ['2'], }); + + expect(slackPostMessage).toHaveBeenCalledTimes(2); }); it('should not allow connecting source if existing connection is already present', async () => { @@ -407,6 +413,46 @@ describe('slack integration', () => { 'FORBIDDEN', ); }); + + it('should not post join message to channel if already connected', async () => { + loggedUser = '1'; + const userIntegration = await getIntegration({ + type: UserIntegrationType.Slack, + userId: loggedUser, + }); + + const res = await client.mutate( + MUTATION({ + integrationId: userIntegration.id, + channelId: '1', + sourceId: 'squadslack', + }), + ); + + expect(res.errors).toBeFalsy(); + expect(slackPostMessage).toHaveBeenCalledTimes(1); + + const userSourceIntegration = await con + .getRepository(UserSourceIntegration) + .findOneByOrFail({ + userIntegrationId: userIntegration.id, + sourceId: 'squadslack', + }); + expect(userSourceIntegration).toMatchObject({ + channelIds: ['1'], + }); + + const resUpdate = await client.mutate( + MUTATION({ + integrationId: userIntegration.id, + channelId: '1', + sourceId: 'squadslack', + }), + ); + + expect(resUpdate.errors).toBeFalsy(); + expect(slackPostMessage).toHaveBeenCalledTimes(1); + }); }); describe('query sourceIntegration', () => { diff --git a/src/schema/integrations.ts b/src/schema/integrations.ts index 28bb07a6f..def2bb8f8 100644 --- a/src/schema/integrations.ts +++ b/src/schema/integrations.ts @@ -358,7 +358,11 @@ export const resolvers: IResolvers = traceResolvers({ await client.conversations.join({ channel: args.channelId, }); + } + + const channelChanged = existing?.channelIds?.[0] !== args.channelId; + if (channelChanged) { const sourceTypeName = source.type === SourceType.Squad ? 'squad' : 'source';