vedmalex / mws-sdk

Complete Amazon marketplace web services client for Node.js.
MIT License
27 stars 29 forks source link

StartDate and EndDate params for RequestReport API do not seem to follow ISO #21

Closed zxr90 closed 6 years ago

zxr90 commented 6 years ago
const requestOrdersReport = (client, args) => {
  const req = MWS.Reports.requests.RequestReport();
  req.set(args);
  return client.invoke(req);
};

app.get('/requestOrdersReport', async (req, res) => {
  try {
    const result = await requestOrdersReport(mwsClient, {
      ReportType: '_GET_FLAT_FILE_ORDERS_DATA_',
      StartDate: new Date(2017, 12, 10),
      EndDate: new Date(2017, 12, 16)
    });
    res.send(result);
  } catch (error) {
    console.log(error);
  }
});

My response for this request is as follow:

{
    "RequestReportResponse": {
        "$": {
            "xmlns": "http://mws.amazonaws.com/doc/2009-01-01/"
        },
        "RequestReportResult": [
            {
                "ReportRequestInfo": [
                    {
                        "ReportType": [
                            "_GET_FLAT_FILE_ORDERS_DATA_"
                        ],
                        "ReportProcessingStatus": [
                            "_SUBMITTED_"
                        ],
                        "EndDate": [
                            "2018-01-15T16:00:00+00:00"
                        ],
                        "Scheduled": [
                            "false"
                        ],
                        "ReportRequestId": [
                            "REQUEST_ID_HERE"
                        ],
                        "SubmittedDate": [
                            "2017-12-18T05:39:09+00:00"
                        ],
                        "StartDate": [
                            "2018-01-09T16:00:00+00:00"
                        ]
                    }
                ]
            }
        ],
        "ResponseMetadata": [
            {
                "RequestId": [
                    "REQUEST_ID_HERE"
                ]
            }
        ]
    }
}

As you can see, although I have specified my date range for StartDate and EndDate in Dec 2017, the response returned from Amazon changed it into Jan 2018

I took a look at mws.js and saw this code block converting values to valid ISO timestamp.

// Handles the actual setting based on type
    var setValue = function setValue(name, val) {
      if (p.type == 'Timestamp') {
        self.params[name].value = val.toISOString();
      } else if (p.type == 'Boolean') {
        self.params[name].value = val ? 'true' : 'false';
      } else {
        self.params[name].value = val;
      }
    };

new Date().toISOString returns a new date in ISO format: 2017-12-18T05:44:50.476Z

However for RequestReport API, StartDate and EndDate should be in this format 2018-01-15T16:00:00+00:00 as seen from Amazon's response.

Using that timestamp, I modified mws.js to not convert values to ISOString, I have managed to successfully get the date range I wanted.

// Handles the actual setting based on type
    var setValue = function setValue(name, val) {
      if (p.type == 'Timestamp') {
        self.params[name].value = val; // Remove to ISO
      } else if (p.type == 'Boolean') {
        self.params[name].value = val ? 'true' : 'false';
      } else {
        self.params[name].value = val;
      }
    };

// Update my API to take in date string values
app.get('/requestOrdersReport', async (req, res) => {
  try {
    const result = await requestOrdersReport(mwsClient, {
      ReportType: '_GET_FLAT_FILE_ORDERS_DATA_',
      StartDate: '2017-12-10T00:00:00+00:00',
      EndDate: '2017-12-15T00:00:00+00:00'
    });
    res.send(result);
  } catch (error) {
    console.log(error);
  }
});

// Response from Amazon
{
    "RequestReportResponse": {
        "$": {
            "xmlns": "http://mws.amazonaws.com/doc/2009-01-01/"
        },
        "RequestReportResult": [
            {
                "ReportRequestInfo": [
                    {
                        "ReportType": [
                            "_GET_FLAT_FILE_ORDERS_DATA_"
                        ],
                        "ReportProcessingStatus": [
                            "_SUBMITTED_"
                        ],
                        "EndDate": [
                            "2017-12-15T00:00:00+00:00"
                        ],
                        "Scheduled": [
                            "false"
                        ],
                        "ReportRequestId": [
                            "REQUEST_ID"
                        ],
                        "SubmittedDate": [
                            "2017-12-18T05:53:25+00:00"
                        ],
                        "StartDate": [
                            "2017-12-10T00:00:00+00:00"
                        ]
                    }
                ]
            }
        ],
        "ResponseMetadata": [
            {
                "RequestId": [
                    "REQUEST_ID"
                ]
            }
        ]
    }
}
bhushankummar commented 6 years ago

@zxr90 Here is the workaround available. https://www.npmjs.com/package/amazon-mws