Commit 3198b857 authored by kevinflo's avatar kevinflo

moved a lot of code towards new postgres db use

parent f103acea
......@@ -20,25 +20,26 @@ var Raven = require("raven");
passport.use(
new LocalStrategy(function(email, password, done) {
db.query("SELECT person_uuid, password FROM person WHERE email = ?", email)
.then(rows => {
if (!rows[0]) {
db.query("SELECT person_uuid, password FROM person WHERE email = $1", [email])
.then(result => {
if (!result.rows[0]) {
return done(null, false, { message: "User not found" });
}
bcrypt.compare(password, rows[0].password, function(error, isMatch) {
bcrypt.compare(password, result.rows[0].password, function(error, isMatch) {
if (error) {
return done(error);
}
if (isMatch) {
return done(null, _.pick(rows[0], "person_uuid"));
return done(null, _.pick(result.rows[0], "person_uuid"));
} else {
return done(null, false, { message: "Incorrect password" });
}
});
})
.catch(err => {
console.error("local strategy error", err)
return done(err);
});
})
......@@ -50,12 +51,19 @@ passport.serializeUser(function(user, done) {
passport.deserializeUser(function(uuid, done) {
db.query(
"SELECT person_id, test_segment_pro_flow, is_update_newsletter_emailable, is_dataset_participation_consented, person_uuid, is_pro, email, email_confirmed_at, is_sponsorship_banner_hidden, is_vestiture_emailable, telegram_username, stripe_plan_id FROM person WHERE person_uuid = ?",
uuid,
function(err, rows) {
return done(err, rows[0]);
"SELECT person_id, test_segment_pro_flow, is_update_newsletter_emailable, is_dataset_participation_consented, person_uuid, is_pro, email, email_confirmed_at, is_sponsorship_banner_hidden, is_vestiture_emailable, telegram_username, stripe_plan_id FROM person WHERE person_uuid = $1",
[uuid]
).then(result => {
if (!result.rows[0]){
var noUserError = new Error("No user");
console.error(noUserError);
return done(noUserError);
}
);
return done(null, result.rows[0]);
}).catch(err => {
return done(err);
});
});
var app = express();
......
var pg = require("pg");
var pgPromise = require("pg-promise")();
var pgConfig = require("../secrets/pgConfig")
const pool = new pg.Pool(pgConfig);
module.exports = {
query: (text, params) => pool.query(text, params),
pool
pool,
helpers: pgPromise.helpers
};
......@@ -1464,6 +1464,11 @@
"resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz",
"integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc="
},
"manakin": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/manakin/-/manakin-0.5.1.tgz",
"integrity": "sha1-xKcRb2sA3z1fGjetPKUV0iBlplg="
},
"md5": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/md5/-/md5-2.2.1.tgz",
......@@ -1660,11 +1665,32 @@
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz",
"integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc="
},
"pg-escape": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/pg-escape/-/pg-escape-0.2.0.tgz",
"integrity": "sha1-ZVlMFpFlm0q24Mu/nVB0S+R0mY4="
},
"pg-minify": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/pg-minify/-/pg-minify-0.5.4.tgz",
"integrity": "sha512-GHB2v4OiMHDgwiHH86ZWNfvgEPVijrnfuWLQocseX6Zlf30k+x0imA65zBy4skIpEwfBBEplIEEKP4n3q9KkVA=="
},
"pg-pool": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.3.tgz",
"integrity": "sha1-wCIDLIlJ8xKk+R+2QJzgQHa+Mlc="
},
"pg-promise": {
"version": "8.4.5",
"resolved": "https://registry.npmjs.org/pg-promise/-/pg-promise-8.4.5.tgz",
"integrity": "sha512-0KqI14s/y/KN142T+ingQugXwz8OZGkZIQESXLEN1Ei1kq9sZWpatAGNO6/Vr9tsZp6UN4a0V9YsHaLUszNyxw==",
"requires": {
"manakin": "0.5.1",
"pg": "7.4.3",
"pg-minify": "0.5.4",
"spex": "2.0.2"
}
},
"pg-types": {
"version": "1.12.1",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-1.12.1.tgz",
......@@ -1891,6 +1917,11 @@
"amdefine": ">=0.0.4"
}
},
"spex": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/spex/-/spex-2.0.2.tgz",
"integrity": "sha512-LU6TS3qTEpRth+FnNs/fIWEmridYN7JmaN2k1Jk31XVC4ex7+wYxiHMnKguRxS7oKjbOFl4H6seeWNDFFgkVRg=="
},
"split": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz",
......
......@@ -27,6 +27,8 @@
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"pg": "^7.4.3",
"pg-escape": "^0.2.0",
"pg-promise": "^8.4.5",
"raven": "^2.6.3",
"stripe": "^6.2.1",
"uuid": "^3.3.2"
......
......@@ -21,8 +21,8 @@ router.post("/", function(req, res) {
}
db.query(
"SELECT test_segment_pro_flow FROM person WHERE person_id = ?",
req.user.person_id,
"SELECT test_segment_pro_flow FROM person WHERE person_id = $1",
[req.user.person_id],
function(err, rows) {
if (err) {
throw err;
......@@ -61,7 +61,7 @@ router.post("/", function(req, res) {
},
function(err, subscription) {
db.query(
"UPDATE person SET is_pro = ?, stripe_plan_id = ?, stripe_subscription_id = ?, stripe_customer_id = ?, stripe_subscription_created_at = ?, stripe_subscription_period_start_at = ?, stripe_subscription_period_end_at = ? WHERE person_id = ?",
"UPDATE person SET is_pro = $1, stripe_plan_id = $2, stripe_subscription_id = $3, stripe_customer_id = $4, stripe_subscription_created_at = $5, stripe_subscription_period_start_at = $6, stripe_subscription_period_end_at = $7 WHERE person_id = $8",
[
true,
planId,
......
......@@ -4,6 +4,7 @@ var router = new Router();
var normalizers = require("../utils/SQLnormalizers");
var mainUtils = require("../utils/mainUtils");
var db = require("../db/index");
var _ = require("lodash")
module.exports = router;
......@@ -95,38 +96,38 @@ router.get("/allstate", async function(req, res) {
} else {
try {
var grantsPromise = db.query(
"select * from gra where fk_person_id = ? and is_removed = 0",
req.user.person_id
"select * from gra where fk_person_id = $1 and is_removed = false",
[req.user.person_id]
);
var exercisesPromise = db.query(
"select * from exercise where fk_person_id = ? and is_removed = 0",
req.user.person_id
"select * from exercise where fk_person_id = $1 and is_removed = false",
[req.user.person_id]
);
var salesPromise = db.query(
"select * from sale where fk_person_id = ? and is_removed = 0",
req.user.person_id
"select * from sale where fk_person_id = $1 and is_removed = false",
[req.user.person_id]
);
var companiesPromise = db.query(
"select * from company where fk_person_id = ? and is_removed = 0",
req.user.person_id
"select * from company where fk_person_id = $1 and is_removed = false",
[req.user.person_id]
);
var allRows = await Promise.all(
var allResults = await Promise.all([
grantsPromise,
exercisesPromise,
salesPromise,
companiesPromise
);
]);
var normalizedGrantRows = _.map(allRows[0], normalizers.grant.toJSON);
var exerciseRows = allRows[1];
mainUtils.addSourceGrantUuidsToExercises(allRows[0], exerciseRows);
var normalizedGrantRows = _.map(allResults[0].rows, normalizers.grant.toJSON);
var exerciseRows = allResults[1].rows;
mainUtils.addSourceGrantUuidsToExercises(allResults[0].rows, exerciseRows);
var normalizedExerciseRows = _.map(
exerciseRows,
normalizers.exercise.toJSON
);
var normalizedSaleRows = _.map(allRows[2], normalizers.sale.toJSON);
var normalizedCompanyRows = _.map(allRows[3], normalizers.company.toJSON);
var normalizedSaleRows = _.map(allResults[2].rows, normalizers.sale.toJSON);
var normalizedCompanyRows = _.map(allResults[3].rows, normalizers.company.toJSON);
res.json({
grants: normalizedGrantRows,
......@@ -135,6 +136,7 @@ router.get("/allstate", async function(req, res) {
company: normalizedCompanyRows[0]
});
} catch (err) {
console.error(err)
res.status(400).json({ errors: [err && err.message] });
}
}
......
......@@ -18,7 +18,7 @@ router.post("/banner", function(req, res) {
}
db.query(
"UPDATE person SET is_sponsorship_banner_hidden = ? WHERE person_id = ?",
"UPDATE person SET is_sponsorship_banner_hidden = $1 WHERE person_id = $2",
[hide, req.user.person_id],
function(err, rows) {
if (err) {
......@@ -46,7 +46,7 @@ router.post("/vestitureemails", function(req, res) {
}
db.query(
"UPDATE person SET is_vestiture_emailable = ? WHERE person_id = ?",
"UPDATE person SET is_vestiture_emailable = $1 WHERE person_id = $2",
[enabled, req.user.person_id],
function(err, rows) {
if (err) {
......@@ -72,7 +72,7 @@ router.post("/telegramusername", function(req, res) {
}
db.query(
"UPDATE person SET telegram_username = ? WHERE person_id = ?",
"UPDATE person SET telegram_username = $1 WHERE person_id = $2",
[username, req.user.person_id],
function(err, rows) {
if (err) {
......
......@@ -6,6 +6,7 @@ var emailUtils = require("../utils/emailUtils");
var crypto = require("crypto");
var uuidv4 = require("uuid/v4");
var passport = require("passport");
var bcrypt = require("bcrypt");
module.exports = router;
......@@ -23,10 +24,11 @@ router.post("/signup", async function(req, res, err) {
} else {
try {
var userRows = await db.query(
"SELECT person_uuid FROM person WHERE email = ?",
req.body.email
"SELECT person_uuid FROM person WHERE email = $1",
[req.body.email]
);
console.log("userrows", userRows);
if (userRows[0]) {
//user already exists
res.status(409).json({ errors: [{ msg: "username not available" }] });
......@@ -40,39 +42,46 @@ router.post("/signup", async function(req, res, err) {
var normalizedNewsletter =
req.body.isUpdateNewsletterEmailable === true ||
req.body.isUpdateNewsletterEmailable === "true";
var normalizedIsDatasetParticipationConsented =
var normalizedIsDatasetParticipationConsented =
req.body.isDatasetParticipationConsented === true ||
req.body.isDatasetParticipationConsented === "true";
var proFlow = req.body.proFlow;
if (!proFlow) {
proFlow = "a";
}
var insertData = {
password: hash,
email_confirmation_token: token1,
email_confirmation_token_created_at: now,
email: normalizedEmail,
is_update_newsletter_emailable: normalizedNewsletter,
is_dataset_participation_consented: normalizedIsDatasetParticipationConsented,
created_at: now,
person_uuid: uuid,
test_segment_pro_flow: proFlow
};
var insertQuery = db.helpers.insert(insertData, null, 'person');
console.log("insertQuery\n\n", insertQuery)
db.query(
"INSERT INTO user SET ?",
{
password: hash,
email_confirmation_token: token1,
email_confirmation_token_created_at: now,
email: normalizedEmail,
is_update_newsletter_emailable: normalizedNewsletter,
is_dataset_participation_consented: normalizedIsDatasetParticipationConsented,
created_at: now,
person_uuid: uuid,
test_segment_pro_flow: proFlow
}, function(err, rows){
if (err){
insertQuery,
function(err, rows) {
if (err) {
console.error("signup error INNER1", err);
res.status(400).json({ errors: [err] });
} else {
emailUtils.sendEmailConfirmation(normalizedEmail, token1);
req.login({ person_uuid: uuid }, function(err) {
if (err) {
console.error(err);
res.status(400).json({ errors: ["login error"] });
}
var userId = result.insertId;
console.log("insert rows thing", rows)
var userId = rows.insertId;
if (
req.body.state &&
mainUtils.shouldSaveState(req.body.state)
......@@ -85,12 +94,14 @@ router.post("/signup", async function(req, res, err) {
}
});
}
})
}
);
});
});
}
} catch (err){
res.status(400).json({ errors: [err] });
} catch (err) {
console.error("signup error OUTER1", err);
res.status(400).json({ errors: ["signup error"] });
}
}
});
......
......@@ -142,15 +142,15 @@ var normalizers = {
normalized.shares = entity.shareCount || 0;
normalized.fmv = entity.FMV || 0;
normalized.lpp = entity.LPP || 0;
var normalizedFounder = 0;
var normalizedFounder = false;
//omegalul
if (typeof entity.founder === "undefined") {
normalizedFounder = 0;
normalizedFounder = false;
} else if (typeof entity.founder === "boolean") {
normalizedFounder = entity.founder ? 1 : 0;
normalizedFounder = entity.founder ? true : false;
} else if (typeof entity.founder === "string") {
normalizedFounder = entity.founder === "true" ? 1 : 0;
normalizedFounder = entity.founder === "true" ? true : false;
}
normalized.founder = normalizedFounder;
normalized.employee_number = entity.employeeNumber || 0;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment