From 2aa1448c0a0abf2fa679c4e873cf927a83521899 Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Mon, 17 Sep 2018 19:26:37 +0100 Subject: [PATCH 1/6] fixes multiple challenge acceptance and completion issue Relates #48 #5 #19 --- db/db_build.sql | 3 +++ src/controllers/dashboard.js | 2 +- src/controllers/get-accepted-challenges.js | 17 +---------------- src/model/acceptChallenge.js | 3 ++- src/model/completeChallenge.js | 8 ++++++-- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/db/db_build.sql b/db/db_build.sql index 01077d2..65f1772 100644 --- a/db/db_build.sql +++ b/db/db_build.sql @@ -85,4 +85,7 @@ CREATE TABLE challenge_status status INTEGER ); +ALTER TABLE challenge_status +ADD CONSTRAINT chall_stat_unique UNIQUE (challenge_id, user_id, status); + COMMIT; \ No newline at end of file diff --git a/src/controllers/dashboard.js b/src/controllers/dashboard.js index 298a49a..30998c6 100644 --- a/src/controllers/dashboard.js +++ b/src/controllers/dashboard.js @@ -8,7 +8,7 @@ exports.get = (req, res) => { Promise.all([newChallenges, acceptedChallenges, completedChallenges]) .then(challenges => { - console.log(challenges[2]); + // console.log(challenges[2]); res.render("dashboard", { newChallenges: challenges[0], diff --git a/src/controllers/get-accepted-challenges.js b/src/controllers/get-accepted-challenges.js index ebb6d7b..f6b3845 100644 --- a/src/controllers/get-accepted-challenges.js +++ b/src/controllers/get-accepted-challenges.js @@ -1,22 +1,7 @@ -// const queries = require("../model/index"); +const queries = require("../model/index"); -<<<<<<< HEAD -// exports.get = (req, res) => { -// queries.getAcceptedChallenges(userId, challengeStatus).then(challenges => { -// res.render("dashboard", { challenges }); -// }); -// }; -||||||| merged common ancestors exports.get = (req, res) => { queries.getAcceptedChallenges(1, 1).then(acceptedChallenges => { - console.log("HELLO"); res.render("dashboard", { acceptedChallenges }); }); }; -======= -exports.get = (req, res) => { - queries.getAcceptedChallenges(1, 1).then(acceptedChallenges => { - res.render("dashboard", { acceptedChallenges }); - }); -}; ->>>>>>> master diff --git a/src/model/acceptChallenge.js b/src/model/acceptChallenge.js index f894fd3..dfa3aa3 100644 --- a/src/model/acceptChallenge.js +++ b/src/model/acceptChallenge.js @@ -1,10 +1,11 @@ // Adds the accepted challenges id, the users id and the status for accepted(1) to the table challenge_status +// Only inserts if the challenge is not already accepted by that user, if it is the query does nothing const db = require('../../db/db_connection'); const acceptChallenge = (challengeId, userId) => new Promise((resolve, reject) => { - db.query(`INSERT INTO challenge_status (challenge_id, user_id, status) VALUES ($1, $2, $3);`, [challengeId, userId, 1]) + db.query(`INSERT INTO challenge_status (challenge_id, user_id, status) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT chall_stat_unique DO NOTHING;`, [challengeId, userId, 1]) .then(res => { resolve(res) }) diff --git a/src/model/completeChallenge.js b/src/model/completeChallenge.js index d4fdceb..587558a 100644 --- a/src/model/completeChallenge.js +++ b/src/model/completeChallenge.js @@ -1,8 +1,12 @@ -// completeChallenges + +// Deletes the selected challenge from challenge_status if it's already been completed +// then updates the accepted challenge to complete +// this ensures there are never any duplicate completed challenges in the db + const db = require('../../db/db_connection'); const completeChallenge = (challengeId, userId) => new Promise((resolve, reject) => { - db.query('UPDATE challenge_status SET status = 2 WHERE challenge_id = $1 AND user_id = $2 RETURNING *', [challengeId, userId]) + db.query('DELETE from challenge_status WHERE challenge_id = $1 AND user_id = $2 AND status = 2; UPDATE challenge_status SET status = 2 WHERE challenge_id = $1 AND user_id = $2 RETURNING *;', [challengeId, userId]) .then(res => resolve(res)) .catch(err => reject(err)) }) From 1f74ff915c5063870f6de2cc9c4989452b8bc0f3 Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Mon, 17 Sep 2018 19:40:26 +0100 Subject: [PATCH 2/6] fixed the db tests so that travis will love me again --- db/test_build.sql | 3 +++ src/controllers/get-accepted-challenges.js | 12 ++++++------ src/controllers/get-completed-challenges.js | 18 ++---------------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/db/test_build.sql b/db/test_build.sql index 6c3397d..a152b9a 100644 --- a/db/test_build.sql +++ b/db/test_build.sql @@ -92,4 +92,7 @@ VALUES (1, 3, 0), (2, 3, 1); +ALTER TABLE challenge_status +ADD CONSTRAINT chall_stat_unique UNIQUE (challenge_id, user_id, status); + COMMIT; \ No newline at end of file diff --git a/src/controllers/get-accepted-challenges.js b/src/controllers/get-accepted-challenges.js index f6b3845..275c68c 100644 --- a/src/controllers/get-accepted-challenges.js +++ b/src/controllers/get-accepted-challenges.js @@ -1,7 +1,7 @@ -const queries = require("../model/index"); +// const queries = require("../model/index"); -exports.get = (req, res) => { - queries.getAcceptedChallenges(1, 1).then(acceptedChallenges => { - res.render("dashboard", { acceptedChallenges }); - }); -}; +// exports.get = (req, res) => { +// queries.getAcceptedChallenges(1, 1).then(acceptedChallenges => { +// res.render("dashboard", { acceptedChallenges }); +// }); +// }; diff --git a/src/controllers/get-completed-challenges.js b/src/controllers/get-completed-challenges.js index d490a4f..864b048 100644 --- a/src/controllers/get-completed-challenges.js +++ b/src/controllers/get-completed-challenges.js @@ -1,21 +1,7 @@ // const queries = require("../model/index"); -<<<<<<< HEAD // exports.get = (req, res) => { -// queries.getCompletedChallenges().then(completedChallenges => { -// res.render("dashboard", { completedChallenges: completedChallenges }); +// queries.getCompletedChallenges(1, 2).then(completedChallenges => { +// res.render("dashboard", { completedChallenges }); // }); // }; -||||||| merged common ancestors -exports.get = (req, res) => { - queries.getCompletedChallenges().then(completedChallenges => { - res.render("dashboard", { completedChallenges: completedChallenges }); - }); -}; -======= -exports.get = (req, res) => { - queries.getCompletedChallenges(1, 2).then(completedChallenges => { - res.render("dashboard", { completedChallenges }); - }); -}; ->>>>>>> master From 1979e18542576aba44fc93a17a6b6735a1faba83 Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Mon, 17 Sep 2018 21:24:39 +0100 Subject: [PATCH 3/6] Fix attempt 2 for travis db tests --- db/test_build.sql | 5 ++--- test/queries.test.js | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/db/test_build.sql b/db/test_build.sql index a152b9a..1428b38 100644 --- a/db/test_build.sql +++ b/db/test_build.sql @@ -82,6 +82,8 @@ CREATE TABLE challenge_status status INTEGER ); +ALTER TABLE challenge_status ADD CONSTRAINT chall_stat_unique UNIQUE (challenge_id, user_id, status); + INSERT INTO challenge_status (challenge_id, user_id, status) VALUES @@ -92,7 +94,4 @@ VALUES (1, 3, 0), (2, 3, 1); -ALTER TABLE challenge_status -ADD CONSTRAINT chall_stat_unique UNIQUE (challenge_id, user_id, status); - COMMIT; \ No newline at end of file diff --git a/test/queries.test.js b/test/queries.test.js index 33e0d6a..feeaea8 100644 --- a/test/queries.test.js +++ b/test/queries.test.js @@ -39,6 +39,7 @@ describe("accept challenge", () => { expect.assertions(1); queries.acceptChallenge(1, 1); return queries.getAcceptedChallenges(1, 1).then(res => { + // console.log(res); expect(res.length).toBe(2); }) }) From dbf2523d63672678476ef68d219218e1f79de47d Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Mon, 17 Sep 2018 21:48:15 +0100 Subject: [PATCH 4/6] Change psql version on travis to 10 from 9.2 --- .travis.yml | 12 +++++++++++- test/queries.test.js | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8f569c7..57f7c38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,17 @@ notifications: on_failure: never services: - postgresql + addons: + postgresql: "10" + apt: + packages: + - postgresql-10 + - postgresql-client-10 +env: + global: + - PGPORT=5433 before_script: + - psql --version - psql -c 'CREATE DATABASE test_race2;' -U postgres - psql -c "CREATE USER simon WITH PASSWORD 'pw123';" -U postgres - - psql -c "ALTER DATABASE test_race2 OWNER TO simon;" -U postgres + - psql -c "ALTER DATABASE test_race2 OWNER TO simon;" -U postgres \ No newline at end of file diff --git a/test/queries.test.js b/test/queries.test.js index feeaea8..5943d8f 100644 --- a/test/queries.test.js +++ b/test/queries.test.js @@ -49,7 +49,7 @@ describe('get accepted challenges', () => { test('returns the accepted challenges of specific user', () => { expect.assertions(1); return queries.getAcceptedChallenges(1, 1).then(res => { - console.log(res); + // console.log(res); expect(res).toBeTruthy(); }) }) From 4f1e2ab6776a6a60ea0fd6918fe3b82dfb68ef6a Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Mon, 17 Sep 2018 21:58:59 +0100 Subject: [PATCH 5/6] Attempt 2 to change psql version on travis --- .travis.yml | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 57f7c38..3854d39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,19 +6,15 @@ notifications: email: on_success: never on_failure: never +sudo: false +dist: trusty +addons: + postgresql: "9.6" services: - - postgresql - addons: - postgresql: "10" - apt: - packages: - - postgresql-10 - - postgresql-client-10 -env: - global: - - PGPORT=5433 + - postgresql before_script: - psql --version - psql -c 'CREATE DATABASE test_race2;' -U postgres - psql -c "CREATE USER simon WITH PASSWORD 'pw123';" -U postgres - - psql -c "ALTER DATABASE test_race2 OWNER TO simon;" -U postgres \ No newline at end of file + - psql -c "ALTER DATABASE test_race2 OWNER TO simon;" -U postgres + \ No newline at end of file From d8100da2b5ad6e6da10afb6054d3b9c58887f91b Mon Sep 17 00:00:00 2001 From: shouston3 Date: Tue, 18 Sep 2018 07:49:36 +0100 Subject: [PATCH 6/6] configure eslint --- .eslintignore | 1 + .eslintrc | 2 +- db/db_build.js | 13 +++++++------ db/db_connection.js | 15 --------------- db/test_build.js | 10 +++++++--- db/test_clear.js | 12 +++++++++--- package.json | 3 ++- 7 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..2189bae --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +coverage/**/*.js diff --git a/.eslintrc b/.eslintrc index d60e8a2..94afe2f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -13,4 +13,4 @@ "no-console": 0, "import/no-nodejs-modules": 0 } -} \ No newline at end of file +} diff --git a/db/db_build.js b/db/db_build.js index 19cbe01..def8233 100644 --- a/db/db_build.js +++ b/db/db_build.js @@ -1,12 +1,13 @@ const fs = require("fs"); +const assert = require("assert"); const dbConnection = require("./db_connection"); -const sql = fs.readFileSync(`${__dirname}/db_build.sql`).toString(); +const initialiseDatabase = () => { + fs.readFile(`${__dirname}/db_build.sql`, 'utf8', (err, sql) => { + assert(!err, err); -const initialiseDatabase = () => dbConnection.query(sql); - -initialiseDatabase(); + dbConnection.query(sql); + }); +} module.exports = initialiseDatabase; - - diff --git a/db/db_connection.js b/db/db_connection.js index 2d03ef0..ef5d609 100644 --- a/db/db_connection.js +++ b/db/db_connection.js @@ -1,21 +1,6 @@ -// go connections "use strict"; const pgp = require("pg-promise")(); -const url = require("url"); -require("env2")("./config.env"); - -let DB_URL = process.env.DATABASE_URL; -console.log("overall process env ", process.env.DATABASE_URL); -if (process.env.NODE_ENV === "test") { - DB_URL = process.env.TEST_DB_URL; - console.log("in if condition ", process.env.TEST_DB_URL); -} - -if (!DB_URL) throw new Error("Environment vatialbe must be set"); - -const params = url.parse(DB_URL); -const [username, password] = params.auth.split(":"); const testDB = { host: "localhost", diff --git a/db/test_build.js b/db/test_build.js index 4f5babd..060e1ff 100644 --- a/db/test_build.js +++ b/db/test_build.js @@ -1,9 +1,13 @@ -// go tests const fs = require("fs"); const dbConnection = require("./db_connection"); +const assert = require("assert"); -const sql = fs.readFileSync(`${__dirname}/test_build.sql`).toString(); +const initialiseTestDatabase = () => { + fs.readFile(`${__dirname}/test_build.sql`, 'utf8', (err, sql) => { + assert(!err, err); -const initialiseTestDatabase = () => dbConnection.query(sql); + dbConnection.query(sql); + }); +} module.exports = initialiseTestDatabase; diff --git a/db/test_clear.js b/db/test_clear.js index 2d87ba3..b0b14e9 100644 --- a/db/test_clear.js +++ b/db/test_clear.js @@ -1,8 +1,14 @@ const fs = require("fs"); const db = require("./db_connection"); +const assert = require("assert"); -const sql = fs.readFileSync(`${__dirname}/test_clear.sql`).toString(); -const clearTestDatabase = () => db.query(sql); +const clearTestDatabase = () => { + fs.readFile(`${__dirname}/test_clear.sql`, 'utf8', (err, sql) => { + assert(!err, err); -module.exports = clearTestDatabase; \ No newline at end of file + db.query(sql); + }); +} + +module.exports = clearTestDatabase; diff --git a/package.json b/package.json index 0100044..286ca77 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ }, "scripts": { "start": "node src/index.js", - "test": "jest --detectOpenHandles", + "lint": "eslint .", + "test": "npm run lint && jest --detectOpenHandles", "dev": "nodemon src/index.js", "build": "node db/db_build.js", "report-coverage": "codecov"