|
|
|
|
@ -1,5 +1,6 @@ |
|
|
|
|
import type { ILivechatBusinessHour } from '@rocket.chat/core-typings'; |
|
|
|
|
import { LivechatBusinessHourTypes } from '@rocket.chat/core-typings'; |
|
|
|
|
import type { POSTLivechatBusinessHoursSaveParams } from '@rocket.chat/rest-typings'; |
|
|
|
|
import moment from 'moment'; |
|
|
|
|
|
|
|
|
|
import { api, credentials, methodCall, request } from '../api-data'; |
|
|
|
|
@ -9,26 +10,30 @@ type ISaveBhApiWorkHour = Omit<ILivechatBusinessHour, '_id' | 'ts' | 'timezone'> |
|
|
|
|
workHours: { day: string; start: string; finish: string; open: boolean }[]; |
|
|
|
|
} & { departmentsToApplyBusinessHour?: string } & { timezoneName: string }; |
|
|
|
|
|
|
|
|
|
// TODO: Migrate to an API call and return the business hour updated/created
|
|
|
|
|
export const saveBusinessHour = async (businessHour: ISaveBhApiWorkHour) => { |
|
|
|
|
const { body } = await request |
|
|
|
|
.post(methodCall('livechat:saveBusinessHour')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.send({ message: JSON.stringify({ params: [businessHour], msg: 'method', method: 'livechat:saveBusinessHour', id: '101' }) }) |
|
|
|
|
.expect(200); |
|
|
|
|
export const saveBusinessHour = async (businessHour: POSTLivechatBusinessHoursSaveParams) => { |
|
|
|
|
const { body } = await request.post(api('livechat/business-hours.save')).set(credentials).send(businessHour); |
|
|
|
|
|
|
|
|
|
return JSON.parse(body.message); |
|
|
|
|
return body; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const createCustomBusinessHour = async (departments: string[], open = true): Promise<ILivechatBusinessHour> => { |
|
|
|
|
const name = `business-hour-${Date.now()}`; |
|
|
|
|
const businessHour: ISaveBhApiWorkHour = { |
|
|
|
|
const businessHour: POSTLivechatBusinessHoursSaveParams = { |
|
|
|
|
name, |
|
|
|
|
active: true, |
|
|
|
|
type: LivechatBusinessHourTypes.CUSTOM, |
|
|
|
|
workHours: getWorkHours(open), |
|
|
|
|
timezoneName: 'Asia/Calcutta', |
|
|
|
|
timezone: 'Asia/Calcutta', |
|
|
|
|
departmentsToApplyBusinessHour: '', |
|
|
|
|
daysOpen: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], |
|
|
|
|
daysTime: [ |
|
|
|
|
{ day: 'Monday', start: { time: '08:00' }, finish: { time: '18:00' }, open }, |
|
|
|
|
{ day: 'Tuesday', start: { time: '08:00' }, finish: { time: '18:00' }, open }, |
|
|
|
|
{ day: 'Wednesday', start: { time: '08:00' }, finish: { time: '18:00' }, open }, |
|
|
|
|
{ day: 'Thursday', start: { time: '08:00' }, finish: { time: '18:00' }, open }, |
|
|
|
|
{ day: 'Friday', start: { time: '08:00' }, finish: { time: '18:00' }, open }, |
|
|
|
|
], |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if (departments.length) { |
|
|
|
|
@ -56,31 +61,35 @@ export const makeDefaultBusinessHourActiveAndClosed = async () => { |
|
|
|
|
body: { businessHour }, |
|
|
|
|
} = await request.get(api('livechat/business-hour')).query({ type: 'default' }).set(credentials).send(); |
|
|
|
|
|
|
|
|
|
// TODO: Refactor this to use openOrCloseBusinessHour() instead
|
|
|
|
|
const workHours = businessHour.workHours as { start: string; finish: string; day: string; open: boolean }[]; |
|
|
|
|
const allEnabledWorkHours = workHours.map((workHour) => { |
|
|
|
|
const { workHours } = businessHour; |
|
|
|
|
|
|
|
|
|
// Remove properties not accepted by the endpoint schema
|
|
|
|
|
const { _updatedAt, ts, ...cleanedBusinessHour } = businessHour; |
|
|
|
|
|
|
|
|
|
const enabledBusinessHour = { |
|
|
|
|
...cleanedBusinessHour, |
|
|
|
|
timezoneName: 'America/Sao_Paulo', |
|
|
|
|
timezone: 'America/Sao_Paulo', |
|
|
|
|
departmentsToApplyBusinessHour: '', |
|
|
|
|
daysOpen: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], |
|
|
|
|
daysTime: workHours.map((workHour: { open: boolean; start: { time: string }; finish: { time: string }; day: string }) => { |
|
|
|
|
return { |
|
|
|
|
open: true, |
|
|
|
|
start: { time: '00:00' }, |
|
|
|
|
finish: { time: '00:01' }, |
|
|
|
|
day: workHour.day, |
|
|
|
|
}; |
|
|
|
|
}), |
|
|
|
|
workHours: workHours.map((workHour: { open: boolean; start: string; finish: string; day: string; code?: number }) => { |
|
|
|
|
workHour.open = true; |
|
|
|
|
workHour.start = '00:00'; |
|
|
|
|
workHour.finish = '00:01'; // if a job runs between 00:00 and 00:01, then this test will fail :P
|
|
|
|
|
delete workHour.code; |
|
|
|
|
return workHour; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const enabledBusinessHour = { |
|
|
|
|
...businessHour, |
|
|
|
|
workHours: allEnabledWorkHours, |
|
|
|
|
}), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
await request |
|
|
|
|
.post(methodCall('livechat:saveBusinessHour')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.send({ |
|
|
|
|
message: JSON.stringify({ |
|
|
|
|
method: 'livechat:saveBusinessHour', |
|
|
|
|
params: [enabledBusinessHour], |
|
|
|
|
id: 'id', |
|
|
|
|
msg: 'method', |
|
|
|
|
}), |
|
|
|
|
}); |
|
|
|
|
return request.post(api('livechat/business-hours.save')).set(credentials).send(enabledBusinessHour).expect(200); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const disableDefaultBusinessHour = async () => { |
|
|
|
|
@ -93,31 +102,33 @@ export const disableDefaultBusinessHour = async () => { |
|
|
|
|
body: { businessHour }, |
|
|
|
|
} = await request.get(api('livechat/business-hour')).query({ type: 'default' }).set(credentials).send(); |
|
|
|
|
|
|
|
|
|
// TODO: Refactor this to use openOrCloseBusinessHour() instead
|
|
|
|
|
const workHours = businessHour.workHours as { start: string; finish: string; day: string; open: boolean }[]; |
|
|
|
|
const allDisabledWorkHours = workHours.map((workHour) => { |
|
|
|
|
const { workHours } = businessHour; |
|
|
|
|
|
|
|
|
|
// Remove properties not accepted by the endpoint schema
|
|
|
|
|
const { _updatedAt, ts, ...cleanedBusinessHour } = businessHour; |
|
|
|
|
const disabledBusinessHour = { |
|
|
|
|
...cleanedBusinessHour, |
|
|
|
|
timezoneName: 'America/Sao_Paulo', |
|
|
|
|
timezone: 'America/Sao_Paulo', |
|
|
|
|
departmentsToApplyBusinessHour: '', |
|
|
|
|
daysOpen: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], |
|
|
|
|
daysTime: workHours.map((workHour: { open: boolean; start: { time: string }; finish: { time: string }; day: string }) => { |
|
|
|
|
return { |
|
|
|
|
open: false, |
|
|
|
|
start: { time: '00:00' }, |
|
|
|
|
finish: { time: '23:59' }, |
|
|
|
|
day: workHour.day, |
|
|
|
|
}; |
|
|
|
|
}), |
|
|
|
|
workHours: workHours.map((workHour: { open: boolean; start: string; finish: string; day: string }) => { |
|
|
|
|
workHour.open = false; |
|
|
|
|
workHour.start = '00:00'; |
|
|
|
|
workHour.finish = '23:59'; |
|
|
|
|
return workHour; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const disabledBusinessHour = { |
|
|
|
|
...businessHour, |
|
|
|
|
workHours: allDisabledWorkHours, |
|
|
|
|
}), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
await request |
|
|
|
|
.post(methodCall('livechat:saveBusinessHour')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.send({ |
|
|
|
|
message: JSON.stringify({ |
|
|
|
|
method: 'livechat:saveBusinessHour', |
|
|
|
|
params: [disabledBusinessHour], |
|
|
|
|
id: 'id', |
|
|
|
|
msg: 'method', |
|
|
|
|
}), |
|
|
|
|
}); |
|
|
|
|
return request.post(api('livechat/business-hours.save')).set(credentials).send(disabledBusinessHour).expect(200); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const removeCustomBusinessHour = async (businessHourId: string) => { |
|
|
|
|
@ -167,10 +178,15 @@ export const getCustomBusinessHourById = async (businessHourId: string): Promise |
|
|
|
|
return response.body.businessHour; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// TODO: Refactor logic so object passed is of the correct type for POST /livechat/business-hours.save. See: CORE-1552
|
|
|
|
|
export const openOrCloseBusinessHour = async (businessHour: ILivechatBusinessHour, open: boolean) => { |
|
|
|
|
const { _updatedAt, ts, ...cleanedBusinessHour } = businessHour; |
|
|
|
|
const timezoneName = businessHour.timezone.name; |
|
|
|
|
|
|
|
|
|
const enabledBusinessHour = { |
|
|
|
|
...businessHour, |
|
|
|
|
timezoneName: businessHour.timezone.name, |
|
|
|
|
...cleanedBusinessHour, |
|
|
|
|
timezoneName, |
|
|
|
|
timezone: timezoneName, |
|
|
|
|
workHours: getWorkHours().map((workHour) => { |
|
|
|
|
return { |
|
|
|
|
...workHour, |
|
|
|
|
|