feat(services): add GET /channels/[channelId]
This commit is contained in:
parent
2871c4c9e0
commit
567b5aa2d8
64
src/services/channels/[channelId]/__test__/get.test.ts
Normal file
64
src/services/channels/[channelId]/__test__/get.test.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import { application } from '../../../../application.js'
|
||||||
|
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||||
|
import { prismaMock } from '../../../../__test__/setup.js'
|
||||||
|
import { memberExample } from '../../../../models/Member'
|
||||||
|
import { channelExample } from '../../../../models/Channel.js'
|
||||||
|
|
||||||
|
describe('GET /channels/[channelId]', () => {
|
||||||
|
it('succeeds', async () => {
|
||||||
|
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||||
|
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
const response = await application.inject({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/channels/${channelExample.id}`,
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${accessToken}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const responseJson = response.json()
|
||||||
|
expect(response.statusCode).toEqual(200)
|
||||||
|
expect(responseJson.channel.id).toEqual(channelExample.id)
|
||||||
|
expect(responseJson.channel.name).toEqual(channelExample.name)
|
||||||
|
expect(responseJson.channel.guildId).toEqual(channelExample.guildId)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails with not found member', async () => {
|
||||||
|
prismaMock.channel.findUnique.mockResolvedValue(null)
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
const response = await application.inject({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/channels/1',
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${accessToken}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const responseJson = response.json()
|
||||||
|
expect(response.statusCode).toEqual(404)
|
||||||
|
expect(responseJson.message).toEqual('Channel not found')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails with not found member', async () => {
|
||||||
|
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||||
|
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
const response = await application.inject({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/channels/${channelExample.id}`,
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${accessToken}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const responseJson = response.json()
|
||||||
|
expect(response.statusCode).toEqual(404)
|
||||||
|
expect(responseJson.message).toEqual('Channel not found')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails with unauthenticated user', async () => {
|
||||||
|
const response = await application.inject({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/channels/1'
|
||||||
|
})
|
||||||
|
expect(response.statusCode).toEqual(401)
|
||||||
|
})
|
||||||
|
})
|
66
src/services/channels/[channelId]/get.ts
Normal file
66
src/services/channels/[channelId]/get.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
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 { channelSchema } from '../../../models/Channel'
|
||||||
|
|
||||||
|
const parametersSchema = Type.Object({
|
||||||
|
channelId: channelSchema.id
|
||||||
|
})
|
||||||
|
|
||||||
|
type Parameters = Static<typeof parametersSchema>
|
||||||
|
|
||||||
|
const getServiceSchema: FastifySchema = {
|
||||||
|
description: 'GET a channel with its id.',
|
||||||
|
tags: ['channels'] as string[],
|
||||||
|
security: [
|
||||||
|
{
|
||||||
|
bearerAuth: []
|
||||||
|
}
|
||||||
|
] as Array<{ [key: string]: [] }>,
|
||||||
|
params: parametersSchema,
|
||||||
|
response: {
|
||||||
|
200: Type.Object({
|
||||||
|
channel: Type.Object(channelSchema)
|
||||||
|
}),
|
||||||
|
400: fastifyErrors[400],
|
||||||
|
401: fastifyErrors[401],
|
||||||
|
403: fastifyErrors[403],
|
||||||
|
404: fastifyErrors[404],
|
||||||
|
500: fastifyErrors[500]
|
||||||
|
}
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const getChannelByIdService: FastifyPluginAsync = async (fastify) => {
|
||||||
|
await fastify.register(authenticateUser)
|
||||||
|
|
||||||
|
fastify.route<{
|
||||||
|
Params: Parameters
|
||||||
|
}>({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/channels/:channelId',
|
||||||
|
schema: getServiceSchema,
|
||||||
|
handler: async (request, reply) => {
|
||||||
|
if (request.user == null) {
|
||||||
|
throw fastify.httpErrors.forbidden()
|
||||||
|
}
|
||||||
|
const { channelId } = request.params
|
||||||
|
const channel = await prisma.channel.findUnique({
|
||||||
|
where: { id: channelId }
|
||||||
|
})
|
||||||
|
if (channel == null) {
|
||||||
|
throw fastify.httpErrors.notFound('Channel not found')
|
||||||
|
}
|
||||||
|
const member = await prisma.member.findFirst({
|
||||||
|
where: { guildId: channel.guildId, userId: request.user.current.id }
|
||||||
|
})
|
||||||
|
if (member == null) {
|
||||||
|
throw fastify.httpErrors.notFound('Channel not found')
|
||||||
|
}
|
||||||
|
reply.statusCode = 200
|
||||||
|
return { channel }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
7
src/services/channels/index.ts
Normal file
7
src/services/channels/index.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { FastifyPluginAsync } from 'fastify'
|
||||||
|
|
||||||
|
import { getChannelByIdService } from './[channelId]/get'
|
||||||
|
|
||||||
|
export const channelsService: FastifyPluginAsync = async (fastify) => {
|
||||||
|
await fastify.register(getChannelByIdService)
|
||||||
|
}
|
@ -26,8 +26,8 @@ describe('GET /guilds/[guildId]/channels', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('fails with not found guild', async () => {
|
it('fails with not found guild', async () => {
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
const response = await application.inject({
|
const response = await application.inject({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/guilds/1/channels',
|
url: '/guilds/1/channels',
|
||||||
|
@ -3,9 +3,11 @@ import { FastifyPluginAsync } from 'fastify'
|
|||||||
import { usersService } from './users/index.js'
|
import { usersService } from './users/index.js'
|
||||||
import { guildsService } from './guilds/index.js'
|
import { guildsService } from './guilds/index.js'
|
||||||
import { uploadsService } from './uploads/index.js'
|
import { uploadsService } from './uploads/index.js'
|
||||||
|
import { channelsService } from './channels/index.js'
|
||||||
|
|
||||||
export const services: FastifyPluginAsync = async (fastify) => {
|
export const services: FastifyPluginAsync = async (fastify) => {
|
||||||
await fastify.register(usersService)
|
await fastify.register(channelsService)
|
||||||
await fastify.register(guildsService)
|
await fastify.register(guildsService)
|
||||||
await fastify.register(uploadsService)
|
await fastify.register(uploadsService)
|
||||||
|
await fastify.register(usersService)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user