TT#112013 CF - Fix date range data creation and interpretation

AC:
Can see the data converted properly after saving
Can see the data rendered properly after fetching it again

Change-Id: I98259176b0dcbcfd76030305905e4e8c454df004
mr9.4
Sergii Leonenko 5 years ago
parent 6c8dd34ec1
commit 793e634d46

@ -162,41 +162,23 @@ export async function cfDeleteTimeSet (timesetId) {
})
}
export async function cfCreateTimeSetDateRange (subscriberId, date) {
export async function cfCreateTimeSetDateRange (subscriberId, times) {
return post({
resource: 'cftimesets',
body: {
subscriber_id: subscriberId,
name: 'csc-date-range-' + v4(),
times: [
{
minute: null,
month: date.from.month + '-' + date.to.month,
hour: null,
mday: date.from.date + '-' + date.to.date,
year: date.from.year + '-' + date.to.year,
wday: null
}
]
times: times
}
})
}
export async function cfUpdateTimeSetDateRange (timeSetId, date) {
export async function cfUpdateTimeSetDateRange (timeSetId, times) {
return patchReplace({
resource: 'cftimesets',
resourceId: timeSetId,
fieldPath: 'times',
value: [
{
minute: null,
month: date.from.month + '-' + date.to.month,
hour: null,
mday: date.from.date + '-' + date.to.date,
year: date.from.year + '-' + date.to.year,
wday: null
}
]
value: times
})
}

@ -6,14 +6,32 @@
v-bind="$attrs"
v-on="$listeners"
>
<q-date
v-model="selectedDate"
class="no-margin no-padding"
flat
square
minimal
range
/>
<template
v-if="invalidDateset"
>
<q-banner
rounded
dense
class="bg-red-8 text-white q-pt-md q-ma-md half-screen-width"
>
<template v-slot:avatar>
<q-icon name="date_range" />
</template>
{{ $t('The "{timeset}" timeset contains incompatible values. You can resolve this by deleting it and recreating from the scratch.', { timeset: timeSet.name }) }}
</q-banner>
</template>
<template
v-else
>
<q-date
v-model="selectedDate"
class="no-margin no-padding"
flat
square
minimal
range
/>
</template>
<template
v-slot:actions
>
@ -26,6 +44,7 @@
@click="deleteSourceSetEvent"
/>
<q-btn
v-if="!invalidDateset"
:label="$t('Save')"
flat
color="primary"
@ -39,7 +58,7 @@
<script>
import CscCfGroupCondition from 'components/call-forwarding/CscCfGroupCondition'
import { mapActions } from 'vuex'
import { timeSetDateRange } from 'src/filters/time-set'
import { humanDatesetToKamailio, kamailioDatesetToHuman } from 'src/helpers/kamailio-timesets-converter'
export default {
name: 'CscCfGroupConditionDateRange',
components: {
@ -69,30 +88,12 @@ export default {
},
data () {
return {
selectedDate: this.transformedDate
}
},
computed: {
formattedDate () {
if (this.timeSet) {
return timeSetDateRange(this.timeSet.times)
}
return null
},
transformedDate () {
if (this.timeSet) {
const dateRangeParts = timeSetDateRange(this.timeSet.times).split('-')
return {
from: dateRangeParts[0],
to: dateRangeParts[1]
}
} else {
return null
}
invalidDateset: false,
selectedDate: null
}
},
mounted () {
this.selectedDate = this.transformedDate
this.transformDateset()
},
methods: {
...mapActions('callForwarding', [
@ -100,23 +101,32 @@ export default {
'updateTimeSetDateRange',
'deleteTimeSet'
]),
transformDateset () {
if (this.timeSet) {
let hDateset
try {
hDateset = kamailioDatesetToHuman(this.timeSet.times)
} catch (e) {
this.invalidDateset = true
console.info(e)
return
}
if (hDateset.length === 0) {
this.selectedDate = null
} else {
this.selectedDate = (hDateset[0].from === hDateset[0].to) ? hDateset[0].from : hDateset[0]
}
} else {
this.selectedDate = null
}
},
async createTimeSetEvent () {
const datePartsFrom = this.selectedDate.from.split('/')
const datePartsTo = this.selectedDate.to.split('/')
const dateFrom = this.selectedDate.from ? this.selectedDate.from : this.selectedDate
const dateTo = this.selectedDate.to ? this.selectedDate.to : this.selectedDate
const kamilioTimesets = humanDatesetToKamailio([{ from: dateFrom, to: dateTo }])
const payload = {
mapping: this.mapping,
date: {
from: {
date: datePartsFrom[2],
month: datePartsFrom[1],
year: datePartsFrom[0]
},
to: {
date: datePartsTo[2],
month: datePartsTo[1],
year: datePartsTo[0]
}
}
date: kamilioTimesets
}
if (this.timeSet) {
payload.id = this.timeSet.id

@ -467,8 +467,6 @@ export default {
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.half-screen-width
width 50vw
.time-range-list-height
// NOTE: 65vh is a default max-height for q-dialog. Other magic numbers are sum of another elements heights, like headers, buttons etc

@ -33,3 +33,6 @@ body.body--dark
background-color $item-stripe-color
> :nth-of-type(2n)
background-color alpha($main-menu-background, 0.2)
.half-screen-width
width 50vw

@ -1,6 +1,6 @@
import { i18n } from 'boot/i18n'
import { getKamailioRangeElements } from 'src/helpers/kamailio-timesets-converter'
import { getKamailioRangeElements, kamailioDatesetToHuman } from 'src/helpers/kamailio-timesets-converter'
export const DAY_MAP = [2, 3, 4, 5, 6, 7, 1]
export const DEFAULT_WEEKDAYS = [
@ -39,11 +39,14 @@ export function timeSetDateExact (times) {
}
export function timeSetDateRange (times) {
const years = times[0].year.split('-')
const months = times[0].month.split('-')
const dates = times[0].mday.split('-')
return years[0] + '/' + months[0] + '/' + dates[0] + '-' +
years[1] + '/' + months[1] + '/' + dates[1]
try {
const hDateset = kamailioDatesetToHuman(times)
return (hDateset.length === 0)
? i18n.t('empty')
: hDateset.map(d => (d.from === d.to) ? d.from : d.from + '-' + d.to).join(', ')
} catch (e) {
return i18n.t('data error')
}
}
export function timeSetWeekdays (times) {

@ -31,6 +31,10 @@ kamailioTimeset = [{
]
*/
function getAsTrimmedString (data) {
return (data === null || data === undefined) ? '' : String(data).trim()
}
export function getTimeStrElements (timeStr) {
const [hours, minutes] = timeStr.split(':').map(t => Number(t))
return { hours, minutes }
@ -168,7 +172,7 @@ export function humanTimesetToKamailio (hTimeset = []) {
return result
})
const kamailioTimeset = kamailioTimesetRaw.reduce((acc, item) => {
let kamailioTimeset = kamailioTimesetRaw.reduce((acc, item) => {
const optimizedItemRanges = item.map(item => {
// if minute or hour contains range like "a-a" convert to just "a"
if (item.hour) {
@ -186,25 +190,44 @@ export function humanTimesetToKamailio (hTimeset = []) {
// similar to flatMap
return [...acc, ...optimizedItemRanges]
}, [])
.reduce((acc, item) => {
// combine the same time ranges by "wday"
if (acc.length === 0) {
kamailioTimeset = kamailioTimeset.reduce((acc, item) => {
// combine the same time ranges by "wday"
if (acc.length === 0) {
acc.push(item)
} else {
const mergeCandidate = acc.find(accItem =>
accItem.minute === item.minute &&
accItem.hour === item.hour &&
getKamailioRangeElements(accItem.wday)[0].to + 1 === getKamailioRangeElements(item.wday)[0].from
)
if (mergeCandidate) {
const mergeCandidateWday = getKamailioRangeElements(mergeCandidate.wday)[0]
mergeCandidate.wday = `${mergeCandidateWday.from}-${mergeCandidateWday.to + 1}`
} else {
acc.push(item)
}
}
return acc
}, [])
kamailioTimeset = kamailioTimeset.reduce((acc, item) => {
// combine the same time ranges by "hour"
if (acc.length === 0) {
acc.push(item)
} else {
const mergeCandidate = acc.find(accItem =>
accItem.minute === item.minute &&
getKamailioRangeElements(accItem.hour)[0].to + 1 === getKamailioRangeElements(item.hour)[0].from &&
accItem.wday === item.wday
)
if (mergeCandidate) {
const mergeCandidateHour = getKamailioRangeElements(mergeCandidate.hour)[0]
mergeCandidate.hour = `${mergeCandidateHour.from}-${mergeCandidateHour.to + 1}`
} else {
const mergeCandidate = acc.find(accItem =>
accItem.hour === item.hour &&
accItem.minute === item.minute &&
getKamailioRangeElements(accItem.wday)[0].to + 1 === getKamailioRangeElements(item.wday)[0].from
)
if (mergeCandidate) {
const mergeCandidateWday = getKamailioRangeElements(mergeCandidate.wday)[0]
mergeCandidate.wday = `${mergeCandidateWday.from}-${mergeCandidateWday.to + 1}`
} else {
acc.push(item)
}
acc.push(item)
}
return acc
}, [])
}
return acc
}, [])
return kamailioTimeset
}
@ -270,9 +293,9 @@ export function validateKamailioRange (kamailioRangeStr = '', minValue, maxValue
export function validateKamailioTimesets (kTimeset) {
kTimeset.forEach(timesetItem => {
let { wday, hour, minute } = timesetItem
wday = (wday === null || wday === undefined) ? '' : String(wday).trim()
hour = (hour === null || hour === undefined) ? '' : String(hour).trim()
minute = (minute === null || minute === undefined) ? '' : String(minute).trim()
wday = getAsTrimmedString(wday)
hour = getAsTrimmedString(hour)
minute = getAsTrimmedString(minute)
if (wday !== '') {
validateKamailioRange(wday, 1, 7)
} else {
@ -288,7 +311,7 @@ export function validateKamailioTimesets (kTimeset) {
Object.entries(timesetItem)
.filter(([key]) => !['wday', 'hour', 'minute'].includes(key))
.forEach(([key, value]) => {
if (!(value === null || value === undefined || String(value).trim().length === 0)) {
if (getAsTrimmedString(value).length !== 0) {
throw Error(`The "${key}" scale of Kamailio timesets is not supported: ${JSON.stringify(timesetItem)}`)
}
})
@ -301,8 +324,8 @@ export function kamailioTimesetToHuman (kTimeset = []) {
// convert Kamailio timeset into Human readable format
const hTimesetRaw = kTimeset.map(timesetItem => {
let { wday, hour, minute } = timesetItem
hour = (hour === null || hour === undefined) ? '' : String(hour).trim()
minute = (minute === null || minute === undefined) ? '' : String(minute).trim()
hour = getAsTrimmedString(hour)
minute = getAsTrimmedString(minute)
wday = getKamailioRangeElements(wday)[0]
if (hour !== '') {
@ -316,8 +339,8 @@ export function kamailioTimesetToHuman (kTimeset = []) {
minute = { from: 0, to: 59 }
}
const nestedRules = [wday, hour, minute].reverse()
const rulesOutput = nestedRules.reduce((acc, range) => {
const nestedRules = [wday, hour, minute]
const rulesOutput = nestedRules.reduceRight((acc, range) => {
const newAcc = []
if (acc.length === 0) {
newAcc.push({ from: String(range.from), to: String(range.to + 1) })
@ -351,5 +374,398 @@ export function kamailioTimesetToHuman (kTimeset = []) {
})
.reduce((acc, item) => [...acc, ...item], [])
return getHumanTimesetsNormalized(hTimesetRaw)
const res = getHumanTimesetsNormalized(hTimesetRaw)
return res
}
// ---- implementation for date ranges ----
// ------------------------------------------
/*
humanReadableDateset = [{
from: '2020/01/01'
to: '2020/12/30'
//Date format is 'YYYY/MM/DD'
}, ...
]
kamailioDateset = [{
//SUPPORTED
year - Either given as a full 4-digit number >= 1970, or as a 2-digit number, in which case it will be understood to be within the current century.
month - Month of the year, either a number between 1 and 12, or at least the first 3 letters of a spelled out month name, e.g. jan, janua or january will all work.
mday - Day of the month, a number between 1 and 31.
//NOT SUPPORTED !!!
reversed ranges, like: "mday {15-3}"
complex ranges, like: "mday {1 2 5-8}"
Aliases: yr, mo, md, wd, hr, min, etc
wday - Day of the week, either a number between 1 and 7, or at least the first 2 letters of a spelled out weekday name (analogous to the month scale). Sunday is the first day of the week.
hour - A number between 0 and 23. Unlike the Perl Time::Period module, am or pm specifications are not supported.
minute - A number between 0 and 59.
week or wk - Week of the month, a number between 1 and 6. The first day of the week is Sunday.
yday or yd - Day of the year, a number between 1 and 366.
second or sec - A number between 0 and 60 (to allow for leap seconds).
}, ...
]
*/
export function getDateStrElements (dateStr) {
const [year, month, date] = dateStr.split('/').map(t => Number(t))
return { year, month, date }
}
export function isDateExist (year, month, date) {
const dateObj = new Date(year, month - 1, date)
return dateObj.getFullYear() === year && dateObj.getMonth() === (month - 1) && dateObj.getDate() === date
}
export function isDateStrValid (dateStr) {
if (typeof dateStr === 'string') {
const { year, month, date } = getDateStrElements(dateStr)
let isValid = year >= 1900 && year <= 2100 && month >= 1 && month <= 12 && date >= 1 && date <= 31
// recheck that passed date is really exist in that year
isValid = isValid && isDateExist(year, month, date)
return isValid
}
return false
}
export function dateStrToDays (dateStr) {
const { year, month, date } = getDateStrElements(dateStr)
const dateObj = new Date(year, month - 1, date)
const dateAsMilliseconds = dateObj.valueOf()
const millisecondsInOneDay = 24 * 60 * 60 * 1000
return Math.round(dateAsMilliseconds / millisecondsInOneDay)
}
export function validateHumanDatesets (hDateset) {
hDateset.forEach(datesetItem => {
const { from, to } = datesetItem
if (
!isDateStrValid(from) || !isDateStrValid(to)
) {
throw Error('A human dateset item has invalid format: ' + JSON.stringify(datesetItem))
} else if (dateStrToDays(from) > dateStrToDays(to)) {
throw Error('A human dateset item should have "from" date < or = "to" date: ' + JSON.stringify(datesetItem))
}
})
}
export function getHumanDatesetsNormalized (hDateset = []) {
// sort dateset by "from" columns
// clone input data to prevent original data object mutation
const hDatesetCloned = hDateset.map(i => ({ ...i }))
const hdSorted = hDatesetCloned.sort((a, b) => {
return dateStrToDays(a.from) - dateStrToDays(b.from)
})
// combine, merge periods. For example 2020/01/01-2020/10/10 and 2020/05/05-2020/11/11 should be merged into 2020/01/01-2020/11/11
// Important: the dateset should be sorted by "from"!
const hdNormalized = hdSorted.reduce((acc, currentItem) => {
const prevItem = acc.pop()
if (!prevItem) {
acc.push(currentItem)
} else {
const prevItemDays = { fromD: dateStrToDays(prevItem.from), toD: dateStrToDays(prevItem.to) }
const currentItemDays = { fromD: dateStrToDays(currentItem.from), toD: dateStrToDays(currentItem.to) }
if (prevItemDays.fromD <= currentItemDays.fromD && currentItemDays.toD <= prevItemDays.toD) {
// current date range is completely inside previous date range --> just skipping current dateSet item
acc.push(prevItem)
} else if ((prevItemDays.toD + 1) < currentItemDays.fromD) {
// current date range is not part of previous date range --> adding both as separate date ranges
acc.push(prevItem)
acc.push(currentItem)
} else if ((prevItemDays.toD + 1) >= currentItemDays.fromD) {
// current time range is the next chunk\part of the previous date range
// OR they are intercepting --> extending\combining previous date range with current date range
prevItem.to = currentItem.to
acc.push(prevItem)
} else {
console.info('Acc:', acc, 'prevItem:', prevItem, 'currentItem:', currentItem)
console.info('prevItemDays:', prevItemDays, 'currentItemDays:', currentItemDays)
throw Error('Internal error in "getHumanDatesetsNormalized"')
}
}
return acc
}, [])
return hdNormalized
}
export function getLastDayOfMonth (year, month) {
return (new Date(year, (month - 1) + 1, 0)).getDate()
}
export function humanDatesetToKamailio (hDateset = []) {
validateHumanDatesets(hDateset)
const hdNormalized = getHumanDatesetsNormalized(hDateset)
const kamailioDatesetRaw = hdNormalized.map(datesetItem => {
const { from, to } = datesetItem
const fromYMD = getDateStrElements(from)
const toYMD = getDateStrElements(to)
const result = []
if (fromYMD.year === toYMD.year) {
if (fromYMD.month === toYMD.month) {
if (fromYMD.date === toYMD.date) {
result.push({ year: fromYMD.year, month: fromYMD.month, mday: fromYMD.date })
} else {
result.push({ year: fromYMD.year, month: fromYMD.month, mday: `${fromYMD.date}-${toYMD.date}` })
}
} else {
if ((fromYMD.month + 1) === toYMD.month) {
result.push({ year: fromYMD.year, month: fromYMD.month, mday: `${fromYMD.date}-31` })
result.push({ year: fromYMD.year, month: toYMD.month, mday: `1-${toYMD.date}` })
} else {
result.push({ year: fromYMD.year, month: fromYMD.month, mday: `${fromYMD.date}-31` })
result.push({ year: fromYMD.year, month: `${fromYMD.month + 1}-${toYMD.month - 1}` })
result.push({ year: fromYMD.year, month: toYMD.month, mday: `1-${toYMD.date}` })
}
}
} else {
// NOTE: any other human readable date range can be represented as next set of Kamailio ranges
// for example "2020/02/02 - 2022/03/03" can be represented as "2020/02/02 - 2020/02/29(31)",
// "2020/03/01 - 2020/12/31", "2021/01/01 - 2021/12/31", "2022/01/01 - 2022/02/28(31)", "2022/03/01 - 2022/03/03"
// and than coded in Kamailio format
// [
// { year: fromYMD.year, month: fromYMD.month, mday: `${fromYMD.date}-31` },
// { year: fromYMD.year, month: `${fromYMD.month + 1}-12` },
// { year: `${fromYMD.year+1}-${toYMD.year-1}` },
// { year: toYMD.year, month: `1-${toYMD.month - 1}` },
// { year: toYMD.year, month: `${toYMD.month}`, mday: `1-${toYMD.date}` }
// ]
// but code below will output a little more optimized version
// "Starting (month day)" range
if (fromYMD.date > 1) {
result.push({ year: fromYMD.year, month: fromYMD.month, mday: `${fromYMD.date}-31` })
} else fromYMD.month -= 1
// "Starting (months)" range
if (fromYMD.month > 0) {
result.push({ year: fromYMD.year, month: `${fromYMD.month + 1}-12` })
} else fromYMD.year -= 1
// "Middle" range
if (fromYMD.year + 1 <= toYMD.year - 1) {
result.push({ year: `${fromYMD.year + 1}-${toYMD.year - 1}` })
}
// "Ending (months)" range
if (toYMD.month > 1) {
result.push({ year: toYMD.year, month: `1-${toYMD.month - 1}` })
}
// "Ending (month day)" range
result.push({ year: toYMD.year, month: toYMD.month, mday: `1-${toYMD.date}` })
}
return result
})
let kamailioDateset = kamailioDatesetRaw.reduce((acc, item) => {
const optimizedItemRanges = item.map(item => {
// if year or month or date contains range like "a-a" convert to just "a"
if (typeof item.year === 'string') {
const [yearF, yearT] = item.year.split('-')
if (yearF === yearT) item.year = Number(yearF)
}
if (typeof item.month === 'string') {
const [monthF, monthT] = item.month.split('-')
if (monthF === monthT) item.month = Number(monthF)
}
if (typeof item.mday === 'string') {
const [mdayF, mdayT] = item.mday.split('-')
if (mdayF === mdayT) item.mday = Number(mdayF)
}
return item
})
// similar to flatMap
return [...acc, ...optimizedItemRanges]
}, [])
kamailioDateset = kamailioDateset.reduce((acc, item) => {
// Kamailio considers the date range condition "Feb 1..29" almost equal to "Feb 1..31" so we can transform days of
// the month like 28,29,30 into 31 if it is the last day of the month. It will help to optimize ranges better
// in other optimization functions
if (item.year && item.month && item.mday) {
const yearRange = getKamailioRangeElements(item.year)[0]
const monthRange = getKamailioRangeElements(item.month)[0]
const mdayRange = getKamailioRangeElements(item.mday)[0]
if (yearRange.from === yearRange.to && monthRange.from === monthRange.to &&
mdayRange.from !== mdayRange.to && [28, 29, 30].includes(mdayRange.to)) {
const lastDayOfMonth = getLastDayOfMonth(yearRange.from, monthRange.from)
if (lastDayOfMonth === mdayRange.to) {
// replacing "mdayRange.to" with 31.
item.mday = `${mdayRange.from}-31`
}
}
}
acc.push(item)
return acc
}, [])
kamailioDateset = kamailioDateset.reduce((acc, item) => {
// a rule defined as "full range" can be omitted: a {x} b {min-max} c {y} --> a {x} c {y}
// For example: year {2020} month {1-12} mday {1-31} ---> year {2020}
if (item.month) {
const monthRange = getKamailioRangeElements(item.month)[0]
if (monthRange.from === 1 && monthRange.to === 12) {
delete item.month
}
}
if (item.mday) {
const mdayRange = getKamailioRangeElements(item.mday)[0]
if (mdayRange.from === 1 && mdayRange.to === 31) {
delete item.mday
}
}
acc.push(item)
return acc
}, [])
kamailioDateset = kamailioDateset.reduce((acc, item) => {
// combine the same date ranges by "year"
if (acc.length === 0) {
acc.push(item)
} else {
const mergeCandidate = acc.find(accItem =>
getKamailioRangeElements(accItem.year)[0].to + 1 === getKamailioRangeElements(item.year)[0].from &&
accItem.month === item.month &&
accItem.mday === item.mday
)
if (mergeCandidate) {
const mergeCandidateYear = getKamailioRangeElements(mergeCandidate.year)[0]
mergeCandidate.year = `${mergeCandidateYear.from}-${mergeCandidateYear.to + 1}`
} else {
acc.push(item)
}
}
return acc
}, [])
kamailioDateset = kamailioDateset.reduce((acc, item) => {
// combine the same date ranges by "month"
if (acc.length === 0) {
acc.push(item)
} else {
const mergeCandidate = acc.find(accItem =>
accItem.year === item.year &&
getKamailioRangeElements(accItem.month)[0].to + 1 === getKamailioRangeElements(item.month)[0].from &&
accItem.mday === item.mday
)
if (mergeCandidate) {
const mergeCandidateMonth = getKamailioRangeElements(mergeCandidate.month)[0]
mergeCandidate.month = `${mergeCandidateMonth.from}-${mergeCandidateMonth.to + 1}`
} else {
acc.push(item)
}
}
return acc
}, [])
return kamailioDateset
}
export function validateKamailioDatesets (kDateset) {
kDateset.forEach(datesetItem => {
let { year, month, mday } = datesetItem
year = getAsTrimmedString(year)
month = getAsTrimmedString(month)
mday = getAsTrimmedString(mday)
if (year !== '') {
validateKamailioRange(year, 1900, 2100)
} else {
throw Error('A Kamailio dateset should have "year" range: ' + JSON.stringify(datesetItem))
}
if (month !== '') {
validateKamailioRange(month, 1, 12)
}
if (mday !== '') {
validateKamailioRange(mday, 1, 31)
}
Object.entries(datesetItem)
.filter(([key]) => !['year', 'month', 'mday'].includes(key))
.forEach(([key, value]) => {
if (getAsTrimmedString(value).length !== 0) {
throw Error(`The "${key}" scale of Kamailio datesets is not supported: ${JSON.stringify(datesetItem)}`)
}
})
})
}
export function kamailioDatesetToHuman (kDateset = []) {
validateKamailioDatesets(kDateset)
// convert Kamailio dateset into Human readable format
const hDatesetRaw = kDateset.map(datesetItem => {
let { year, month, mday } = datesetItem
year = getAsTrimmedString(year)
month = getAsTrimmedString(month)
mday = getAsTrimmedString(mday)
year = getKamailioRangeElements(year)[0]
if (month !== '') {
month = getKamailioRangeElements(month)[0]
} else {
month = { from: 1, to: 12 }
}
if (mday !== '') {
mday = getKamailioRangeElements(mday)[0]
} else {
mday = { from: 1, to: 31 }
}
const nestedRules = [year, month, mday]
const rulesOutput = nestedRules.reduceRight((acc, range) => {
const newAcc = []
if (acc.length === 0) {
newAcc.push({ from: String(range.from), to: String(range.to) })
} else {
for (let i = range.from; i <= range.to; i++) {
acc.forEach(accItem =>
newAcc.push({ from: [i, accItem.from].join('_'), to: [i, accItem.to].join('_') })
)
}
}
return newAcc
}, [])
const hDateset = rulesOutput.reduce((acc, ruleOutput) => {
const [fromYear, fromMonth, fromDate] = ruleOutput.from.split('_').map(i => Number(i))
const [toYear, toMonth, toDate] = ruleOutput.to.split('_').map(i => Number(i))
// if "from" date of the range is not exist (like "Feb 31")
// we are considering that entire range is not valid and just SKIPPING it
if (isDateExist(fromYear, fromMonth, fromDate)) {
const from = [fromYear, fromMonth, fromDate]
.map(i => String(i).padStart(2, '0')).join('/')
// if "to" date of the range is not exist (like "Feb 31")
// we are considering that the range is valid but it was optimized for creating smaller Kamailio rule
// so we need NORMALIZE it by using last day of the month
const lastDayOfMonth = getLastDayOfMonth(toYear, toMonth)
const to = [
toYear,
toMonth,
(toDate > lastDayOfMonth) ? lastDayOfMonth : toDate
].map(i => String(i).padStart(2, '0')).join('/')
acc.push({ from, to })
}
return acc
}, [])
return hDateset
})
.reduce((acc, item) => [...acc, ...item], [])
const res = getHumanDatesetsNormalized(hDatesetRaw)
return res
}

@ -635,6 +635,7 @@
"call not from ...": "",
"callers": "Anrufer",
"condition": "",
"data error": "",
"date is": "",
"date is ...": "",
"date range is": "",
@ -643,6 +644,7 @@
"description": "Beschreibung",
"disabled": "Deaktiviert",
"do not ring own phone": "Eigene Rufnummer klingelt nicht",
"empty": "",
"enabled": "Aktiviert",
"first ring": "zuerst klingeln",
"for": "für",

@ -635,6 +635,7 @@
"call not from ...": "call not from ...",
"callers": "callers",
"condition": "condition",
"data error": "data error",
"date is": "date is",
"date is ...": "date is ...",
"date range is": "date range is",
@ -643,6 +644,7 @@
"description": "description",
"disabled": "disabled",
"do not ring own phone": "do not ring own phone",
"empty": "empty",
"enabled": "enabled",
"first ring": "first ring",
"for": "for",

@ -635,6 +635,7 @@
"call not from ...": "",
"callers": "llamantes",
"condition": "",
"data error": "",
"date is": "",
"date is ...": "",
"date range is": "",
@ -643,6 +644,7 @@
"description": "descripción",
"disabled": "desactivado",
"do not ring own phone": "no hacer sonar el teléfono propio",
"empty": "",
"enabled": "activado",
"first ring": "primero llama",
"for": "por",

@ -635,6 +635,7 @@
"call not from ...": "",
"callers": "appelants",
"condition": "",
"data error": "",
"date is": "",
"date is ...": "",
"date range is": "",
@ -643,6 +644,7 @@
"description": "",
"disabled": "désactivé",
"do not ring own phone": "ne pas faire sonner le téléphone personnel",
"empty": "",
"enabled": "activé",
"first ring": "appeler dabord",
"for": "pendant",

@ -635,6 +635,7 @@
"call not from ...": "",
"callers": "chiamanti",
"condition": "",
"data error": "",
"date is": "",
"date is ...": "",
"date range is": "",
@ -643,6 +644,7 @@
"description": "descrizione",
"disabled": "disabilitato",
"do not ring own phone": "non far squillare il proprio telefono",
"empty": "",
"enabled": "abilitato",
"first ring": "chiama prima",
"for": "per",

@ -1,373 +0,0 @@
'use strict';
import { assert } from 'chai';
import _ from 'lodash';
import {
getHumanTimesetsNormalized,
humanTimesetToKamailio,
kamailioTimesetToHuman,
} from '../../src/helpers/kamailio-timesets-converter'
function deepEqualCheckAndInputDataMutationCheck(data, expectedResult, actionFn) {
const clonnedData = _.cloneDeep(data)
assert.deepEqual(actionFn(clonnedData), expectedResult)
assert.deepEqual(clonnedData, data, 'Original data were mutated')
}
describe('function getHumanTimesetsNormalized', function() {
it('case: resort', function(){
const options = [
{ weekday: 3, from: '9:00', to: '10:00' },
{ weekday: 2, from: '11:25', to: '15:00' },
{ weekday: 2, from: '9:12', to: '9:22' },
{ weekday: 2, from: '9:00', to: '9:10' }
];
const convertedData = [
{ weekday: 2, from: '9:00', to: '9:10' },
{ weekday: 2, from: '9:12', to: '9:22' },
{ weekday: 2, from: '11:25', to: '15:00' },
{ weekday: 3, from: '9:00', to: '10:00' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => getHumanTimesetsNormalized(data))
});
it('case: combine chunks and remove duplicates', function(){
const options = [
{ weekday: 2, from: '9:00', to: '10:00' },
{ weekday: 2, from: '10:00', to: '12:00' },
{ weekday: 3, from: '11:25', to: '15:00' },
{ weekday: 3, from: '11:25', to: '15:00' },
{ weekday: 3, from: '9:00', to: '9:30' },
{ weekday: 3, from: '9:10', to: '11:30' },
{ weekday: 3, from: '10:00', to: '11:30' },
{ weekday: 3, from: '8:00', to: '8:59' }
];
const convertedData = [
{ weekday: 2, from: '9:00', to: '12:00' },
{ weekday: 3, from: '8:00', to: '8:59' },
{ weekday: 3, from: '9:00', to: '15:00' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => getHumanTimesetsNormalized(data))
});
})
describe('Human readable timesets to Kamailio format converter helpers', function() {
it('case: an empty set', function(){
const options = [];
const convertedData = []
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
const options2 = undefined;
const convertedData2 = []
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => humanTimesetToKamailio(data))
});
it('case: required field', function(){
let options = [{}];
assert.throws(() => humanTimesetToKamailio(options))
options = [{ weekday: 1 }];
assert.throws(() => humanTimesetToKamailio(options))
options = [{ weekday: 1, from: '1:00' }];
assert.throws(() => humanTimesetToKamailio(options))
options = [{ weekday: 1, to: '1:00' }];
assert.throws(() => humanTimesetToKamailio(options))
options = [{ weekday: 1, from: '1:00', to: '2:00' }];
assert.doesNotThrow(() => humanTimesetToKamailio(options))
});
it('case: Mon 6:00-6:05', function(){
let options = [
{ weekday: 2, from: '6:00', to: '6:05' }
];
let convertedData = [
{ wday: 2, hour: '6', minute: '0-4' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
it('case: Mon 14:05-15:00', function(){
let options = [
{ weekday: 2, from: '14:05', to: '15:00' }
];
let convertedData = [
{ wday: 2, hour: '14', minute: '5-59' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
it('case: Mon 6:00-15:00', function(){
let options = [
{ weekday: 2, from: '6:00', to: '15:00' }
];
let convertedData = [
{ wday: 2, hour: '6-14' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
it('case: Fri 3:02-21:41', function(){
let options = [
{ weekday: 6, from: '3:02', to: '21:41' }
];
let convertedData = [
{ wday: 6, hour: '3', minute: '2-59' },
{ wday: 6, hour: '4-20' },
{ wday: 6, hour: '21', minute: '0-40' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
it('case: Mon 9:01-10:00, Mon 14:05-15:00, Mon 11:00-11:29', function(){
let options = [
{ weekday: 2, from: '9:01', to: '10:00' },
{ weekday: 2, from: '14:05', to: '15:00' },
{ weekday: 2, from: '11:00', to: '11:29' }
];
let convertedData = [
{ wday: 2, hour: '9', minute: '1-59' },
{ wday: 2, hour: '11', minute: '0-28' },
{ wday: 2, hour: '14', minute: '5-59' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
it('case: Mon 0:00-23:59, Tue 0:00-23:58', function(){
//NOTE: 23:59 should be treated as 24:00 for TO field in interval
let options = [
{ weekday: 2, from: '0:00', to: '23:59' },
{ weekday: 3, from: '0:00', to: '23:58' }
];
let convertedData = [
{ wday: 2, hour: '0-23' },
{ wday: 3, hour: '0-22' },
{ wday: 3, hour: '23', minute: '0-57' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
it('case: automatic combining time intervals', function(){
let options = [
{ weekday: 3, from: '6:22', to: '7:44' },
{ weekday: 3, from: '5:10', to: '8:00' },
{ weekday: 3, from: '4:00', to: '7:00' },
{ weekday: 3, from: '8:30', to: '11:00' },
{ weekday: 3, from: '8:00', to: '9:30' },
{ weekday: 2, from: '2:00', to: '20:00' },
];
let convertedData = [
{ wday: 2, hour: '2-19' },
{ wday: 3, hour: '4-10' },
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
it('case: wday range', function(){
let options = [
{ weekday: 6, from: '8:00', to: '17:00' },
{ weekday: 7, from: '8:00', to: '17:00' },
{ weekday: 1, from: '8:00', to: '17:00' },
{ weekday: 2, from: '8:00', to: '17:30' },
{ weekday: 3, from: '8:00', to: '17:30' },
{ weekday: 4, from: '8:00', to: '17:30' }
];
let convertedData = [
{ wday: '1-4', hour: '8-16' },
{ wday: '2-4', hour: '17', minute: '0-29' },
{ wday: '6-7', hour: '8-16' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
});
describe('Kamailio format to Human readable timesets converter helpers', function() {
it('case: an empty set', function(){
const options = [];
const convertedData = []
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
const options2 = undefined;
const convertedData2 = []
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => kamailioTimesetToHuman(data))
});
it('case: required field', function(){
const options = [{}];
assert.throws(() => kamailioTimesetToHuman(options))
const options2 = [{ wday: 1 }];
assert.doesNotThrow(() => kamailioTimesetToHuman(options2))
});
it('case: not supported fields', function(){
//There is not exception if we have empty not supported fields
let options = [
{ wday: 1, hour: '1', minute: '1',
second: '', month: '', year: null, week: '', yday: '', mday: '' }
];
assert.doesNotThrow(() => kamailioTimesetToHuman(options))
//There should be an exception if we have a not empty not supported field
let options2 = [
{ weekday: 1, from: '1:00', to: '2:00',
second: '1', month: '1'}
];
assert.throws(() => kamailioTimesetToHuman(options2))
});
it('case: Mon 6:00-6:05', function(){
let options = [
{ wday: 2, hour: '6', minute: '0-4' }
];
let convertedData = [
{ weekday: 2, from: '6:00', to: '6:05' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
it('case: Mon 14:05-15:00', function(){
let options = [
{ wday: 2, hour: '14', minute: '5-59' }
];
let convertedData = [
{ weekday: 2, from: '14:05', to: '15:00' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
it('case: Mon 6:00-15:00', function(){
let options = [
{ wday: 2, hour: '6-14' }
];
let convertedData = [
{ weekday: 2, from: '6:00', to: '15:00' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
it('case: Fri 3:02-21:41', function(){
let options = [
{ wday: 6, hour: '3', minute: '2-59' },
{ wday: 6, hour: '4-20' },
{ wday: 6, hour: '21', minute: '0-40' }
];
let convertedData = [
{ weekday: 6, from: '3:02', to: '21:41' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
it('case: Mon Mon 11:00-11:29, 9:01-10:00, Mon 14:05-15:00', function(){
let options = [
{ wday: 2, hour: '11', minute: '0-28' },
{ wday: 2, hour: '9', minute: '1-59' },
{ wday: 2, hour: '14', minute: '5-59' }
];
let convertedData = [
{ weekday: 2, from: '9:01', to: '10:00' },
{ weekday: 2, from: '11:00', to: '11:29' },
{ weekday: 2, from: '14:05', to: '15:00' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
it('case: Mon 0:00-23:59, Tue 0:00-23:58', function(){
//NOTE: 23:59 should be treated as 24:00 for TO field in interval
let options = [
{ wday: 2, hour: '0-23' },
{ wday: 3, hour: '0-22' },
{ wday: 3, hour: '23', minute: '0-57' }
];
let convertedData = [
{ weekday: 2, from: '0:00', to: '23:59' },
{ weekday: 3, from: '0:00', to: '23:58' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
it('case: automatic combining time intervals', function(){
let options = [
{ wday: 7, hour: '9-13' },
{ wday: 2, hour: '10-13' },
{ wday: 2, hour: '9', minute: '0-29' },
{ wday: 2, hour: '9', minute: '30-59' },
{ wday: 2, hour: '11-12', minute: '5-20' }
];
let convertedData = [
{ weekday: 2, from: '9:00', to: '14:00' },
{ weekday: 7, from: '9:00', to: '14:00' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
it('case: wday range', function(){
let options = [
{ wday: '1-4', hour: '8-16' },
{ wday: '2-4', hour: '17', minute: '0-29' },
{ wday: '6-7', hour: '8-16' }
];
let convertedData = [
{ weekday: 1, from: '8:00', to: '17:00' },
{ weekday: 2, from: '8:00', to: '17:30' },
{ weekday: 3, from: '8:00', to: '17:30' },
{ weekday: 4, from: '8:00', to: '17:30' },
{ weekday: 6, from: '8:00', to: '17:00' },
{ weekday: 7, from: '8:00', to: '17:00' }
];
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
});
})

@ -4,11 +4,13 @@ import { assert } from 'chai'
import _ from 'lodash'
import {
getHumanTimesetsNormalized,
humanDatesetToKamailio,
humanTimesetToKamailio,
kamailioDatesetToHuman,
kamailioTimesetToHuman
} from 'src/helpers/kamailio-timesets-converter'
function deepEqualCheckAndInputDataMutationCheck(data, expectedResult, actionFn) {
function deepEqualCheckAndInputDataMutationCheck (data, expectedResult, actionFn) {
const clonnedData = _.cloneDeep(data)
assert.deepEqual(actionFn(clonnedData), expectedResult)
assert.deepEqual(clonnedData, data, 'Original data were mutated')
@ -30,8 +32,8 @@ describe('function getHumanTimesetsNormalized', function () {
{ weekday: 2, from: '11:25', to: '15:00' },
{ weekday: 3, from: '9:00', to: '10:00' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => getHumanTimesetsNormalized(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => getHumanTimesetsNormalized(data))
})
it('case: combine chunks and remove duplicates', function () {
@ -50,8 +52,8 @@ describe('function getHumanTimesetsNormalized', function () {
{ weekday: 3, from: '8:00', to: '8:59' },
{ weekday: 3, from: '9:00', to: '15:00' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => getHumanTimesetsNormalized(data))
});
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => getHumanTimesetsNormalized(data))
})
})
describe('Human readable timesets to Kamailio format converter helpers', function () {
@ -60,12 +62,12 @@ describe('Human readable timesets to Kamailio format converter helpers', functio
const options = []
const convertedData = []
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
const options2 = undefined
const convertedData2 = []
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => humanTimesetToKamailio(data))
});
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => humanTimesetToKamailio(data))
})
it('case: required field', function () {
@ -83,31 +85,31 @@ describe('Human readable timesets to Kamailio format converter helpers', functio
options = [{ weekday: 1, from: '1:00', to: '2:00' }]
assert.doesNotThrow(() => humanTimesetToKamailio(options))
});
})
it('case: Mon 6:00-6:05', function(){
it('case: Mon 6:00-6:05', function () {
let options = [
{ weekday: 2, from: '6:00', to: '6:05' }
]
let convertedData = [
{ wday: 2, hour: '6', minute: '0-4' }
]
const options = [
{ weekday: 2, from: '6:00', to: '6:05' }
]
const convertedData = [
{ wday: 2, hour: '6', minute: '0-4' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: Mon 14:05-15:00', function(){
it('case: Mon 14:05-15:00', function () {
let options = [
{ weekday: 2, from: '14:05', to: '15:00' }
]
let convertedData = [
{ wday: 2, hour: '14', minute: '5-59' }
]
const options = [
{ weekday: 2, from: '14:05', to: '15:00' }
]
const convertedData = [
{ wday: 2, hour: '14', minute: '5-59' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: Mon 6:00-15:00', function () {
@ -116,24 +118,24 @@ describe('Human readable timesets to Kamailio format converter helpers', functio
]
const convertedData = [
{ wday: 2, hour: '6-14' }
];
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
});
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: Fri 3:02-21:41', function(){
it('case: Fri 3:02-21:41', function () {
let options = [
{ weekday: 6, from: '3:02', to: '21:41' }
];
let convertedData = [
{ wday: 6, hour: '3', minute: '2-59' },
{ wday: 6, hour: '4-20' },
{ wday: 6, hour: '21', minute: '0-40' }
];
const options = [
{ weekday: 6, from: '3:02', to: '21:41' }
]
const convertedData = [
{ wday: 6, hour: '3', minute: '2-59' },
{ wday: 6, hour: '4-20' },
{ wday: 6, hour: '21', minute: '0-40' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: Mon 9:01-10:00, Mon 14:05-15:00, Mon 11:00-11:29', function () {
@ -148,8 +150,8 @@ describe('Human readable timesets to Kamailio format converter helpers', functio
{ wday: 2, hour: '14', minute: '5-59' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: Mon 0:00-23:59, Tue 0:00-23:58', function () {
@ -164,8 +166,8 @@ describe('Human readable timesets to Kamailio format converter helpers', functio
{ wday: 3, hour: '23', minute: '0-57' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: automatic combining time intervals', function () {
@ -183,8 +185,8 @@ describe('Human readable timesets to Kamailio format converter helpers', functio
{ wday: 3, hour: '4-10' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: wday range', function () {
@ -204,9 +206,30 @@ describe('Human readable timesets to Kamailio format converter helpers', functio
{ wday: '6-7', hour: '8-16' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
it('case: hour range', function () {
const options = [
{ weekday: 2, from: '9:05', to: '9:30' },
{ weekday: 2, from: '10:05', to: '10:30' },
{ weekday: 2, from: '11:05', to: '11:30' },
{ weekday: 3, from: '9:05', to: '9:30' },
{ weekday: 3, from: '10:05', to: '10:30' },
{ weekday: 3, from: '11:05', to: '11:30' },
{ weekday: 4, from: '9:05', to: '9:30' },
{ weekday: 4, from: '10:05', to: '10:30' },
{ weekday: 4, from: '11:05', to: '11:30' }
]
const convertedData = [
{ wday: '2-4', hour: '9-11', minute: '5-29' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanTimesetToKamailio(data))
})
})
describe('Kamailio format to Human readable timesets converter helpers', function () {
@ -215,12 +238,12 @@ describe('Kamailio format to Human readable timesets converter helpers', functio
const options = []
const convertedData = []
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
const options2 = undefined;
const options2 = undefined
const convertedData2 = []
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => kamailioTimesetToHuman(data))
})
it('case: required field', function () {
@ -252,31 +275,31 @@ describe('Kamailio format to Human readable timesets converter helpers', functio
]
assert.throws(() => kamailioTimesetToHuman(options2))
});
})
it('case: Mon 6:00-6:05', function(){
it('case: Mon 6:00-6:05', function () {
let options = [
{ wday: 2, hour: '6', minute: '0-4' }
]
let convertedData = [
{ weekday: 2, from: '6:00', to: '6:05' }
]
const options = [
{ wday: 2, hour: '6', minute: '0-4' }
]
const convertedData = [
{ weekday: 2, from: '6:00', to: '6:05' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: Mon 14:05-15:00', function(){
it('case: Mon 14:05-15:00', function () {
let options = [
{ wday: 2, hour: '14', minute: '5-59' }
]
let convertedData = [
{ weekday: 2, from: '14:05', to: '15:00' }
]
const options = [
{ wday: 2, hour: '14', minute: '5-59' }
]
const convertedData = [
{ weekday: 2, from: '14:05', to: '15:00' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: Mon 6:00-15:00', function () {
const options = [
@ -284,24 +307,24 @@ describe('Kamailio format to Human readable timesets converter helpers', functio
]
const convertedData = [
{ weekday: 2, from: '6:00', to: '15:00' }
];
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: Fri 3:02-21:41', function(){
it('case: Fri 3:02-21:41', function () {
let options = [
{ wday: 6, hour: '3', minute: '2-59' },
{ wday: 6, hour: '4-20' },
{ wday: 6, hour: '21', minute: '0-40' }
]
let convertedData = [
{ weekday: 6, from: '3:02', to: '21:41' }
]
const options = [
{ wday: 6, hour: '3', minute: '2-59' },
{ wday: 6, hour: '4-20' },
{ wday: 6, hour: '21', minute: '0-40' }
]
const convertedData = [
{ weekday: 6, from: '3:02', to: '21:41' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: Mon Mon 11:00-11:29, 9:01-10:00, Mon 14:05-15:00', function () {
@ -314,10 +337,10 @@ describe('Kamailio format to Human readable timesets converter helpers', functio
{ weekday: 2, from: '9:01', to: '10:00' },
{ weekday: 2, from: '11:00', to: '11:29' },
{ weekday: 2, from: '14:05', to: '15:00' }
]
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: Mon 0:00-23:59, Tue 0:00-23:58', function () {
@ -333,8 +356,8 @@ describe('Kamailio format to Human readable timesets converter helpers', functio
{ weekday: 3, from: '0:00', to: '23:58' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: automatic combining time intervals', function () {
@ -351,8 +374,8 @@ describe('Kamailio format to Human readable timesets converter helpers', functio
{ weekday: 7, from: '9:00', to: '14:00' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: wday range', function () {
@ -372,6 +395,573 @@ describe('Kamailio format to Human readable timesets converter helpers', functio
{ weekday: 7, from: '8:00', to: '17:00' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
it('case: hour range', function () {
const options = [
{ wday: '2-4', hour: '9-11', minute: '5-29' }
]
const convertedData = [
{ weekday: 2, from: '9:05', to: '9:30' },
{ weekday: 2, from: '10:05', to: '10:30' },
{ weekday: 2, from: '11:05', to: '11:30' },
{ weekday: 3, from: '9:05', to: '9:30' },
{ weekday: 3, from: '10:05', to: '10:30' },
{ weekday: 3, from: '11:05', to: '11:30' },
{ weekday: 4, from: '9:05', to: '9:30' },
{ weekday: 4, from: '10:05', to: '10:30' },
{ weekday: 4, from: '11:05', to: '11:30' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioTimesetToHuman(data))
})
})
// ---- implementation for date ranges ----
// ------------------------------------------
describe('Human readable datesets to Kamailio format converter helpers', function () {
it('case: an empty set', function () {
const options = []
const convertedData = []
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
const options2 = undefined
const convertedData2 = []
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => humanDatesetToKamailio(data))
})
it('case: required field have correct format', function () {
let options = [{}]
assert.throws(() => humanDatesetToKamailio(options))
options = [{ from: '', to: '' }]
assert.throws(() => humanDatesetToKamailio(options))
options = [{ from: '2020/01/01' }]
assert.throws(() => humanDatesetToKamailio(options))
options = [{ to: '2020/01/01' }]
assert.throws(() => humanDatesetToKamailio(options))
options = [{ from: '1:00', to: '2:00' }]
assert.throws(() => humanDatesetToKamailio(options))
options = [{ from: '2020/01/00', to: '2020/01/33' }]
assert.throws(() => humanDatesetToKamailio(options))
options = [{ from: '2020/01/01', to: '2020/01/02' }]
assert.doesNotThrow(() => humanDatesetToKamailio(options))
})
it('case: "from" date should be equal or less than "to" date', function () {
let options = [{ from: '2020/01/02', to: '2020/01/01' }]
assert.throws(() => humanDatesetToKamailio(options))
options = [{ from: '2020/01/01', to: '2020/01/01' }]
assert.doesNotThrow(() => humanDatesetToKamailio(options))
options = [{ from: '2020/01/01', to: '2020/01/23' }]
assert.doesNotThrow(() => humanDatesetToKamailio(options))
})
it('case: 2020/05/28', function () {
const options = [
{ from: '2020/05/28', to: '2020/05/28' }
]
const convertedData = [
{ year: 2020, month: 5, mday: 28 }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/04/05 and 2021/02/28', function () {
const options = [
{ from: '2021/02/28', to: '2021/02/28' },
{ from: '2020/04/05', to: '2020/04/05' }
]
const convertedData = [
{ year: 2020, month: 4, mday: 5 },
{ year: 2021, month: 2, mday: 28 }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: no leading zeroes 2020/1/2', function () {
const options = [
{ from: '2020/1/2', to: '2020/1/2' }
]
const convertedData = [
{ year: 2020, month: 1, mday: 2 }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/03/01-2020/03/31', function () {
const options = [
{ from: '2020/03/01', to: '2020/03/31' }
]
const convertedData = [
{ year: 2020, month: 3 }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/02/02-2020/02/11', function () {
const options = [
{ from: '2020/02/02', to: '2020/02/11' }
]
const convertedData = [
{ year: 2020, month: 2, mday: '2-11' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/01-2020/02/29', function () {
const options = [
{ from: '2020/01/01', to: '2020/02/29' }
]
const convertedData = [
{ year: 2020, month: '1-2' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/02-2020/02/15', function () {
const options = [
{ from: '2020/01/02', to: '2020/02/15' }
]
const convertedData = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: 2, mday: '1-15' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/01-2020/03/31', function () {
const options = [
{ from: '2020/01/01', to: '2020/03/31' }
]
const convertedData = [
{ year: 2020, month: '1-3' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/02-2020/03/31', function () {
const options = [
{ from: '2020/01/02', to: '2020/03/31' }
]
const convertedData = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-3' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/02-2020/03/23', function () {
const options = [
{ from: '2020/01/02', to: '2020/03/23' }
]
const convertedData = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: 2 },
{ year: 2020, month: 3, mday: '1-23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/02-2021/03/23', function () {
const options = [
{ from: '2020/01/02', to: '2021/03/23' }
]
const convertedData = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-12' },
{ year: 2021, month: '1-2' },
{ year: 2021, month: 3, mday: '1-23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/02-2022/03/23', function () {
const options = [
{ from: '2020/01/02', to: '2022/03/23' }
]
const convertedData = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-12' },
{ year: 2021 },
{ year: 2022, month: '1-2' },
{ year: 2022, month: 3, mday: '1-23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: merge 2020/01/02-2022/03/23, 2021/01/02-2023/03/23', function () {
const options = [
{ from: '2021/01/02', to: '2023/03/23' },
{ from: '2020/01/02', to: '2022/03/23' }
]
const convertedData = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-12' },
{ year: '2021-2022' },
{ year: 2023, month: '1-2' },
{ year: 2023, month: 3, mday: '1-23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: merge 2020/01/02-2021/02/28, 2021/03/01-2023/03/23', function () {
const options = [
{ from: '2021/03/01', to: '2023/03/23' },
{ from: '2020/01/02', to: '2021/02/28' }
]
const convertedData = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-12' },
{ year: '2021-2022' },
{ year: 2023, month: '1-2' },
{ year: 2023, month: 3, mday: '1-23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
it('case: 2020/01/02-2020/02/28 and 2021/01/02-2021/02/28 and 2022/01/02-2022/02/28', function () {
const options = [
{ from: '2021/01/02', to: '2021/02/28' },
{ from: '2020/01/02', to: '2020/02/28' },
{ from: '2022/01/02', to: '2022/02/28' }
]
const convertedData = [
{ year: '2020-2022', month: 1, mday: '2-31' },
{ year: 2020, month: 2, mday: '1-28' },
{ year: '2021-2022', month: 2 }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => humanDatesetToKamailio(data))
})
})
describe('Kamailio format to Human readable datesets converter helpers', function () {
it('case: an empty set', function () {
const options = []
const convertedData = []
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
const options2 = undefined
const convertedData2 = []
deepEqualCheckAndInputDataMutationCheck(options2, convertedData2, data => kamailioDatesetToHuman(data))
})
it('case: required field have correct format', function () {
let options = [{}]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ year: '', month: '', mday: '' }]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ month: '1', mday: '2' }]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ year: 2020, month: 2, mday: 3 }]
assert.doesNotThrow(() => kamailioDatesetToHuman(options))
options = [{ year: '2020-2023', month: '3-10', mday: '5-15' }]
assert.doesNotThrow(() => kamailioDatesetToHuman(options))
})
it('case: allowed values for ranges', function () {
let options = [{ year: '2020', month: '0' }]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ year: '2020', month: '13' }]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ year: '2020', month: '1', mday: '0' }]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ year: '2020', month: '1', mday: '32' }]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ year: '2021', month: '2', mday: '1-31' }]
assert.doesNotThrow(() => kamailioDatesetToHuman(options))
})
it('case: ranges with not existing dates', function () {
// Note: we are considering that even a Kamailio date range contains some rules withs not existing dates
// it's ok and it is might be some part of Kamailio format optimizations for smaller ruleset.
// So we just skipping not existing dates in output or normalizing
let options = [
{ year: '2020', month: '2', mday: '31' }
]
let convertedData = []
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
options = [
{ year: '2020', month: '1-2', mday: '31' }
]
convertedData = [
{ from: '2020/01/31', to: '2020/01/31' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
options = [
{ year: '2021', month: '2', mday: '1-31' }
]
convertedData = [
{ from: '2021/02/01', to: '2021/02/28' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
options = [
{ year: '2020-2021', month: '2', mday: '29-31' }
]
convertedData = [
{ from: '2020/02/29', to: '2020/02/29' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: reverse ranges', function () {
let options = [{ year: '2021', month: '10-2' }]
assert.throws(() => kamailioDatesetToHuman(options))
options = [{ year: '2021', mday: '30-1' }]
assert.throws(() => kamailioDatesetToHuman(options))
})
it('case: 2020/05/28', function () {
const options = [
{ year: 2020, month: 5, mday: 28 }
]
const convertedData = [
{ from: '2020/05/28', to: '2020/05/28' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/04/05 and 2021/02/28', function () {
const options = [
{ year: 2021, month: 2, mday: 28 },
{ year: 2020, month: 4, mday: 5 }
]
const convertedData = [
{ from: '2020/04/05', to: '2020/04/05' },
{ from: '2021/02/28', to: '2021/02/28' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: leading zeroes in string 2020/01/02', function () {
const options = [
{ year: '2020', month: '01', mday: '02' }
]
const convertedData = [
{ from: '2020/01/02', to: '2020/01/02' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/03/01-2020/03/31', function () {
const options = [
{ year: 2020, month: 3 }
]
const convertedData = [
{ from: '2020/03/01', to: '2020/03/31' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/02/02-2020/02/11', function () {
const options = [
{ year: 2020, month: 2, mday: '2-11' }
]
const convertedData = [
{ from: '2020/02/02', to: '2020/02/11' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/01-2020/02/29', function () {
const options = [
{ year: 2020, month: '1-2' }
]
const convertedData = [
{ from: '2020/01/01', to: '2020/02/29' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/02-2020/02/15', function () {
const options = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: 2, mday: '1-15' }
]
const convertedData = [
{ from: '2020/01/02', to: '2020/02/15' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/01-2020/03/31', function () {
const options = [
{ year: 2020, month: '1-3' }
]
const convertedData = [
{ from: '2020/01/01', to: '2020/03/31' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/02-2020/03/31', function () {
const options = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-3' }
]
const convertedData = [
{ from: '2020/01/02', to: '2020/03/31' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/02-2020/03/23', function () {
const options = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: 2 },
{ year: 2020, month: 3, mday: '1-23' }
]
const convertedData = [
{ from: '2020/01/02', to: '2020/03/23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/02-2021/03/23', function () {
const options = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-12' },
{ year: 2021, month: '1-2' },
{ year: 2021, month: 3, mday: '1-23' }
]
const convertedData = [
{ from: '2020/01/02', to: '2021/03/23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/02-2022/03/23', function () {
const options = [
{ year: 2020, month: 1, mday: '2-31' },
{ year: 2020, month: '2-12' },
{ year: 2021 },
{ year: 2022, month: '1-2' },
{ year: 2022, month: 3, mday: '1-23' }
]
const convertedData = [
{ from: '2020/01/02', to: '2022/03/23' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: merge 2020/01/01-2020/01/15 and 2020/01/05-2020/01/31', function () {
const options = [
{ year: 2020, month: 1, mday: '5-31' },
{ year: 2020, month: 1, mday: '5-31' },
{ year: 2020, month: 1, mday: '1-15' },
{ year: 2020, month: 1, mday: '1-15' }
]
const convertedData = [
{ from: '2020/01/01', to: '2020/01/31' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
it('case: 2020/01/02-2020/02/28 and 2021/01/02-2021/02/28 and 2022/01/02-2022/02/28', function () {
const options = [
{ year: '2021-2022', month: 2 },
{ year: 2020, month: 2, mday: '1-28' },
{ year: '2020-2022', month: 1, mday: '2-31' }
]
const convertedData = [
{ from: '2020/01/02', to: '2020/02/28' },
{ from: '2021/01/02', to: '2021/02/28' },
{ from: '2022/01/02', to: '2022/02/28' }
]
deepEqualCheckAndInputDataMutationCheck(options, convertedData, data => kamailioDatesetToHuman(data))
})
})

Loading…
Cancel
Save