var express = require('express') var router = express.Router() const DB = require("../mongoDB.js"); const Profile = require("../def/profile.js"); const Notifications = require("./../notifications.js"); DB.getDB.then((DB)=>{ const getUserId = function(req){ const user_sid = req.cookies.user_sid || req.query.user_sid || req.body.user_sid; return DB.ObjectID(user_sid); } const getProfileId = (req)=>{ return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id); } router.get("/mine", async (req, res) => { let userid = req.cookies.user_sid; let profiles = await DB.getUserProfiles(userid); return res.json({ status: "ok", profiles }); }); router.get("/new", async (req, res) => { //Deprecated please use route post("/") let profile = { userid: getUserId(req), ... req.query.content }; let profileObj = new Profile(profile); let r = await DB.newProfile(profileObj); return res.json({ status: "ok", ... profileObj.toObj() }); }); router.post("/", async (req, res) => { let profile = { userid: getUserId(req), ... req.body.content }; let profileObj = new Profile(profile); let r = await DB.newProfile(profileObj); return res.json({ status: "ok", ... profileObj.toObj() }); }); router.post("/invite", async (req, res) => { const userid = getUserId(req); const name = req.body.name; const email = req.body.email; //validate email? if(!name || !email) return res.json({status: "incomplete request"}); let r = await DB.newInvitation(userid, name, email); if(!r.toLowerCase){ //send email invitation let senderProfile = await DB.getProfile(getProfileId(req)); Notifications.youHaveAnInvitation(name, email, senderProfile); return res.json({ status: "ok" }); } return res.json({ status: r }); }); router.get("/invite/:email", async (req, res) => { const userid = getUserId(req); const email = req.body.email; //validate email? if(email) return res.json({status: "provide valid email"}); let r = await DB.getInvitation(email); if(!r) return res.json({status: "no invitation found with that email"}); return res.json({status: "ok", ... r}); }); router.get("/groups", async (req, res) => { let groups = await DB.getGroups(); return res.json({ status: "ok", groups }); }); router.post("/groups", async (req, res) => { let profile = { userid: getUserId(req), isGroup: true, ... req.body }; console.log("newGroup", profile) let profileObj = new Profile(profile); DB.newProfile(profileObj) return res.json({ status: "ok", ... profileObj.toObj() }); }); router.get("/courses", async (req, res) => { let groups = await DB.getCourses(); return res.json({ status: "ok", groups }); }); router.post("/groups/accept", async (req, res) => { //This function should be called to accept the join request //of an user that attempt to join a private group. const groupid = getProfileId(req); //It needs to have this profile context const profileAcepted = DB.ObjectID(req.body.profileid); DB.acceptGroupJoinReq(profileAcepted, groupid); //Add Notification to accepted user return res.json({ status: "ok" }); }); router.get("/groups/:id", async (req, res) => { const groupid = req.params.id; let groups = await DB.getGroup(groupid); return res.json({ status: "ok", groups }); }); router.get("/groups/:id/subscribe", async (req, res) => { const groupid = req.params.id; const profileid = getProfileId(req); const isPrivate = await DB.isGroupPrivate(groupid); DB.subscribeToGroup(profileid, groupid, isPrivate); //Add notification to group owner return res.json({ status: "ok" }); }); router.get("/groups/:id/unsubscribe", async (req, res) => { const groupid = req.params.id; const profileid = getProfileId(req); DB.unsubscribeToGroup(profileid, groupid); //Add notification to group owner return res.json({ status: "ok" }); }); router.get("/search", async (req, res) => { let query = req.query.query; let profiles = await DB.getProfiles(query); return res.json({ status: "ok", profiles }); }); router.post("/setData", (req, res) => { const key = req.body.key; const value = req.body.value; const profileid = getProfileId(req); DB.setData(profileid, key, value); return res.json({ status: "ok", }); }); router.post("/myProfile", async (req, res) => { let profile = { userid: getUserId(req), profile: req.body }; let profileObj = new Profile(profile); //validates profile DB.updateProfile(getProfileId(req), profileObj); return res.json({ status: "ok" }); }); router.get("/:id", async (req, res) => { let profileId = req.params.id; let profile = await DB.getProfile(profileId); return res.json({ status: "ok", ... profile }); }); router.get("/:id/follow", async (req, res) => { let followProfileId = req.params.id; const profileid = getProfileId(req); DB.followProfile(profileid, followProfileId); //Add notification to user return res.json({ status: "ok" }); }); router.get("/:id/unfollow", async (req, res) => { let followProfileId = req.params.id; const profileid = getProfileId(req); DB.unfollowProfile(profileid, followProfileId); //Add notification to user return res.json({ status: "ok" }); }); }); module.exports = router