Skip to content

Commit ba0326b

Browse files
author
vikasrohit
authored
Merge pull request #275 from topcoder-platform/feature/seed-projects
Feature/seed projects
2 parents b624a4e + ed3e625 commit ba0326b

File tree

7 files changed

+285
-18
lines changed

7 files changed

+285
-18
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
config/local.js
22
config/mock.local.js
33
config/m2m.local.js
4+
local/seed/
45
node_modules
56
dist
67
.ebextensions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ Microservice to manage CRUD operations for all things Projects.
8282
Runs the Project Service using nodemon, so it would be restarted after any of the files is updated.
8383
The project service will be served on `http://localhost:8001`.
8484

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

local/seed/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const seedMetadata = require('./seedMetadata');
2+
const seedProjects = require('./seedProjects');
3+
4+
const targetUrl = 'http://localhost:8001/v4/';
5+
const token =
6+
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJUb3Bjb2RlciBVc2VyIiwiYWRtaW5pc3RyYXRvciJdLCJpc3MiOiJodHRwczovL2FwaS50b3Bjb2Rlci1kZXYuY29tIiwiaGFuZGxlIjoidGVzdDEiLCJleHAiOjI1NjMwNzY2ODksInVzZXJJZCI6IjQwMDUxMzMzIiwiaWF0IjoxNDYzMDc2MDg5LCJlbWFpbCI6InRlc3RAdG9wY29kZXIuY29tIiwianRpIjoiYjMzYjc3Y2QtYjUyZS00MGZlLTgzN2UtYmViOGUwYWU2YTRhIn0.wKWUe0-SaiFVN-VR_-GwgFlvWaDkSbc8H55ktb9LAVw';
7+
8+
async function seed() {
9+
await seedMetadata(targetUrl, token);
10+
await seedProjects(targetUrl, token);
11+
}
12+
13+
seed();

local/seed/projects.json

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
[
2+
{
3+
"param": {
4+
"name": "Develop app",
5+
"details": {
6+
"utm": {
7+
"code": "R&amp;D"
8+
},
9+
"appDefinition": {
10+
"primaryTarget": "phone",
11+
"goal": {
12+
"value": "Nothing"
13+
},
14+
"users": {
15+
"value": "No one"
16+
},
17+
"notes": ""
18+
},
19+
"hideDiscussions": true
20+
},
21+
"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",
22+
"templateId": 1,
23+
"type": "app",
24+
"status": "draft"
25+
}
26+
},
27+
{
28+
"param": {
29+
"name": "Develop website",
30+
"details": {
31+
"utm": {
32+
"code": ""
33+
},
34+
"appDefinition": {
35+
"primaryTarget": "phone",
36+
"goal": {
37+
"value": "Nothing"
38+
},
39+
"users": {
40+
"value": "No one"
41+
},
42+
"notes": ""
43+
},
44+
"hideDiscussions": true
45+
},
46+
"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",
47+
"templateId": 2,
48+
"type": "chatbot",
49+
"status": "in_review"
50+
}
51+
},
52+
{
53+
"param": {
54+
"name": "Develop website 2",
55+
"details": {
56+
"utm": {
57+
"code": ""
58+
},
59+
"appDefinition": {
60+
"primaryTarget": "phone",
61+
"goal": {
62+
"value": "Nothing"
63+
},
64+
"users": {
65+
"value": "No one"
66+
},
67+
"notes": ""
68+
},
69+
"hideDiscussions": true
70+
},
71+
"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",
72+
"templateId": 3,
73+
"type": "website",
74+
"status": "reviewed"
75+
}
76+
},
77+
{
78+
"param": {
79+
"name": "Develop chatbot",
80+
"details": {
81+
"utm": {
82+
"code": ""
83+
},
84+
"appDefinition": {
85+
"primaryTarget": "phone",
86+
"goal": {
87+
"value": "Nothing"
88+
},
89+
"users": {
90+
"value": "No one"
91+
},
92+
"notes": ""
93+
},
94+
"hideDiscussions": true
95+
},
96+
"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",
97+
"templateId": 4,
98+
"type": "chatbot",
99+
"status": "active"
100+
}
101+
},
102+
{
103+
"param": {
104+
"name": "Develop app 2",
105+
"details": {
106+
"utm": {
107+
"code": ""
108+
},
109+
"appDefinition": {
110+
"primaryTarget": "phone",
111+
"goal": {
112+
"value": "Nothing"
113+
},
114+
"users": {
115+
"value": "No one"
116+
},
117+
"notes": ""
118+
},
119+
"hideDiscussions": true
120+
},
121+
"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",
122+
"templateId": 1,
123+
"type": "app",
124+
"status": "completed"
125+
}
126+
},
127+
{
128+
"param": {
129+
"name": "Develop website 3",
130+
"details": {
131+
"utm": {
132+
"code": ""
133+
},
134+
"appDefinition": {
135+
"primaryTarget": "phone",
136+
"goal": {
137+
"value": "Nothing"
138+
},
139+
"users": {
140+
"value": "No one"
141+
},
142+
"notes": ""
143+
},
144+
"hideDiscussions": true
145+
},
146+
"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",
147+
"templateId": 2,
148+
"type": "website",
149+
"status": "paused"
150+
}
151+
},
152+
{
153+
"param": {
154+
"name": "Develop app 3",
155+
"details": {
156+
"utm": {
157+
"code": ""
158+
},
159+
"appDefinition": {
160+
"primaryTarget": "phone",
161+
"goal": {
162+
"value": "Nothing"
163+
},
164+
"users": {
165+
"value": "No one"
166+
},
167+
"notes": ""
168+
},
169+
"hideDiscussions": true
170+
},
171+
"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",
172+
"templateId": 1,
173+
"type": "app",
174+
"status": "cancelled",
175+
"cancelReason": "Test cancel"
176+
}
177+
}
178+
]

