fix(services): add missing real time

This commit is contained in:
Divlo
2022-03-02 11:47:13 +01:00
parent c23239c0da
commit 1bcee76324
5 changed files with 92 additions and 32 deletions

View File

@ -8,13 +8,13 @@ import { userExample } from '../../../../../../models/User.js'
describe('POST /guilds/[guildId]/members/join', () => {
it('succeeds', async () => {
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
prismaMock.member.findFirst.mockResolvedValue(null)
prismaMock.member.create.mockResolvedValue({
...memberExample,
user: userExample
} as any)
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
const { accessToken, user } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
@ -34,8 +34,10 @@ describe('POST /guilds/[guildId]/members/join', () => {
expect(responseJson.guild.defaultChannelId).toEqual(channelExample.id)
})
it('fails if the user is already in the guild', async () => {
prismaMock.member.findFirst.mockResolvedValue(memberExample)
it('fails if the guild is not found', async () => {
prismaMock.guild.findUnique.mockResolvedValue(null)
prismaMock.member.findFirst.mockResolvedValue(null)
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
@ -44,6 +46,27 @@ describe('POST /guilds/[guildId]/members/join', () => {
authorization: `Bearer ${accessToken}`
}
})
expect(response.statusCode).toEqual(404)
})
it('fails if the user is already in the guild', async () => {
const defaultChannelId = 5
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
prismaMock.member.findFirst.mockResolvedValue(memberExample)
prismaMock.channel.findFirst.mockResolvedValue({
...channelExample,
id: defaultChannelId
})
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
url: `/guilds/${guildExample.id}/members/join`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(400)
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
})
})

View File

@ -2,11 +2,16 @@ import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../../tools/database/prisma.js'
import { fastifyErrors, id } from '../../../../../models/utils.js'
import {
fastifyErrors,
fastifyErrorsSchema,
id
} from '../../../../../models/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../../../models/Guild.js'
import { memberSchema } from '../../../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../../../models/User.js'
import { channelSchema } from '../../../../../models/Channel.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id
@ -32,9 +37,13 @@ const postServiceSchema: FastifySchema = {
}),
user: Type.Object(userPublicWithoutSettingsSchema)
}),
400: fastifyErrors[400],
400: Type.Object({
...fastifyErrorsSchema[400],
defaultChannelId: channelSchema.id
}),
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
@ -54,15 +63,35 @@ export const postMemberService: FastifyPluginAsync = async (fastify) => {
}
const { user } = request
const { guildId } = request.params
const memberCheck = await prisma.member.findFirst({
const guild = await prisma.guild.findUnique({
where: {
id: guildId
}
})
if (guild == null) {
throw fastify.httpErrors.notFound('Guild not found')
}
const defaultChannel = await prisma.channel.findFirst({
where: {
userId: user.current.id,
guildId
}
})
if (defaultChannel == null) {
throw fastify.httpErrors.internalServerError()
}
const memberCheck = await prisma.member.findFirst({
where: {
userId: user.current.id,
guildId: guild.id
}
})
if (memberCheck != null) {
throw fastify.httpErrors.badRequest(
"Guild doesn't exist or you are already in the guild"
throw fastify.httpErrors.createError(
400,
'You are already in the guild',
{
defaultChannelId: defaultChannel.id
}
)
}
const member = await prisma.member.create({
@ -85,16 +114,7 @@ export const postMemberService: FastifyPluginAsync = async (fastify) => {
}
}
})
const channel = await prisma.channel.findFirst({
where: {
guildId: member.guildId
}
})
const guild = await prisma.guild.findUnique({
where: {
id: member.guildId
}
})
const item = {
...member,
user: {
@ -103,7 +123,7 @@ export const postMemberService: FastifyPluginAsync = async (fastify) => {
},
guild: {
...guild,
defaultChannelId: channel?.id
defaultChannelId: defaultChannel.id
}
}
await fastify.io.emitToMembers({

View File

@ -131,6 +131,14 @@ export const putCurrentUser: FastifyPluginAsync = async (fastify) => {
website: parseStringNullish(request.user.current.website, website)
}
})
await fastify.io.emitToAuthorizedUsers({
event: 'users',
isAuthorizedCallback: () => true,
payload: {
action: 'update',
item: user
}
})
reply.statusCode = 200
return {
user: {