35 lines
965 B
JavaScript
35 lines
965 B
JavaScript
const admin = require('firebase-admin');
|
|
|
|
module.exports = async (req, res) => {
|
|
const { name, text, uid, roomId, type, replyTo } = 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 || '',
|
|
replyTo: replyTo || null,
|
|
})
|
|
.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", error: messageErr });
|
|
});
|
|
res.status(200).send({ message: 'Sent a message' });
|
|
} else {
|
|
res.status(404).send({ message: 'Looks like this party is over' });
|
|
}
|
|
};
|