const admin = require('firebase-admin'); module.exports = async (req, res) => { const { name, text, uid, roomId, type } = JSON.parse(req.body); const roomExists = await admin .firestore() .collection('rooms') .doc(roomId) .get() .then((roomSnap) => roomSnap.exists); if (roomExists) { await admin .firestore() .collection('rooms') .doc(roomId) .collection('messages') .add({ name, text: text || '', time: new Date(), uid, type: type || '', }) .catch((err) => { console.error(`Failed to send message to party : \n\t Room: ${roomId} \n\t ${uid}`, err); res.status(500).send({ message: "Couldn't send your message", error: messageErr }); }); await admin .firestore() .collection('rooms') .doc(roomId) .update({ updatedAt: new Date(), }) .catch((err) => { console.error( `Failed to set updated time when sending messasge to party : \n\t Room: ${roomId} \n\t User: ${uid}`, err ); }); res.status(200).send({ message: 'Sent a message' }); } else { res.status(404).send({ message: 'Looks like this party is over' }); } };