From a47d27dcaaab6c438e83c3a2949e170909795858 Mon Sep 17 00:00:00 2001 From: devchima Date: Tue, 24 Oct 2023 10:58:21 -0400 Subject: [PATCH] Dob and edd format --- go-app-ussd_popi_rapidpro.js | 23 ++++++++++++++++++----- src/ussd_popi_rapidpro.js | 23 ++++++++++++++++++----- test/ussd_popi_rapidpro.test.js | 8 ++++---- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/go-app-ussd_popi_rapidpro.js b/go-app-ussd_popi_rapidpro.js index dd0932ae..9976066f 100644 --- a/go-app-ussd_popi_rapidpro.js +++ b/go-app-ussd_popi_rapidpro.js @@ -246,6 +246,11 @@ go.app = function() { }); }; + self.dateformat = function(timestamp){ + timestamp = (timestamp) ? timestamp.split('T')[0] : timestamp; + return timestamp; + }; + self.states.add("state_start", function(name, opts) { // Reset user answers when restarting the app self.im.user.answers = {}; @@ -449,15 +454,23 @@ go.app = function() { self.add("state_change_info", function(name) { var contact = self.im.user.answers.contact; - var baby_dob1, baby_dob2, baby_dob3, dates_count, edd, postbirth; + var dates_count, postbirth; var dates_list = []; var channel = _.get(contact, "fields.preferred_channel"); + var baby_dob1 = _.get(contact, "fields.baby_dob1", null); + var baby_dob2 = _.get(contact, "fields.baby_dob2", null); + var baby_dob3 = _.get(contact, "fields.baby_dob3", null); + var edd = _.get(contact, "fields.edd", null); + baby_dob1 = self.dateformat(baby_dob1); + baby_dob2 = self.dateformat(baby_dob2); + baby_dob3 = self.dateformat(baby_dob3); + edd = self.dateformat(edd); var context = { dobs: _.map(_.filter([ - new moment(_.get(contact, "fields.edd", null)), - new moment(_.get(contact, "fields.baby_dob1", null)), - new moment(_.get(contact, "fields.baby_dob2", null)), - new moment(_.get(contact, "fields.baby_dob3", null)), + new moment(edd), + new moment(baby_dob1), + new moment(baby_dob2), + new moment(baby_dob3), ], _.method("isValid")), _.method("format", "DD-MM-YYYY")).join(", ") || $("None") }; var dates_entry = Object.values(context); diff --git a/src/ussd_popi_rapidpro.js b/src/ussd_popi_rapidpro.js index 342f3b76..cf671eab 100644 --- a/src/ussd_popi_rapidpro.js +++ b/src/ussd_popi_rapidpro.js @@ -86,6 +86,11 @@ go.app = function() { }); }; + self.dateformat = function(timestamp){ + timestamp = (timestamp) ? timestamp.split('T')[0] : timestamp; + return timestamp; + }; + self.states.add("state_start", function(name, opts) { // Reset user answers when restarting the app self.im.user.answers = {}; @@ -289,15 +294,23 @@ go.app = function() { self.add("state_change_info", function(name) { var contact = self.im.user.answers.contact; - var baby_dob1, baby_dob2, baby_dob3, dates_count, edd, postbirth; + var dates_count, postbirth; var dates_list = []; var channel = _.get(contact, "fields.preferred_channel"); + var baby_dob1 = _.get(contact, "fields.baby_dob1", null); + var baby_dob2 = _.get(contact, "fields.baby_dob2", null); + var baby_dob3 = _.get(contact, "fields.baby_dob3", null); + var edd = _.get(contact, "fields.edd", null); + baby_dob1 = self.dateformat(baby_dob1); + baby_dob2 = self.dateformat(baby_dob2); + baby_dob3 = self.dateformat(baby_dob3); + edd = self.dateformat(edd); var context = { dobs: _.map(_.filter([ - new moment(_.get(contact, "fields.edd", null)), - new moment(_.get(contact, "fields.baby_dob1", null)), - new moment(_.get(contact, "fields.baby_dob2", null)), - new moment(_.get(contact, "fields.baby_dob3", null)), + new moment(edd), + new moment(baby_dob1), + new moment(baby_dob2), + new moment(baby_dob3), ], _.method("isValid")), _.method("format", "DD-MM-YYYY")).join(", ") || $("None") }; var dates_entry = Object.values(context); diff --git a/test/ussd_popi_rapidpro.test.js b/test/ussd_popi_rapidpro.test.js index 569187b8..050cf269 100644 --- a/test/ussd_popi_rapidpro.test.js +++ b/test/ussd_popi_rapidpro.test.js @@ -397,10 +397,10 @@ describe("ussd_popi_rapidpro app", function() { return tester .setup.user.state("state_change_info") .setup.user.answer("contact", {fields: {preferred_channel: "SMS", - baby_dob1: "2021-03-10", - baby_dob2: "2021-11-11", - baby_dob3: "2022-07-07", - edd: "2020-06-04", + baby_dob1: "2021-03-10T00:00:00.000000Z", + baby_dob2: "2021-11-11T00:00:00.000000Z", + baby_dob3: "2022-07-07T00:00:00.000000Z", + edd: "2020-06-04T00:00:00.000000Z", }}) .check.interaction({ reply: [