53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
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' });
|
|
const roomId = await admin
|
|
.firestore()
|
|
.collection('rooms')
|
|
.add({
|
|
name: bodydata.roomName,
|
|
player: bodydata.player,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
})
|
|
.then((ref) => ref.id)
|
|
.catch((err) => {
|
|
res.status(500).send({ message: 'Unable to start party.' });
|
|
console.error(err);
|
|
});
|
|
await admin
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.collection('messages')
|
|
.add({
|
|
uid: '<system>',
|
|
name: '',
|
|
text: `${bodydata.userName} started the party.`,
|
|
time: new Date(),
|
|
})
|
|
.catch((err) => {
|
|
console.error(`Failed to add system create party message : \n\t Room: ${roomId}`, err);
|
|
});
|
|
await admin
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.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: ${roomId} \n\t User: ${bodydata.uid}`,
|
|
err
|
|
);
|
|
});
|
|
res.status(201).send({ roomId, message: 'Started a party!' });
|
|
};
|