Skip to content

Commit

Permalink
[VigilanceAreas] fix period filter
Browse files Browse the repository at this point in the history
  • Loading branch information
claire2212 committed Jan 17, 2025
1 parent f276e67 commit b00b1b4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { customDayjs } from '@mtes-mct/monitor-ui'
import { getFilterVigilanceAreasPerPeriod } from './getFilteredVigilanceAreasPerPeriod'

describe('filterVigilanceAreas', () => {
const todayMin2Days = {
computedEndDate: undefined,
createdAt: undefined,
endDatePeriod: `${customDayjs().subtract(11, 'day').format('YYYY-MM-DD')} 23:59:59.99999`,
endingCondition: VigilanceArea.EndingCondition.NEVER,
endingOccurrenceDate: undefined,
endingOccurrencesNumber: undefined,
frequency: VigilanceArea.Frequency.ALL_WEEKS,
id: 1,
isArchived: false,
isAtAllTimes: false,
isDraft: false,
name: 'todayMin2Days',
seaFront: 'MED',
startDatePeriod: `${customDayjs().subtract(12, 'day').format('YYYY-MM-DD')} 00:00:00.00000`,
updatedAt: undefined
}
const today = {
computedEndDate: `${customDayjs().add(1, 'year').format('YYYY-MM-DD')} 23:59:59.99999`,
createdAt: undefined,
Expand All @@ -13,7 +30,7 @@ describe('filterVigilanceAreas', () => {
endingOccurrenceDate: `${customDayjs().add(1, 'year').format('YYYY-MM-DD')} 23:59:59.00000`,
endingOccurrencesNumber: undefined,
frequency: VigilanceArea.Frequency.ALL_MONTHS,
id: 1,
id: 2,
isArchived: false,
isAtAllTimes: false,
isDraft: false,
Expand All @@ -23,14 +40,14 @@ describe('filterVigilanceAreas', () => {
updatedAt: undefined
}
const quarter = {
computedEndDate: `${customDayjs().startOf('quarter').add(3, 'weeks').format('YYYY-MM-DD')} 23:59:59.99999`,
computedEndDate: `${customDayjs().endOf('quarter').add(3, 'weeks').format('YYYY-MM-DD')} 23:59:59.99999`,
createdAt: undefined,
endDatePeriod: `${customDayjs().endOf('quarter').format('YYYY-MM-DD')} 00:00:00.00000`,
endDatePeriod: `${customDayjs().endOf('quarter').format('YYYY-MM-DD')} 23:59:59.99999`,
endingCondition: VigilanceArea.EndingCondition.OCCURENCES_NUMBER,
endingOccurrenceDate: undefined,
endingOccurrencesNumber: 3,
frequency: VigilanceArea.Frequency.ALL_WEEKS,
id: 2,
id: 3,
isArchived: false,
isAtAllTimes: false,
isDraft: false,
Expand Down Expand Up @@ -68,7 +85,7 @@ describe('filterVigilanceAreas', () => {
endingOccurrenceDate: undefined,
endingOccurrencesNumber: undefined,
frequency: VigilanceArea.Frequency.ALL_YEARS,
id: 3,
id: 5,
isArchived: false,
isAtAllTimes: false,
isDraft: false,
Expand All @@ -77,6 +94,23 @@ describe('filterVigilanceAreas', () => {
startDatePeriod: `${customDayjs().add(3, 'days').format('YYYY-MM-DD')} 00:00:00.00000`,
updatedAt: undefined
}
const allYear = {
computedEndDate: undefined,
createdAt: undefined,
endDatePeriod: `${customDayjs('12/31/2024').format('YYYY-MM-DD')} 23:59:59.99999`,
endingCondition: VigilanceArea.EndingCondition.NEVER,
endingOccurrenceDate: undefined,
endingOccurrencesNumber: undefined,
frequency: VigilanceArea.Frequency.ALL_YEARS,
id: 6,
isArchived: false,
isAtAllTimes: false,
isDraft: false,
name: 'allYear',
seaFront: 'MED',
startDatePeriod: `${customDayjs('01/01/2024').format('YYYY-MM-DD')} 00:00:00.00000`,
updatedAt: undefined
}

const infinite = {
computedEndDate: undefined,
Expand All @@ -86,7 +120,7 @@ describe('filterVigilanceAreas', () => {
endingOccurrenceDate: undefined,
endingOccurrencesNumber: undefined,
frequency: undefined,
id: 5,
id: 7,
isArchived: false,
isAtAllTimes: true,
isDraft: false,
Expand All @@ -96,26 +130,26 @@ describe('filterVigilanceAreas', () => {
updatedAt: undefined
}

const areas = [today, quarter, year, outsideFilteredDate, infinite]
const areas = [todayMin2Days, today, quarter, year, allYear, outsideFilteredDate, infinite]

it('filters areas for today', () => {
const result = getFilterVigilanceAreasPerPeriod(areas, VigilanceArea.VigilanceAreaFilterPeriod.AT_THE_MOMENT)
expect(result).toEqual([today, quarter, infinite])
expect(result).toEqual([today, quarter, allYear, infinite])
})

it('filters areas within current quarter', () => {
const result = getFilterVigilanceAreasPerPeriod(areas, VigilanceArea.VigilanceAreaFilterPeriod.CURRENT_QUARTER)
expect(result).toEqual([today, quarter, year, infinite])
expect(result).toEqual([todayMin2Days, today, quarter, year, allYear, infinite])
})

it('filters areas within current year', () => {
const result = getFilterVigilanceAreasPerPeriod(areas, VigilanceArea.VigilanceAreaFilterPeriod.CURRENT_YEAR)
expect(result).toEqual([today, quarter, year, infinite])
expect(result).toEqual([todayMin2Days, today, quarter, year, allYear, infinite])
})

it('filters areas within next three months', () => {
const result = getFilterVigilanceAreasPerPeriod(areas, VigilanceArea.VigilanceAreaFilterPeriod.NEXT_THREE_MONTHS)
expect(result).toEqual([today, quarter, year, infinite])
expect(result).toEqual([todayMin2Days, today, quarter, year, allYear, infinite])
})
it('filters areas with vigilance area one complete year', () => {
const vigilanceAreaOneCompleteYear = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import type { Dayjs } from 'dayjs'

customDayjs.extend(isBetween)

function isWithinPeriod(date: Dayjs, startDateFilter: Dayjs, endDateFilter: Dayjs) {
function isWithinPeriod(endDate: Dayjs, startDate: Dayjs, startDateFilter: Dayjs, endDateFilter: Dayjs) {
return (
(date.isAfter(startDateFilter) && date.isBefore(endDateFilter)) ||
date.isSame(startDateFilter) ||
date.isSame(endDateFilter)
endDate.isBetween(startDateFilter, endDateFilter) ||
startDate.isBetween(startDateFilter, endDateFilter) ||
startDateFilter.isBetween(startDate, endDate) ||
endDateFilter.isBetween(startDate, endDate) ||
endDate.isSame(startDateFilter) ||
endDate.isSame(endDateFilter) ||
startDate.isSame(startDateFilter) ||
startDate.isSame(endDateFilter)
)
}

Expand Down Expand Up @@ -48,44 +53,37 @@ function isMatchForSingleOccurrence(
startDateFilter: Dayjs,
endDateFilter: Dayjs
): boolean {
return (
isWithinPeriod(startDate, startDateFilter, endDateFilter) ||
isWithinPeriod(endDate, startDateFilter, endDateFilter) ||
startDateFilter.isBetween(startDate, endDate) ||
endDateFilter.isBetween(startDate, endDate)
)
return isWithinPeriod(endDate, startDate, startDateFilter, endDateFilter)
}

function isMatchForRecurringOccurrence(
startDate: Dayjs,
computedEndDate: Dayjs,
endDate: Dayjs,
startDateFilter: Dayjs,
endDateFilter: Dayjs,
frequency: VigilanceArea.Frequency
frequency: VigilanceArea.Frequency,
loopStopDate: Dayjs
): boolean {
let occurrenceDate = startDate
let occurrenceStartDate = startDate
let occurrenceEndDate = endDate

while (occurrenceDate.isBefore(computedEndDate, 'day') || occurrenceDate.isSame(computedEndDate, 'day')) {
if (isWithinPeriod(occurrenceDate, startDateFilter, endDateFilter)) {
return true
}

if (
startDateFilter.isBetween(startDate, occurrenceDate) ||
endDateFilter.isBetween(computedEndDate, occurrenceDate)
) {
while (occurrenceEndDate.isBefore(loopStopDate, 'day') || occurrenceEndDate.isSame(loopStopDate, 'day')) {
if (isWithinPeriod(occurrenceEndDate, occurrenceStartDate, startDateFilter, endDateFilter)) {
return true
}

switch (frequency) {
case VigilanceArea.Frequency.ALL_WEEKS:
occurrenceDate = occurrenceDate.add(7, 'day')
occurrenceStartDate = occurrenceStartDate.add(7, 'day')
occurrenceEndDate = occurrenceEndDate.add(7, 'day')
break
case VigilanceArea.Frequency.ALL_MONTHS:
occurrenceDate = occurrenceDate.add(1, 'month')
occurrenceStartDate = occurrenceStartDate.add(1, 'month')
occurrenceEndDate = occurrenceEndDate.add(1, 'month')
break
case VigilanceArea.Frequency.ALL_YEARS:
occurrenceDate = occurrenceDate.add(1, 'year')
occurrenceStartDate = occurrenceStartDate.add(1, 'year')
occurrenceEndDate = occurrenceEndDate.add(1, 'year')
break
default:
return false
Expand Down Expand Up @@ -122,9 +120,9 @@ export const getFilterVigilanceAreasPerPeriod = (
const endDate = customDayjs(vigilanceArea.endDatePeriod).utc()

// in case there is no end of recurrence (because endingCondition is NEVER) we set a default end date to the end of the period filter
const computedEndDate = vigilanceArea.computedEndDate
const loopStopDate = vigilanceArea.computedEndDate
? customDayjs(vigilanceArea.computedEndDate)
: endDateFilter.add(1, 'year')
: customDayjs(endDate).add(5, 'year')

if (vigilanceArea.frequency === VigilanceArea.Frequency.NONE) {
return isMatchForSingleOccurrence(startDate, endDate, startDateFilter, endDateFilter)
Expand All @@ -141,10 +139,11 @@ export const getFilterVigilanceAreasPerPeriod = (
if (vigilanceArea.frequency) {
return isMatchForRecurringOccurrence(
startDate,
computedEndDate,
endDate,
startDateFilter,
endDateFilter,
vigilanceArea.frequency
vigilanceArea.frequency,
loopStopDate
)
}

Expand Down

0 comments on commit b00b1b4

Please sign in to comment.