feat(services): add channels endpoints

This commit is contained in:
Divlo
2022-02-28 15:02:09 +00:00
parent e6d8b64f8a
commit 560b966a61
15 changed files with 1471 additions and 739 deletions

View File

@ -0,0 +1,71 @@
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { prismaMock } from '../../../../__test__/setup.js'
import { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js'
describe('DELETE /channels/[channelId]', () => {
it('succeeds', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(memberExample)
prismaMock.channel.delete.mockResolvedValue(channelExample)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'DELETE',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(200)
expect(responseJson.id).toEqual(channelExample.id)
expect(responseJson.name).toEqual(channelExample.name)
expect(responseJson.guildId).toEqual(channelExample.guildId)
})
it('fails if the channel is not found', async () => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'DELETE',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
expect(response.statusCode).toEqual(404)
})
it('fails if the member is not found', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'DELETE',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
expect(response.statusCode).toEqual(404)
})
it('fails if the member is not owner', async () => {
const member = {
...memberExample,
isOwner: false
}
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(member)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'DELETE',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
expect(response.statusCode).toEqual(400)
})
})

View File

@ -0,0 +1,75 @@
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { prismaMock } from '../../../../__test__/setup.js'
import { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js'
describe('PUT /channels/[channelId]', () => {
it('succeeds', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(memberExample)
prismaMock.channel.update.mockResolvedValue(channelExample)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'PUT',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: { name: channelExample.name }
})
const responseJson = response.json()
expect(response.statusCode).toEqual(200)
expect(responseJson.id).toEqual(channelExample.id)
expect(responseJson.name).toEqual(channelExample.name)
expect(responseJson.guildId).toEqual(channelExample.guildId)
})
it('fails if the channel is not found', async () => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'PUT',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: { name: channelExample.name }
})
expect(response.statusCode).toEqual(404)
})
it('fails if the member is not found', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'PUT',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: { name: channelExample.name }
})
expect(response.statusCode).toEqual(404)
})
it('fails if the member is not owner', async () => {
const member = {
...memberExample,
isOwner: false
}
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(member)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'PUT',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: { name: channelExample.name }
})
expect(response.statusCode).toEqual(400)
})
})

View File

@ -0,0 +1,79 @@
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.js'
const parametersSchema = Type.Object({
channelId: channelSchema.id
})
type Parameters = Static<typeof parametersSchema>
const deleteServiceSchema: FastifySchema = {
description: 'DELETE a channel with its id.',
tags: ['channels'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
params: parametersSchema,
response: {
200: Type.Object(channelSchema),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const deleteChannelService: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route<{
Params: Parameters
}>({
method: 'DELETE',
url: '/channels/:channelId',
schema: deleteServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { channelId } = request.params
const channelCheck = await prisma.channel.findUnique({
where: { id: channelId }
})
if (channelCheck == null) {
throw fastify.httpErrors.notFound('Channel not found')
}
const member = await prisma.member.findFirst({
where: { guildId: channelCheck.guildId, userId: user.current.id }
})
if (member == null) {
throw fastify.httpErrors.notFound('Member not found')
}
if (!member.isOwner) {
throw fastify.httpErrors.badRequest('You should be a member owner')
}
const channel = await prisma.channel.delete({
where: { id: channelId }
})
await fastify.io.emitToMembers({
event: 'channels',
guildId: member.guildId,
payload: {
action: 'delete',
item: channel
}
})
reply.statusCode = 200
return channel
}
})
}

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 { channelSchema } from '../../../models/Channel.js'
const bodyPutServiceSchema = Type.Object({
name: channelSchema.name
})
type BodyPutServiceSchemaType = Static<typeof bodyPutServiceSchema>
const parametersSchema = Type.Object({
channelId: channelSchema.id
})
type Parameters = Static<typeof parametersSchema>
const putServiceSchema: FastifySchema = {
description: 'UPDATE a channel with its id.',
tags: ['channels'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
params: parametersSchema,
body: bodyPutServiceSchema,
response: {
200: Type.Object(channelSchema),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const putChannelService: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route<{
Body: BodyPutServiceSchemaType
Params: Parameters
}>({
method: 'PUT',
url: '/channels/:channelId',
schema: putServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { channelId } = request.params
const { name } = request.body
const channelCheck = await prisma.channel.findUnique({
where: { id: channelId }
})
if (channelCheck == null) {
throw fastify.httpErrors.notFound('Channel not found')
}
const member = await prisma.member.findFirst({
where: { guildId: channelCheck.guildId, userId: user.current.id }
})
if (member == null) {
throw fastify.httpErrors.notFound('Member not found')
}
if (!member.isOwner) {
throw fastify.httpErrors.badRequest('You should be a member owner')
}
const channel = await prisma.channel.update({
where: { id: channelId },
data: { name }
})
await fastify.io.emitToMembers({
event: 'channels',
guildId: member.guildId,
payload: {
action: 'update',
item: channel
}
})
reply.statusCode = 200
return channel
}
})
}

View File

@ -1,13 +1,17 @@
import { FastifyPluginAsync } from 'fastify'
import { deleteChannelService } from './[channelId]/delete.js'
import { getChannelByIdService } from './[channelId]/get.js'
import { getMessagesByChannelIdService } from './[channelId]/messages/get.js'
import { postMessageByChannelIdService } from './[channelId]/messages/post.js'
import { postMessageUploadsByChannelIdService } from './[channelId]/messages/uploads/post.js'
import { putChannelService } from './[channelId]/put.js'
export const channelsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(getChannelByIdService)
await fastify.register(getMessagesByChannelIdService)
await fastify.register(postMessageByChannelIdService)
await fastify.register(postMessageUploadsByChannelIdService)
await fastify.register(putChannelService)
await fastify.register(deleteChannelService)
}