Skip to content

winning submission from challenge 30081357 - Topcoder Connect - Organization configs #242

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 1 commit into from
Jan 30, 2019
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
1 change: 1 addition & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"idleTimeout": 1000
},
"kafkaConfig": {
"hosts": "localhost:9092"
},
"analyticsKey": "",
"VALID_ISSUERS": "[\"https:\/\/topcoder-newauth.auth0.com\/\",\"https:\/\/api.topcoder-dev.com\"]",
Expand Down
28 changes: 28 additions & 0 deletions migrations/20190129_organization_config.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--
-- CREATE NEW TABLE:
-- org_config
--
CREATE TABLE org_config (
id bigint NOT NULL,
orgId character varying(45) NOT NULL,
configName character varying(45) NOT NULL,
configValue character varying(512),
"deletedAt" timestamp with time zone,
"createdAt" timestamp with time zone,
"updatedAt" timestamp with time zone,
"deletedBy" bigint,
"createdBy" bigint NOT NULL,
"updatedBy" bigint NOT NULL
);

CREATE SEQUENCE org_config_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

ALTER SEQUENCE org_config_id_seq OWNED BY org_config.id;

ALTER TABLE org_config
ALTER COLUMN id SET DEFAULT nextval('org_config_id_seq');
176 changes: 175 additions & 1 deletion postman.json
Original file line number Diff line number Diff line change
Expand Up @@ -3301,6 +3301,180 @@
}
]
},
{
"name": "Organization Config",
"description": "",
"item": [
{
"name": "Create organization config",
"request": {
"url": "{{api-url}}/v4/orgConfig",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer {{jwt-token}}",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"param\":{\r\n \"orgId\": \"20000013\",\r\n \"configName\": \"project_catalog_url\",\r\n \"configValue\": \"/projects/1\"\r\n }\r\n}"
},
"description": ""
},
"response": []
},
{
"name": "List organization config",
"request": {
"url": "{{api-url}}/v4/orgConfig",
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer {{jwt-token}}",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": ""
},
"description": ""
},
"response": []
},
{
"name": "List organization config - filter",
"request": {
"url": {
"raw": "{{api-url}}/v4/orgConfig?filter=orgId=in(20000010,20000013,20000015)%26configName%3Dproject_catalog_url",
"host": [
"{{api-url}}"
],
"path": [
"v4",
"orgConfig"
],
"query": [
{
"key": "filter",
"value": "orgId=in(20000010,20000013,20000015)%26configName%3Dproject_catalog_url",
"equals": true,
"description": ""
}
],
"variable": []
},
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer {{jwt-token}}",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": ""
},
"description": ""
},
"response": []
},
{
"name": "Get organization config",
"request": {
"url": "{{api-url}}/v4/orgConfig/1",
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer {{jwt-token}}",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": ""
},
"description": ""
},
"response": []
},
{
"name": "Update organization config",
"request": {
"url": "{{api-url}}/v4/orgConfig/1",
"method": "PATCH",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer {{jwt-token}}",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"param\":{\r\n \"configName\": \"project_catalog_url\"\r\n }\r\n}"
},
"description": ""
},
"response": []
},
{
"name": "Delete organization config",
"request": {
"url": "{{api-url}}/v4/orgConfig/1",
"method": "DELETE",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer {{jwt-token}}",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": ""
},
"description": ""
},
"response": []
}
]
},
{
"name": "Product Category",
"item": [
Expand Down Expand Up @@ -5010,4 +5184,4 @@
]
}
]
}
}
28 changes: 28 additions & 0 deletions src/models/orgConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable valid-jsdoc */

