fix(services): realtime edge cases
This commit is contained in:
@ -6,10 +6,15 @@ import { memberExample } from '../../../../models/Member.js'
|
||||
|
||||
describe('DELETE /channels/[channelId]', () => {
|
||||
it('succeeds', async () => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.count.mockResolvedValue(2)
|
||||
prismaMock.channel.delete.mockResolvedValue(channelExample)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
@ -23,6 +28,7 @@ describe('DELETE /channels/[channelId]', () => {
|
||||
expect(responseJson.id).toEqual(channelExample.id)
|
||||
expect(responseJson.name).toEqual(channelExample.name)
|
||||
expect(responseJson.guildId).toEqual(channelExample.guildId)
|
||||
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
|
||||
})
|
||||
|
||||
it('fails if there is only one channel', async () => {
|
||||
|
@ -6,9 +6,14 @@ import { memberExample } from '../../../../models/Member.js'
|
||||
|
||||
describe('PUT /channels/[channelId]', () => {
|
||||
it('succeeds', async () => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.update.mockResolvedValue(channelExample)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
@ -23,6 +28,7 @@ describe('PUT /channels/[channelId]', () => {
|
||||
expect(responseJson.id).toEqual(channelExample.id)
|
||||
expect(responseJson.name).toEqual(channelExample.name)
|
||||
expect(responseJson.guildId).toEqual(channelExample.guildId)
|
||||
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
|
||||
})
|
||||
|
||||
it('fails if the channel is not found', async () => {
|
||||
|
@ -22,7 +22,10 @@ const deleteServiceSchema: FastifySchema = {
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
200: Type.Object(channelSchema),
|
||||
200: Type.Object({
|
||||
...channelSchema,
|
||||
defaultChannelId: channelSchema.id
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
@ -72,16 +75,26 @@ export const deleteChannelService: FastifyPluginAsync = async (fastify) => {
|
||||
const channel = await prisma.channel.delete({
|
||||
where: { id: channelId }
|
||||
})
|
||||
const defaultChannel = await prisma.channel.findFirst({
|
||||
where: { guildId: member.guildId }
|
||||
})
|
||||
if (defaultChannel == null) {
|
||||
throw fastify.httpErrors.internalServerError()
|
||||
}
|
||||
const item = {
|
||||
...channel,
|
||||
defaultChannelId: defaultChannel.id
|
||||
}
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'channels',
|
||||
guildId: member.guildId,
|
||||
payload: {
|
||||
action: 'delete',
|
||||
item: channel
|
||||
item
|
||||
}
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return channel
|
||||
return item
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -29,7 +29,10 @@ const putServiceSchema: FastifySchema = {
|
||||
params: parametersSchema,
|
||||
body: bodyPutServiceSchema,
|
||||
response: {
|
||||
200: Type.Object(channelSchema),
|
||||
200: Type.Object({
|
||||
...channelSchema,
|
||||
defaultChannelId: channelSchema.id
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
@ -74,16 +77,26 @@ export const putChannelService: FastifyPluginAsync = async (fastify) => {
|
||||
where: { id: channelId },
|
||||
data: { name }
|
||||
})
|
||||
const defaultChannel = await prisma.channel.findFirst({
|
||||
where: { guildId: member.guildId }
|
||||
})
|
||||
if (defaultChannel == null) {
|
||||
throw fastify.httpErrors.internalServerError()
|
||||
}
|
||||
const item = {
|
||||
...channel,
|
||||
defaultChannelId: defaultChannel.id
|
||||
}
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'channels',
|
||||
guildId: member.guildId,
|
||||
payload: {
|
||||
action: 'update',
|
||||
item: channel
|
||||
item
|
||||
}
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return channel
|
||||
return item
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user