I'm fairly new to JavaScript and Node and am wondering if I'm on the right track.
Module file:
'use strict';
var chalk = require('chalk');
var config = require('../config');
var moment = require('moment');
var rp = require('request-promise');
var todayDay = moment().format('dddd'); // ["Monday"]"
var todayDate = moment().format('DD').replace(/^0+/, ''); // "1"
var todayMonth = moment().format('MMMM'); // ["January"]
rp({
method: 'GET',
uri: config.api.uri + '/tasks',
qs: {
access_token: config.api.accessToken,
project_id: config.api.projectId,
page_size: 5
},
json: true
})
.then(function(res) {
console.log(res);
var data = res.data;
data.forEach(function(val) {
// day is today or date and month is today
if (val.custom_fields.custom_353.indexOf(todayDay) > -1 ||
val.custom_fields.custom_356.indexOf(todayMonth) > -1 && val.custom_fields.custom_358 == todayDate) {
// post
rp({
method: 'POST',
uri: config.api.uri + '/incidents',
qs: {
access_token: config.api.accessToken
},
body: {
item: {
name: val.name,
description: val.description,
due_date: moment().toISOString(),
assigned_to: {
type: val.assigned_to.type,
id: val.assigned_to.id
},
project: {
id: config.api.projectId
},
custom_fields: {
custom_361: val.custom_fields.custom_384, // sequence
custom_352: val.custom_fields.custom_270, // time
custom_354: val.custom_fields.custom_269, // procedure
custom_277: val.custom_fields.custom_183, // service group
custom_278: val.custom_fields.custom_184 // service
}
}
},
json: true
})
.then(function(res) {
console.log(chalk.green(val.name));
})
.catch(function(err) {
console.log(chalk.red(err));
});
} else {
console.log(chalk.grey(val.name));
}
});
})
.catch(function(err) {
console.log(chalk.red(err));
});
Config file:
'use strict';
var env = process.env.NODE_ENV || 'development';
var config = {};
switch (env) {
case 'development':
config = require('../env/development/config.json');
break;
case 'production':
config = require('../env/production/config.json');
break;
};
module.exports = config;