const admin = require('firebase-admin'); module.exports = async (req, res) => { const { conference, partyName, partyPin, userName } = JSON.parse(req.body); // GET ROOM PIN const roomPin = await admin .firestore() .collection('rooms') .doc(partyName) .get() .then((roomSnap) => (roomSnap.exists ? roomSnap.data().pin : null)); // CHECK IF ROOM EXISTS if (roomPin) { // CHECK FOR CORRECT PIN if (roomPin === partyPin) { // UPDATE CONFERENCE DETAILS await admin .firestore() .collection('rooms') .doc(partyName) .update({ conference }) .catch((err) => res.status(500).send({ message: 'Unable to update the call details', error: err }) ); // ADD SYSTEM MESSAGE await admin .firestore() .collection('rooms') .doc(partyName) .collection('messages') .add({ uid: '', name: '', text: `${userName} started the call.`, time: new Date(), }) .catch((err) => console.error('Failed to add update conference message', err)); res.status(200).send({ message: 'Started the conference' }); } else { // WRONG ROOM PIN return res.status(401).send({ message: 'Wrong party pin, try rejoining' }); } } else { // ROOM DOESN'T EXIST return res.status(404).send({ message: 'Party Not Found' }); } };