Subscriptions

This commit is contained in:
aeroreyna
2022-02-04 22:54:48 -06:00
parent cb056a078a
commit 294cacf963
4 changed files with 78 additions and 12 deletions

View File

@@ -4,17 +4,59 @@ 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) => {
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;
});
}
}

View File

@@ -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 = {

View File

@@ -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'

View File

@@ -21,7 +21,16 @@ DB.getDB.then((DB)=>{
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);
});