Skip to content
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 .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
config/local.js
config/mock.local.js
config/m2m.local.js
local/seed/
node_modules
dist
.ebextensions
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ Microservice to manage CRUD operations for all things Projects.
Runs the Project Service using nodemon, so it would be restarted after any of the files is updated.
The project service will be served on `http://localhost:8001`.

### Import sample metadata
### Import sample metadata & projects
```bash
CONNECT_USER_TOKEN=<connect user token> node migrations/seedMetadata.js
CONNECT_USER_TOKEN=<connect user token> npm run demo-data
```
This command will create sample metadata entries in the DB (duplicate what is currently in development environment).

Expand Down
13 changes: 13 additions & 0 deletions local/seed/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const seedMetadata = require('./seedMetadata');
const seedProjects = require('./seedProjects');

const targetUrl = 'http://localhost:8001/v4/';
const token =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJUb3Bjb2RlciBVc2VyIiwiYWRtaW5pc3RyYXRvciJdLCJpc3MiOiJodHRwczovL2FwaS50b3Bjb2Rlci1kZXYuY29tIiwiaGFuZGxlIjoidGVzdDEiLCJleHAiOjI1NjMwNzY2ODksInVzZXJJZCI6IjQwMDUxMzMzIiwiaWF0IjoxNDYzMDc2MDg5LCJlbWFpbCI6InRlc3RAdG9wY29kZXIuY29tIiwianRpIjoiYjMzYjc3Y2QtYjUyZS00MGZlLTgzN2UtYmViOGUwYWU2YTRhIn0.wKWUe0-SaiFVN-VR_-GwgFlvWaDkSbc8H55ktb9LAVw';

async function seed() {
await seedMetadata(targetUrl, token);
await seedProjects(targetUrl, token);
}

