fix: Update business hour calculation (#4496)

This commit is contained in:
Pranav Raj S 2022-04-18 18:15:20 +05:30 committed by GitHub
parent e010f0c6f0
commit 17fb6b8d55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 125 additions and 46 deletions

View file

@ -1,5 +1,5 @@
import compareAsc from 'date-fns/compareAsc';
import { buildDateFromTime } from 'shared/helpers/DateHelper';
import { utcToZonedTime } from 'date-fns-tz';
import { isTimeAfter } from 'shared/helpers/DateHelper';
export default {
computed: {
@ -33,23 +33,32 @@ export default {
closedAllDay,
openAllDay,
} = this.currentDayAvailability;
if (openAllDay || closedAllDay) {
return true;
}
const { utcOffset } = this.channelConfig;
if (openAllDay) return true;
if (closedAllDay) return false;
const startTime = buildDateFromTime(openHour, openMinute, utcOffset);
const endTime = buildDateFromTime(closeHour, closeMinute, utcOffset);
const isBetween =
compareAsc(new Date(), startTime) === 1 &&
compareAsc(endTime, new Date()) === 1;
if (isBetween) return true;
return false;
const today = this.getDateWithOffset(utcOffset);
const currentHours = today.getHours();
const currentMinutes = today.getMinutes();
const isAfterStartTime = isTimeAfter(
currentHours,
currentMinutes,
openHour,
openMinute
);
const isBeforeEndTime = isTimeAfter(
closeHour,
closeMinute,
currentHours,
currentMinutes
);
return isAfterStartTime && isBeforeEndTime;
},
currentDayAvailability() {
const dayOfTheWeek = new Date().getDay();
const { utcOffset } = this.channelConfig;
const dayOfTheWeek = this.getDateWithOffset(utcOffset).getDay();
const [workingHourConfig = {}] = this.channelConfig.workingHours.filter(
workingHour => workingHour.day_of_week === dayOfTheWeek
);
@ -63,8 +72,14 @@ export default {
};
},
isInBusinessHours() {
const { workingHoursEnabled } = window.chatwootWebChannel;
const { workingHoursEnabled } = this.channelConfig;
return workingHoursEnabled ? this.isInBetweenTheWorkingHours : true;
},
},
methods: {
getDateWithOffset(utcOffset) {
return utcToZonedTime(new Date().toISOString(), utcOffset);
},
},
};