Files
watchparty/functions/modules/updateAdminOnly.js
T
2020-08-01 13:53:58 -04:00

37 lines
1.1 KiB
JavaScript

const admin = require('firebase-admin');
module.exports = async (req, res) => {
const bodydata = JSON.parse(req.body);
const isAdmin = await admin
.firestore()
.collection('rooms')
.doc(req.params.rid)
.collection('users')
.doc(bodydata.uid)
.get()
.then((v) => {
if (v.exists) {
return v.data().admin;
}
return false;
})
.catch((err) => {
console.err('Failed to get party to update adminOnly', err);
return res.status(404).send({ message: 'Failed to get party.' });
});
if (!isAdmin) {
console.log('[UNAUTHORIZED] Requested Admin Mode Update - Room - ', req.params.rid);
res.status(401).send({ message: 'Not authorized to update admin only mode for this party.' });
return;
}
const adminOnly = req.params.adminOnly === 'true';
await admin.firestore().collection('rooms').doc(req.params.rid).update({
adminOnly
}).catch((err) => {
console.err('Failed to update adminOnly for party', err);
res.status(409).send({ message: 'Failed to update party.' });
});
res.status(200).send({ message: 'Updated admin only mode for party' });
return;
};