1
\$\begingroup\$

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;
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
var todayDate = moment().format('DD').replace(/^0+/, ''); // "1"

Looks like you're taking a zero-padded day of month here... and then stripping off the leading zero.

You should use moment().format('D'), it will give you day of month without a leading zero.

Aside from that... you shouldn't use multiple instances of moment(). What if the time changes between the two lines of code? You might get things like 31 February.

var todayMoment = moment();

var todayDay = todayMoment.format('dddd'); // ["Monday"]
var todayDate = todayMoment.format('D'); // "1"
var todayMonth = todayMoment.format('MMMM'); // ["January"]

Like so, both problems are fixed.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.