From 0b37c70605b98a83c1e13dbeec97e0d9be8f251d Mon Sep 17 00:00:00 2001 From: ajibadeabd <43596899+ajibadeabd@users.noreply.github.com> Date: Tue, 16 Jul 2024 16:13:05 +0100 Subject: [PATCH] fix bug to save merchat id and tokens in the db --- app.js | 14 ++-- database/index.js | 84 +++++++++----------- helpers/ORMs/Mongoose/index.js | 9 ++- helpers/ORMs/Mongoose/schemas/oauthtokens.js | 2 +- helpers/ORMs/Mongoose/schemas/users.js | 4 +- package.json | 2 +- 6 files changed, 56 insertions(+), 59 deletions(-) diff --git a/app.js b/app.js index cd96f1e..1445112 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,7 @@ const passport = require("passport"); const consolidate = require("consolidate"); const getUnixTimestamp = require("./helpers/getUnixTimestamp"); const bodyParser = require("body-parser"); -const port = process.argv[2] || 8081; +const port = process.argv[2] || 8082; /* Create a .env file in the root directory of your project. @@ -59,15 +59,15 @@ SallaAPI.onAuth(async (accessToken, refreshToken, expires_in, data) => { verified_at: getUnixTimestamp(), password: "", remember_token: "", - }); + }); await SallaDatabase.saveOauth( { - merchant: data.store.id, + merchant: data.merchant.id, access_token: accessToken, expires_in: expires_in, refresh_token: refreshToken, + user_id }, - user_id ); }) .catch((err) => { @@ -207,8 +207,10 @@ app.get("/customers", ensureAuthenticated, async function (req, res) { // logout from passport app.get("/logout", function (req, res) { SallaAPI.logout(); - req.logout(); - res.redirect("/"); + req.logout(function(err) { + if (err) { return next(err); } + res.redirect("/"); + }); }); app.listen(port, function () { diff --git a/database/index.js b/database/index.js index 9e48b35..5a8b8f1 100644 --- a/database/index.js +++ b/database/index.js @@ -18,14 +18,7 @@ class SallaDatabase { if (this.DATABASE_ORM == "TypeORM") { var userRepository = this.connection.getRepository("User"); userRepository - .save({ - username: data.name, - email: data.email, - email_verified_at: getUnixTimestamp(), - verified_at: getUnixTimestamp(), - password: "", - remember_token: "", - }) + .save(data) .then(function (savedUser) { console.log("User has been saved: ", savedUser); console.log("Now lets load all users: "); @@ -43,66 +36,63 @@ class SallaDatabase { where: { email: data.email }, })) ) { - let user = await this.connection.models.User.create({ - username: data.name, - email: data.email, - email_verified_at: getUnixTimestamp(), - verified_at: getUnixTimestamp(), - password: "", - remember_token: "", - }); + let user = await this.connection.models.User.create(data); return user.id; } } if (this.DATABASE_ORM == "Mongoose") { + let userObj try { - let userObj = this.connection.Mongoose.userModel({ - username: data.name, - email: data.email, - email_verified_at: getUnixTimestamp(), - verified_at: getUnixTimestamp(), - password: "", - remember_token: "", - }); - - userObj.save(); + userObj = await this.connection.Mongoose.models.User.findOneAndUpdate( + { email:data.email }, + data , + { upsert: true, new: true } + ) + // userObj = await this.connection.Mongoose.models.User(data) + // await userObj.save(); + // console.log(userObj) + console.log("user has been created") return userObj._id; - } catch (err) {} + } catch (err) { + // if(err.code=="11000"){ + // return (await this.connection.Mongoose.models.User.findOne({ email:data.email }))._id + // } + } } } - async saveOauth(data, user_id) { + async saveOauth({user_id, ...data }) { if (this.DATABASE_ORM == "Sequelize") { if ( - // if not found then create new user - !(await this.connection.models.User.findOne({ + // if not found then create new user exist, create an oath token + await this.connection.models.User.findOne({ where: { email: data.email }, - })) + }) ) { this.connection.models.OauthTokens.create({ user_id: user_id, - merchant: data.store.id, - access_token: data.accessToken, - expires_in: data.expires_in, - refresh_token: data.refreshToken, + ...data }) - .then((data) => {}) + .then((data) => { + return data + }) .catch((err) => { - console.log("error inserting oath toekn", err); + console.log("error inserting oath token", err); }); } } if (this.DATABASE_ORM == "Mongoose") { try { - let oauthobj = this.connection.Mongoose.oauthTokenModel({ - user: user_id, - merchant: data.store.id, - access_token: data.accessToken, - expires_in: data.expires_in, - refresh_token: data.refreshToken, - }); - - oauthobj.save(); - } catch (err) {} + return this.connection.Mongoose.models.oAuthToken.findOneAndUpdate( + { user: user_id }, + { user: user_id, ...data }, + { upsert: true, new: true } + ).then(results => { + console.log(results); + return results + }); + } catch (err) { + console.log(err) + } } } } diff --git a/helpers/ORMs/Mongoose/index.js b/helpers/ORMs/Mongoose/index.js index 9593ea4..accba34 100644 --- a/helpers/ORMs/Mongoose/index.js +++ b/helpers/ORMs/Mongoose/index.js @@ -6,15 +6,20 @@ const username = process.env.DATABASE_USERNAME; // REPLACE WITH YOUR DB USERNAME const password = process.env.DATABASE_PASSWORD; // REPLACE WITH YOUR DB PASSWORD require("./schemas/users"); require("./schemas/oauthtokens"); + + + +console.log( `mongodb+srv://${username}:${password}@${server}/${database}`) + class Database { constructor() { this.Mongoose = mongoose; this._connect(); } - _connect() { this.Mongoose.connect( - `mongodb+srv://${username}:${password}@${server}/${database}` + "mongodb://localhost:27017/salla-db" + // `mongodb+srv://${username}:${password}@${server}/${database}` ) .then(() => { console.log("Database connection successful"); diff --git a/helpers/ORMs/Mongoose/schemas/oauthtokens.js b/helpers/ORMs/Mongoose/schemas/oauthtokens.js index b1d63c7..daa1670 100644 --- a/helpers/ORMs/Mongoose/schemas/oauthtokens.js +++ b/helpers/ORMs/Mongoose/schemas/oauthtokens.js @@ -7,5 +7,5 @@ const oauthtokenSchema = new mongoose.Schema({ access_token: String, expires_in: Number, refresh_token: String, -}); +}, { timestamps: true }); module.exports = mongoose.model("oAuthToken", oauthtokenSchema); diff --git a/helpers/ORMs/Mongoose/schemas/users.js b/helpers/ORMs/Mongoose/schemas/users.js index 5ebe86d..6c20523 100644 --- a/helpers/ORMs/Mongoose/schemas/users.js +++ b/helpers/ORMs/Mongoose/schemas/users.js @@ -2,10 +2,10 @@ let mongoose = require("mongoose"); const usersSchema = new mongoose.Schema({ username: String, - email: String, + email: { type: String, unique: true }, email_verified_at: Number, verified_at: Number, password: String, remember_token: String, -}); +}, { timestamps: true }); module.exports = mongoose.model("User", usersSchema); diff --git a/package.json b/package.json index 3e9dfb8..591ceb4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "serve": "salla app serve", - "dev":"node app.js" + "dev":"nodemon app.js" }, "license": "ISC", "dependencies": {