90 lines
2.7 KiB
JavaScript
90 lines
2.7 KiB
JavaScript
const admin = require("firebase-admin");
|
|
|
|
module.exports = async (req, res) => {
|
|
const { conference, partyName, partyPin, userName, uid } = JSON.parse(
|
|
req.body
|
|
);
|
|
// GET ROOM PIN
|
|
const [exists, roomPin, conferencePending, partyConference] = await admin
|
|
.firestore()
|
|
.collection("rooms")
|
|
.doc(partyName)
|
|
.get()
|
|
.then((roomSnap) => {
|
|
const data = roomSnap.exists ? roomSnap.data() : {};
|
|
return [
|
|
roomSnap.exists,
|
|
data.pin,
|
|
data.conferencePending,
|
|
data.conference,
|
|
];
|
|
});
|
|
// CHECK IF ROOM EXISTS
|
|
if (exists === true) {
|
|
if (roomPin) {
|
|
// CHECK FOR CORRECT PIN
|
|
if (roomPin === partyPin) {
|
|
if (conferencePending) {
|
|
if (conferencePending === uid) {
|
|
// UPDATE CONFERENCE DETAILS
|
|
await admin
|
|
.firestore()
|
|
.collection("rooms")
|
|
.doc(partyName)
|
|
.update({ conference })
|
|
.catch((err) =>
|
|
res.status(500).send({
|
|
message: "Unable to update the call details",
|
|
error: err,
|
|
})
|
|
);
|
|
// REMOVE PENDING STATUS
|
|
await admin
|
|
.firestore()
|
|
.collection("rooms")
|
|
.doc(partyName)
|
|
.update({
|
|
conferencePending: admin.firestore.FieldValue.delete(),
|
|
})
|
|
.catch((err) =>
|
|
console.error("Failed to remove pending status", err)
|
|
);
|
|
// ADD SYSTEM MESSAGE
|
|
await admin
|
|
.firestore()
|
|
.collection("rooms")
|
|
.doc(partyName)
|
|
.collection("messages")
|
|
.add({
|
|
uid: "<system>",
|
|
name: "",
|
|
text: `${userName} started the call, everyone can join now.`,
|
|
time: new Date(),
|
|
})
|
|
.catch((err) =>
|
|
console.error("Failed to add update conference message", err)
|
|
);
|
|
res.status(200).send({ message: "Started the conference" });
|
|
} else {
|
|
res.status(409).send({
|
|
message: "Looks like someone is already starting a call",
|
|
});
|
|
}
|
|
} else if (partyConference) {
|
|
res.status(409).send({
|
|
message: "Call already exists",
|
|
conference: partyConference,
|
|
});
|
|
}
|
|
} else {
|
|
// WRONG ROOM PIN
|
|
res.status(401).send({ message: "Wrong party pin, try rejoining" });
|
|
}
|
|
}
|
|
} else {
|
|
// ROOM DOESN'T EXIST
|
|
res.status(404).send({ message: "Party Not Found" });
|
|
}
|
|
return;
|
|
};
|