@@ -15,9 +15,8 @@ const i18n = require('i18next');
1515// We import a language strings object containing all of our strings.
1616// The keys for each string will then be referenced in our code, e.g. handlerInput.t('WELCOME_MSG')
1717const languageStrings = require ( './languageStrings' ) ;
18-
19- // We will use the moment.js package in order to make sure that we calculate the remaining days to the user's birthday correctly
20- const moment = require ( 'moment' ) ;
18+ // We will use the moment.js package in order to make sure that we calculate the date correctly
19+ const moment = require ( 'moment-timezone' ) ;
2120
2221/////////////////////////////////
2322// Handlers Definition
@@ -63,38 +62,39 @@ const HasBirthdayLaunchRequestHandler = {
6362 }
6463 console . log ( 'userTimeZone' , userTimeZone ) ;
6564
66- // getting the current date with the time
67- const locale = Alexa . getLocale ( requestEnvelope ) ;
68- const currentDateTime = new Date ( new Date ( ) . toLocaleString ( locale , { timeZone : userTimeZone } ) ) ;
69- // removing the time from the date because it affects our difference calculation
70- const currentDate = moment ( new Date ( currentDateTime . getFullYear ( ) , currentDateTime . getMonth ( ) , currentDateTime . getDate ( ) ) ) ;
65+ // getting the current date with the time set to the start of the day, aka 00:00AM
66+ const currentDate = moment ( ) . tz ( userTimeZone ) . startOf ( 'day' )
67+ // getting the current year
7168 const currentYear = currentDate . year ( ) ;
72-
73- console . log ( 'currentDateTime:' , currentDateTime ) ;
69+
7470 console . log ( 'currentDate:' , currentDate . toString ( ) ) ;
75-
71+
7672 // getting the next birthday
77- let dateStr = currentYear . toString ( ) + ' ' + month + ' ' + day . toString ( ) ;
73+ const dateStr = currentYear . toString ( ) + ' ' + month + ' ' + day . toString ( ) ;
74+ const locale = Alexa . getLocale ( requestEnvelope ) ;
7875 let nextBirthday = moment ( dateStr , 'YYYY MMM DD' , locale ) ;
7976 console . log ( 'nextBirthday:' , nextBirthday . toString ( ) )
8077
81- // check the difference between the current date and the next birthday
78+ // calculate the difference between the current date and the next birthday
8279 let diffDays = nextBirthday . diff ( currentDate , 'days' ) ;
8380
8481 // setting the default speakOutput to Happy xth Birthday!!
8582 // Alexa will automatically correct the ordinal for you.
8683 // no need to worry about when to use st, th, rd
8784 let age = currentYear - year ;
88-
8985 let speakOutput = handlerInput . t ( 'HAPPY_BIRTHDAY_MSG' , { age : age } ) ;
90- // checking if birthday still this year or
86+
87+ // checking if birthday is still to happen or...
9188 if ( diffDays > 0 ) {
9289 speakOutput = handlerInput . t ( 'WELCOME_BACK_MSG' , { count : diffDays , age : age } ) ;
9390 }
94- // has already happened
91+ // has already happened this year
9592 else if ( diffDays < 0 ) {
93+ // in this case, add one year to the next birthday,
9694 nextBirthday = nextBirthday . add ( 1 , 'Y' ) ;
95+ // recalculate the difference,
9796 diffDays = nextBirthday . diff ( currentDate , 'days' )
97+ // and add on extra year to the age
9898 age ++
9999 speakOutput = handlerInput . t ( 'WELCOME_BACK_MSG' , { count : diffDays , age : age } ) ;
100100 }
@@ -260,21 +260,13 @@ const ErrorHandler = {
260260 */
261261const LoggingRequestInterceptor = {
262262 process ( handlerInput ) {
263- const { requestEnvelope } = handlerInput ;
264- const type = Alexa . getRequestType ( requestEnvelope ) ;
265- const locale = Alexa . getLocale ( requestEnvelope ) ;
266- if ( type !== 'IntentRequest' ) {
267- console . log ( `[INFO] ${ type } (${ locale } )` ) ;
268- } else {
269- console . log ( `[INFO] ${ handlerInput . requestEnvelope . request . intent . name } (${ locale } )` ) ;
270- }
271263 console . log ( "\n" + "********** REQUEST *********\n" +
272264 JSON . stringify ( handlerInput , null , 4 ) ) ;
273265 }
274266} ;
275267
276268/**
277- * This response interceptor will log all outgoing responses in the associated Logs (CloudWatch) of the AWS Lambda functions
269+ * This response interceptor will log outgoing responses if any in the associated Logs (CloudWatch) of the AWS Lambda functions
278270 */
279271const LoggingResponseInterceptor = {
280272 process ( handlerInput , response ) {
0 commit comments