fix(services): allow deletion of channel if there's more than 1 channel
This commit is contained in:
parent
f74cf25a68
commit
d3a777c82a
2
.github/workflows/analyze.yml
vendored
2
.github/workflows/analyze.yml
vendored
@ -2,7 +2,7 @@ name: 'Analyze'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
branches: [develop]
|
||||
pull_request:
|
||||
branches: [master, develop]
|
||||
|
||||
|
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -2,7 +2,7 @@ name: 'Build'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
branches: [develop]
|
||||
pull_request:
|
||||
branches: [master, develop]
|
||||
|
||||
|
2
.github/workflows/lint.yml
vendored
2
.github/workflows/lint.yml
vendored
@ -2,7 +2,7 @@ name: 'Lint'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
branches: [develop]
|
||||
pull_request:
|
||||
branches: [master, develop]
|
||||
|
||||
|
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -2,7 +2,7 @@ name: 'Test'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
branches: [develop]
|
||||
pull_request:
|
||||
branches: [master, develop]
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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 }
|
||||
})
|
||||
|
Reference in New Issue
Block a user