Skip to content

Commit

Permalink
Lf 707/invitation flow test (#449)
Browse files Browse the repository at this point in the history
* invite pseudo user

* babel-eslint

* remove comment

* endpoint for converting pseudo user to normal user

* invite pseudoUser front end

* invite pseudoUser front end

* Set userfarm as disabled on choose farm screen if access is revoked

* hide deleted field in get roles request

* tests

* invitation chooseFarmMenuItem

* update storybook

* fix chooseFarmMenuItem address flex box

* fix reactive userFarm
  • Loading branch information
JimmyRowland authored Jan 28, 2021
1 parent 58be86a commit b023345
Show file tree
Hide file tree
Showing 65 changed files with 5,269 additions and 4,140 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
1 change: 1 addition & 0 deletions packages/api/.babelrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 2 additions & 1 deletion packages/api/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/tests
/tests
.eslintrc.js
7 changes: 4 additions & 3 deletions packages/api/.eslintrc → packages/api/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
module.exports = {
"extends": [
"eslint:recommended",
"prettier"
],
"env": {
"node": true,
"es6": true
"es6": true,
node: true
},
"parserOptions": {
"ecmaVersion": 2018
},
"parser": "@babel/eslint-parser",
"rules": {
"comma-dangle": ["error", "always-multiline"],
"comma-spacing": ["error", { "before": false, "after": true }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ exports.down = function (knex) {
return Promise.all([
knex.schema.dropTable('harvestUseType'),
])
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ exports.down = function (knex) {
return Promise.all([
knex('harvestUseType').del(),
]);
};
};
1,090 changes: 918 additions & 172 deletions packages/api/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"crypto-js": "^4.0.0",
"csvtojson": "^2.0.10",
"dotenv": "^8.2.0",
"eslint": "^7.10.0",
"express": "^4.17.1",
"express-jwt": "^6.0.0",
"express-jwt-authz": "^2.4.1",
Expand Down Expand Up @@ -78,7 +77,10 @@
"uuid": "^8.3.0"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/eslint-parser": "^7.12.1",
"chromatic": "^5.5.0",
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.0.0",
"faker": "^5.1.0",
"jest": "^26.4.2",
Expand Down
25 changes: 16 additions & 9 deletions packages/api/src/controllers/loginController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const userModel = require('../models/userModel');
const passwordModel = require('../models/passwordModel');
const userFarmModel = require('../models/userFarmModel');
const bcrypt = require('bcryptjs');
const userController = require("./userController");
const userController = require('./userController');
const { sendEmailTemplate, emails } = require('../templates/sendEmailTemplate');
const parser = require('ua-parser-js');
const userLogModel = require('../models/userLogModel');
Expand Down Expand Up @@ -61,7 +61,7 @@ class loginController extends baseController {
screen_height,
reason_for_failure: 'password_mismatch',
});
return res.sendStatus(401)
return res.sendStatus(401);
};

const id_token = await createToken('access', { user_id: userData.user_id });
Expand Down Expand Up @@ -167,7 +167,7 @@ class loginController extends baseController {
}
// User signed up with Google SSO
if (/^\d+$/.test(data.user_id)) {
res.status(200).send({
return res.status(200).send({
first_name: data.first_name,
email: data.email,
exists: true,
Expand All @@ -176,15 +176,17 @@ class loginController extends baseController {
invited: false,
expired: false,
});
} else if (/^.*@pseudo\.com$/.test(data.email)) {
return res.sendStatus(400);
} else {
res.status(200).send({
return res.status(200).send({
first_name: data.first_name,
email: data.email,
exists: true,
sso: false,
language: data.language_preference,
invited: false,
expired: false
expired: false,
});
}
}
Expand All @@ -202,18 +204,23 @@ async function sendMissingInvitations(user) {
const userFarms = await userFarmModel.query().select('users.*', 'farm.farm_name', 'farm.farm_id')
.join('farm', 'userFarm.farm_id', 'farm.farm_id')
.join('users', 'users.user_id', 'userFarm.user_id')
.where('users.user_id', user.user_id).andWhere('userFarm.status', 'Invited')
.where('users.user_id', user.user_id).andWhere('userFarm.status', 'Invited');
if (userFarms) {
await Promise.all(userFarms.map((userFarm) => {
return userController.createTokenSendEmail(user, userFarm, userFarm.farm_name)
}))
return userController.createTokenSendEmail(user, userFarm, userFarm.farm_name);
}));
}
}

async function sendPasswordReset(data) {
const created_at = new Date();
const pw = await passwordModel.query()
.insert({ user_id: data.user_id, reset_token_version: 1, password_hash: `${Math.random()}`, created_at: created_at.toISOString()}).returning('*');
.insert({
user_id: data.user_id,
reset_token_version: 1,
password_hash: `${Math.random()}`,
created_at: created_at.toISOString(),
}).returning('*');
const tokenPayload = {
...data,
reset_token_version: 0,
Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/controllers/rolesController.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const baseController = require('../controllers/baseController');
const { Model } = require('objection');
const knex = Model.knex();
const roleModel = require('../models/roleModel')


class rolesController extends baseController {
static getRoles() {
return async (req, res) => {
try {
const data = await knex('role').whereNot('role_id', 4);
const data = await roleModel.query().whereNot('role_id', 4);
res.status(200).send(data);
if (!data.length) {
res.sendStatus(404)
}
}
catch (error) {
// handle more exceptions
res.status(400).send(error);
}
}
Expand Down
21 changes: 17 additions & 4 deletions packages/api/src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class userController extends baseController {
user = isUserAlreadyCreated;
}
const { user_id } = user;
const userFarm = await userFarmModel.query(trx).insert({
await userFarmModel.query(trx).insert({
user_id,
farm_id,
status: 'Invited',
Expand All @@ -179,6 +179,12 @@ class userController extends baseController {
step_four: true,
step_five: true,
});
const userFarm = await userFarmModel.query(trx)
.join('users', 'userFarm.user_id', '=', 'users.user_id')
.join('farm', 'farm.farm_id', '=', 'userFarm.farm_id')
.join('role', 'userFarm.role_id', '=', 'role.role_id')
.where({ 'users.email': email, 'userFarm.farm_id': farm_id }).first()
.select('*');
await trx.commit();
res.status(201).send({ ...user, ...userFarm });
try {
Expand Down Expand Up @@ -277,7 +283,7 @@ class userController extends baseController {
/* End of input validation */

const user = await baseController.post(userModel, req.body, trx);
const userFarm = await userFarmModel.query(trx).insert({
await userFarmModel.query(trx).insert({
user_id,
farm_id,
status: 'Active',
Expand All @@ -290,8 +296,14 @@ class userController extends baseController {
step_four: true,
step_five: true,
});
const userFarm = await userFarmModel.query(trx)
.join('users', 'userFarm.user_id', '=', 'users.user_id')
.join('farm', 'farm.farm_id', '=', 'userFarm.farm_id')
.join('role', 'userFarm.role_id', '=', 'role.role_id')
.where({ 'users.email': email, 'userFarm.farm_id': farm_id }).first()
.select('*');
await trx.commit();
res.status(201).send({ ...user, ...userFarm });
res.status(201).send(userFarm);
} catch (error) {
// handle more exceptions
await trx.rollback();
Expand Down Expand Up @@ -449,6 +461,7 @@ class userController extends baseController {
return async (req, res) => {
const trx = await transaction.start(Model.knex());
try {
delete req.body.status_id;
const updated = await baseController.put(userModel, req.params.user_id, req.body, trx);
await trx.commit();
if (!updated.length) {
Expand Down Expand Up @@ -538,7 +551,7 @@ class userController extends baseController {
}).patch({ status: 'Active' });
const userFarms = await userFarmModel.query(trx).where({ user_id });
await userFarmModel.query(trx).insert(userFarms.map(userFarm => ({ ...userFarm, user_id: sub })));
await shiftModel.query(trx).context({user_id: sub}).where({ user_id }).patch({ user_id: sub }).first().returning('*');
await shiftModel.query(trx).context({user_id: sub}).where({ user_id }).patch({ user_id: sub });
await emailTokenModel.query(trx).where({ user_id }).patch({ user_id: sub });
await userFarmModel.query(trx).where({ user_id }).delete();
await userModel.query(trx).findById(user_id).delete();
Expand Down
Loading

0 comments on commit b023345

Please sign in to comment.