feat(services): add channels endpoints
This commit is contained in:
parent
e6d8b64f8a
commit
560b966a61
@ -12,6 +12,6 @@
|
||||
"prettier/prettier": "error",
|
||||
"import/extensions": ["error", "always"],
|
||||
"unicorn/prevent-abbreviations": "error",
|
||||
"require-atomic-updates": "off"
|
||||
"@typescript-eslint/await-thenable": "off"
|
||||
}
|
||||
}
|
||||
|
1416
package-lock.json
generated
1416
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
20
package.json
20
package.json
@ -31,7 +31,7 @@
|
||||
"postinstall": "husky install"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "3.9.2",
|
||||
"@prisma/client": "3.10.0",
|
||||
"@sinclair/typebox": "0.23.4",
|
||||
"@thream/socketio-jwt": "2.2.1",
|
||||
"axios": "0.26.0",
|
||||
@ -39,7 +39,7 @@
|
||||
"dotenv": "16.0.0",
|
||||
"ejs": "3.1.6",
|
||||
"fastify": "3.27.2",
|
||||
"fastify-cors": "6.0.2",
|
||||
"fastify-cors": "6.0.3",
|
||||
"fastify-helmet": "7.0.1",
|
||||
"fastify-multipart": "5.3.1",
|
||||
"fastify-plugin": "3.0.1",
|
||||
@ -60,24 +60,24 @@
|
||||
"@commitlint/config-conventional": "16.2.1",
|
||||
"@saithodev/semantic-release-backmerge": "2.1.1",
|
||||
"@swc/cli": "0.1.55",
|
||||
"@swc/core": "1.2.143",
|
||||
"@swc/jest": "0.2.17",
|
||||
"@swc/core": "1.2.146",
|
||||
"@swc/jest": "0.2.20",
|
||||
"@types/bcryptjs": "2.4.2",
|
||||
"@types/busboy": "1.3.0",
|
||||
"@types/ejs": "3.1.0",
|
||||
"@types/http-errors": "1.8.2",
|
||||
"@types/jest": "27.4.0",
|
||||
"@types/jest": "27.4.1",
|
||||
"@types/jsonwebtoken": "8.5.8",
|
||||
"@types/ms": "0.7.31",
|
||||
"@types/node": "17.0.18",
|
||||
"@types/node": "17.0.21",
|
||||
"@types/nodemailer": "6.4.4",
|
||||
"@typescript-eslint/eslint-plugin": "5.12.0",
|
||||
"@typescript-eslint/eslint-plugin": "5.12.1",
|
||||
"concurrently": "7.0.0",
|
||||
"cross-env": "7.0.3",
|
||||
"editorconfig-checker": "4.0.2",
|
||||
"eslint": "8.9.0",
|
||||
"eslint": "8.10.0",
|
||||
"eslint-config-prettier": "8.4.0",
|
||||
"eslint-config-conventions": "1.0.2",
|
||||
"eslint-config-conventions": "1.1.0",
|
||||
"eslint-plugin-import": "2.25.4",
|
||||
"eslint-plugin-prettier": "4.0.0",
|
||||
"eslint-plugin-promise": "6.0.0",
|
||||
@ -91,7 +91,7 @@
|
||||
"nodemon": "2.0.15",
|
||||
"plop": "3.0.5",
|
||||
"prettier": "2.5.1",
|
||||
"prisma": "3.9.2",
|
||||
"prisma": "3.10.0",
|
||||
"rimraf": "3.0.2",
|
||||
"semantic-release": "19.0.2",
|
||||
"typescript": "4.5.5"
|
||||
|
71
src/services/channels/[channelId]/__test__/delete.test.ts
Normal file
71
src/services/channels/[channelId]/__test__/delete.test.ts
Normal 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)
|
||||
})
|
||||
})
|
75
src/services/channels/[channelId]/__test__/put.test.ts
Normal file
75
src/services/channels/[channelId]/__test__/put.test.ts
Normal 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)
|
||||
})
|
||||
})
|
79
src/services/channels/[channelId]/delete.ts
Normal file
79
src/services/channels/[channelId]/delete.ts
Normal 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
|
||||
}
|
||||
})
|
||||
}
|
89
src/services/channels/[channelId]/put.ts
Normal file
89
src/services/channels/[channelId]/put.ts
Normal 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
|
||||
}
|
||||
})
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
59
src/services/guilds/[guildId]/channels/__test__/post.test.ts
Normal file
59
src/services/guilds/[guildId]/channels/__test__/post.test.ts
Normal file
@ -0,0 +1,59 @@
|
||||
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'
|
||||
import { guildExample } from '../../../../../models/Guild.js'
|
||||
|
||||
describe('POST /guilds/[guildId]/channels', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.create.mockResolvedValue(channelExample)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/channels`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(201)
|
||||
expect(responseJson.id).toEqual(channelExample.id)
|
||||
expect(responseJson.name).toEqual(channelExample.name)
|
||||
expect(responseJson.guildId).toEqual(channelExample.guildId)
|
||||
})
|
||||
|
||||
it('fails if the member is not found', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/channels`,
|
||||
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.member.findFirst.mockResolvedValue(member)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/channels`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
})
|
||||
})
|
86
src/services/guilds/[guildId]/channels/post.ts
Normal file
86
src/services/guilds/[guildId]/channels/post.ts
Normal file
@ -0,0 +1,86 @@
|
||||
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'
|
||||
import { guildSchema } from '../../../../models/Guild.js'
|
||||
|
||||
const bodyPostServiceSchema = Type.Object({
|
||||
name: channelSchema.name
|
||||
})
|
||||
|
||||
type BodyPostServiceSchemaType = Static<typeof bodyPostServiceSchema>
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
})
|
||||
|
||||
type Parameters = Static<typeof parametersSchema>
|
||||
|
||||
const postChannelServiceSchema: FastifySchema = {
|
||||
description: 'Create a channel.',
|
||||
tags: ['channels'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
body: bodyPostServiceSchema,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
201: Type.Object(channelSchema),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
404: fastifyErrors[404],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
||||
export const postChannelService: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
fastify.route<{
|
||||
Body: BodyPostServiceSchemaType
|
||||
Params: Parameters
|
||||
}>({
|
||||
method: 'POST',
|
||||
url: '/guilds/:guildId/channels',
|
||||
schema: postChannelServiceSchema,
|
||||
handler: async (request, reply) => {
|
||||
if (request.user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const { user } = request
|
||||
const { guildId } = request.params
|
||||
const { name } = request.body
|
||||
const member = await prisma.member.findFirst({
|
||||
where: { 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.create({
|
||||
data: {
|
||||
name,
|
||||
guildId
|
||||
}
|
||||
})
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'channels',
|
||||
guildId,
|
||||
payload: {
|
||||
action: 'create',
|
||||
item: channel
|
||||
}
|
||||
})
|
||||
reply.statusCode = 201
|
||||
return channel
|
||||
}
|
||||
})
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
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 { channelExample } from '../../../../../../models/Channel.js'
|
||||
import { memberExample } from '../../../../../../models/Member.js'
|
||||
import { userExample } from '../../../../../../models/User.js'
|
||||
|
||||
describe('POST /guilds/[guildId]/members/join', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
prismaMock.member.create.mockResolvedValue({
|
||||
...memberExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
|
||||
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/members/join`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(201)
|
||||
expect(responseJson.id).toEqual(memberExample.id)
|
||||
expect(responseJson.userId).toEqual(memberExample.userId)
|
||||
expect(responseJson.user.name).toEqual(user.name)
|
||||
expect(responseJson.user.email).toEqual(null)
|
||||
expect(responseJson.guild.id).toEqual(guildExample.id)
|
||||
expect(responseJson.guild.name).toEqual(guildExample.name)
|
||||
expect(responseJson.guild.defaultChannelId).toEqual(channelExample.id)
|
||||
})
|
||||
|
||||
it('fails if the user is already in the guild', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/members/join`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
})
|
||||
})
|
121
src/services/guilds/[guildId]/members/join/post.ts
Normal file
121
src/services/guilds/[guildId]/members/join/post.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors, id } 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 postServiceSchema: FastifySchema = {
|
||||
description: 'Join a guild (create a member).',
|
||||
tags: ['members'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
201: Type.Object({
|
||||
...memberSchema,
|
||||
guild: Type.Object({
|
||||
...guildSchema,
|
||||
defaultChannelId: id
|
||||
}),
|
||||
user: Type.Object(userPublicWithoutSettingsSchema)
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
||||
export const postMemberService: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
fastify.route<{
|
||||
Params: Parameters
|
||||
}>({
|
||||
method: 'POST',
|
||||
url: '/guilds/:guildId/members/join',
|
||||
schema: postServiceSchema,
|
||||
handler: async (request, reply) => {
|
||||
if (request.user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const { user } = request
|
||||
const { guildId } = request.params
|
||||
const memberCheck = await prisma.member.findFirst({
|
||||
where: {
|
||||
userId: user.current.id,
|
||||
guildId
|
||||
}
|
||||
})
|
||||
if (memberCheck != null) {
|
||||
throw fastify.httpErrors.badRequest(
|
||||
"Guild doesn't exist or you are already in the guild"
|
||||
)
|
||||
}
|
||||
const member = await prisma.member.create({
|
||||
data: {
|
||||
guildId,
|
||||
userId: user.current.id
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
logo: true,
|
||||
status: true,
|
||||
biography: true,
|
||||
website: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
const channel = await prisma.channel.findFirst({
|
||||
where: {
|
||||
guildId: member.guildId
|
||||
}
|
||||
})
|
||||
const guild = await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: member.guildId
|
||||
}
|
||||
})
|
||||
const item = {
|
||||
...member,
|
||||
user: {
|
||||
...member.user,
|
||||
email: null
|
||||
},
|
||||
guild: {
|
||||
...guild,
|
||||
defaultChannelId: channel?.id
|
||||
}
|
||||
}
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'members',
|
||||
guildId,
|
||||
payload: {
|
||||
action: 'create',
|
||||
item
|
||||
}
|
||||
})
|
||||
reply.statusCode = 201
|
||||
return item
|
||||
}
|
||||
})
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
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('DELETE /guilds/[guildId]/members/leave', () => {
|
||||
it('succeeds', async () => {
|
||||
const member = {
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
}
|
||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
||||
prismaMock.member.delete.mockResolvedValue(member)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}/members/leave`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.id).toEqual(member.id)
|
||||
expect(responseJson.isOwner).toEqual(member.isOwner)
|
||||
expect(responseJson.userId).toEqual(member.userId)
|
||||
})
|
||||
|
||||
it('fails if the member is not found', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}/members/leave`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
})
|
||||
|
||||
it('fails if the member is owner', async () => {
|
||||
const member = {
|
||||
...memberExample,
|
||||
isOwner: true
|
||||
}
|
||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}/members/leave`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
})
|
||||
})
|
74
src/services/guilds/[guildId]/members/leave/delete.ts
Normal file
74
src/services/guilds/[guildId]/members/leave/delete.ts
Normal file
@ -0,0 +1,74 @@
|
||||
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'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
})
|
||||
|
||||
type Parameters = Static<typeof parametersSchema>
|
||||
|
||||
const deleteServiceSchema: FastifySchema = {
|
||||
description: 'Leave a guild (delete a member).',
|
||||
tags: ['members'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
200: Type.Object(memberSchema),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
404: fastifyErrors[404],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
||||
export const deleteMemberService: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
fastify.route<{
|
||||
Params: Parameters
|
||||
}>({
|
||||
method: 'DELETE',
|
||||
url: '/guilds/:guildId/members/leave',
|
||||
schema: deleteServiceSchema,
|
||||
handler: async (request, reply) => {
|
||||
if (request.user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const { user } = request
|
||||
const { guildId } = request.params
|
||||
const member = await prisma.member.findFirst({
|
||||
where: { guildId, userId: user.current.id }
|
||||
})
|
||||
if (member == null) {
|
||||
throw fastify.httpErrors.notFound('Member not found')
|
||||
}
|
||||
if (member.isOwner) {
|
||||
throw fastify.httpErrors.badRequest(
|
||||
"The member owner can't leave the guild (you can delete it instead)"
|
||||
)
|
||||
}
|
||||
await prisma.member.delete({ where: { id: member.id } })
|
||||
await fastify.io.emitToMembers({
|
||||
event: 'members',
|
||||
guildId,
|
||||
payload: {
|
||||
action: 'delete',
|
||||
item: member
|
||||
}
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return member
|
||||
}
|
||||
})
|
||||
}
|
@ -4,10 +4,13 @@ import { getGuilds } from './get.js'
|
||||
import { postGuilds } from './post.js'
|
||||
import { getGuildsPublic } from './public/get.js'
|
||||
import { getChannelsByGuildIdService } from './[guildId]/channels/get.js'
|
||||
import { postChannelService } from './[guildId]/channels/post.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 { postMemberService } from './[guildId]/members/join/post.js'
|
||||
import { deleteMemberService } from './[guildId]/members/leave/delete.js'
|
||||
import { putGuildByIdService } from './[guildId]/put.js'
|
||||
|
||||
export const guildsService: FastifyPluginAsync = async (fastify) => {
|
||||
@ -20,4 +23,7 @@ export const guildsService: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(getMembersByGuildIdService)
|
||||
await fastify.register(putGuildByIdService)
|
||||
await fastify.register(deleteGuildByIdService)
|
||||
await fastify.register(postMemberService)
|
||||
await fastify.register(deleteMemberService)
|
||||
await fastify.register(postChannelService)
|
||||
}
|
||||
|
Reference in New Issue
Block a user