Subscriptions
This commit is contained in:
@@ -4,14 +4,56 @@ postDB = (DB)=>{
|
|||||||
DB.paymentsCols = DB.db.db(DBName).collection("payments");
|
DB.paymentsCols = DB.db.db(DBName).collection("payments");
|
||||||
|
|
||||||
DB.newIntent = (intent) => {
|
DB.newIntent = (intent) => {
|
||||||
|
intent.ts = new Date();
|
||||||
return DB.paymentsCols.insertOne(intent).catch((err)=>{
|
return DB.paymentsCols.insertOne(intent).catch((err)=>{
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DB.newResult = (paymentResult) => {
|
DB.getIntent = (client_secret) => {
|
||||||
return DB.paymentsCols.insertOne(paymentResult).catch((err)=>{
|
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);
|
console.log(err);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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
|
//Groups
|
||||||
DB.getGroups = async (excludePrivate = false) => {
|
DB.getGroups = async (excludePrivate = false) => {
|
||||||
let query = {
|
let query = {
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ DB.getDB.then((DB) => {
|
|||||||
paymentIntent,
|
paymentIntent,
|
||||||
userid,
|
userid,
|
||||||
price,
|
price,
|
||||||
description
|
description,
|
||||||
|
client_secret: paymentIntent.client_secret,
|
||||||
};
|
};
|
||||||
DB.newIntent(intent);
|
DB.newIntent(intent);
|
||||||
|
|
||||||
@@ -81,7 +82,16 @@ DB.getDB.then((DB) => {
|
|||||||
result,
|
result,
|
||||||
userid
|
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({
|
return res.send({
|
||||||
status: 'ok'
|
status: 'ok'
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ const DB = require("./../mongoDB.js");
|
|||||||
const Post = require("./../def/post.js");
|
const Post = require("./../def/post.js");
|
||||||
const Notifications = require("./../notifications.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);
|
return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,7 +21,16 @@ DB.getDB.then((DB)=>{
|
|||||||
|
|
||||||
router.get("/usr/:id", async (req, res) => {
|
router.get("/usr/:id", async (req, res) => {
|
||||||
const profileId = req.params.id;
|
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);
|
return res.json(posts);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -30,10 +39,10 @@ DB.getDB.then((DB)=>{
|
|||||||
profileid: getProfileId(req),
|
profileid: getProfileId(req),
|
||||||
...req.body
|
...req.body
|
||||||
}
|
}
|
||||||
if(post.toProfile && await DB.isGroupPrivate(post.toProfile)){
|
if (post.toProfile && await DB.isGroupPrivate(post.toProfile)) {
|
||||||
let requestProfile = getProfileId(req) + "";
|
let requestProfile = getProfileId(req) + "";
|
||||||
let group = await DB.getProfileCache(post.toProfile);
|
let group = await DB.getProfileCache(post.toProfile);
|
||||||
if(!group.subscribed[requestProfile] && group._id != requestProfile){
|
if (!group.subscribed[requestProfile] && group._id != requestProfile) {
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "You are not part of this private group",
|
status: "You are not part of this private group",
|
||||||
});
|
});
|
||||||
@@ -44,7 +53,7 @@ DB.getDB.then((DB)=>{
|
|||||||
let dbr = await DB.newPost(postObj);
|
let dbr = await DB.newPost(postObj);
|
||||||
post = postObj.toObj();
|
post = postObj.toObj();
|
||||||
post._id = dbr.insertedId;
|
post._id = dbr.insertedId;
|
||||||
if(post.toProfile && post.toProfile != post.profileid){
|
if (post.toProfile && post.toProfile != post.profileid) {
|
||||||
Notifications.youGotANewPost(post)
|
Notifications.youGotANewPost(post)
|
||||||
}
|
}
|
||||||
return res.json({
|
return res.json({
|
||||||
@@ -131,7 +140,7 @@ DB.getDB.then((DB)=>{
|
|||||||
const post = await DB.getPost(postId);
|
const post = await DB.getPost(postId);
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "ok",
|
status: "ok",
|
||||||
... post
|
...post
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user