diff --git a/.github/workflows/analyze.yml b/.github/workflows/analyze.yml index 2801756..1bc15ca 100644 --- a/.github/workflows/analyze.yml +++ b/.github/workflows/analyze.yml @@ -2,7 +2,7 @@ name: 'Analyze' on: push: - branches: [master, develop] + branches: [develop] pull_request: branches: [master, develop] diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 516e026..480655a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,7 +2,7 @@ name: 'Build' on: push: - branches: [master, develop] + branches: [develop] pull_request: branches: [master, develop] diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 8071e79..55a04d6 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,7 +2,7 @@ name: 'Lint' on: push: - branches: [master, develop] + branches: [develop] pull_request: branches: [master, develop] diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 94ea20d..03dc0fe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,7 +2,7 @@ name: 'Test' on: push: - branches: [master, develop] + branches: [develop] pull_request: branches: [master, develop] diff --git a/src/services/channels/[channelId]/__test__/delete.test.ts b/src/services/channels/[channelId]/__test__/delete.test.ts index 60d04c6..f025940 100644 --- a/src/services/channels/[channelId]/__test__/delete.test.ts +++ b/src/services/channels/[channelId]/__test__/delete.test.ts @@ -8,6 +8,7 @@ describe('DELETE /channels/[channelId]', () => { it('succeeds', async () => { prismaMock.channel.findUnique.mockResolvedValue(channelExample) prismaMock.member.findFirst.mockResolvedValue(memberExample) + prismaMock.channel.count.mockResolvedValue(2) prismaMock.channel.delete.mockResolvedValue(channelExample) const { accessToken } = await authenticateUserTest() const response = await application.inject({ @@ -24,6 +25,21 @@ describe('DELETE /channels/[channelId]', () => { expect(responseJson.guildId).toEqual(channelExample.guildId) }) + it('fails if there is only one channel', async () => { + prismaMock.channel.findUnique.mockResolvedValue(channelExample) + prismaMock.member.findFirst.mockResolvedValue(memberExample) + prismaMock.channel.count.mockResolvedValue(1) + const { accessToken } = await authenticateUserTest() + const response = await application.inject({ + method: 'DELETE', + url: `/channels/${channelExample.id}`, + headers: { + authorization: `Bearer ${accessToken}` + } + }) + expect(response.statusCode).toEqual(400) + }) + it('fails if the channel is not found', async () => { prismaMock.channel.findUnique.mockResolvedValue(null) const { accessToken } = await authenticateUserTest() diff --git a/src/services/channels/[channelId]/delete.ts b/src/services/channels/[channelId]/delete.ts index e756618..a3951cc 100644 --- a/src/services/channels/[channelId]/delete.ts +++ b/src/services/channels/[channelId]/delete.ts @@ -61,6 +61,14 @@ export const deleteChannelService: FastifyPluginAsync = async (fastify) => { if (!member.isOwner) { throw fastify.httpErrors.badRequest('You should be a member owner') } + const channelCount = await prisma.channel.count({ + where: { guildId: channelCheck.guildId } + }) + if (channelCount <= 1) { + throw fastify.httpErrors.badRequest( + 'The guild should have at least one channel' + ) + } const channel = await prisma.channel.delete({ where: { id: channelId } })