/**
* The Organization config model
*/
module.exports = (sequelize, DataTypes) => {
const OrgConfig = sequelize.define('OrgConfig', {
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
orgId: { type: DataTypes.STRING(45), allowNull: false },
configName: { type: DataTypes.STRING(45), allowNull: false },
configValue: { type: DataTypes.STRING(512) },
deletedAt: DataTypes.DATE,
createdAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
updatedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
deletedBy: DataTypes.BIGINT,
createdBy: { type: DataTypes.BIGINT, allowNull: false },
updatedBy: { type: DataTypes.BIGINT, allowNull: false },
}, {
tableName: 'org_config',
paranoid: true,
timestamps: true,
updatedAt: 'updatedAt',
createdAt: 'createdAt',
deletedAt: 'deletedAt',
});

return OrgConfig;
};
5 changes: 5 additions & 0 deletions src/permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ module.exports = () => {
Authorizer.setPolicy('projectType.delete', projectAdmin);
Authorizer.setPolicy('projectType.view', true); // anyone can view project types

Authorizer.setPolicy('orgConfig.create', projectAdmin);
Authorizer.setPolicy('orgConfig.edit', projectAdmin);
Authorizer.setPolicy('orgConfig.delete', projectAdmin);
Authorizer.setPolicy('orgConfig.view', true); // anyone can view project types

Authorizer.setPolicy('productCategory.create', projectAdmin);
Authorizer.setPolicy('productCategory.edit', projectAdmin);
Authorizer.setPolicy('productCategory.delete', projectAdmin);
Expand Down
15 changes: 14 additions & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ router.route('/v4/projects/metadata/projectTypes')
router.route('/v4/projects/metadata/projectTypes/:key')
.get(require('./projectTypes/get'));

router.route('/v4/orgConfig')
.get(require('./orgConfig/list'));

router.route('/v4/orgConfig/:id(\\d+)')
.get(require('./orgConfig/get'));

router.route('/v4/projects/metadata/productCategories')
.get(require('./productCategories/list'));
router.route('/v4/projects/metadata/productCategories/:key')
Expand All @@ -53,7 +59,7 @@ router.route('/v4/projects/metadata')
.get(require('./metadata/list'));

router.all(
RegExp(`\\/${apiVersion}\\/(projects|timelines)(?!\\/health).*`), (req, res, next) => (
RegExp(`\\/${apiVersion}\\/(projects|timelines|orgConfig)(?!\\/health).*`), (req, res, next) => (
// JWT authentication
jwtAuth(config)(req, res, next)
),
Expand Down Expand Up @@ -182,6 +188,13 @@ router.route('/v4/projects/:projectId(\\d+)/members/invite')
.put(require('./projectMemberInvites/update'))
.get(require('./projectMemberInvites/get'));

router.route('/v4/orgConfig')
.post(require('./orgConfig/create'));

router.route('/v4/orgConfig/:id(\\d+)')
.patch(require('./orgConfig/update'))
.delete(require('./orgConfig/delete'));

// register error handler
router.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
// DO NOT REMOVE next arg.. even though eslint
Expand Down
58 changes: 58 additions & 0 deletions src/routes/orgConfig/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* API to add a organization config
*/
import validate from 'express-validation';
import _ from 'lodash';
import Joi from 'joi';
import { middleware as tcMiddleware } from 'tc-core-library-js';
import util from '../../util';
import models from '../../models';

const permissions = tcMiddleware.permissions;

const schema = {
body: {
param: Joi.object().keys({
id: Joi.any().strip(),
orgId: Joi.string().max(45).required(),
configName: Joi.string().max(45).required(),
configValue: Joi.string().max(512),
createdAt: Joi.any().strip(),
updatedAt: Joi.any().strip(),
deletedAt: Joi.any().strip(),
createdBy: Joi.any().strip(),
updatedBy: Joi.any().strip(),
deletedBy: Joi.any().strip(),
}).required(),
},
};

module.exports = [
validate(schema),
permissions('orgConfig.create'),
(req, res, next) => {
const entity = _.assign(req.body.param, {
createdBy: req.authUser.userId,
updatedBy: req.authUser.userId,
});

// Check if duplicated key
return models.OrgConfig.findOne({ where: { orgId: req.body.param.orgId, configName: req.body.param.configName } })
.then((existing) => {
if (existing) {
const apiErr = new Error(`Organization config exists for orgId ${req.body.param.orgId}
and configName ${req.body.param.configName}`);
apiErr.status = 422;
return Promise.reject(apiErr);
}

// Create
return models.OrgConfig.create(entity);
}).then((createdEntity) => {
// Omit deletedAt, deletedBy
res.status(201).json(util.wrapResponse(
req.id, _.omit(createdEntity.toJSON(), 'deletedAt', 'deletedBy'), 1, 201));
})
.catch(next);
},
];
Loading