feat(services): add GET /guilds/[guildId]
This commit is contained in:
53
src/services/guilds/[guildId]/__test__/get.test.ts
Normal file
53
src/services/guilds/[guildId]/__test__/get.test.ts
Normal file
@ -0,0 +1,53 @@
|
||||
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'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
|
||||
describe('GET /guilds/[guildId]', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
guild: guildExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner)
|
||||
expect(responseJson.member.user.name).toEqual(user.name)
|
||||
expect(responseJson.member.user.email).toBeNull()
|
||||
expect(responseJson.guild.name).toEqual(guildExample.name)
|
||||
})
|
||||
|
||||
it('fails with not found guild', async () => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/guilds/1',
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Member not found')
|
||||
})
|
||||
|
||||
it('fails with unauthenticated user', async () => {
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/guilds/1'
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
})
|
||||
})
|
91
src/services/guilds/[guildId]/get.ts
Normal file
91
src/services/guilds/[guildId]/get.ts
Normal file
@ -0,0 +1,91 @@
|
||||
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 { memberSchema } from '../../../models/Member.js'
|
||||
import { userPublicWithoutSettingsSchema } from '../../../models/User.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
})
|
||||
|
||||
type Parameters = Static<typeof parametersSchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GET a guild member with the guildId.',
|
||||
tags: ['guilds'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
200: Type.Object({
|
||||
guild: Type.Object(guildSchema),
|
||||
member: Type.Object({
|
||||
...memberSchema,
|
||||
user: Type.Object(userPublicWithoutSettingsSchema)
|
||||
})
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
||||
export const getGuildMemberByIdService: FastifyPluginAsync = async (
|
||||
fastify
|
||||
) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
fastify.route<{
|
||||
Params: Parameters
|
||||
}>({
|
||||
method: 'GET',
|
||||
url: '/guilds/:guildId',
|
||||
schema: getServiceSchema,
|
||||
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 },
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
logo: true,
|
||||
status: true,
|
||||
biography: true,
|
||||
website: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
},
|
||||
guild: true
|
||||
}
|
||||
})
|
||||
if (member == null) {
|
||||
throw fastify.httpErrors.notFound('Member not found')
|
||||
}
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
guild: member.guild,
|
||||
member: {
|
||||
...member,
|
||||
user: {
|
||||
...member.user,
|
||||
email: null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
@ -3,11 +3,13 @@ import { FastifyPluginAsync } from 'fastify'
|
||||
import { getGuilds } from './get.js'
|
||||
import { postGuilds } from './post.js'
|
||||
import { getGuildsPublic } from './public/get.js'
|
||||
import { getGuildMemberByIdService } from './[guildId]/get.js'
|
||||
import { putGuildIconById } from './[guildId]/icon/put.js'
|
||||
|
||||
export const guildsService: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(postGuilds)
|
||||
await fastify.register(getGuilds)
|
||||
await fastify.register(putGuildIconById)
|
||||
await fastify.register(getGuildMemberByIdService)
|
||||
await fastify.register(getGuildsPublic)
|
||||
}
|
||||
|
@ -17,4 +17,15 @@ describe('GET /users/[userId]', () => {
|
||||
expect(responseJson.user.id).toEqual(userExample.id)
|
||||
expect(responseJson.user.name).toEqual(userExample.name)
|
||||
})
|
||||
|
||||
it('fails with not found user', async () => {
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(null)
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/users/1`
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('User not found')
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user