v1.2.1-beta/functions refactor

This commit is contained in:
Ayushya Amitabh
2020-05-26 14:04:04 -04:00
parent 0184c01504
commit ab13a67d08
15 changed files with 561 additions and 492 deletions
+46
View File
@@ -0,0 +1,46 @@
const admin = require('firebase-admin');
module.exports = async (req, res) => {
const { name, text, time, uid, roomId, type } = JSON.parse(req.body);
const roomExists = await admin
.firestore()
.collection('rooms')
.doc(roomId)
.get()
.then((roomSnap) => roomSnap.exists);
if (roomExists) {
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.collection('messages')
.add({
name,
text: text || '',
time: new Date(),
uid,
type: type || '',
})
.catch((err) => {
console.error(`Failed to send message to party : \n\t Room: ${roomId} \n\t ${uid}`, err);
res.status(500).send({ message: "Couldn't send your message", ...messageErr });
});
await admin
.firestore()
.collection('rooms')
.doc(roomId)
.update({
updatedAt: new Date(),
})
.catch((err) => {
console.error(
`Failed to set updated time when sending messasge to party : \n\t Room: ${roomId} \n\t User: ${uid}`,
err
);
});
res.status(200).send({ message: 'Sent a message' });
} else {
res.status(404).send({ message: 'Looks like this party is over' });
}
};