feat(services): add POST /channels/[channelId]/messages

This commit is contained in:
Divlo 2022-01-01 01:35:56 +00:00
parent 0003c91f69
commit 766c9fdbd6
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
12 changed files with 1233 additions and 628 deletions

22
.swcrc Normal file
View File

@ -0,0 +1,22 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"target": "es2022",
"loose": true
},
"module": {
"type": "commonjs",
"strict": true,
"strictMode": true,
"lazy": false,
"noInterop": false
}
}

View File

@ -1,7 +1,9 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"resolver": "jest-ts-webcompat-resolver",
"transform": {
"^.+\\.(t|j)sx?$": ["@swc/jest"]
},
"setupFiles": ["./__test__/setEnvironmentsVariables.ts"],
"setupFilesAfterEnv": ["./__test__/setup.ts"],
"rootDir": "./src",

1592
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,9 +12,9 @@
"npm": ">=8.0.0"
},
"scripts": {
"build": "rimraf ./build && tsc",
"build": "rimraf ./build && swc ./src --out-dir ./build && tsc",
"start": "cross-env NODE_ENV=production node build/index.js",
"dev": "concurrently -k -n \"TypeScript,Node\" -p \"[{name}]\" -c \"blue,green\" \"tsc --watch\" \"cross-env NODE_ENV=development nodemon -e js,json,yaml build/index.js\"",
"dev": "concurrently -k -n \"TypeScript,Node\" -p \"[{name}]\" -c \"blue,green\" \"swc ./src --out-dir ./build --watch\" \"cross-env NODE_ENV=development nodemon -e js,json,yaml build/index.js\"",
"generate": "plop",
"lint:commit": "commitlint",
"lint:docker": "dockerfilelint './Dockerfile'",
@ -37,7 +37,7 @@
"bcryptjs": "2.4.3",
"dotenv": "10.0.0",
"ejs": "3.1.6",
"fastify": "3.25.2",
"fastify": "3.25.3",
"fastify-cors": "6.0.2",
"fastify-helmet": "5.3.2",
"fastify-multipart": "5.2.1",
@ -58,11 +58,14 @@
"@commitlint/cli": "16.0.1",
"@commitlint/config-conventional": "16.0.0",
"@saithodev/semantic-release-backmerge": "2.1.0",
"@swc/cli": "0.1.55",
"@swc/core": "1.2.124",
"@swc/jest": "0.2.15",
"@types/bcryptjs": "2.4.2",
"@types/busboy": "0.3.1",
"@types/ejs": "3.1.0",
"@types/http-errors": "1.8.1",
"@types/jest": "27.0.3",
"@types/jest": "27.4.0",
"@types/jsonwebtoken": "8.5.6",
"@types/ms": "0.7.31",
"@types/node": "17.0.5",
@ -79,7 +82,7 @@
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-promise": "5.1.1",
"eslint-plugin-unicorn": "39.0.0",
"eslint-plugin-unicorn": "40.0.0",
"husky": "7.0.4",
"jest": "27.4.5",
"jest-mock-extended": "2.0.4",
@ -92,7 +95,6 @@
"prisma": "3.7.0",
"rimraf": "3.0.2",
"semantic-release": "18.0.1",
"ts-jest": "27.1.2",
"typescript": "4.5.4"
"typescript": "4.4.4"
}
}

View File

