33 EVENT
44} from '../../constants'
55import util from '../../util'
6+ import config from 'config'
7+ import querystring from 'querystring'
68import topicService from '../../services/topicService'
79
810const _addProjectStatus = ( req , logger , project ) => {
@@ -29,6 +31,41 @@ const _addProjectStatus = (req, logger, project) => {
2931 return true
3032 } )
3133}
34+
35+ /**
36+ * Creates a lead in salesforce for the connect project.
37+ *
38+ * @param token JWT token of the admin user which would be used to fetch user info
39+ * @param logger logger to be used for logging
40+ * @param project connect project for which lead is to be created
41+ *
42+ * @return promise which resolves to the HTML content where salesforce web to lead form redirects
43+ */
44+ const _addSalesforceLead = ( token , logger , project ) => {
45+ logger . debug ( 'Getting topcoder user with userId: ' , project . createdBy )
46+ return util . getTopcoderUser ( project . createdBy , token , logger )
47+ . then ( ( userInfo ) => {
48+ var httpClient = util . getHttpClient ( { id : 2 , log : logger } )
49+ httpClient . defaults . timeout = 3000
50+ httpClient . defaults . headers . common [ 'Content-Type' ] = 'application/x-www-form-urlencoded'
51+ var data = {
52+ oid : config . get ( 'salesforceLead.orgId' ) ,
53+ first_name : userInfo . firstName ,
54+ last_name : userInfo . lastName ,
55+ email : userInfo . email
56+ }
57+ data [ config . get ( 'salesforceLead.projectIdFieldId' ) ] = project . id
58+ data [ config . get ( 'salesforceLead.projectNameFieldId' ) ] = project . name
59+ data [ config . get ( 'salesforceLead.projectDescFieldId' ) ] = project . description
60+ data [ config . get ( 'salesforceLead.projectLinkFieldId' ) ] = config . get ( 'connectProjectsUrl' ) + project . id
61+ var body = querystring . stringify ( data )
62+ var webToLeadUrl = config . get ( 'salesforceLead.webToLeadUrl' )
63+ logger . debug ( 'initiaiting salesforce web to lead call for project: ' , project . id )
64+ return httpClient . post ( webToLeadUrl , body )
65+ } )
66+ }
67+
68+
3269/**
3370 * Handler for project creation event
3471 * @param {[type] } logger logger to log along with trace id
@@ -46,10 +83,13 @@ const projectCreatedHandler = (logger, msg, channel) => {
4683 authorization : `Bearer ${ token } `
4784 }
4885 }
49- return _addProjectStatus ( req , logger , project )
86+ return Promise . all ( [
87+ _addProjectStatus ( req , logger , project ) ,
88+ _addSalesforceLead ( token , logger , project ) . then ( ( resp ) => logger . debug ( 'web to lead response:' , resp . status ) )
89+ ] ) ;
5090 } )
5191 . then ( ( ) => {
52- channel . ack ( msg , true )
92+ channel . ack ( msg )
5393 } )
5494 . catch ( err => {
5595 // don't requeue for now
0 commit comments