migrations/seedMetadata.js renamed to local/seed/seedMetadata.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable */
21
const _ = require('lodash')
32
const axios = require('axios');
43
const Promise = require('bluebird');
@@ -12,18 +11,18 @@ if (!process.env.CONNECT_USER_TOKEN) {
1211
const CONNECT_USER_TOKEN = process.env.CONNECT_USER_TOKEN;
1312

1413
var url = 'https://api.topcoder-dev.com/v4/projects/metadata';
15-
var targetUrl = 'http://localhost:8001/v4/';
16-
var destUrl = targetUrl + 'projects/';
17-
var destTimelines = targetUrl;
18-
19-
console.log('Getting metadata from DEV environment...');
20-
21-
axios.get(url, {
22-
headers: {
23-
'Content-Type': 'application/json',
24-
'Authorization': `Bearer ${CONNECT_USER_TOKEN}`
25-
}
26-
})
14+
15+
module.exports = (targetUrl, token) => {
16+
var destUrl = targetUrl + 'projects/';
17+
var destTimelines = targetUrl;
18+
19+
console.log('Getting metadata from DEV environment...');
20+
return axios.get(url, {
21+
headers: {
22+
'Content-Type': 'application/json',
23+
'Authorization': `Bearer ${CONNECT_USER_TOKEN}`
24+
}
25+
})
2726
.catch((err) => {
2827
const errMessage = _.get(err, 'response.data.result.content.message');
2928
throw errMessage ? new Error('Error during obtaining data from DEV: ' + errMessage) : err
@@ -35,7 +34,7 @@ axios.get(url, {
3534

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

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

9493
// handle success
95-
console.log('Done');
94+
console.log('Done metadata seed');
9695
}).catch(err=>{
9796
console.error(err && err.response ? err.response : err);
9897
});
98+
}

local/seed/seedProjects.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const axios = require('axios');
2+
const Promise = require('bluebird');
3+
const _ = require('lodash');
4+
5+
const projects = require('./projects.json');
6+
7+
/**
8+
* Create projects and update their statuses.
9+
*/
10+
module.exports = (targetUrl, token) => {
11+
let projectPromises;
12+
13+
const projectsUrl = `${targetUrl}projects`;
14+
const headers = {
15+
'Content-Type': 'application/json',
16+
Authorization: `Bearer ${token}`,
17+
};
18+
19+
console.log('Creating projects');
20+
projectPromises = projects.map((project, i) => {
21+
const status = _.get(project, 'param.status');
22+
const cancelReason = _.get(project, 'param.cancelReason');
23+
delete project.param.status;
24+
delete project.param.cancelReason;
25+
26+
return axios
27+
.post(projectsUrl, project, { headers })
28+
.catch((err) => {
29+
console.log(`Failed to create project ${i}: ${err.message}`);
30+
})
31+
.then((response) => {
32+
const projectId = _.get(response, 'data.result.content.id');
33+
34+
return {
35+
projectId,
36+
status,
37+
cancelReason,
38+
};
39+
});
40+
});
41+
42+
return Promise.all(projectPromises)
43+
.then((createdProjects) => {
44+
console.log('Updating statuses');
45+
return Promise.all(
46+
createdProjects.map(({ projectId, status, cancelReason }) =>
47+
updateProjectStatus(projectId, { status, cancelReason }, targetUrl, headers).catch((ex) => {
48+
console.log(`Failed to update project status of project with id ${projectId}: ${ex.message}`);
49+
}),
50+
),
51+
);
52+
})
53+
.then(() => console.log('Done project seed.'))
54+
.catch(ex => console.error(ex));
55+
};
56+
57+
function updateProjectStatus(project, updateParams, targetUrl, headers) {
58+
const projectUpdateUrl = `${targetUrl}projects/${project}`;
59+
60+
// only cancelled status requires cancelReason
61+
if (updateParams.status !== 'cancelled') {
62+
delete updateParams.cancelReason;
63+
}
64+
65+
return axios.patch(
66+
projectUpdateUrl,
67+
{
68+
param: updateParams,
69+
},
70+
{
71+
headers,
72+
},
73+
);
74+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"start:dev": "NODE_ENV=development PORT=8001 nodemon -w src --exec \"babel-node src --presets es2015\" | ./node_modules/.bin/bunyan",
2020
"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*')",
2121
"test:watch": "NODE_ENV=test ./node_modules/.bin/mocha -w --compilers js:babel-core/register $(find src -path '*spec.js*')",
22-
"seed": "babel-node src/tests/seed.js --presets es2015"
22+
"seed": "babel-node src/tests/seed.js --presets es2015",
23+
"demo-data": "babel-node local/seed"
2324
},
2425
"repository": {
2526
"type": "git",

0 commit comments

Comments
 (0)