@@ -15,9 +15,8 @@ const i18n = require('i18next');
15
15
// We import a language strings object containing all of our strings.
16
16
// The keys for each string will then be referenced in our code, e.g. handlerInput.t('WELCOME_MSG')
17
17
const 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' ) ;
21
20
22
21
/////////////////////////////////
23
22
// Handlers Definition
@@ -63,38 +62,39 @@ const HasBirthdayLaunchRequestHandler = {
63
62
}
64
63
console . log ( 'userTimeZone' , userTimeZone ) ;
65
64
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
71
68
const currentYear = currentDate . year ( ) ;
72
-
73
- console . log ( 'currentDateTime:' , currentDateTime ) ;
69
+
74
70
console . log ( 'currentDate:' , currentDate . toString ( ) ) ;
75
-
71
+
76
72
// 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 ) ;
78
75
let nextBirthday = moment ( dateStr , 'YYYY MMM DD' , locale ) ;
79
76
console . log ( 'nextBirthday:' , nextBirthday . toString ( ) )
80
77
81
- // check the difference between the current date and the next birthday
78
+ // calculate the difference between the current date and the next birthday
82
79
let diffDays = nextBirthday . diff ( currentDate , 'days' ) ;
83
80
84
81
// setting the default speakOutput to Happy xth Birthday!!
85
82
// Alexa will automatically correct the ordinal for you.
86
83
// no need to worry about when to use st, th, rd
87
84
let age = currentYear - year ;
88
-
89
85
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...
91
88
if ( diffDays > 0 ) {
92
89
speakOutput = handlerInput . t ( 'WELCOME_BACK_MSG' , { count : diffDays , age : age } ) ;
93
90
}
94
- // has already happened
91
+ // has already happened this year
95
92
else if ( diffDays < 0 ) {
93
+ // in this case, add one year to the next birthday,
96
94
nextBirthday = nextBirthday . add ( 1 , 'Y' ) ;
95
+ // recalculate the difference,
97
96
diffDays = nextBirthday . diff ( currentDate , 'days' )
97
+ // and add on extra year to the age
98
98
age ++
99
99
speakOutput = handlerInput . t ( 'WELCOME_BACK_MSG' , { count : diffDays , age : age } ) ;
100
100
}
@@ -260,21 +260,13 @@ const ErrorHandler = {
260
260
*/
261
261
const LoggingRequestInterceptor = {
262
262
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
- }
271
263
console . log ( "\n" + "********** REQUEST *********\n" +
272
264
JSON . stringify ( handlerInput , null , 4 ) ) ;
273
265
}
274
266
} ;
275
267
276
268
/**
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
278
270
*/
279
271
const LoggingResponseInterceptor = {
280
272
process ( handlerInput , response ) {
0 commit comments