diff --git a/Payments.js b/Payments.js new file mode 100644 index 0000000..0a3c07b --- /dev/null +++ b/Payments.js @@ -0,0 +1,44 @@ +const Stripe = require('stripe'); + +const stripe = Stripe(process.env.STRIPE); + +const addNewCustomer = async (email, profile) => { + const customer = await Stripe.customers.create({ + email, + description: profile ? profile.firstName + " " + profile.lastName : "new costumer", + }); + return customer; +} + +const getCustomerByID = async (id) => { + const customer = await Stripe.customers.retrieve(id); + return customer; +} + +const addNewCustomerCard = async (customer, cardInfo) => { + let template = { + "address_city": cardInfo.address_city, + "address_country": cardInfo.address_country, + "address_line1": cardInfo.address_line1, + "address_line2": cardInfo.address_line2, + "address_state": cardInfo.address_state, + "address_zip": cardInfo.address_zip, + customer, + "exp_month": cardInfo.exp_month, + "exp_year": cardInfo.exp_year, + "name": cardInfo.name, + }; + const card = await stripe.customers.createSource( + 'cus_KKsGI28Nrbm61K', + template + ); + return card; +} + +const getCustomerCard = async (customer, card) => { + const card = await stripe.customers.retrieveSource( + customer, + card + ); + return card; +} diff --git a/PromotionalPosts.js b/PromotionalPosts.js new file mode 100644 index 0000000..ef2a468 --- /dev/null +++ b/PromotionalPosts.js @@ -0,0 +1,31 @@ + +const DB = require("./mongoDB.js"); +const Post = require("./def/post.js"); + +let PromotionalPosts = {}; + +DB.getDB.then((DB)=>{ + PromotionalPosts.PopularGroups = async () => { + let groups = DB.getGroups(false); + + } + + PromotionalPosts.PopularCourses = async () => { + + } + + PromotionalPosts.ImportantNews = async () => { + + } + + PromotionalPosts.IntroPost = async () => { + + } + + PromotionalPosts.continueWatchingCourse = async () => { + + } + +}); + +module.exports = PromotionalPosts; \ No newline at end of file diff --git a/dbTools/profile.js b/dbTools/profile.js index fd3e977..4c51d72 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -134,12 +134,16 @@ userDB = (DB) => { } //Groups - DB.getGroups = async () => { - let r = await DB.profileCols.find({isGroup: true, isCourse: {$ne: true}}) - .sort({ lastUpdate: -1 }).limit(10) - .toArray().catch((err) => { - console.log(err); - return false; + DB.getGroups = async (excludePrivate = false) => { + let query = { + isGroup: true, + isCourse: {$ne: true}, + }; + if(excludePrivate) query.isPrivate = false; + let r = await DB.profileCols.find(query).sort({ lastUpdate: -1 }).limit(10) + .toArray().catch((err) => { + console.log(err); + return false; }); return r; } diff --git a/index.js b/index.js index e008853..8509dc4 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,9 @@ DB.getDB.then((DB)=>{ const password = req.query.password || req.body.password; const email = req.query.email || req.body.email; const profile = req.query.profile || req.body.profile; - if(!username || !password || !email) return res.json({status: "fail"}) + if(!username || !password || !email) return res.json({status: "fail"}); + //Check if the new user has an invitation + if(!await DB.getInvitation(email)) return res.json({status: "Not invitation found!"}); //const hashedPassword = await bcrypt.hash(password, 10); const hashedPassword = await bcrypt.hash(password, 10); const response = await DB.newUser({ diff --git a/mongoDB.js b/mongoDB.js index 807c929..f6b5c36 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -17,6 +17,7 @@ const getDB = new Promise((resolve, reject) => { console.log("Connected to DB!"); DB.usersCol = db.db(DBName).collection("users"); DB.tokensCol = db.db(DBName).collection("tokens"); + DB.invitationCol = db.db(DBName).collection("invitation"); DB.checkSessionOnDB = async (session_id, user_sid)=>{ const temp_id = new mongo.ObjectID(session_id); @@ -48,6 +49,30 @@ const getDB = new Promise((resolve, reject) => { return usernamesCache[userid]; } + DB.newInvitation = (userid, name, email)=>{ + const invitation = { + responsable: new mongo.ObjectID(userid), + email, + name, + ts: new Date() + } + return DB.invitationCol.insertOne(invitation).catch((err)=>{ + if (err.name === 'MongoError' && err.code === 11000) { + // Duplicate invite email + return "This email already has been invited"; + } + console.log(err); + return "System Error"; + }); + }; + + DB.getInvitation = (email)=>{ + return DB.invitationCol.findOne({email}).catch((err)=>{ + console.log(err); + return "System Error"; + }); + }; + DB.newUser = (userInformation)=>{ return DB.usersCol.insertOne(userInformation).catch((err)=>{ if (err.name === 'MongoError' && err.code === 11000) { diff --git a/package-lock.json b/package-lock.json index 917f07b..d68fa55 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,8 @@ "dotenv": "^8.2.0", "express": "^4.17.1", "mongodb": "^3.6.3", - "nodemailer": "^6.6.3" + "nodemailer": "^6.6.3", + "stripe": "^8.178.0" } }, "node_modules/@mapbox/node-pre-gyp": { @@ -38,6 +39,11 @@ "node-pre-gyp": "bin/node-pre-gyp" } }, + "node_modules/@types/node": { + "version": "16.10.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.2.tgz", + "integrity": "sha512-zCclL4/rx+W5SQTzFs9wyvvyCwoK9QtBpratqz2IYJ3O8Umrn0m3nsTv0wQBk9sRGpvUe9CwPDrQFB10f1FIjQ==" + }, "node_modules/abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -1090,6 +1096,18 @@ "node": ">=0.10.0" } }, + "node_modules/stripe": { + "version": "8.178.0", + "resolved": "https://registry.npmjs.org/stripe/-/stripe-8.178.0.tgz", + "integrity": "sha512-Yk31NdIKf+MKTOdS2CTUIAHzUHOQwayoJFHBkrKGo7bJAlkPFzrIeOORH8SpduoXPZF8mq0JA7qNcFuFPBqabA==", + "dependencies": { + "@types/node": ">=8.1.0", + "qs": "^6.6.0" + }, + "engines": { + "node": "^8.1 || >=10.*" + } + }, "node_modules/tar": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/tar/-/tar-6.1.0.tgz", @@ -1191,6 +1209,11 @@ "tar": "^6.1.0" } }, + "@types/node": { + "version": "16.10.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.2.tgz", + "integrity": "sha512-zCclL4/rx+W5SQTzFs9wyvvyCwoK9QtBpratqz2IYJ3O8Umrn0m3nsTv0wQBk9sRGpvUe9CwPDrQFB10f1FIjQ==" + }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -1990,6 +2013,15 @@ "ansi-regex": "^2.0.0" } }, + "stripe": { + "version": "8.178.0", + "resolved": "https://registry.npmjs.org/stripe/-/stripe-8.178.0.tgz", + "integrity": "sha512-Yk31NdIKf+MKTOdS2CTUIAHzUHOQwayoJFHBkrKGo7bJAlkPFzrIeOORH8SpduoXPZF8mq0JA7qNcFuFPBqabA==", + "requires": { + "@types/node": ">=8.1.0", + "qs": "^6.6.0" + } + }, "tar": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/tar/-/tar-6.1.0.tgz", diff --git a/package.json b/package.json index 70a8b70..e725622 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "dotenv": "^8.2.0", "express": "^4.17.1", "mongodb": "^3.6.3", - "nodemailer": "^6.6.3" + "nodemailer": "^6.6.3", + "stripe": "^8.178.0" } } diff --git a/routes/post.js b/routes/post.js index 8aca38d..c367f15 100644 --- a/routes/post.js +++ b/routes/post.js @@ -14,6 +14,8 @@ DB.getDB.then((DB)=>{ router.get("/", async (req, res) => { const profileid = getProfileId(req); let posts = await DB.getFeed(profileid); + //Add non-organic posts + return res.json(posts) }); diff --git a/routes/profile.js b/routes/profile.js index 683a4c8..17553c6 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -24,7 +24,7 @@ DB.getDB.then((DB)=>{ }); }); - router.get("/new", async (req, res) => { + router.get("/new", async (req, res) => { //Deprecated please use route post("/") let profile = { userid: getUserId(req), ... req.query.content @@ -37,6 +37,47 @@ DB.getDB.then((DB)=>{ }); }); + 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 + 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({