Skip to content

Commit

Permalink
init files
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeMaillet committed Jan 9, 2018
0 parents commit 0930a0f
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
parameters: './parameters.yml',
workflows_directory: './workflows/',
jobs_directory: './jobs/',
};
39 changes: 39 additions & 0 deletions jobs/add_reply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const replyService = require('../services/service');

module.exports = async(job) => {

const {
body,
workflow: {
data: wfData
}
} = job.data;

try {
await replyService.api(wfData.api_key).send('GET', `/v1/people?email=${encodeURIComponent(body.email)}`);
job.progress(35);

await replyService.api(wfData.api_key).send('POST', '/v1/actions/removepersonfromallcampaigns', {
'email': body.email
});
job.progress(75);

await replyService.api(wfData.api_key).send('POST', '/v1/actions/pushtocampaign', {
'campaignId': wfData.campaign_id,
'email': body.email,
});
job.progress(100);

} catch(err) {
if(err.statusCode === 404) {
await replyService.api(wfData.api_key).send('POST', '/v1/actions/addandpushtocampaign', {
'campaignId': wfData.campaign_id,
'email': body.email,
'firstName': body.email,
});
job.progress(100);
} else {
throw err;
}
}
};
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "workflow-lib/reply",
"version": "0.0.1",
"description": "Reply jobs for workflow manager",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Maxime Maillet<[email protected]>",
"license": "ISC",
"dependencies": {
"request-promise-native": "^1.0.5"
}
}
5 changes: 5 additions & 0 deletions parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
reply_do_api_key: 'zPrWY3NXsgaW-5Spbs9ciA2'
reply_ssii_api_key: '46cOPrjvAwRw9A4oRd3mrA2'
reply_axel_api_key: 'Adw_Mfu80vjHkF2fZclvhg2'
campaign_reply_doi: 30460
57 changes: 57 additions & 0 deletions services/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const request = require('request-promise-native');

module.exports.api = (apikey) => {
return endpointReply(apikey);
};

/**
* @param apiKey
* @returns {{send: (function(*=, *=, *=))}}
*/
const endpointReply = (apiKey) => {
return {
send: (method, endpoint, params) => {
return sendRequest(method, endpoint, params, apiKey);
}
};
};

/**
* Send request to reply
* @param method
* @param endpoint
* @param params
* @param apiKey
* @returns {*}
*/
function sendRequest(method, endpoint, params, apiKey) {
if (apiKey === null || apiKey === undefined) {
throw new Error('Missing api key');
}

const options = {
uri: `https://api.reply.io${endpoint}`,
port: 443,
method: method,
pathname: '',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Api-Key': apiKey
}
};

if (options.method === 'GET') {
options['qs'] = params;
} else {
options['body'] = JSON.stringify(params);
}

return request(options).then((data) => {
if (data) {
return JSON.parse(data);
} else {
return null;
}
});
}
14 changes: 14 additions & 0 deletions workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "User created from web site"
id: user-created

requirements:
data:
- hostname: 'adequancy.com'

stages:
- lehibou_do_created:
job: add_reply
data:
- api_key: "%reply_do_api_key%"
- campaign_id: "%campaign_reply_doi%"

0 comments on commit 0930a0f

Please sign in to comment.