From f0f4f9f69f23738ceb85bb6d10ee23526fdf27cd Mon Sep 17 00:00:00 2001 From: Divlo Date: Tue, 26 Oct 2021 15:11:41 +0000 Subject: [PATCH] feat(services): add GET /guilds/public --- .gitignore | 1 + src/services/guilds/index.ts | 2 + .../guilds/public/__test__/get.test.ts | 24 +++++++ src/services/guilds/public/get.ts | 69 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 src/services/guilds/public/__test__/get.test.ts create mode 100644 src/services/guilds/public/get.ts diff --git a/.gitignore b/.gitignore index fe3192f..af45a45 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ npm-debug.log* # misc .DS_Store +uploads diff --git a/src/services/guilds/index.ts b/src/services/guilds/index.ts index 5fff260..625d604 100644 --- a/src/services/guilds/index.ts +++ b/src/services/guilds/index.ts @@ -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) } diff --git a/src/services/guilds/public/__test__/get.test.ts b/src/services/guilds/public/__test__/get.test.ts new file mode 100644 index 0000000..fcc45b9 --- /dev/null +++ b/src/services/guilds/public/__test__/get.test.ts @@ -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) + }) +}) diff --git a/src/services/guilds/public/get.ts b/src/services/guilds/public/get.ts new file mode 100644 index 0000000..731cd6d --- /dev/null +++ b/src/services/guilds/public/get.ts @@ -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 + } + }) +}