Skip to content

Commit

Permalink
fix bug to save merchat id and tokens in the db
Browse files Browse the repository at this point in the history
  • Loading branch information
ajibadeabd committed Jul 16, 2024
1 parent c86ed62 commit 0b37c70
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 59 deletions.
14 changes: 8 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 () {
Expand Down
84 changes: 37 additions & 47 deletions database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand All @@ -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 }

Check failure on line 49 in database/index.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

database/index.js#L49

You have a misspelled word: upsert on Identifier
)
// 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 }) {

Check failure on line 63 in database/index.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

database/index.js#L63

You have a misspelled word: Oauth on Identifier
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)
}
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions helpers/ORMs/Mongoose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)

Check failure on line 12 in helpers/ORMs/Mongoose/index.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

helpers/ORMs/Mongoose/index.js#L12

You have a misspelled word: mongodb on Template

class Database {
constructor() {
this.Mongoose = mongoose;
this._connect();
}

_connect() {
this.Mongoose.connect(
`mongodb+srv://${username}:${password}@${server}/${database}`
"mongodb://localhost:27017/salla-db"

Check failure on line 21 in helpers/ORMs/Mongoose/index.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

helpers/ORMs/Mongoose/index.js#L21

You have a misspelled word: mongodb on String
// `mongodb+srv://${username}:${password}@${server}/${database}`

Check failure on line 22 in helpers/ORMs/Mongoose/index.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

helpers/ORMs/Mongoose/index.js#L22

You have a misspelled word: mongodb on Comment
)
.then(() => {
console.log("Database connection successful");
Expand Down
2 changes: 1 addition & 1 deletion helpers/ORMs/Mongoose/schemas/oauthtokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const oauthtokenSchema = new mongoose.Schema({
access_token: String,
expires_in: Number,
refresh_token: String,
});
}, { timestamps: true });

Check failure on line 10 in helpers/ORMs/Mongoose/schemas/oauthtokens.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

helpers/ORMs/Mongoose/schemas/oauthtokens.js#L10

You have a misspelled word: timestamps on Identifier
module.exports = mongoose.model("oAuthToken", oauthtokenSchema);
4 changes: 2 additions & 2 deletions helpers/ORMs/Mongoose/schemas/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Check failure on line 10 in helpers/ORMs/Mongoose/schemas/users.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

helpers/ORMs/Mongoose/schemas/users.js#L10

You have a misspelled word: timestamps on Identifier
module.exports = mongoose.model("User", usersSchema);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check failure on line 18 in package.json

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

package.json#L18

You have a misspelled word: nodemon on String
},
"license": "ISC",
"dependencies": {
Expand Down

0 comments on commit 0b37c70

Please sign in to comment.