diff --git a/app/package.json b/app/package.json index 62f972f..6ad1fb9 100644 --- a/app/package.json +++ b/app/package.json @@ -1,6 +1,6 @@ { "name": "watch-party-crx", - "version": "1.2.0", + "version": "1.2.1", "private": true, "dependencies": { "@giphy/js-fetch-api": "^1.5.8", diff --git a/functions/index.js b/functions/index.js index 9e76768..e3db650 100644 --- a/functions/index.js +++ b/functions/index.js @@ -144,14 +144,14 @@ const joinRoom = async (req, res) => { err ); }); - const userExists = await admin + const [userExists, userAdmin] = await admin .firestore() .collection('rooms') .doc(bodydata.roomId) .collection('users') .doc(bodydata.uid) .get() - .then((userSnap) => userSnap.exists) + .then((userSnap) => [userSnap.exists, userSnap.exists ? userSnap.data().admin : false]) .catch((err) => { console.error( `Failed to get user when joining room : \n\t Room: ${bodydata.roomId} \n\t User: ${bodydata.uid}`, @@ -216,9 +216,30 @@ const joinRoom = async (req, res) => { res.status(200).send({ roomId: bodydata.roomId, message: 'Joined a party' }); } else if (!userExists && !bodydata.userName) res.status(400).send({ message: 'Missing user name' }); - else if (exists && roomSource !== bodydata.source) - res.status(417).send({ message: `This party is on ${roomSource}` }); - else res.status(404).send({ message: 'Party Not Found' }); + else if (exists && roomSource !== bodydata.source) { + if (userAdmin === true) { + await admin + .firestore() + .collection('rooms') + .doc(bodydata.roomId) + .update({ + player: { + showName: null, + source: roomSource, + status: 'NOT_PLAYING', + time: 0, + updatedBy: bodydata.uid, + }, + }) + .catch((err) => { + console.error( + `Failed to update player source when admin joining party \n\t Room: ${roomId} \n\t User: ${bodydata.uid}` + ); + res.status(500).send({ message: 'Unable to move party, please try again' }); + }); + res.status(200).send({ message: `Moved party to ${roomSource}` }); + } else res.status(417).send({ message: `This party is on ${roomSource}` }); + } else res.status(404).send({ message: 'Party Not Found' }); }; const sendMessage = async (req, res) => { diff --git a/functions/package-lock.json b/functions/package-lock.json index 2612773..d69ef49 100644 --- a/functions/package-lock.json +++ b/functions/package-lock.json @@ -1,7 +1,8 @@ { - "name": "functions", - "requires": true, + "name": "watchparty", + "version": "1.0.0-beta", "lockfileVersion": 1, + "requires": true, "dependencies": { "@babel/code-frame": { "version": "7.8.3", diff --git a/functions/package.json b/functions/package.json index d4a2ef1..c300772 100644 --- a/functions/package.json +++ b/functions/package.json @@ -1,6 +1,6 @@ { - "name": "watchparty", - "version": "1.0.0-beta", + "name": "watch-party-api", + "version": "1.2.1", "description": "Cloud Functions for Watch Party", "scripts": { "lint": "eslint .",