feat(services): add guilds endpoints

This commit is contained in:
Divlo
2022-02-19 19:08:25 +00:00
parent 5f2b8c8021
commit e6d8b64f8a
31 changed files with 2213 additions and 2314 deletions

View File

@ -101,7 +101,6 @@ export const postMessageByChannelIdService: FastifyPluginAsync = async (
memberId: memberCheck.id
}
})
reply.statusCode = 201
const item = {
...message,
member: {
@ -117,6 +116,7 @@ export const postMessageByChannelIdService: FastifyPluginAsync = async (
guildId: item.member.guildId,
payload: { action: 'create', item }
})
reply.statusCode = 201
return item
}
})

View File

@ -95,7 +95,7 @@ export const postMessageUploadsByChannelIdService: FastifyPluginAsync = async (
fastify,
request,
folderInUploadsFolder: 'messages',
maximumFileSize: maximumFileSize
maximumFileSize
})
const message = await prisma.message.create({
data: {

View File

@ -0,0 +1,58 @@
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { prismaMock } from '../../../../__test__/setup.js'
import { memberExample } from '../../../../models/Member.js'
import { guildExample } from '../../../../models/Guild.js'
describe('DELETE /guilds/[guildId]', () => {
it('succeeds and delete the guild', async () => {
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
isOwner: true
})
prismaMock.guild.delete.mockResolvedValue(guildExample)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'DELETE',
url: `/guilds/${guildExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(200)
expect(responseJson.name).toEqual(guildExample.name)
expect(responseJson.description).toEqual(guildExample.description)
})
it("fails if the guild doesn't exist", async () => {
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'DELETE',
url: `/guilds/${guildExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
expect(response.statusCode).toEqual(404)
})
it('fails if the user is not the owner', async () => {
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
isOwner: false
})
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'DELETE',
url: `/guilds/${guildExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(403)
expect(responseJson.message).toEqual('You should be an owner of the guild')
})
})

View File

@ -0,0 +1,82 @@
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { prismaMock } from '../../../../__test__/setup.js'
import { guildExample } from '../../../../models/Guild.js'
import { memberExample } from '../../../../models/Member.js'
describe('PUT /guilds/[guildId]', () => {
it('succeeds and edit the guild', async () => {
const newName = 'New guild name'
const newDescription = 'New guild description'
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
isOwner: true,
guild: guildExample
} as any)
prismaMock.guild.update.mockResolvedValue({
...guildExample,
name: newName,
description: newDescription
})
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'PUT',
url: `/guilds/${guildExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {
name: newName,
description: newDescription
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(200)
expect(responseJson.name).toEqual(newName)
expect(responseJson.description).toEqual(newDescription)
})
it("fails if the guild doesn't exist", async () => {
const newName = 'New guild name'
const newDescription = 'New guild description'
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'PUT',
url: `/guilds/${guildExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {
name: newName,
description: newDescription
}
})
expect(response.statusCode).toEqual(404)
})
it('fails if the user is not the owner', async () => {
const newName = 'New guild name'
const newDescription = 'New guild description'
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
isOwner: false,
guild: guildExample
} as any)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'PUT',
url: `/guilds/${guildExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {
name: newName,
description: newDescription
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(403)
expect(responseJson.message).toEqual('You should be an owner of the guild')
})
})

View File

@ -0,0 +1,71 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../models/Guild.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id
})
type Parameters = Static<typeof parametersSchema>
const deleteServiceSchema: FastifySchema = {
description: 'DELETE a guild with the guildId.',
tags: ['guilds'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
params: parametersSchema,
response: {
200: Type.Object(guildSchema),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const deleteGuildByIdService: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route<{
Params: Parameters
}>({
method: 'DELETE',
url: '/guilds/:guildId',
schema: deleteServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { guildId } = request.params
const member = await prisma.member.findFirst({
where: { guildId, userId: request.user.current.id }
})
if (member == null) {
throw fastify.httpErrors.notFound('Member not found')
}
if (!member.isOwner) {
throw fastify.httpErrors.forbidden(
'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 }
})
reply.statusCode = 200
return guild
}
})
}

View File

@ -0,0 +1,89 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
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'
const parametersSchema = Type.Object({
guildId: guildSchema.id
})
type Parameters = Static<typeof parametersSchema>
const bodyPutServiceSchema = Type.Object({
name: Type.Optional(guildSchema.name),
description: Type.Optional(guildSchema.description)
})
type BodyPutServiceSchemaType = Static<typeof bodyPutServiceSchema>
const putServiceSchema: FastifySchema = {
description: 'Update a guild with the guildId.',
tags: ['guilds'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
body: bodyPutServiceSchema,
params: parametersSchema,
response: {
200: Type.Object(guildSchema),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const putGuildByIdService: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route<{
Body: BodyPutServiceSchemaType
Params: Parameters
}>({
method: 'PUT',
url: '/guilds/:guildId',
schema: putServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { guildId } = request.params
const { name, description } = request.body
const member = await prisma.member.findFirst({
where: { guildId, userId: request.user.current.id },
include: {
guild: true
}
})
if (member == null || member.guild == null) {
throw fastify.httpErrors.notFound('Member not found')
}
if (!member.isOwner) {
throw fastify.httpErrors.forbidden(
'You should be an owner of the guild'
)
}
const guild = await prisma.guild.update({
where: { id: guildId },
data: {
name: name ?? member.guild.name,
description: parseStringNullish(member.guild.description, description)
}
})
await fastify.io.emitToMembers({
event: 'guilds',
guildId: guild.id,
payload: { action: 'update', item: guild }
})
reply.statusCode = 200
return guild
}
})
}

View File

@ -4,9 +4,11 @@ import { getGuilds } from './get.js'
import { postGuilds } from './post.js'
import { getGuildsPublic } from './public/get.js'
import { getChannelsByGuildIdService } from './[guildId]/channels/get.js'
import { deleteGuildByIdService } from './[guildId]/delete.js'
import { getGuildMemberByIdService } from './[guildId]/get.js'
import { putGuildIconById } from './[guildId]/icon/put.js'
import { getMembersByGuildIdService } from './[guildId]/members/get.js'
import { putGuildByIdService } from './[guildId]/put.js'
export const guildsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(postGuilds)
@ -16,4 +18,6 @@ export const guildsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(getChannelsByGuildIdService)
await fastify.register(getGuildsPublic)
await fastify.register(getMembersByGuildIdService)
await fastify.register(putGuildByIdService)
await fastify.register(deleteGuildByIdService)
}

View File

@ -26,7 +26,7 @@ export const getServiceSchema: FastifySchema = {
} as const
export const getGuildsUploadsService: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Params: Parameters
}>({
method: 'GET',

View File

@ -26,7 +26,7 @@ export const getServiceSchema: FastifySchema = {
} as const
export const getUsersUploadsService: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Params: Parameters
}>({
method: 'GET',

View File

@ -19,7 +19,7 @@ const getServiceSchema: FastifySchema = {
response: {
200: Type.Object({
user: Type.Object(userPublicSchema),
guilds: Type.Union([Type.Array(Type.Object(guildSchema)), Type.Null()])
guilds: Type.Array(Type.Object(guildSchema))
}),
400: fastifyErrors[400],
404: fastifyErrors[404],
@ -28,7 +28,7 @@ const getServiceSchema: FastifySchema = {
} as const
export const getUserById: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Params: ParametersGetUser
}>({
method: 'GET',
@ -65,11 +65,13 @@ export const getUserById: FastifyPluginAsync = async (fastify) => {
return {
user: {
...user,
email: user.email ?? null,
settings
},
guilds: !settings.isPublicGuilds
? null
? []
: await prisma.guild.findMany({
take: 10,
where: {
members: {
some: {

View File

@ -25,7 +25,7 @@ const getConfirmEmailSchema: FastifySchema = {
} as const
export const getConfirmEmail: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Querystring: QueryGetConfirmEmailSchemaType
}>({
method: 'GET',

View File

@ -35,7 +35,7 @@ const postRefreshTokenSchema: FastifySchema = {
} as const
export const postRefreshTokenUser: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Body: BodyPostRefreshTokenSchemaType
}>({
method: 'POST',

View File

@ -39,7 +39,7 @@ const postResetPasswordSchema: FastifySchema = {
} as const
export const postResetPasswordUser: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Body: BodyPostResetPasswordSchemaType
Querystring: QueryPostResetPasswordSchemaType
}>({

View File

@ -26,7 +26,7 @@ const putResetPasswordSchema: FastifySchema = {
} as const
export const putResetPasswordUser: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Body: BodyPutResetPasswordSchemaType
}>({
method: 'PUT',

View File

@ -60,7 +60,7 @@ describe('POST /users/signin', () => {
const response = await application.inject({
method: 'POST',
url: '/users/signin',
payload: payload
payload
})
expect(response.statusCode).toEqual(400)
})

View File

@ -31,7 +31,7 @@ const postSigninSchema: FastifySchema = {
} as const
export const postSigninUser: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Body: BodyPostSigninSchemaType
}>({
method: 'POST',

View File

@ -24,7 +24,7 @@ const postSignoutSchema: FastifySchema = {
} as const
export const postSignoutUser: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Body: BodyPostSignoutSchemaType
}>({
method: 'POST',

View File

@ -34,7 +34,7 @@ const postSignupSchema: FastifySchema = {
} as const
export const postSignupUser: FastifyPluginAsync = async (fastify) => {
fastify.route<{
await fastify.route<{
Body: BodyUserSchemaType
Querystring: QueryPostSignupSchemaType
}>({