Skip to content

Commit bc38747

Browse files
author
Parth Shah
committed
closes #20
1 parent 648c35a commit bc38747

File tree

6 files changed

+148
-48
lines changed

6 files changed

+148
-48
lines changed

.ebextensions/01-environment-variables.config

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ option_settings:
3232
- namespace: aws:elasticbeanstalk:application:environment
3333
option_name: DIRECT_PROJECT_SERVICE_ENDPOINT
3434
value: TBD
35+
- namespace: aws:elasticbeanstalk:application:environment
36+
option_name: TOPIC_SERVICE_ENDPOINT
37+
value: TBD
3538
- namespace: aws:elasticbeanstalk:application:environment
3639
option_name: FILE_SERVICE_ENDPOINT
3740
value: TBD

config/default.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"pubsubQueueName": "project.service",
1010
"pubsubExchangeName": "projects",
1111
"fileServiceEndpoint": "",
12+
"topicServiceEndpoint": "",
1213
"identityServiceEndpoint": "",
1314
"directProjectServiceEndpoint": "",
1415
"attachmentsS3Bucket": "topcoder-prod-media",

config/sample.local.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if (process.env.ENVIRONMENT === 'test') {
1111
"logentriesToken": "",
1212
"rabbitmqURL": "amqp://dockerhost:5672",
1313
"fileServiceEndpoint": "https://api.topcoder-dev.com/v3/files/",
14+
"topicServiceEndpoint": "https://api.topcoder-dev.com/v4/topics/",
1415
"directProjectServiceEndpoint": "https://api.topcoder-dev.com/v3/direct",
1516
"dbConfig": {
1617
"masterUrl": "postgres://coder:mysecretpassword@dockerhost:5432/projectsdb",

src/events/projects/create.event.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import _ from 'lodash'
2+
import util from '../../util'
3+
import topicService from '../../services/topicService'
4+
5+
/*
6+
NOTE: Use this code segment if you wish to create direct projects async
7+
import directService from '../../services/directProject'
8+
const createDirectProject = (project, logger) => {
9+
return util.getSystemUserToken(logger)
10+
.then(token => {
11+
const req = {
12+
id: 1,
13+
log: logger,
14+
headers: { authorization: `Bearer ${token}` }
15+
}
16+
// create direct project with name and description
17+
var body = {
18+
projectName: project.name,
19+
projectDescription: project.description
20+
}
21+
// billingAccountId is optional field
22+
if(project.billingAccountId){
23+
body.billingAccountId = project.billingAccountId
24+
}
25+
return directService.createDirectProject(req, body)
26+
})
27+
.catch((err) => {
28+
console.log(err)
29+
return Promise.reject(err)
30+
})
31+
32+
// // create project in direct
33+
// if (!project.directProectId) {
34+
// logger.debug('creating direct project')
35+
// createDirectProject(project, logger)
36+
// .then(resp => {
37+
// return models.Project.update(
38+
// { directProjectId: resp.data.result.content.projectId },
39+
// { where: { id: project.id } }
40+
// )
41+
// .then(() => next() )
42+
// .catch(err => next(err))
43+
// })
44+
// } else {
45+
// console.log(project.directProjectId)
46+
// next()
47+
// }
48+
49+
}
50+
*/
51+
const addProjectStatus = (req, logger, project) => {
52+
const topics = [
53+
{
54+
title: 'Hello, Coder here! Your project has been created successfully',
55+
body: 'It took almost 245ms for me to create it, but all is good now.\
56+
That\'s a lot of hard for a robot, you know!'
57+
},
58+
{
59+
title: 'Hey there, I\'m ready with the next steps for your project!',
60+
body: `<p>I went over the project and I see we still need to collect more\
61+
details before I can use my super computational powers and create your quote.</p>\
62+
<p>Head over to the <a href="/projects/${project.id}/specification/">Specification</a>\
63+
section and answer all of the required questions. If you already have a document\
64+
with specification, verify it against our checklist and upload it.</p>`
65+
}
66+
]
67+
let postPromises = []
68+
_.forEach(topics, t => {
69+
console.log(t)
70+
postPromises.push(topicService.createTopic(req, project.id, t.title, t.body))
71+
})
72+
73+
return Promise.all(postPromises)
74+
.then(val => console.log(val))
75+
}
76+
77+
78+
const createEventHandler = (logger, project) => {
79+
return util.getSystemUserToken(logger)
80+
.then(token => {
81+
const req = {
82+
id: 1,
83+
log: logger,
84+
headers: { authorization: `Bearer ${token}` }
85+
}
86+
return addProjectStatus(req, logger, project)
87+
})
88+
}
89+
90+
export default createEventHandler

src/events/projects/index.js

+5-48
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict'
2-
32
import _ from 'lodash'
4-
import util from '../../util'
53
import { EVENT } from '../../constants'
6-
import directService from '../../services/directProject'
4+
import createEventHandler from './create.event'
5+
76
module.exports = (app, logger) => {
87

98
// Handle internal events
@@ -29,50 +28,8 @@ module.exports = (app, logger) => {
2928
logger.debug('received msg \'project.draft-created\'', project.id)
3029

3130
// TODO insert into elasticsearch
32-
next()
33-
34-
// // create project in direct
35-
// if (!project.directProectId) {
36-
// logger.debug('creating direct project')
37-
// createDirectProject(project, logger)
38-
// .then(resp => {
39-
// return models.Project.update(
40-
// { directProjectId: resp.data.result.content.projectId },
41-
// { where: { id: project.id } }
42-
// )
43-
// .then(() => next() )
44-
// .catch(err => next(err))
45-
// })
46-
// } else {
47-
// console.log(project.directProjectId)
48-
// next()
49-
// }
31+
createEventHandler(logger, project)
32+
.then(() => next() )
33+
.catch(err => {console.log('error handling event', err); next(err) })
5034
})
51-
52-
53-
const createDirectProject = (project, logger) => {
54-
console.log('retrieving system user token')
55-
return util.getSystemUserToken(logger)
56-
.then(token => {
57-
const req = {
58-
id: 1,
59-
log: logger,
60-
headers: { authorization: `Bearer ${token}` }
61-
}
62-
// create direct project with name and description
63-
var body = {
64-
projectName: project.name,
65-
projectDescription: project.description
66-
}
67-
// billingAccountId is optional field
68-
if(project.billingAccountId){
69-
body.billingAccountId = project.billingAccountId
70-
}
71-
return directService.createDirectProject(req, body)
72-
})
73-
.catch((err) => {
74-
console.log(err)
75-
return Promise.reject(err)
76-
})
77-
}
7835
}

src/services/topicService.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
import util from '../util'
3+
import config from 'config'
4+
import _ from 'lodash'
5+
/**
6+
* Service methods to handle creating topics
7+
*/
8+
9+
/**
10+
* Build custom http client for request
11+
* @param req request
12+
* @returns custom http client
13+
* @private
14+
*/
15+
function _getHttpClient(req){
16+
var httpClient = util.getHttpClient(req)
17+
httpClient.defaults.headers.common['Authorization'] = req.headers.authorization
18+
httpClient.defaults.baseURL = config.get('topicServiceEndpoint')
19+
httpClient.defaults.timeout = 3000
20+
httpClient.interceptors.response.use((resp) => {
21+
// req.log.debug('resp: ', JSON.stringify(resp.data, null, 2))
22+
if (resp.status !== 200 || resp.data.result.status !== 200) {
23+
req.log.error('error resp: ', JSON.stringify(resp.data, null, 2))
24+
return Promise.reject(new Error(resp.data.result.content.message))
25+
}
26+
return Promise.resolve(resp)
27+
})
28+
return httpClient
29+
}
30+
31+
32+
export default {
33+
34+
/**
35+
* Create topics in topic service
36+
*/
37+
createTopic(req, projectId, title, message, tag='PRIMARY') {
38+
return _getHttpClient(req)
39+
.post('', {
40+
reference: 'project',
41+
referenceId: projectId.toString(),
42+
tag,
43+
title,
44+
body: message
45+
})
46+
47+
}
48+
}

0 commit comments

Comments
 (0)