diff --git a/.env b/.env new file mode 100644 index 0000000..1badf2f --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +JWT_SECRET=something +MONGODB_URI=mongodb+srv://adoodlz:Adoodlz@2020@cluster0.9vvab.mongodb.net/adoodlz-eg +cloud_name=adoodlz +api_key=465625776337811 +api_secret: AsBRTVfC2lFYFKwOkWCHFg1Jx-M +NODE_ENV=production +TELEGRAM_BOT_TOKEN=5445385879:AAHC_x1uliPDS6LS-ZdClG2uRnFhRW0eR-I \ No newline at end of file diff --git a/app/config/db.config.js b/app/config/db.config.js index fb4130c..af88d90 100644 --- a/app/config/db.config.js +++ b/app/config/db.config.js @@ -1,3 +1,3 @@ module.exports = { - url: "mongodb://localhost:27017/bezkoder_db" + url: process.env.MONGODB_URI, }; diff --git a/app/controllers/tutorial.controller.js b/app/controllers/tutorial.controller.js deleted file mode 100644 index ca7d245..0000000 --- a/app/controllers/tutorial.controller.js +++ /dev/null @@ -1,143 +0,0 @@ -const db = require("../models"); -const Tutorial = db.tutorials; - -// Create and Save a new Tutorial -exports.create = (req, res) => { - // Validate request - if (!req.body.title) { - res.status(400).send({ message: "Content can not be empty!" }); - return; - } - - // Create a Tutorial - const tutorial = new Tutorial({ - title: req.body.title, - description: req.body.description, - published: req.body.published ? req.body.published : false - }); - - // Save Tutorial in the database - tutorial - .save(tutorial) - .then(data => { - res.send(data); - }) - .catch(err => { - res.status(500).send({ - message: - err.message || "Some error occurred while creating the Tutorial." - }); - }); -}; - -// Retrieve all Tutorials from the database. -exports.findAll = (req, res) => { - const title = req.query.title; - var condition = title ? { title: { $regex: new RegExp(title), $options: "i" } } : {}; - - Tutorial.find(condition) - .then(data => { - res.send(data); - }) - .catch(err => { - res.status(500).send({ - message: - err.message || "Some error occurred while retrieving tutorials." - }); - }); -}; - -// Find a single Tutorial with an id -exports.findOne = (req, res) => { - const id = req.params.id; - - Tutorial.findById(id) - .then(data => { - if (!data) - res.status(404).send({ message: "Not found Tutorial with id " + id }); - else res.send(data); - }) - .catch(err => { - res - .status(500) - .send({ message: "Error retrieving Tutorial with id=" + id }); - }); -}; - -// Update a Tutorial by the id in the request -exports.update = (req, res) => { - if (!req.body) { - return res.status(400).send({ - message: "Data to update can not be empty!" - }); - } - - const id = req.params.id; - - Tutorial.findByIdAndUpdate(id, req.body, { useFindAndModify: false }) - .then(data => { - if (!data) { - res.status(404).send({ - message: `Cannot update Tutorial with id=${id}. Maybe Tutorial was not found!` - }); - } else res.send({ message: "Tutorial was updated successfully." }); - }) - .catch(err => { - res.status(500).send({ - message: "Error updating Tutorial with id=" + id - }); - }); -}; - -// Delete a Tutorial with the specified id in the request -exports.delete = (req, res) => { - const id = req.params.id; - - Tutorial.findByIdAndRemove(id, { useFindAndModify: false }) - .then(data => { - if (!data) { - res.status(404).send({ - message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!` - }); - } else { - res.send({ - message: "Tutorial was deleted successfully!" - }); - } - }) - .catch(err => { - res.status(500).send({ - message: "Could not delete Tutorial with id=" + id - }); - }); -}; - -// Delete all Tutorials from the database. -exports.deleteAll = (req, res) => { - Tutorial.deleteMany({}) - .then(data => { - res.send({ - message: `${data.deletedCount} Tutorials were deleted successfully!` - }); - }) - .catch(err => { - res.status(500).send({ - message: - err.message || "Some error occurred while removing all tutorials." - }); - }); -}; - -// Find all published Tutorials -exports.findAllPublished = (req, res) => { - Tutorial.find({ published: true }) - .then(data => { - res.send(data); - }) - .catch(err => { - res.status(500).send({ - message: - err.message || "Some error occurred while retrieving tutorials." - }); - }); -}; diff --git a/app/models/index.js b/app/models/index.js deleted file mode 100644 index 8372b21..0000000 --- a/app/models/index.js +++ /dev/null @@ -1,11 +0,0 @@ -const dbConfig = require("../config/db.config.js"); - -const mongoose = require("mongoose"); -mongoose.Promise = global.Promise; - -const db = {}; -db.mongoose = mongoose; -db.url = dbConfig.url; -db.tutorials = require("./tutorial.model.js")(mongoose); - -module.exports = db; diff --git a/app/models/tutorial.model.js b/app/models/tutorial.model.js deleted file mode 100644 index ed90dce..0000000 --- a/app/models/tutorial.model.js +++ /dev/null @@ -1,19 +0,0 @@ -module.exports = mongoose => { - var schema = mongoose.Schema( - { - title: String, - description: String, - published: Boolean - }, - { timestamps: true } - ); - - schema.method("toJSON", function() { - const { __v, _id, ...object } = this.toObject(); - object.id = _id; - return object; - }); - - const Tutorial = mongoose.model("tutorial", schema); - return Tutorial; -}; diff --git a/app/routes/turorial.routes.js b/app/routes/turorial.routes.js deleted file mode 100644 index 238f1e0..0000000 --- a/app/routes/turorial.routes.js +++ /dev/null @@ -1,28 +0,0 @@ -module.exports = app => { - const tutorials = require("../controllers/tutorial.controller.js"); - - var router = require("express").Router(); - - // Create a new Tutorial - router.post("/", tutorials.create); - - // Retrieve all Tutorials - router.get("/", tutorials.findAll); - - // Retrieve all published Tutorials - router.get("/published", tutorials.findAllPublished); - - // Retrieve a single Tutorial with id - router.get("/:id", tutorials.findOne); - - // Update a Tutorial with id - router.put("/:id", tutorials.update); - - // Delete a Tutorial with id - router.delete("/:id", tutorials.delete); - - // Create a new Tutorial - router.delete("/", tutorials.deleteAll); - - app.use("/api/tutorials", router); -}; diff --git a/models/User.js b/models/User.js new file mode 100644 index 0000000..85a2e29 --- /dev/null +++ b/models/User.js @@ -0,0 +1,55 @@ +const mongoose = require('mongoose'); + + +const UserSchema = new mongoose.Schema( + { + id: { + type: String, + unique: true, + }, + name: String, + mobile: { + type: String, + unique: true, + required: [true, "can't be blank"], + }, + version: String, + address: String, + cordinates : String, + password: String, + status: String, + balance: Number, + pending_balance: Number, + email: String, + details: String, + affiliater_id: String, + language: { + type: String, + default: 'EN', + }, + rate: { + type: Number, + default: 0, + }, + image: String, + otp: String, + role: String, + isAgent : { + type : Boolean , + default : false + }, + deviceId : String, + CorporateId : String, + firebase_token: String, + hitsDailyCount: [{ + day: String, + shares: Number, + clicks: Number, + }] + }, + { timestamps: true } +); + + + +module.exports = mongoose.model('User', UserSchema); diff --git a/package.json b/package.json index 97ffd20..447fc24 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Node.js Restful CRUD API with Node.js, Express and MongoDB", "main": "server.js", "scripts": { + "start": "nodemon server.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ @@ -16,9 +17,22 @@ "author": "bezkoder", "license": "ISC", "dependencies": { + "aws-sdk": "^2.1131.0", + "bcrypt": "^5.0.1", + "bcryptjs": "^2.4.3", "body-parser": "^1.19.0", + "cloudinary": "^1.29.1", "cors": "^2.8.5", + "dotenv": "^16.0.0", "express": "^4.17.1", - "mongoose": "^5.8.10" + "jsonwebtoken": "^8.5.1", + "mongoose": "^5.8.10", + "multer": "^1.4.4", + "multer-s3": "^2.10.0", + "node-telegram-bot-api": "^0.57.0", + "nodemon": "^2.0.15", + "path": "^0.12.7", + "streamifier": "^0.1.1", + "uuid": "^8.3.2" } } diff --git a/server.js b/server.js index d49174c..d62ca92 100644 --- a/server.js +++ b/server.js @@ -1,41 +1,150 @@ const express = require("express"); -// const bodyParser = require("body-parser"); /* deprecated */ +const mongoose = require("mongoose") const cors = require("cors"); - +require("dotenv").config(); const app = express(); +const TelegramBot = require('node-telegram-bot-api'); +const User = require('./models/User'); + var corsOptions = { - origin: "http://localhost:8081" + origin: "http://localhost:8081", }; app.use(cors(corsOptions)); -// parse requests of content-type - application/json -app.use(express.json()); /* bodyParser.json() is deprecated */ +app.use(express.json()); + +app.use( + express.urlencoded({ extended: true }) +); -// parse requests of content-type - application/x-www-form-urlencoded -app.use(express.urlencoded({ extended: true })); /* bodyParser.urlencoded() is deprecated */ -const db = require("./app/models"); -db.mongoose - .connect(db.url, { +mongoose + .connect(process.env.MONGODB_URI, { useNewUrlParser: true, - useUnifiedTopology: true + useUnifiedTopology: true, }) .then(() => { console.log("Connected to the database!"); }) - .catch(err => { + .catch((err) => { console.log("Cannot connect to the database!", err); process.exit(); }); // simple route app.get("/", (req, res) => { - res.json({ message: "Welcome to bezkoder application." }); + res.json({ message: "Welcome." }); }); -require("./app/routes/turorial.routes")(app); + + +async function sendTeleram() +{ + const token = process.env.TELEGRAM_BOT_TOKEN; + + // Create a bot that uses 'polling' to fetch new updates + const bot = new TelegramBot(token, {polling: true}); + let mobile = ''; + + bot.on("message" , (msg)=>{ + if(msg.text=="/start") + { + bot.sendMessage(msg.chat.id, 'هل يمكننا الوصول الى رقم هاتفك لمساعدتك ؟', requestPhoneKeyboard); + } + + }) + + +bot.onText("إلغاء" , (msg)=>{ + bot.sendMessage(msg.chat.id,' من فضلك. لتأكيد حسابك يرجى مشاركة رقم هاتفك', requestPhoneKeyboard); +}) + + const requestPhoneKeyboard = { + "reply_markup": { + "one_time_keyboard": true, + "keyboard": [ + [{ + text: "إرسال رقم هاتفى", + request_contact: true, + one_time_keyboard: true + }], + ["إلغاء"] + ] + } +}; + +bot.on('message' , async(msg)=>{ + if(msg.text != "/start"){ + if(msg.contact){ + if(msg.contact.user_id == msg.chat.id) + { + if(msg.contact.phone_number && msg.contact.phone_number != "") + { + let phone = msg.contact.phone_number.replace('+', ""); + mobile=phone; + const user = await User.findOne({mobile:phone}); + if(user) + { + bot.sendMessage(msg.chat.id , `مرحبا ${msg.chat.first_name}`); + setTimeout(() => { + bot.sendMessage(msg.chat.id, `كيف يمكننى مساعدتك الان ؟`, { + 'reply_markup': { + 'keyboard': [['الحصول على رمز التأكيد الخاص بي'],['رمز إعادة تعيين كلمة السر']], + resize_keyboard: true, + one_time_keyboard: true, + force_reply: true, + } + }); + }, 300); + + }else + { + bot.sendMessage(msg.chat.id, 'عفوا,رقم الهاتف هذا غير مرتبط بأي حساب لدينا.', requestPhoneKeyboard); + } + } + + }else + { + bot.sendMessage(msg.chat.id, 'عفوا, يجب عليك استخدام الرقم المسجل على تليجرام.', requestPhoneKeyboard); + } + }else if(msg.text == "الحصول على رمز التأكيد الخاص بي") + { + const user = await User.findOne({mobile:mobile}); + if(user) + { + bot.sendMessage(msg.chat.id , 'رمز تفعيل أدودلز الخاص بحسابك هو : ') + setTimeout(() => { + bot.sendMessage(msg.chat.id , `${user.otp}`) + }, 300); + + setTimeout(() => { + bot.sendMessage(msg.chat.id , "يمكنك تفعيل حسابك الأن وتنفيذ المهمات وكسب النقاط وتبديلها بكوبونات🥳") + }, 2000); + } + }else if(msg.text == 'رمز إعادة تعيين كلمة السر'){ + const user = await User.findOne({mobile:mobile}); + if(user) + { + bot.sendMessage(msg.chat.id , 'رمز إعادة تعيين كلمة السر الخاص بك هو : ') + setTimeout(() => { + bot.sendMessage(msg.chat.id , `${user.otp}`) + }, 200); + } + } + else + { + bot.sendMessage(msg.chat.id,' من فضلك. لتأكيد حسابك يرجى مشاركة رقم هاتفك', requestPhoneKeyboard); + } + } + +}) + +} +sendTeleram(); + + // set port, listen for requests const PORT = process.env.PORT || 8080;