feat(services): add GET /guilds
This commit is contained in:
parent
385c95be90
commit
a746b6a057
1837
package-lock.json
generated
1837
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
26
package.json
26
package.json
@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
src/services/guilds/__test__/get.test.ts
Normal file
29
src/services/guilds/__test__/get.test.ts
Normal 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)
|
||||||
|
})
|
||||||
|
})
|
79
src/services/guilds/get.ts
Normal file
79
src/services/guilds/get.ts
Normal 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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -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)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user