fix(services): allow deletion of channel if there's more than 1 channel
This commit is contained in:
@ -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