-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateRoom.js
57 lines (49 loc) · 1.52 KB
/
updateRoom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const marked = require("marked");
const PROCTION_DOMAIN = "restroom.place";
// function isEmulation() {
// return process.env.FUNCTIONS_EMULATOR === "true";
// }
/**
* Updates room's public profile
*/
exports.updateRoom = functions.https.onCall(
async ({ roomId, bio, name }, context) => {
if (!context.auth || !context.auth.token.email_verified) {
throw new functions.https.HttpsError(
"unauthenticated",
"Nejste ověřeno."
);
}
const db = admin.firestore();
const ref = db.doc(`rooms/${roomId}`);
const profileRef = db.doc(`rooms/${roomId}/public/profile`);
return db.runTransaction(async (transaction) => {
const doc = await transaction.get(ref);
const profileDoc = await transaction.get(profileRef);
if (
profileDoc.get("initialized") &&
[doc.get("initializatorId"), doc.get("creatorId")].includes(
context.auth.uid
) === false
) {
throw new functions.https.HttpsError(
"unauthenticated",
"Na tohle nemáte právo."
);
}
await transaction.update(ref, {
initializatorId: context.auth.uid,
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
await transaction.update(profileRef, {
name,
bio,
html: marked.parse(bio),
initialized: true,
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
});
}
);