Goal
My company uses an extranet platform that lets customers post "ideas" for product enhancements. We would like to post notifications of these ideas in Slack. The extranet platform does not offer a webhook for ideas. It does offer an ideas API. However, it does not offer a way to query for ideas created before, after, or between two timestamps. The ideas API does return the idea creation date as a timestamp like this: 2018-11-13T02:03:31.583. It also returns the name of the author who posted the idea and the text of the idea.
The best solution I can come up with is to use an Azure Logic App that:
- Polls the ideas API for the latest X number of ideas once a day
- For each idea, pushes the creation date, author, and text fields of the returned JSON object to an Azure Function (code is below)
- The Azure Function compares the creation date field to today's date
- If the dates match, the Azure Function sends the author and text fields back to the Azure Logic App to send to Slack
- If the dates do not match, the Azure Function does nothing
Code
Input
(I left other idea metadata in JSON, such as the author and text, out to streamline my question)
{
"date": "2018-12-12T17:34:07.693"
}
Azure Function index.js
module.exports = function (context, data) {
var ideaDate = data.body;
var ideaDate2 = JSON.stringify(ideaDate);
var ideaDate3 = JSON.parse(ideaDate2);
var ideaDate4 = ideaDate3.date;
// Extract date only
var ideaDate5 = ideaDate4.substring(0, 10);
// Get today's date
var todaysDate = new Date();
var localDate = new Date(todaysDate);
var localDate1 = localDate.toISOString().toString()
// Extract date only
var localDate2 = localDate1.substring(0, 10);
///PROBLEMATIC LINE
if (new String(ideaDate5).valueOf() === new String(localDate2).valueOf()) {
// Response of the function to be used later
context.res = {
body: {
ideaDate,
ideaDate2,
ideaDate3,
ideaDate4,
ideaDate5,
todaysDate,
localDate,
localDate1,
localDate2
}
};
}
context.done();
};
Output
500: Internal Service Error
Output that is successful without the problematic if statement line
I am including all variables in the output to demonstrate what happens as the script runs.
{
"ideaDate": {
"date": "2018-12-12T17:34:07.693"
},
"ideaDate2": "{\"date\":\"2018-12-12T17:34:07.693\"}",
"ideaDate3": {
"date": "2018-12-12T17:34:07.693"
},
"ideaDate4": "2018-12-12T17:34:07.693",
"ideaDate5": "2018-12-12",
"todaysDate": "2018-12-12T23:51:26.110Z",
"localDate": "2018-12-12T23:51:26.110Z",
"localDate1": "2018-12-12T23:51:26.110Z",
"localDate2": "2018-12-12"
}
I am new to JavaScript
Thank you for any advice that detects the problem or recommends a better process entirely.
Edit
Thank you to ABOS for pointing out that an if statement must have parentheses around it. I have added these in the code printed above. The Azure Function no longer returns an error. However, it does not return the JSON fields in the output.
But I am very grateful to be one step closer.
Edit 2
function.json in the Azure Function:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
it does not return the JSON fields in the outputyou mean you don't get Json response in Logic app or what? Could you showfunction.json? - Jerry Liu2018-12-12T17:34:07.693in the input, it won't execute the if segment as UTC time is 12-13 now. - Jerry Liu