const admin = require('firebase-admin'); module.exports = async (req, res) => { const { player, roomId, uid, showChanged, userName } = JSON.parse(req.body); if (!roomId) return res.status(204).send({ message: 'Party name not sent.' }); const [roomExists, roomData] = await admin .firestore() .collection('rooms') .doc(roomId) .get() .then((docSnap) => [docSnap.exists, docSnap.data()]); if (!roomExists) return res.status(404).send({ message: 'Party not found.' }); if (showChanged === true && player.showName) { if (roomData.player.showName !== player.showName) { admin .firestore() .collection('rooms') .doc(roomId) .collection('messages') .add({ uid: '', name: '', text: `${userName} changed \n${player.showName ? 'to ' + player.showName : ''}`, time: new Date(), }); } } if (roomData.status !== player.status) { await admin .firestore() .collection('rooms') .doc(roomId) .update({ player, }) .catch((err) => { console.error(`Failed to update player : \n\t Room: ${roomId} \n\t ${uid}`, err); res.status(500).send({ message: "Uh oh party could't be synced", err }); }); return res.status(200).send({ message: 'Synced party player' }); } else { return res.status(208).send({ message: 'Player already up to date' }); } };