Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller class upgrade #33

Merged
merged 38 commits into from
Jun 26, 2017
Merged

Controller class upgrade #33

merged 38 commits into from
Jun 26, 2017

Conversation

olkorhon
Copy link
Contributor

@olkorhon olkorhon commented Jun 8, 2017

Status

FINISHED

Description

This PR refactors controllers to use Class architecture.

Todos

controllers

  • builds
  • campaigns
  • groups
  • index
  • items
  • loans
  • resources
  • results
  • targets
  • testcases
  • users

cleanup

  • Unittests
  • Functional testing
  • Documentation

Impacted areas

  • controllers
  • routes

@olkorhon olkorhon self-assigned this Jun 8, 2017
@olkorhon olkorhon requested a review from jupe June 8, 2017 07:53
@olkorhon
Copy link
Contributor Author

olkorhon commented Jun 8, 2017

No labels for in progress pull requests in this repository, maybe something like "do not merge" would be useful. @jupe

@jupe jupe added the WIP label Jun 8, 2017
var defaultCtrl = new DefaultController(Campaign, 'Campaign');
class Controller extends DefaultController {
constructor() {
super(mongoose.model('Campaign'), 'Campaign');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps it could give model name and super class get actual model object


return this;
};
this.paramFormat = DefaultController.format();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps these could be in super class as well

console.log(Model.name);

super();
this.Model = Model;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do getter for this and move original object to private member

}
}

find(req, res) {
this._model.query(req.query, (error, list) => {
this._model.find(req.query, (error, list) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change? all models are extended with mongoose-query -plugin (like this) so there should be also model.query()-api available. This is important because find doesn't support features we need from TMI API..

Copy link
Contributor Author

@olkorhon olkorhon Jun 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought it was a deprecated call or something similar since I couldn't find references to it in the mongoose Model documentation. Extending seems to be broken at the moment. Looking into it.. Did not extend mock schema used in testing with proper plugins, resolved.

@jupe jupe added this to the v0.2.0 milestone Jun 13, 2017
Olli Korhonen added 5 commits June 13, 2017 11:04
…to default implementation for debugging purposes.
… in loans.js, also slightly reformatted console logging made by all unit tests
…ctures away from actual tests to improve readability and better support for scaling
Testcase.ensureIndexes(function (err) {
if (err) return handleError(err);
});
// TODO figure out how to synchronously ensureIndexes so we do not close db connection before the process is completed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its still better to be asynchronous. Solution could be that we emit some common event bus message that indexes is ensured before allow clients to be connected to API's... -> "kind of boot sequence"...

Copy link
Contributor

@jupe jupe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR is already quite huge - please try to avoid it next time :)

@@ -72,26 +77,25 @@ class ResourcesController extends DefaultController {
static allocMultiple(req, res) {
req.Resource.allocateResources(req.body, (error, allocated) => {
if (error) {
res.status(404).json(error);
res.status(404).json({ error });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change ?

@jupe jupe removed the WIP label Jun 25, 2017
request({
followAllRedirects: true,
url: url,
url,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't look corect

Copy link
Contributor Author

@olkorhon olkorhon Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eslint by default prefers new Object notation where variables can be initialized to objects with their own name as key. Still, I agree that mixing the two notation methods in a single object looks weird.

See: - new notations in ECMAScript 2015 -
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

winston.info('Github auth: checkOrganization error');
callback({ status: 500, msg: err.toString() });
} else {
const belongsOrg = _.find(response.body, { login: nconf.get('github').organization });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move nconf.get..organization to top of the file as const.

add env.example.json:

  "GITHUB_ORG": "ORGANIZATION",
  "GITHUB_ADMINTEAM": "ADMIN-TEAM",

if (belongsOrg) {
callback(null, accessToken, headers, profile);
} else {
winston.info('Github auth: user not in organization');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use warning level + include admin team name to log

callback({ status: 500, msg: err.toString() });
} else {
const isAdmin = _.find(response.body, (team) => {
return team.name === nconf.get('github').adminTeam && team.organization.login === nconf.get('github').organization;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move nconf.get..:s to top of file as const

var headers = { 'User-Agent': 'Satellizer' };
request.get({ url: accessTokenUrl, qs: params }, (err, response, accessToken) => {
if (err) {
winston.info('Github auth: authorization error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use warning level

const getProfile = (accessToken, headers, callback) => {
request.get({ url: userApiUrl, qs: accessToken, headers, json: true }, (err, response, profile) => {
if (err) {
winston.info('Github auth: getProfile error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use warn level

winston.info('Github auth: getProfile error');
callback({ status: 500, msg: err.toString() });
} else if (!err && response.statusCode !== 200) {
winston.info('Github auth: bad profile response');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn/debug level

winston.info('Github auth: bad profile response');
callback({ status: 409, msg: 'Did not get github profile.' });
} else if (!profile.email) {
winston.info('Github auth: no email error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn level. This is by the way problematic: GitHub default configuration use email as private so its not visible here. We have to change email as optional field probably..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point but we still need some user-friendly way to search for users(before it was email). Github username could work, but in any case, one or more fields of user needs to be searchable.

const checkOrganization = (accessToken, headers, profile, callback) => {
request.get({ url: `${userApiUrl}/orgs`, qs: accessToken, headers, json: true }, (err, response) => {
if (err) {
winston.info('Github auth: checkOrganization error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn level

@jupe jupe merged commit 668c578 into master Jun 26, 2017
@jupe jupe deleted the controller-class-upgrade branch June 26, 2017 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants