fix(services): allow deletion of channel if there's more than 1 channel

This commit is contained in:
Divlo 2022-03-01 08:53:20 +01:00
parent f74cf25a68
commit d3a777c82a
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
6 changed files with 28 additions and 4 deletions

View File

@ -2,7 +2,7 @@ name: 'Analyze'
on: on:
push: push:
branches: [master, develop] branches: [develop]
pull_request: pull_request:
branches: [master, develop] branches: [master, develop]

View File

@ -2,7 +2,7 @@ name: 'Build'
on: on:
push: push:
branches: [master, develop] branches: [develop]
pull_request: pull_request:
branches: [master, develop] branches: [master, develop]

View File

@ -2,7 +2,7 @@ name: 'Lint'
on: on:
push: push:
branches: [master, develop] branches: [develop]
pull_request: pull_request:
branches: [master, develop] branches: [master, develop]

View File

@ -2,7 +2,7 @@ name: 'Test'
on: on:
push: push:
branches: [master, develop] branches: [develop]
pull_request: pull_request:
branches: [master, develop] branches: [master, develop]

View File

@ -8,6 +8,7 @@ describe('DELETE /channels/[channelId]', () => {
it('succeeds', async () => { it('succeeds', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample) prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(memberExample) prismaMock.member.findFirst.mockResolvedValue(memberExample)
prismaMock.channel.count.mockResolvedValue(2)
prismaMock.channel.delete.mockResolvedValue(channelExample) prismaMock.channel.delete.mockResolvedValue(channelExample)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
const response = await application.inject({ const response = await application.inject({
@ -24,6 +25,21 @@ describe('DELETE /channels/[channelId]', () => {
expect(responseJson.guildId).toEqual(channelExample.guildId) 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 () => { it('fails if the channel is not found', async () => {
prismaMock.channel.findUnique.mockResolvedValue(null) prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()

View File

@ -61,6 +61,14 @@ export const deleteChannelService: FastifyPluginAsync = async (fastify) => {
if (!member.isOwner) { if (!member.isOwner) {
throw fastify.httpErrors.badRequest('You should be a member owner') 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({ const channel = await prisma.channel.delete({
where: { id: channelId } where: { id: channelId }
}) })