feat(services): add GET /guilds
This commit is contained in:
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 { getGuilds } from './get.js'
|
||||
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(getGuilds)
|
||||
await fastify.register(putGuildIconById)
|
||||
await fastify.register(getGuildsPublic)
|
||||
}
|
||||
|
Reference in New Issue
Block a user