Polarcraft/web/server/api/team/invite.js

21 lines
631 B
JavaScript
Raw Normal View History

2023-05-09 13:57:01 +02:00
import { ObjectId } from 'mongodb'
export default defineEventHandler(async (event) => {
const { id } = await readBody(event);
const user = await getAuth(event)
const teamsColl = db.collection('teams')
const usersColl = db.collection('users')
const team = await teamsColl.findOne({ _id: new ObjectId(user.team.id) })
const invitedUser = await usersColl.findOne({ _id: new ObjectId(id)});
if (invitedUser.team) return createError({ statusCode: 400, statusMessage: 'User already is in a team' })
usersColl.updateOne({ _id: new ObjectId(id) }, { $push: { teamInvites: team._id.toString() } })
return team
});