@ -4,7 +4,7 @@ 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'
import { channelSchema } from '../../../models/Channel.js'
const parametersSchema = Type.Object({
channelId: channelSchema.id

View File

@ -0,0 +1,94 @@
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 { userExample } from '../../../../../models/User.js'
import { messageExample } from '../../../../../models/Message.js'
describe('POST /channels/[channelId]/messages', () => {
it('succeeds', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
user: userExample
} as any)
prismaMock.message.create.mockResolvedValue(messageExample)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
url: `/channels/${channelExample.id}/messages`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {
value: messageExample.value
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(201)
expect(responseJson.id).toEqual(messageExample.id)
expect(responseJson.value).toEqual(messageExample.value)
expect(responseJson.type).toEqual(messageExample.type)
expect(responseJson.mimetype).toEqual(messageExample.mimetype)
expect(responseJson.member.id).toEqual(memberExample.id)
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner)
expect(responseJson.member.user.id).toEqual(userExample.id)
expect(responseJson.member.user.name).toEqual(userExample.name)
})
it('fails with no message value', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
user: userExample
} as any)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
url: `/channels/${channelExample.id}/messages`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {}
})
expect(response.statusCode).toEqual(400)
})
it('fails with not found channel', async () => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
url: '/channels/5/messages',
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {
value: messageExample.value
}
})
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.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
url: `/channels/${channelExample.id}/messages`,
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {
value: messageExample.value
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(404)
expect(responseJson.message).toEqual('Channel not found')
})
})

View File

@ -0,0 +1,116 @@
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 { messageSchema } from '../../../../models/Message.js'
import { channelSchema } from '../../../../models/Channel.js'
import { memberSchema } from '../../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../../models/User.js'
const parametersSchema = Type.Object({
channelId: channelSchema.id
})
type Parameters = Static<typeof parametersSchema>
const bodyPostServiceSchema = Type.Object({
value: messageSchema.value
})
type BodyPostServiceSchemaType = Static<typeof bodyPostServiceSchema>
const postServiceSchema: FastifySchema = {
description: 'POST a new message in a specific channel using its channelId.',
tags: ['messages'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
body: bodyPostServiceSchema,
params: parametersSchema,
response: {
200: Type.Object({
...messageSchema,
member: Type.Object({
...memberSchema,
user: Type.Object(userPublicWithoutSettingsSchema)
})
}),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const postMessageByChannelIdService: FastifyPluginAsync = async (
fastify
) => {
await fastify.register(authenticateUser)
fastify.route<{
Body: BodyPostServiceSchemaType
Params: Parameters
}>({
method: 'POST',
url: '/channels/:channelId/messages',
schema: postServiceSchema,
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 memberCheck = await prisma.member.findFirst({
where: { guildId: channel.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
}
}
}
})
if (memberCheck == null) {
throw fastify.httpErrors.notFound('Channel not found')
}
const { value } = request.body
const message = await prisma.message.create({
data: {
value,
type: 'text',
mimetype: 'text/plain',
channelId,
memberId: memberCheck.id
}
})
reply.statusCode = 201
return {
...message,
member: {
...memberCheck,
user: {
...memberCheck.user,
email: null
}
}
}
}
})
}

View File

@ -1,9 +1,11 @@
import { FastifyPluginAsync } from 'fastify'
import { getChannelByIdService } from './[channelId]/get'
import { getMessagesByChannelIdService } from './[channelId]/messages/get'
import { getChannelByIdService } from './[channelId]/get.js'
import { getMessagesByChannelIdService } from './[channelId]/messages/get.js'
import { postMessageByChannelIdService } from './[channelId]/messages/post.js'
export const channelsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(getChannelByIdService)
await fastify.register(getMessagesByChannelIdService)
await fastify.register(postMessageByChannelIdService)
}

View File

@ -4,7 +4,7 @@ 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'
import { guildSchema } from '../../../../models/Guild.js'
import { channelSchema } from '../../../../models/Channel.js'
import {
getPaginationOptions,

View File

@ -5,11 +5,11 @@ 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'
import { guildSchema } from '../../../models/Guild.js'
import {
getPaginationOptions,
queryPaginationSchema
} from '../../../tools/database/pagination'
} from '../../../tools/database/pagination.js'
const querySchema = Type.Object({
search: Type.Optional(Type.String()),

View File

@ -10,7 +10,7 @@ import {
maximumImageSize,
supportedImageMimetype,
ROOT_URL
} from '../configurations'
} from '../configurations/index.js'
export interface UploadImageOptions {
folderInUploadsFolder: 'guilds' | 'messages' | 'users'

View File

@ -6,6 +6,7 @@
"moduleResolution": "node",
"outDir": "./build",
"rootDir": "./src",
"noEmit": true,
"strict": true,
"esModuleInterop": true
},