chore: better Prettier config for easier reviews

This commit is contained in:
2023-10-23 23:38:50 +02:00
parent 5591449094
commit 2aefe73afa
132 changed files with 4114 additions and 4019 deletions

View File

@ -1,48 +1,48 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import test from "node:test"
import assert from "node:assert/strict"
import sinon from 'sinon'
import sinon from "sinon"
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { messageExample } from '#src/models/Message.js'
import { memberExample } from '#src/models/Member.js'
import { userExample } from '#src/models/User.js'
import { channelExample } from '#src/models/Channel.js'
import { application } from "#src/application.js"
import { authenticateUserTest } from "#src/__test__/utils/authenticateUserTest.js"
import prisma from "#src/tools/database/prisma.js"
import { messageExample } from "#src/models/Message.js"
import { memberExample } from "#src/models/Member.js"
import { userExample } from "#src/models/User.js"
import { channelExample } from "#src/models/Channel.js"
await test('DELETE /messsages/[messageId]', async (t) => {
await test("DELETE /messsages/[messageId]", async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async () => {
await t.test("succeeds", async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'message').value({
sinon.stub(prisma, "message").value({
findFirst: async () => {
return {
...messageExample,
channel: channelExample
channel: channelExample,
}
},
delete: async () => {
return messageExample
}
},
})
sinon.stub(prisma, 'member').value({
sinon.stub(prisma, "member").value({
findFirst: async () => {
return {
...memberExample,
user: userExample
user: userExample,
}
}
},
})
const response = await application.inject({
method: 'DELETE',
method: "DELETE",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
authorization: `Bearer ${accessToken}`,
},
})
const responseJson = response.json()
assert.strictEqual(response.statusCode, 200)
@ -56,73 +56,73 @@ await test('DELETE /messsages/[messageId]', async (t) => {
assert.strictEqual(responseJson.member.user.name, userExample.name)
})
await t.test('fails if the message is not found', async () => {
await t.test("fails if the message is not found", async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'message').value({
sinon.stub(prisma, "message").value({
findFirst: async () => {
return null
}
},
})
const response = await application.inject({
method: 'DELETE',
method: "DELETE",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
authorization: `Bearer ${accessToken}`,
},
})
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not found', async () => {
await t.test("fails if the member is not found", async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'message').value({
sinon.stub(prisma, "message").value({
findFirst: async () => {
return {
...messageExample,
channel: channelExample
channel: channelExample,
}
}
},
})
sinon.stub(prisma, 'member').value({
sinon.stub(prisma, "member").value({
findFirst: async () => {
return null
}
},
})
const response = await application.inject({
method: 'DELETE',
method: "DELETE",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
authorization: `Bearer ${accessToken}`,
},
})
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not owner of the message', async () => {
await t.test("fails if the member is not owner of the message", async () => {
const { accessToken } = await authenticateUserTest()
const randomUserIdOwnerOfMessage = 14
sinon.stub(prisma, 'message').value({
sinon.stub(prisma, "message").value({
findFirst: async () => {
return {
...messageExample,
channel: channelExample
channel: channelExample,
}
}
},
})
sinon.stub(prisma, 'member').value({
sinon.stub(prisma, "member").value({
findFirst: async () => {
return {
...memberExample,
userId: randomUserIdOwnerOfMessage
userId: randomUserIdOwnerOfMessage,
}
}
},
})
const response = await application.inject({
method: 'DELETE',
method: "DELETE",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
authorization: `Bearer ${accessToken}`,
},
})
assert.strictEqual(response.statusCode, 400)
})

View File

@ -1,53 +1,53 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import test from "node:test"
import assert from "node:assert/strict"
import sinon from 'sinon'
import sinon from "sinon"
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { messageExample } from '#src/models/Message.js'
import { memberExample } from '#src/models/Member.js'
import { userExample } from '#src/models/User.js'
import { channelExample } from '#src/models/Channel.js'
import { application } from "#src/application.js"
import { authenticateUserTest } from "#src/__test__/utils/authenticateUserTest.js"
import prisma from "#src/tools/database/prisma.js"
import { messageExample } from "#src/models/Message.js"
import { memberExample } from "#src/models/Member.js"
import { userExample } from "#src/models/User.js"
import { channelExample } from "#src/models/Channel.js"
await test('PUT /messsages/[messageId]', async (t) => {
await test("PUT /messsages/[messageId]", async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async () => {
await t.test("succeeds", async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
sinon.stub(prisma, 'message').value({
const newValue = "some message"
sinon.stub(prisma, "message").value({
findFirst: async () => {
return {
...messageExample,
channel: channelExample
channel: channelExample,
}
},
update: async () => {
return {
...messageExample,
value: newValue
value: newValue,
}
}
},
})
sinon.stub(prisma, 'member').value({
sinon.stub(prisma, "member").value({
findFirst: async () => {
return {
...memberExample,
user: userExample
user: userExample,
}
}
},
})
const response = await application.inject({
method: 'PUT',
method: "PUT",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
authorization: `Bearer ${accessToken}`,
},
payload: { value: newValue }
payload: { value: newValue },
})
const responseJson = response.json()
assert.strictEqual(response.statusCode, 200)
@ -61,83 +61,83 @@ await test('PUT /messsages/[messageId]', async (t) => {
assert.strictEqual(responseJson.member.user.name, userExample.name)
})
await t.test('fails if the message is not found', async () => {
await t.test("fails if the message is not found", async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
sinon.stub(prisma, 'message').value({
const newValue = "some message"
sinon.stub(prisma, "message").value({
findFirst: async () => {
return null
}
},
})
const response = await application.inject({
method: 'PUT',
method: "PUT",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
authorization: `Bearer ${accessToken}`,
},
payload: { value: newValue }
payload: { value: newValue },
})
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not found', async () => {
await t.test("fails if the member is not found", async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
sinon.stub(prisma, 'message').value({
const newValue = "some message"
sinon.stub(prisma, "message").value({
findFirst: async () => {
return {
...messageExample,
channel: channelExample
channel: channelExample,
}
}
},
})
sinon.stub(prisma, 'member').value({
sinon.stub(prisma, "member").value({
findFirst: async () => {
return null
}
},
})
const response = await application.inject({
method: 'PUT',
method: "PUT",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
authorization: `Bearer ${accessToken}`,
},
payload: { value: newValue }
payload: { value: newValue },
})
assert.strictEqual(response.statusCode, 404)
})
await t.test(
'fails if the member is not the owner of the message',
"fails if the member is not the owner of the message",
async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
const newValue = "some message"
const randomUserIdOwnerOfMessage = 14
sinon.stub(prisma, 'message').value({
sinon.stub(prisma, "message").value({
findFirst: async () => {
return {
...messageExample,
channel: channelExample
channel: channelExample,
}
}
},
})
sinon.stub(prisma, 'member').value({
sinon.stub(prisma, "member").value({
findFirst: async () => {
return {
...memberExample,
userId: randomUserIdOwnerOfMessage
userId: randomUserIdOwnerOfMessage,
}
}
},
})
const response = await application.inject({
method: 'PUT',
method: "PUT",
url: `/messages/${messageExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
authorization: `Bearer ${accessToken}`,
},
payload: { value: newValue }
payload: { value: newValue },
})
assert.strictEqual(response.statusCode, 400)
}
},
)
})

View File

@ -1,27 +1,27 @@
import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import type { Static } from "@sinclair/typebox"
import { Type } from "@sinclair/typebox"
import type { FastifyPluginAsync, FastifySchema } from "fastify"
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { messageSchema } from '#src/models/Message.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
import prisma from "#src/tools/database/prisma.js"
import { fastifyErrors } from "#src/models/utils.js"
import authenticateUser from "#src/tools/plugins/authenticateUser.js"
import { messageSchema } from "#src/models/Message.js"
import { memberSchema } from "#src/models/Member.js"
import { userPublicWithoutSettingsSchema } from "#src/models/User.js"
const parametersSchema = Type.Object({
messageId: messageSchema.id
messageId: messageSchema.id,
})
type Parameters = Static<typeof parametersSchema>
const putServiceSchema: FastifySchema = {
description: 'UPDATE a message with its id.',
tags: ['messages'] as string[],
description: "UPDATE a message with its id.",
tags: ["messages"] as string[],
security: [
{
bearerAuth: []
}
bearerAuth: [],
},
] as Array<{ [key: string]: [] }>,
params: parametersSchema,
response: {
@ -29,15 +29,15 @@ const putServiceSchema: FastifySchema = {
...messageSchema,
member: Type.Object({
...memberSchema,
user: Type.Object(userPublicWithoutSettingsSchema)
})
user: Type.Object(userPublicWithoutSettingsSchema),
}),
}),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
500: fastifyErrors[500],
},
} as const
export const deleteMessageService: FastifyPluginAsync = async (fastify) => {
@ -46,8 +46,8 @@ export const deleteMessageService: FastifyPluginAsync = async (fastify) => {
fastify.route<{
Params: Parameters
}>({
method: 'DELETE',
url: '/messages/:messageId',
method: "DELETE",
url: "/messages/:messageId",
schema: putServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
@ -58,16 +58,16 @@ export const deleteMessageService: FastifyPluginAsync = async (fastify) => {
const messageCheck = await prisma.message.findFirst({
where: { id: messageId },
include: {
channel: true
}
channel: true,
},
})
if (messageCheck == null || messageCheck.channel == null) {
throw fastify.httpErrors.notFound('Message not found')
throw fastify.httpErrors.notFound("Message not found")
}
const member = await prisma.member.findFirst({
where: {
guildId: messageCheck.channel.guildId,
userId: user.current.id
userId: user.current.id,
},
include: {
user: {
@ -79,23 +79,23 @@ export const deleteMessageService: FastifyPluginAsync = async (fastify) => {
biography: true,
website: true,
createdAt: true,
updatedAt: true
}
}
}
updatedAt: true,
},
},
},
})
if (member == null) {
throw fastify.httpErrors.notFound('Member not found')
throw fastify.httpErrors.notFound("Member not found")
}
if (member.userId !== user.current.id) {
throw fastify.httpErrors.badRequest(
'You should be the owner of the message'
"You should be the owner of the message",
)
}
const message = await prisma.message.delete({
where: {
id: messageCheck.id
}
id: messageCheck.id,
},
})
const item = {
...message,
@ -103,17 +103,17 @@ export const deleteMessageService: FastifyPluginAsync = async (fastify) => {
...member,
user: {
...member.user,
email: null
}
}
email: null,
},
},
}
await fastify.io.emitToMembers({
event: 'messages',
event: "messages",
guildId: item.member.guildId,
payload: { action: 'delete', item }
payload: { action: "delete", item },
})
reply.statusCode = 200
return item
}
},
})
}

View File

@ -1,33 +1,33 @@
import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import type { Static } from "@sinclair/typebox"
import { Type } from "@sinclair/typebox"
import type { FastifyPluginAsync, FastifySchema } from "fastify"
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { messageSchema } from '#src/models/Message.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
import prisma from "#src/tools/database/prisma.js"
import { fastifyErrors } from "#src/models/utils.js"
import authenticateUser from "#src/tools/plugins/authenticateUser.js"
import { messageSchema } from "#src/models/Message.js"
import { memberSchema } from "#src/models/Member.js"
import { userPublicWithoutSettingsSchema } from "#src/models/User.js"
const bodyPutServiceSchema = Type.Object({
value: messageSchema.value
value: messageSchema.value,
})
type BodyPutServiceSchemaType = Static<typeof bodyPutServiceSchema>
const parametersSchema = Type.Object({
messageId: messageSchema.id
messageId: messageSchema.id,
})
type Parameters = Static<typeof parametersSchema>
const putServiceSchema: FastifySchema = {
description: 'UPDATE a message with its id.',
tags: ['messages'] as string[],
description: "UPDATE a message with its id.",
tags: ["messages"] as string[],
security: [
{
bearerAuth: []
}
bearerAuth: [],
},
] as Array<{ [key: string]: [] }>,
body: bodyPutServiceSchema,
params: parametersSchema,
@ -36,15 +36,15 @@ const putServiceSchema: FastifySchema = {
...messageSchema,
member: Type.Object({
...memberSchema,
user: Type.Object(userPublicWithoutSettingsSchema)
})
user: Type.Object(userPublicWithoutSettingsSchema),
}),
}),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
500: fastifyErrors[500],
},
} as const
export const putMessageService: FastifyPluginAsync = async (fastify) => {
@ -54,8 +54,8 @@ export const putMessageService: FastifyPluginAsync = async (fastify) => {
Body: BodyPutServiceSchemaType
Params: Parameters
}>({
method: 'PUT',
url: '/messages/:messageId',
method: "PUT",
url: "/messages/:messageId",
schema: putServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
@ -65,18 +65,18 @@ export const putMessageService: FastifyPluginAsync = async (fastify) => {
const { messageId } = params
const { value } = body
const messageCheck = await prisma.message.findFirst({
where: { id: messageId, type: 'text' },
where: { id: messageId, type: "text" },
include: {
channel: true
}
channel: true,
},
})
if (messageCheck == null || messageCheck.channel == null) {
throw fastify.httpErrors.notFound('Message not found')
throw fastify.httpErrors.notFound("Message not found")
}
const member = await prisma.member.findFirst({
where: {
guildId: messageCheck.channel.guildId,
userId: user.current.id
userId: user.current.id,
},
include: {
user: {
@ -88,26 +88,26 @@ export const putMessageService: FastifyPluginAsync = async (fastify) => {
biography: true,
website: true,
createdAt: true,
updatedAt: true
}
}
}
updatedAt: true,
},
},
},
})
if (member == null) {
throw fastify.httpErrors.notFound('Member not found')
throw fastify.httpErrors.notFound("Member not found")
}
if (member.userId !== user.current.id) {
throw fastify.httpErrors.badRequest(
'You should be the owner of the message'
"You should be the owner of the message",
)
}
const message = await prisma.message.update({
where: {
id: messageCheck.id
id: messageCheck.id,
},
data: {
value
}
value,
},
})
const item = {
...message,
@ -115,17 +115,17 @@ export const putMessageService: FastifyPluginAsync = async (fastify) => {
...member,
user: {
...member.user,
email: null
}
}
email: null,
},
},
}
await fastify.io.emitToMembers({
event: 'messages',
event: "messages",
guildId: item.member.guildId,
payload: { action: 'update', item }
payload: { action: "update", item },
})
reply.statusCode = 200
return item
}
},
})
}

View File

@ -1,7 +1,7 @@
import type { FastifyPluginAsync } from 'fastify'
import type { FastifyPluginAsync } from "fastify"
import { deleteMessageService } from './[messageId]/delete.js'
import { putMessageService } from './[messageId]/put.js'
import { deleteMessageService } from "./[messageId]/delete.js"
import { putMessageService } from "./[messageId]/put.js"
export const messagesService: FastifyPluginAsync = async (fastify) => {
await fastify.register(putMessageService)