feat(services): add GET /guilds/public

This commit is contained in:
Divlo 2021-10-26 15:11:41 +00:00
parent 56c613b5cf
commit f0f4f9f69f
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
4 changed files with 96 additions and 0 deletions

1
.gitignore vendored
View File

@ -33,3 +33,4 @@ npm-debug.log*
# misc
.DS_Store
uploads

View File

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

View File

@ -0,0 +1,24 @@
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { prismaMock } from '../../../../__test__/setup.js'
import { guildExample } from '../../../../models/Guild.js'
describe('GET /guilds/public', () => {
it('succeeds', async () => {
prismaMock.guild.findMany.mockResolvedValue([guildExample])
prismaMock.member.count.mockResolvedValue(2)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'GET',
url: '/guilds/public',
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(200)
expect(responseJson.length).toEqual(1)
expect(responseJson[0].name).toEqual(guildExample.name)
expect(responseJson[0].membersCount).toEqual(2)
})
})

View File

@ -0,0 +1,69 @@
import { 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'
import {
getPaginationOptions,
queryPaginationSchema,
QueryPaginationSchemaType
} from '../../../tools/database/pagination'
const getServiceSchema: FastifySchema = {
description: 'GET all the public guilds.',
tags: ['guilds'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
querystring: queryPaginationSchema,
response: {
200: Type.Array(
Type.Object({
...guildSchema,
membersCount: Type.Integer()
})
),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
500: fastifyErrors[500]
}
} as const
export const getGuildsPublic: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route<{
Querystring: QueryPaginationSchemaType
}>({
method: 'GET',
url: '/guilds/public',
schema: getServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const guildsRequest = await prisma.guild.findMany({
...getPaginationOptions(request.query)
})
const guilds = await Promise.all(
guildsRequest.map(async (guild) => {
const membersCount = await prisma.member.count({
where: { guildId: guild.id }
})
return {
...guild,
membersCount
}
})
)
reply.statusCode = 200
return guilds
}
})
}