seed();
178 changes: 178 additions & 0 deletions local/seed/projects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
[
{
"param": {
"name": "Develop app",
"details": {
"utm": {
"code": "R&amp;D"
},
"appDefinition": {
"primaryTarget": "phone",
"goal": {
"value": "Nothing"
},
"users": {
"value": "No one"
},
"notes": ""
},
"hideDiscussions": true
},
"description": "Hello this is a sample description... This requires at least 160 characters. I'm trying to satisfy this condition. But I could n't if I don't type this unnecessary message",
"templateId": 1,
"type": "app",
"status": "draft"
}
},
{
"param": {
"name": "Develop website",
"details": {
"utm": {
"code": ""
},
"appDefinition": {
"primaryTarget": "phone",
"goal": {
"value": "Nothing"
},
"users": {
"value": "No one"
},
"notes": ""
},
"hideDiscussions": true
},
"description": "Hello this is a sample description... This requires at least 160 characters. I'm trying to satisfy this condition. But I could n't if I don't type this unnecessary message",
"templateId": 2,
"type": "chatbot",
"status": "in_review"
}
},
{
"param": {
"name": "Develop website 2",
"details": {
"utm": {
"code": ""
},
"appDefinition": {
"primaryTarget": "phone",
"goal": {
"value": "Nothing"
},
"users": {
"value": "No one"
},
"notes": ""
},
"hideDiscussions": true
},
"description": "Hello this is a sample description... This requires at least 160 characters. I'm trying to satisfy this condition. But I could n't if I don't type this unnecessary message",
"templateId": 3,
"type": "website",
"status": "reviewed"
}
},
{
"param": {
"name": "Develop chatbot",
"details": {
"utm": {
"code": ""
},
"appDefinition": {
"primaryTarget": "phone",
"goal": {
"value": "Nothing"
},
"users": {
"value": "No one"
},
"notes": ""
},
"hideDiscussions": true
},
"description": "Hello this is a sample description... This requires at least 160 characters. I'm trying to satisfy this condition. But I could n't if I don't type this unnecessary message",
"templateId": 4,
"type": "chatbot",
"status": "active"
}
},
{
"param": {
"name": "Develop app 2",
"details": {
"utm": {
"code": ""
},
"appDefinition": {
"primaryTarget": "phone",
"goal": {
"value": "Nothing"
},
"users": {
"value": "No one"
},
"notes": ""
},
"hideDiscussions": true
},
"description": "Hello this is a sample description... This requires at least 160 characters. I'm trying to satisfy this condition. But I could n't if I don't type this unnecessary message",
"templateId": 1,
"type": "app",
"status": "completed"
}
},
{
"param": {
"name": "Develop website 3",
"details": {
"utm": {
"code": ""
},
"appDefinition": {
"primaryTarget": "phone",
"goal": {
"value": "Nothing"
},
"users": {
"value": "No one"
},
"notes": ""
},
"hideDiscussions": true
},
"description": "Hello this is a sample description... This requires at least 160 characters. I'm trying to satisfy this condition. But I could n't if I don't type this unnecessary message",
"templateId": 2,
"type": "website",
"status": "paused"
}
},
{
"param": {
"name": "Develop app 3",
"details": {
"utm": {
"code": ""
},
"appDefinition": {
"primaryTarget": "phone",
"goal": {
"value": "Nothing"
},
"users": {
"value": "No one"
},
"notes": ""
},
"hideDiscussions": true
},
"description": "Hello this is a sample description... This requires at least 160 characters. I'm trying to satisfy this condition. But I could n't if I don't type this unnecessary message",
"templateId": 1,
"type": "app",
"status": "cancelled",
"cancelReason": "Test cancel"
}
}
]
30 changes: 15 additions & 15 deletions migrations/seedMetadata.js → local/seed/seedMetadata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable */
const _ = require('lodash')
const axios = require('axios');
const Promise = require('bluebird');
Expand All @@ -12,18 +11,18 @@ if (!process.env.CONNECT_USER_TOKEN) {
const CONNECT_USER_TOKEN = process.env.CONNECT_USER_TOKEN;

var url = 'https://api.topcoder-dev.com/v4/projects/metadata';
var targetUrl = 'http://localhost:8001/v4/';
var destUrl = targetUrl + 'projects/';
var destTimelines = targetUrl;

console.log('Getting metadata from DEV environment...');

axios.get(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${CONNECT_USER_TOKEN}`
}
})

module.exports = (targetUrl, token) => {
var destUrl = targetUrl + 'projects/';
var destTimelines = targetUrl;

console.log('Getting metadata from DEV environment...');
return axios.get(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${CONNECT_USER_TOKEN}`
}
})
.catch((err) => {
const errMessage = _.get(err, 'response.data.result.content.message');
throw errMessage ? new Error('Error during obtaining data from DEV: ' + errMessage) : err
Expand All @@ -35,7 +34,7 @@ axios.get(url, {

var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJUb3Bjb2RlciBVc2VyIiwiYWRtaW5pc3RyYXRvciJdLCJpc3MiOiJodHRwczovL2FwaS50b3Bjb2Rlci1kZXYuY29tIiwiaGFuZGxlIjoidGVzdDEiLCJleHAiOjI1NjMwNzY2ODksInVzZXJJZCI6IjQwMDUxMzMzIiwiaWF0IjoxNDYzMDc2MDg5LCJlbWFpbCI6InRlc3RAdG9wY29kZXIuY29tIiwianRpIjoiYjMzYjc3Y2QtYjUyZS00MGZlLTgzN2UtYmViOGUwYWU2YTRhIn0.wKWUe0-SaiFVN-VR_-GwgFlvWaDkSbc8H55ktb9LAVw'
'Authorization': 'Bearer ' + token
}

let promises = _(data.result.content.projectTypes).map(pt=>{
Expand Down Expand Up @@ -92,7 +91,8 @@ axios.get(url, {
));

// handle success
console.log('Done');
console.log('Done metadata seed');
}).catch(err=>{
console.error(err && err.response ? err.response : err);
});
}
74 changes: 74 additions & 0 deletions local/seed/seedProjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const axios = require('axios');
const Promise = require('bluebird');
const _ = require('lodash');

const projects = require('./projects.json');

/**
* Create projects and update their statuses.
*/
module.exports = (targetUrl, token) => {
let projectPromises;

const projectsUrl = `${targetUrl}projects`;
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
};

console.log('Creating projects');
projectPromises = projects.map((project, i) => {
const status = _.get(project, 'param.status');
const cancelReason = _.get(project, 'param.cancelReason');
delete project.param.status;
delete project.param.cancelReason;

return axios
.post(projectsUrl, project, { headers })
.catch((err) => {
console.log(`Failed to create project ${i}: ${err.message}`);
})
.then((response) => {
const projectId = _.get(response, 'data.result.content.id');

return {
projectId,
status,
cancelReason,
};
});
});

return Promise.all(projectPromises)
.then((createdProjects) => {
console.log('Updating statuses');
return Promise.all(
createdProjects.map(({ projectId, status, cancelReason }) =>
updateProjectStatus(projectId, { status, cancelReason }, targetUrl, headers).catch((ex) => {
console.log(`Failed to update project status of project with id ${projectId}: ${ex.message}`);
}),
),
);
})
.then(() => console.log('Done project seed.'))
.catch(ex => console.error(ex));
};

function updateProjectStatus(project, updateParams, targetUrl, headers) {
const projectUpdateUrl = `${targetUrl}projects/${project}`;

// only cancelled status requires cancelReason
if (updateParams.status !== 'cancelled') {
delete updateParams.cancelReason;
}

return axios.patch(
projectUpdateUrl,
{
param: updateParams,
},
{
headers,
},
);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"start:dev": "NODE_ENV=development PORT=8001 nodemon -w src --exec \"babel-node src --presets es2015\" | ./node_modules/.bin/bunyan",
"test": "NODE_ENV=test npm run lint && NODE_ENV=test npm run sync:es && NODE_ENV=test npm run sync:db && NODE_ENV=test ./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- --timeout 10000 --compilers js:babel-core/register $(find src -path '*spec.js*')",
"test:watch": "NODE_ENV=test ./node_modules/.bin/mocha -w --compilers js:babel-core/register $(find src -path '*spec.js*')",
"seed": "babel-node src/tests/seed.js --presets es2015"
"seed": "babel-node src/tests/seed.js --presets es2015",
"demo-data": "babel-node local/seed"
},
"repository": {
"type": "git",
Expand Down