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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -8,8 +8,9 @@ describe('DELETE /guilds/[guildId]', () => {
|
||||
it('succeeds and delete the guild', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
isOwner: true
|
||||
})
|
||||
isOwner: true,
|
||||
guild: guildExample
|
||||
} as any)
|
||||
prismaMock.guild.delete.mockResolvedValue(guildExample)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
@ -41,8 +42,9 @@ describe('DELETE /guilds/[guildId]', () => {
|
||||
it('fails if the user is not the owner', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
})
|
||||
isOwner: false,
|
||||
guild: guildExample
|
||||
} as any)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
@ -52,7 +54,7 @@ describe('DELETE /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(403)
|
||||
expect(response.statusCode).toEqual(400)
|
||||
expect(responseJson.message).toEqual('You should be an owner of the guild')
|
||||
})
|
||||
})
|
||||
|
@ -4,14 +4,20 @@ import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
import { guildExample } from '../../../../models/Guild.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
|
||||
describe('GET /guilds/[guildId]', () => {
|
||||
it('succeeds', async () => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
guild: guildExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
@ -25,7 +31,9 @@ describe('GET /guilds/[guildId]', () => {
|
||||
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner)
|
||||
expect(responseJson.member.user.name).toEqual(user.name)
|
||||
expect(responseJson.member.user.email).toBeNull()
|
||||
expect(responseJson.guild.id).toEqual(guildExample.id)
|
||||
expect(responseJson.guild.name).toEqual(guildExample.name)
|
||||
expect(responseJson.guild.defaultChannelId).toEqual(defaultChannelId)
|
||||
})
|
||||
|
||||
it('fails with not found guild', async () => {
|
||||
|
@ -3,9 +3,11 @@ import { authenticateUserTest } from '../../../../__test__/utils/authenticateUse
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { guildExample } from '../../../../models/Guild.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
|
||||
describe('PUT /guilds/[guildId]', () => {
|
||||
it('succeeds and edit the guild', async () => {
|
||||
const defaultChannelId = 5
|
||||
const newName = 'New guild name'
|
||||
const newDescription = 'New guild description'
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
@ -18,6 +20,10 @@ describe('PUT /guilds/[guildId]', () => {
|
||||
name: newName,
|
||||
description: newDescription
|
||||
})
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
@ -34,6 +40,7 @@ describe('PUT /guilds/[guildId]', () => {
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.name).toEqual(newName)
|
||||
expect(responseJson.description).toEqual(newDescription)
|
||||
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
|
||||
})
|
||||
|
||||
it("fails if the guild doesn't exist", async () => {
|
||||
@ -76,7 +83,7 @@ describe('PUT /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(403)
|
||||
expect(response.statusCode).toEqual(400)
|
||||
expect(responseJson.message).toEqual('You should be an owner of the guild')
|
||||
})
|
||||
})
|
||||
|
@ -7,8 +7,13 @@ import { guildExample } from '../../../../../models/Guild.js'
|
||||
|
||||
describe('POST /guilds/[guildId]/channels', () => {
|
||||
it('succeeds', async () => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.create.mockResolvedValue(channelExample)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
@ -23,6 +28,7 @@ describe('POST /guilds/[guildId]/channels', () => {
|
||||
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 member is not found', async () => {
|
||||
|
@ -30,7 +30,10 @@ const postChannelServiceSchema: FastifySchema = {
|
||||
body: bodyPostServiceSchema,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
201: Type.Object(channelSchema),
|
||||
201: Type.Object({
|
||||
...channelSchema,
|
||||
defaultChannelId: channelSchema.id
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
@ -71,16 +74,26 @@ export const postChannelService: FastifyPluginAsync = async (fastify) => {
|
||||
guildId
|
||||
}
|
||||
})
|
||||
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,
|
||||
payload: {
|
||||
action: 'create',
|
||||
item: channel
|
||||
item
|
||||
}
|
||||
})
|
||||
reply.statusCode = 201
|
||||
return channel
|
||||
return item
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -46,23 +46,29 @@ export const deleteGuildByIdService: FastifyPluginAsync = async (fastify) => {
|
||||
}
|
||||
const { guildId } = request.params
|
||||
const member = await prisma.member.findFirst({
|
||||
where: { guildId, userId: request.user.current.id }
|
||||
where: { guildId, userId: request.user.current.id },
|
||||
include: {
|
||||
guild: true
|
||||
}
|
||||
})
|
||||
if (member == null) {
|
||||
if (member == null || member.guild == null) {
|
||||
throw fastify.httpErrors.notFound('Member not found')
|
||||
}
|
||||
if (!member.isOwner) {
|
||||
throw fastify.httpErrors.forbidden(
|
||||
throw fastify.httpErrors.badRequest(
|
||||
'You should be an owner of the guild'
|
||||
)
|
||||
}
|
||||
const guild = await prisma.guild.delete({
|
||||
where: { id: member.guildId }
|
||||
})
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'guilds',
|
||||
guildId: guild.id,
|
||||
payload: { action: 'delete', item: guild }
|
||||
guildId: member.guildId,
|
||||
payload: {
|
||||
action: 'delete',
|
||||
item: member.guild
|
||||
}
|
||||
})
|
||||
const guild = await prisma.guild.delete({
|
||||
where: { id: member.guildId }
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return guild
|
||||
|
@ -7,6 +7,7 @@ import authenticateUser from '../../../tools/plugins/authenticateUser.js'
|
||||
import { guildSchema } from '../../../models/Guild.js'
|
||||
import { memberSchema } from '../../../models/Member.js'
|
||||
import { userPublicWithoutSettingsSchema } from '../../../models/User.js'
|
||||
import { channelSchema } from '../../../models/Channel.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
@ -25,7 +26,10 @@ const getServiceSchema: FastifySchema = {
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
200: Type.Object({
|
||||
guild: Type.Object(guildSchema),
|
||||
guild: Type.Object({
|
||||
...guildSchema,
|
||||
defaultChannelId: channelSchema.id
|
||||
}),
|
||||
member: Type.Object({
|
||||
...memberSchema,
|
||||
user: Type.Object(userPublicWithoutSettingsSchema)
|
||||
@ -76,9 +80,17 @@ export const getGuildMemberByIdService: FastifyPluginAsync = async (
|
||||
if (member == null) {
|
||||
throw fastify.httpErrors.notFound('Member not found')
|
||||
}
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
guild: member.guild,
|
||||
const defaultChannel = await prisma.channel.findFirst({
|
||||
where: { guildId: member.guildId }
|
||||
})
|
||||
if (defaultChannel == null) {
|
||||
throw fastify.httpErrors.internalServerError()
|
||||
}
|
||||
const item = {
|
||||
guild: {
|
||||
...member.guild,
|
||||
defaultChannelId: defaultChannel.id
|
||||
},
|
||||
member: {
|
||||
...member,
|
||||
user: {
|
||||
@ -87,6 +99,8 @@ export const getGuildMemberByIdService: FastifyPluginAsync = async (
|
||||
}
|
||||
}
|
||||
}
|
||||
reply.statusCode = 200
|
||||
return item
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import {
|
||||
maximumImageSize,
|
||||
supportedImageMimetype
|
||||
} from '../../../../tools/configurations/index.js'
|
||||
import { channelSchema } from '../../../../models/Channel.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
@ -31,9 +32,8 @@ const putServiceSchema: FastifySchema = {
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
200: Type.Object({
|
||||
guild: Type.Object({
|
||||
icon: Type.String()
|
||||
})
|
||||
...guildSchema,
|
||||
defaultChannelId: channelSchema.id
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
@ -75,12 +75,24 @@ export const putGuildIconById: FastifyPluginAsync = async (fastify) => {
|
||||
where: { id: guildId },
|
||||
data: { icon: file.pathToStoreInDatabase }
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
guild: {
|
||||
icon: file.pathToStoreInDatabase
|
||||
}
|
||||
const defaultChannel = await prisma.channel.findFirst({
|
||||
where: { guildId: guild.id }
|
||||
})
|
||||
if (defaultChannel == null) {
|
||||
throw fastify.httpErrors.internalServerError()
|
||||
}
|
||||
const item = {
|
||||
...guild,
|
||||
icon: file.pathToStoreInDatabase,
|
||||
defaultChannelId: defaultChannel.id
|
||||
}
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'guilds',
|
||||
guildId: guild.id,
|
||||
payload: { action: 'update', item }
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return item
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import { fastifyErrors } from '../../../models/utils.js'
|
||||
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
|
||||
import { guildSchema } from '../../../models/Guild.js'
|
||||
import { parseStringNullish } from '../../../tools/utils/parseStringNullish.js'
|
||||
import { channelSchema } from '../../../models/Channel.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
@ -31,7 +32,10 @@ const putServiceSchema: FastifySchema = {
|
||||
body: bodyPutServiceSchema,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
200: Type.Object(guildSchema),
|
||||
200: Type.Object({
|
||||
...guildSchema,
|
||||
defaultChannelId: channelSchema.id
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
@ -66,7 +70,7 @@ export const putGuildByIdService: FastifyPluginAsync = async (fastify) => {
|
||||
throw fastify.httpErrors.notFound('Member not found')
|
||||
}
|
||||
if (!member.isOwner) {
|
||||
throw fastify.httpErrors.forbidden(
|
||||
throw fastify.httpErrors.badRequest(
|
||||
'You should be an owner of the guild'
|
||||
)
|
||||
}
|
||||
@ -77,13 +81,23 @@ export const putGuildByIdService: FastifyPluginAsync = async (fastify) => {
|
||||
description: parseStringNullish(member.guild.description, description)
|
||||
}
|
||||
})
|
||||
const defaultChannel = await prisma.channel.findFirst({
|
||||
where: { guildId: guild.id }
|
||||
})
|
||||
if (defaultChannel == null) {
|
||||
throw fastify.httpErrors.internalServerError()
|
||||
}
|
||||
const item = {
|
||||
...guild,
|
||||
defaultChannelId: defaultChannel.id
|
||||
}
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'guilds',
|
||||
guildId: guild.id,
|
||||
payload: { action: 'update', item: guild }
|
||||
payload: { action: 'update', item }
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return guild
|
||||
return item
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user