Skip to content

Commit

Permalink
Merge pull request #66 from freshworks/timezon-moment-update
Browse files Browse the repository at this point in the history
Timezone moment update
  • Loading branch information
aishik-biswas authored Dec 17, 2021
2 parents 6c6bc32 + 72b4922 commit 5b7ad8c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
4 changes: 2 additions & 2 deletions packages/freshchat-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@freshworks-jaya/freshchat-api",
"version": "0.7.27",
"version": "0.7.28-alpha.1",
"description": "Provides simple interface for accessing Freshchat's public APIs",
"repository": "[email protected]:freshdesk/jaya-lib.git",
"main": "lib/index.js",
Expand Down Expand Up @@ -43,7 +43,7 @@
"typescript": "^4.3.2"
},
"dependencies": {
"@freshworks-jaya/utilities": "^0.2.8",
"@freshworks-jaya/utilities": "^0.2.9-alpha.1",
"axios": "^0.21.4",
"dayjs": "^1.10.5",
"handlebars": "^4.7.7",
Expand Down
6 changes: 3 additions & 3 deletions packages/rule-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@freshworks-jaya/rule-engine",
"version": "0.17.3",
"version": "0.17.4-alpha.1",
"description": "Provides methods to process rules in product events in marketplace app",
"repository": "[email protected]:freshdesk/jaya-lib.git",
"main": "lib/index.js",
Expand Down Expand Up @@ -44,10 +44,10 @@
"typescript": "^4.3.2"
},
"dependencies": {
"@freshworks-jaya/freshchat-api": "^0.7.27",
"@freshworks-jaya/freshchat-api": "^0.7.28-alpha.1",
"@freshworks-jaya/kairos-api": "^0.1.5",
"@freshworks-jaya/marketplace-models": "^0.1.14",
"@freshworks-jaya/utilities": "^0.2.8",
"@freshworks-jaya/utilities": "^0.2.9-alpha.1",
"@google-cloud/logging": "^9.3.1",
"axios": "^0.21.4",
"bluebird": "^3.7.2",
Expand Down
5 changes: 2 additions & 3 deletions packages/utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@freshworks-jaya/utilities",
"version": "0.2.8",
"version": "0.2.9-alpha.1",
"description": "Provides a function that can replace placeholders in a string with a given map.",
"repository": "[email protected]:freshdesk/jaya-lib.git",
"main": "lib/index.js",
Expand Down Expand Up @@ -36,7 +36,6 @@
"typescript": "^4.3.2"
},
"dependencies": {
"dayjs": "^1.10.5",
"timezone-support": "^2.0.2"
"moment-timezone": "^0.5.33"
}
}
26 changes: 6 additions & 20 deletions packages/utilities/src/is-outside-business-hours.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import dayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import relativeTime from 'dayjs/plugin/relativeTime';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import { findTimeZone, getZonedTime } from 'timezone-support';
import { formatZonedTime } from 'timezone-support/dist/parse-format';

dayjs.extend(advancedFormat);
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
import moment from 'moment-timezone';

type DateInput = Date | number;

Expand Down Expand Up @@ -52,15 +43,9 @@ const getWorkingHours = (data: string): string[][] => {
*/
const toTimeZone = (timeStamp: DateInput, preferredTimeZone: string): string => {
if (timeStamp && preferredTimeZone) {
try {
const timeZoneData = findTimeZone(preferredTimeZone);
const convertedTime = getZonedTime(timeStamp, timeZoneData);
const format = 'YYYY-MM-DD HH:mm:ss';

return formatZonedTime(convertedTime, format);
} catch (err) {
return '';
}
const format = 'YYYY-MM-DD HH:mm:ss';
const convertedTime = moment(timeStamp).tz(preferredTimeZone);
return convertedTime.isValid() ? convertedTime.format(format) : '';
}
return '';
};
Expand All @@ -77,7 +62,7 @@ const isOutsideBusinessHours = (businessHour: BusinessHour, currentTimeInMillis:
if (!operatingHours.enabled) {
isAway = false;
} else {
agentTime = dayjs(toTimeZone(currentTimeInMillis, operatingHours.timezone.replace(' - ', '/')));
agentTime = moment(toTimeZone(currentTimeInMillis, operatingHours.timezone.replace(' - ', '/')));
agentDayOfWeek = (agentTime.day() + 6) % 7;
if (operatingHours.working[agentDayOfWeek] !== 'true') {
isAway = true;
Expand All @@ -89,6 +74,7 @@ const isOutsideBusinessHours = (businessHour: BusinessHour, currentTimeInMillis:
toTimeSeconds = parseInt(fromToArr[1], 10),
agentTimeFrom = agentTime.clone().startOf('day').add(fromTimeSeconds, 's'),
agentTimeTo = agentTime.clone().startOf('day').add(toTimeSeconds, 's');

if (agentTime.isAfter(agentTimeFrom) && agentTime.isBefore(agentTimeTo)) {
isAway = false;
break;
Expand Down
6 changes: 5 additions & 1 deletion packages/utilities/test/is-outside-business-hours.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Utils test', () => {
appId: 123,
created: '2020-04-08T12:51:20.654Z',
days: {
//8-12am, 2-4pm and 4-6pm UTC working hours
//8-12pm, 2-4pm and 4-6pm UTC working hours
'0': '28800;43200;50400;57600;57600;64800;',
'1': '28800;43200;50400;57600;57600;64800;',
'2': '28800;43200;50400;57600;57600;64800;',
Expand Down Expand Up @@ -83,5 +83,9 @@ describe('Utils test', () => {
const agentTime = 1586437800201;
assert.equal(true, isOutsideBusinessHours(businessHour, agentTime));
});
it('should go to the error block when wrong timestamp is provided', () => {
businessHour.enabled = true;
assert.equal(true, isOutsideBusinessHours(businessHour, 'agentTime' as unknown as number));
});
});
});
21 changes: 8 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2519,11 +2519,6 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
dependencies:
delayed-stream "~1.0.0"

[email protected]:
version "2.20.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==

commander@^2.19.0:
version "2.20.3"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
Expand Down Expand Up @@ -5722,7 +5717,14 @@ modify-values@^1.0.0:
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
integrity sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==

moment@^2.18.1:
moment-timezone@^0.5.33:
version "0.5.34"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.34.tgz#a75938f7476b88f155d3504a9343f7519d9a405c"
integrity sha512-3zAEHh2hKUs3EXLESx/wsgw6IQdusOT8Bxm3D9UrHPQR7zlMmzwybC8zHEM1tQ4LJwP7fcxrWr8tuBg05fFCbg==
dependencies:
moment ">= 2.9.0"

"moment@>= 2.9.0", moment@^2.18.1:
version "2.29.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
Expand Down Expand Up @@ -7833,13 +7835,6 @@ time-stamp@^1.0.1:
resolved "https://registry.yarnpkg.com/time-stamp/-/time-stamp-1.1.0.tgz#764a5a11af50561921b133f3b44e618687e0f5c3"
integrity sha1-dkpaEa9QVhkhsTPztE5hhofg9cM=

timezone-support@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/timezone-support/-/timezone-support-2.0.2.tgz#801d6924478b1b60f09b90699ce1127a6044cbe7"
integrity sha512-J/1PyHCX76vOPuJzCyHMQMH2wTjXCJ30R5EXaS/QTi+xYsL0thS0pubDrHCWnfG4zU1jpPJtctnBBRCOpcJZeQ==
dependencies:
commander "2.20.0"

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
Expand Down

0 comments on commit 5b7ad8c

Please sign in to comment.