2
1
mirror of https://github.com/Thream/api.git synced 2024-07-21 03:38:31 +02:00

feat(services): add GET /guilds

This commit is contained in:
Divlo 2021-11-19 21:52:22 +01:00
parent 385c95be90
commit a746b6a057
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
5 changed files with 1217 additions and 756 deletions

1831
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -31,13 +31,13 @@
"postinstall": "husky install" "postinstall": "husky install"
}, },
"dependencies": { "dependencies": {
"@prisma/client": "3.4.2", "@prisma/client": "3.5.0",
"@sinclair/typebox": "0.20.5", "@sinclair/typebox": "0.20.6",
"axios": "0.24.0", "axios": "0.24.0",
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"dotenv": "10.0.0", "dotenv": "10.0.0",
"ejs": "3.1.6", "ejs": "3.1.6",
"fastify": "3.23.1", "fastify": "3.24.0",
"fastify-cors": "6.0.2", "fastify-cors": "6.0.2",
"fastify-helmet": "5.3.2", "fastify-helmet": "5.3.2",
"fastify-multipart": "5.1.0", "fastify-multipart": "5.1.0",
@ -47,28 +47,28 @@
"fastify-static": "4.5.0", "fastify-static": "4.5.0",
"fastify-swagger": "4.12.6", "fastify-swagger": "4.12.6",
"fastify-url-data": "3.0.3", "fastify-url-data": "3.0.3",
"http-errors": "1.8.0", "http-errors": "1.8.1",
"jsonwebtoken": "8.5.1", "jsonwebtoken": "8.5.1",
"ms": "2.1.3", "ms": "2.1.3",
"nodemailer": "6.7.0", "nodemailer": "6.7.1",
"read-pkg": "5.2.0", "read-pkg": "5.2.0",
"socket.io": "4.3.2" "socket.io": "4.4.0"
}, },
"devDependencies": { "devDependencies": {
"@commitlint/cli": "14.1.0", "@commitlint/cli": "15.0.0",
"@commitlint/config-conventional": "14.1.0", "@commitlint/config-conventional": "15.0.0",
"@saithodev/semantic-release-backmerge": "2.1.0", "@saithodev/semantic-release-backmerge": "2.1.0",
"@types/bcryptjs": "2.4.2", "@types/bcryptjs": "2.4.2",
"@types/busboy": "0.3.0", "@types/busboy": "0.3.0",
"@types/ejs": "3.1.0", "@types/ejs": "3.1.0",
"@types/http-errors": "1.8.1", "@types/http-errors": "1.8.1",
"@types/jest": "27.0.2", "@types/jest": "27.0.2",
"@types/jsonwebtoken": "8.5.5", "@types/jsonwebtoken": "8.5.6",
"@types/ms": "0.7.31", "@types/ms": "0.7.31",
"@types/node": "16.11.7", "@types/node": "16.11.7",
"@types/nodemailer": "6.4.4", "@types/nodemailer": "6.4.4",
"@typescript-eslint/eslint-plugin": "4.33.0", "@typescript-eslint/eslint-plugin": "4.33.0",
"concurrently": "6.3.0", "concurrently": "6.4.0",
"cross-env": "7.0.3", "cross-env": "7.0.3",
"dockerfilelint": "1.8.0", "dockerfilelint": "1.8.0",
"editorconfig-checker": "4.0.2", "editorconfig-checker": "4.0.2",
@ -84,15 +84,15 @@
"jest": "27.3.1", "jest": "27.3.1",
"jest-mock-extended": "2.0.4", "jest-mock-extended": "2.0.4",
"jest-ts-webcompat-resolver": "1.0.0", "jest-ts-webcompat-resolver": "1.0.0",
"lint-staged": "11.2.6", "lint-staged": "12.0.2",
"markdownlint-cli": "0.29.0", "markdownlint-cli": "0.29.0",
"nodemon": "2.0.15", "nodemon": "2.0.15",
"plop": "2.7.6", "plop": "2.7.6",
"prettier": "2.4.1", "prettier": "2.4.1",
"prisma": "3.4.2", "prisma": "3.5.0",
"rimraf": "3.0.2", "rimraf": "3.0.2",
"semantic-release": "18.0.0", "semantic-release": "18.0.0",
"ts-jest": "27.0.7", "ts-jest": "27.0.7",
"typescript": "4.4.4" "typescript": "4.5.2"
} }
} }

View File

@ -0,0 +1,29 @@
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'
import { channelExample } from '../../../models/Channel.js'
describe('GET /guilds', () => {
it('succeeds', async () => {
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
prismaMock.member.findMany.mockResolvedValue([memberExample])
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'GET',
url: '/guilds',
headers: {
authorization: `Bearer ${accessToken}`
},
payload: {}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(200)
expect(responseJson.length).toEqual(1)
expect(responseJson[0].name).toEqual(guildExample.name)
expect(responseJson[0].description).toEqual(guildExample.description)
expect(responseJson[0].defaultChannelId).toEqual(channelExample.id)
})
})

View File

@ -0,0 +1,79 @@
import { Type, Static } 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 {
getPaginationOptions,
queryPaginationObjectSchema
} from '../../tools/database/pagination.js'
export type QuerySchemaType = Static<typeof queryPaginationObjectSchema>
const getServiceSchema: FastifySchema = {
description: 'GET all the guilds of an user.',
tags: ['guilds'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
querystring: queryPaginationObjectSchema,
response: {
200: Type.Array(
Type.Object({
...guildSchema,
defaultChannelId: id
})
),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
500: fastifyErrors[500]
}
} as const
export const getGuilds: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/guilds',
schema: getServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const membersRequest = await prisma.member.findMany({
...getPaginationOptions(request.query),
where: {
userId: request.user.current.id
}
})
const guilds = await Promise.all(
membersRequest.map(async (member) => {
const channel = await prisma.channel.findFirst({
where: {
guildId: member.guildId
}
})
const guild = await prisma.guild.findUnique({
where: {
id: member.guildId
}
})
return {
...guild,
defaultChannelId: channel?.id
}
})
)
reply.statusCode = 200
return guilds
}
})
}

View File

@ -1,11 +1,13 @@
import { FastifyPluginAsync } from 'fastify' import { FastifyPluginAsync } from 'fastify'
import { getGuilds } from './get.js'
import { postGuilds } from './post.js' import { postGuilds } from './post.js'
import { getGuildsPublic } from './public/get.js' import { getGuildsPublic } from './public/get.js'
import { putGuildIconById } from './[guildId]/icon/put.js' import { putGuildIconById } from './[guildId]/icon/put.js'
export const guildsService: FastifyPluginAsync = async (fastify) => { export const guildsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(postGuilds) await fastify.register(postGuilds)
await fastify.register(getGuilds)
await fastify.register(putGuildIconById) await fastify.register(putGuildIconById)
await fastify.register(getGuildsPublic) await fastify.register(getGuildsPublic)
} }