diff --git a/dbTools/payments.js b/dbTools/payments.js index af7f116..d63c7cb 100644 --- a/dbTools/payments.js +++ b/dbTools/payments.js @@ -4,14 +4,56 @@ postDB = (DB)=>{ DB.paymentsCols = DB.db.db(DBName).collection("payments"); DB.newIntent = (intent) => { + intent.ts = new Date(); return DB.paymentsCols.insertOne(intent).catch((err)=>{ console.log(err); return false; }); } - DB.newResult = (paymentResult) => { - return DB.paymentsCols.insertOne(paymentResult).catch((err)=>{ + DB.getIntent = (client_secret) => { + return DB.paymentsCols.findOne({client_secret}).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.newResult = (paymentResult, client_secret) => { + if(!client_secret) + return DB.paymentsCols.insertOne(paymentResult).catch((err)=>{ + console.log(err); + return false; + }); + const update = { + $set: { + payment: paymentResult.result + } + } + return DB.paymentsCols.updateOne({client_secret}, update, {upsert: 1}).catch((err)=>{ + console.log(err); + return false; + }); + } + + //Maybe this should be in dbTools/profile.js + DB.updateProfileSubscription = (profileid, restart=false) => { + const inc = 1000 * 60 * 60 * 24 * 30; //30 days + let update = {}; + if(restart){ + update = { + $set: { + subscription: (new Date() - 0 ) + inc, + } + } + }else{ + update = { + $inc: { + subscription: inc, + } + } + } + const _id = DB.ObjectID(profileid); + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ console.log(err); return false; }); diff --git a/dbTools/profile.js b/dbTools/profile.js index c293c60..87eb716 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -153,6 +153,11 @@ userDB = (DB) => { }); } + DB.isSubscriptor = async (profileid) => { + const profile = DB.getProfileCache(profileid); + return profile && profile.subscription && profile.subscription > (new Date() - 0); + } + //Groups DB.getGroups = async (excludePrivate = false) => { let query = { diff --git a/routes/payments.js b/routes/payments.js index 32a83c6..f3b902d 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -57,7 +57,8 @@ DB.getDB.then((DB) => { paymentIntent, userid, price, - description + description, + client_secret: paymentIntent.client_secret, }; DB.newIntent(intent); @@ -81,7 +82,16 @@ DB.getDB.then((DB) => { result, userid }; - DB.newResult(payment); + //console.log(payment); + const intent = await DB.getIntent(result.client_secret); + if(intent.description === "Subscription 1 Month"){ + //update profile subscription status + const profileid = getProfileId(req); + const isSubscriptor = await DB.isSubscriptor(profileid); + const updateR = await DB.updateProfileSubscription(profileid, !isSubscriptor); + console.log(updateR); + } + await DB.newResult(payment, result.client_secret); return res.send({ status: 'ok' diff --git a/routes/post.js b/routes/post.js index ce6bc43..b43b706 100644 --- a/routes/post.js +++ b/routes/post.js @@ -5,9 +5,9 @@ const DB = require("./../mongoDB.js"); const Post = require("./../def/post.js"); const Notifications = require("./../notifications.js"); -DB.getDB.then((DB)=>{ +DB.getDB.then((DB) => { - const getProfileId = (req)=>{ + const getProfileId = (req) => { return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id); } @@ -15,13 +15,22 @@ DB.getDB.then((DB)=>{ const profileid = getProfileId(req); let posts = await DB.getFeed(profileid); //Add non-organic posts - + return res.json(posts) }); router.get("/usr/:id", async (req, res) => { const profileId = req.params.id; - const posts = await DB.getPosts(profileId, getProfileId(req)); + const viewerProdileId = getProfileId(req); + const profile = await DB.getProfileCache(profileId); + const posts = await DB.getPosts(profileId, viewerProdileId); + if (profile.isCourse) { + //check for subscription + //const viewerProdile = await DB.getProfileCache(viewerProdileId); + //if (!viewerProdile.subscription || viewerProdile.subscription < (new Date()-1)) { + // return res.json([posts[posts.length - 1]]); + //} + } return res.json(posts); }); @@ -30,10 +39,10 @@ DB.getDB.then((DB)=>{ profileid: getProfileId(req), ...req.body } - if(post.toProfile && await DB.isGroupPrivate(post.toProfile)){ + if (post.toProfile && await DB.isGroupPrivate(post.toProfile)) { let requestProfile = getProfileId(req) + ""; let group = await DB.getProfileCache(post.toProfile); - if(!group.subscribed[requestProfile] && group._id != requestProfile){ + if (!group.subscribed[requestProfile] && group._id != requestProfile) { return res.json({ status: "You are not part of this private group", }); @@ -44,7 +53,7 @@ DB.getDB.then((DB)=>{ let dbr = await DB.newPost(postObj); post = postObj.toObj(); post._id = dbr.insertedId; - if(post.toProfile && post.toProfile != post.profileid){ + if (post.toProfile && post.toProfile != post.profileid) { Notifications.youGotANewPost(post) } return res.json({ @@ -131,7 +140,7 @@ DB.getDB.then((DB)=>{ const post = await DB.getPost(postId); return res.json({ status: "ok", - ... post + ...post }); });