const admin = require("firebase-admin"); module.exports = async (req, res) => { const bodydata = JSON.parse(req.body); console.log("Requested Create - UID - ", bodydata.uid); if (!bodydata.userName) res.status(400).send({ message: "Missing user name" }); if (!bodydata.partyName) res.status(400).send({ message: "Missing party name" }); if (!bodydata.partyPin) res.status(400).send({ message: "Missing party pin" }); const roomNameTaken = await admin .firestore() .collection("rooms") .doc(bodydata.partyName.replace("/", "\\")) .get() .then((docSnap) => docSnap.exists); if (roomNameTaken) return res .status(409) .send({ message: "That party name is already taken" }); await admin .firestore() .collection("rooms") .doc(bodydata.partyName) .set({ name: bodydata.partyName, pin: bodydata.partyPin, player: bodydata.player, createdAt: new Date(), updatedAt: new Date(), adminUid: bodydata.uid, }) .catch((err) => { res.status(500).send({ message: "Unable to start party." }); console.error(err); }); await admin .firestore() .collection("rooms") .doc(bodydata.partyName) .collection("messages") .add({ uid: "", name: "", text: `${bodydata.userName} started the party.`, time: new Date(), }) .catch((err) => { console.error( `Failed to add system create party message : \n\t Room: ${bodydata.partyName}`, err ); }); await admin .firestore() .collection("rooms") .doc(bodydata.partyName) .collection("users") .doc(bodydata.uid) .set({ admin: true, name: bodydata.userName, }) .catch((err) => { console.error( `Failed to set admin when creating party : \n\t Room: ${bodydata.partyName} \n\t User: ${bodydata.uid}`, err ); }); admin .firestore() .collection("rooms") .doc(bodydata.partyName) .collection("messages") .add({ uid: "", name: "", text: `Big update coming on Monday (Aug. 10). Watch Party will be unavailable during the afternoon as we upgrade our services. See what's new at whatsnew.chromewatchparty.com`, time: new Date(), }); if (player.source === "peacocktv.") { admin .firestore() .collection("rooms") .doc(bodydata.partyName) .collection("messages") .add({ uid: "", name: "", text: `Sync may not work flawlessly with PeacockTV till Monday's update. Please stick with us as we address this issue.`, time: new Date(), }); } res .status(201) .send({ roomId: bodydata.partyName, message: "Started a party!" }); };