Skip to content

Commit

Permalink
feat: always send join message when channel is changed for source (#2109
Browse files Browse the repository at this point in the history
)

- with last change it was not sending if bot was part of slack channel
already
  • Loading branch information
capJavert authored Aug 7, 2024
1 parent 95e887b commit c9e27a7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
52 changes: 49 additions & 3 deletions __tests__/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>),
WebClient: function () {
Expand Down Expand Up @@ -66,9 +70,7 @@ jest.mock('@slack/web-api', () => ({
}),
},
chat: {
postMessage: jest.fn().mockResolvedValue({
ok: true,
}),
postMessage: slackPostMessage,
},
};
},
Expand All @@ -89,6 +91,7 @@ beforeAll(async () => {

beforeEach(async () => {
loggedUser = undefined;
jest.clearAllMocks();
});

afterAll(() => disposeGraphQLTesting(state));
Expand Down Expand Up @@ -274,6 +277,7 @@ describe('slack integration', () => {
);

expect(res.errors).toBeFalsy();
expect(slackPostMessage).toHaveBeenCalledTimes(1);
});

it('should update channel for source', async () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/schema/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,11 @@ export const resolvers: IResolvers<unknown, BaseContext> = 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';

Expand Down

0 comments on commit c9e27a7

Please sign in to comment.