Skip to content

Moving admin endpoints to production #73

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

Merged
merged 10 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies:

deployment:
development:
branch: dev
branch: [dev, 'feature/admin-endpoints']
commands:
- ./ebs_deploy.sh tc-project-service DEV $CIRCLE_BUILD_NUM

Expand Down
2 changes: 2 additions & 0 deletions config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"docType": "projectV4"
},
"rabbitmqURL": "RABBITMQ_URL",
"pubsubQueueName": "PUBSUB_QUEUE_NAME",
"pubsubExchangeName": "PUBSUB_EXCHANGE_NAME",
"directProjectServiceEndpoint": "DIRECT_PROJECT_SERVICE_ENDPOINT",
"directProjectServiceTimeout": "DIRECT_PROJECT_SERVICE_TIMEOUt",
"fileServiceEndpoint": "FILE_SERVICE_ENDPOINT",
Expand Down
7 changes: 7 additions & 0 deletions src/models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ module.exports = function defineProject(sequelize, DataTypes) {
.then(projects => ({ rows: projects, count }));
});
},
findProjectRange(startId, endId, fields) {
return this.findAll({
where: { id: { $between: [startId, endId] } },
attributes: _.get(fields, 'projects', null),
raw: true,
});
},
},
});

Expand Down
19 changes: 19 additions & 0 deletions src/permissions/admin.ops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import util from '../util';

/**
* Only super admin are allowed to perform admin operations
* @param {Object} freq the express request instance
* @return {Promise} Returns a promise
*/
module.exports = freq => new Promise((resolve, reject) => {
const req = freq;
req.context = req.context || {};
// check if auth user has acecss to this project
const hasAccess = util.hasAdminRole(req);

if (!hasAccess) {
// user is not an admin nor is a registered project member
return reject(new Error('You do not have permissions to perform this action'));
}
return resolve(true);
});
2 changes: 2 additions & 0 deletions src/permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const projectView = require('./project.view');
const projectEdit = require('./project.edit');
const projectDelete = require('./project.delete');
const projectMemberDelete = require('./projectMember.delete');
const projectAdmin = require('./admin.ops');

module.exports = () => {
Authorizer.setDeniedStatusCode(403);
Expand All @@ -21,4 +22,5 @@ module.exports = () => {
Authorizer.setPolicy('project.removeAttachment', projectEdit);
Authorizer.setPolicy('project.downloadAttachment', projectView);
Authorizer.setPolicy('project.updateMember', projectEdit);
Authorizer.setPolicy('project.admin', projectAdmin);
};
327 changes: 327 additions & 0 deletions src/routes/admin/project-create-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@

/* globals Promise */

import _ from 'lodash';
import config from 'config';
import { middleware as tcMiddleware } from 'tc-core-library-js';
import util from '../../util';

/**
/**
* API to handle retrieving a single project by id
*
* Permissions:
* Only users that have access to the project can retrieve it.
*
*/

// var permissions = require('tc-core-library-js').middleware.permissions
const permissions = tcMiddleware.permissions;
const ES_PROJECT_INDEX = config.get('elasticsearchConfig.indexName');
const ES_PROJECT_TYPE = config.get('elasticsearchConfig.docType');

/**
* Get the request body for the specified index name
* @private
*
* @param {String} indexName the index name
* @param {String} docType document type
* @return {Object} the request body for the specified index name
*/
function getRequestBody(indexName, docType) {
const projectMapping = {
_all: { enabled: false },
properties: {
actualPrice: {
type: 'double',
},
attachments: {
type: 'nested',
properties: {
category: {
type: 'string',
index: 'not_analyzed',
},
contentType: {
type: 'string',
index: 'not_analyzed',
},
createdAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis',
},
createdBy: {
type: 'integer',
},
description: {
type: 'string',
},
filePath: {
type: 'string',
},
id: {
type: 'long',
},
projectId: {
type: 'long',
},
size: {
type: 'double',
},
title: {
type: 'string',
},
updatedAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis',
},
updatedBy: {
type: 'integer',
},
},
},
billingAccountId: {
type: 'long',
},
bookmarks: {
type: 'nested',
properties: {
address: {
type: 'string',
},
title: {
type: 'string',
},
},
},
cancelReason: {
type: 'string',
},
challengeEligibility: {
type: 'nested',
properties: {
groups: {
type: 'long',
},
role: {
type: 'string',
index: 'not_analyzed',
},
users: {
type: 'long',
},
},
},
createdAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis',
},
createdBy: {
type: 'integer',
},
description: {
type: 'string',
},
details: {
type: 'nested',
properties: {
TBD_features: {
type: 'nested',
properties: {
description: {
type: 'string',
},
id: {
type: 'integer',
},
isCustom: {
type: 'boolean',
},
title: {
type: 'string',
},
},
},
TBD_usageDescription: {
type: 'string',
},
appDefinition: {
properties: {
goal: {
properties: {
value: {
type: 'string',
},
},
},
primaryTarget: {
type: 'string',
},
users: {
properties: {
value: {
type: 'string',
},
},
},
},
},
hideDiscussions: {
type: 'boolean',
},
products: {
type: 'string',
},
summary: {
type: 'string',
},
utm: {
type: 'nested',
properties: {
code: {
type: 'string',
},
},
},
},
},
directProjectId: {
type: 'long',
},
estimatedPrice: {
type: 'double',
},
external: {
properties: {
data: {
type: 'string',
},
id: {
type: 'string',
index: 'not_analyzed',
},
type: {
type: 'string',
index: 'not_analyzed',
},
},
},
id: {
type: 'long',
},
members: {
type: 'nested',
properties: {
createdAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis',
},
createdBy: {
type: 'integer',
},
email: {
type: 'string',
index: 'not_analyzed',
},
firstName: {
type: 'string',
},
handle: {
type: 'string',
index: 'not_analyzed',
},
id: {
type: 'long',
},
isPrimary: {
type: 'boolean',
},
lastName: {
type: 'string',
},
projectId: {
type: 'long',
},
role: {
type: 'string',
index: 'not_analyzed',
},
updatedAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis',
},
updatedBy: {
type: 'integer',
},
userId: {
type: 'long',
},
},
},
name: {
type: 'string',
},
status: {
type: 'string',
index: 'not_analyzed',
},
terms: {
type: 'integer',
},
type: {
type: 'string',
index: 'not_analyzed',
},
updatedAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis',
},
updatedBy: {
type: 'integer',
},
utm: {
properties: {
campaign: {
type: 'string',
},
medium: {
type: 'string',
},
source: {
type: 'string',
},
},
},
},
};
const result = {
index: indexName,
updateAllTypes: true,
body: {
mappings: { },
},
};
result.body.mappings[docType] = projectMapping;
return result;
}


module.exports = [
permissions('project.admin'),
/**
* GET projects/{projectId}
* Get a project by id
*/
(req, res, next) => { // eslint-disable-line no-unused-vars
const logger = req.log;
logger.debug('Entered Admin#createIndex');
const indexName = _.get(req, 'body.param.indexName', ES_PROJECT_INDEX);
const docType = _.get(req, 'body.param.docType', ES_PROJECT_TYPE);
logger.debug('indexName', indexName);
logger.debug('docType', docType);

const esClient = util.getElasticSearchClient();
esClient.indices.create(getRequestBody(indexName, docType));
res.status(200).json(util.wrapResponse(req.id, { message: 'Create index request successfully submitted' }));
},
];
Loading