test and fix some recurrent events

This commit is contained in:
lesion
2023-06-18 21:56:48 +02:00
parent acc1d1d4d0
commit 892f560634
3 changed files with 47 additions and 7 deletions

View File

@@ -690,7 +690,7 @@ const eventController = {
const parentStartDatetime = DateTime.fromSeconds(e.start_datetime) const parentStartDatetime = DateTime.fromSeconds(e.start_datetime)
// cursor is when start to count // cursor is when start to count
// sets it to // in case parent is in past, start to calculate from now
let cursor = parentStartDatetime > startAt ? parentStartDatetime : startAt let cursor = parentStartDatetime > startAt ? parentStartDatetime : startAt
startAt = cursor startAt = cursor
@@ -711,6 +711,8 @@ const eventController = {
cursor = cursor.plus({ days: 7 * Number(frequency[0]) }) cursor = cursor.plus({ days: 7 * Number(frequency[0]) })
} }
} else if (frequency === '1m') { } else if (frequency === '1m') {
// day n.X each month
if (type === 'ordinal') { if (type === 'ordinal') {
cursor = cursor.set({ day: parentStartDatetime.day }) cursor = cursor.set({ day: parentStartDatetime.day })
@@ -718,10 +720,10 @@ const eventController = {
cursor = cursor.plus({ months: 1 }) cursor = cursor.plus({ months: 1 })
} }
} else { // weekday } else { // weekday
// get weekday
// get recurrent freq details // get recurrent freq details
cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday) cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday)
if (cursor< startAt) { if (cursor < startAt) {
cursor = cursor.plus({ months: 1 }) cursor = cursor.plus({ months: 1 })
cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday) cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday)
} }

View File

@@ -259,7 +259,7 @@ module.exports = {
} else { } else {
cursor = date.startOf('month') cursor = date.startOf('month')
// cursor = cursor.add(cursor.day <= date.day ? n - 1 : n, 'week') // cursor = cursor.add(cursor.day <= date.day ? n - 1 : n, 'week')
cursor = cursor.plus({ days: cursor.weekday <= date.weekday ? (n-1) * 7 : n * 7}) cursor = cursor.plus({ weeks: cursor.weekday <= weekday ? n-1 : n })
cursor = cursor.set({ weekday }) cursor = cursor.set({ weekday })
} }
cursor = cursor.set({ hour: date.hour, minute: date.minute, second: 0 }) cursor = cursor.set({ hour: date.hour, minute: date.minute, second: 0 })
@@ -293,5 +293,10 @@ module.exports = {
} else { } else {
res.sendStatus(403) res.sendStatus(403)
} }
},
queryParamToBool (value) {
return ((value+'').toLowerCase() === 'true')
} }
} }

View File

@@ -143,9 +143,9 @@ describe('Recurrent events', () => {
const eventController = require('../server/api/controller/event') const eventController = require('../server/api/controller/event')
const { Event } = require('../server/api/models/models') const { Event } = require('../server/api/models/models')
// each week starting from past // each week
let ret = await Event.create({ let ret = await Event.create({
title: 'each last monday starting from past', title: 'each last monday starting',
is_visible: true, is_visible: true,
recurrent: { frequency: '1m', type: -1 }, recurrent: { frequency: '1m', type: -1 },
start_datetime: DateTime.local(2033, 3, 27, 8).toUnixInteger(), start_datetime: DateTime.local(2033, 3, 27, 8).toUnixInteger(),
@@ -159,11 +159,44 @@ describe('Recurrent events', () => {
ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false) ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false)
expect(ev.start_datetime).toBe(DateTime.local(2033, 4, 24, 8).toUnixInteger()) expect(ev.start_datetime).toBe(DateTime.local(2033, 4, 24, 8).toUnixInteger())
// 24 April 2033 08:00 -> 1m -> 29 May 2033 08:00 // 24 April 2033 08:00 -> 1m -> 29 May 2033 08:00
ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false) ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false)
expect(ev.start_datetime).toBe(DateTime.local(2033, 5, 29, 8).toUnixInteger()) expect(ev.start_datetime).toBe(DateTime.local(2033, 5, 29, 8).toUnixInteger())
}) })
test('shoud create an occurrence each second tuesday', async () => {
const eventController = require('../server/api/controller/event')
const { Event } = require('../server/api/models/models')
// each week starting from past
let ret = await Event.create({
title: 'each second tuesday',
is_visible: true,
recurrent: { frequency: '1m', type: 2 },
start_datetime: DateTime.local(2033, 2, 8, 8).toUnixInteger(),
})
ev = await eventController._createRecurrentOccurrence(ret)
expect(ev.start_datetime).toBe(DateTime.local(2033, 2, 8, 8).toUnixInteger())
ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false)
expect(ev.start_datetime).toBe(DateTime.local(2033, 3, 8, 8).toUnixInteger())
// 8 March 2033 08:00 -> 1m -> 12 April 2033 08:00
ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false)
expect(ev.start_datetime).toBe(DateTime.local(2033, 4, 12, 8).toUnixInteger())
// 12 Apr 2033 08:00 -> 1m -> 10 May 2033 08:00
ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false)
expect(ev.start_datetime).toBe(DateTime.local(2033, 5, 10, 8).toUnixInteger())
// 10 May 2033 08:00 -> 1m -> 9 June 2033 08:00
ev = await eventController._createRecurrentOccurrence(ret, DateTime.fromSeconds(ev.start_datetime+1), false)
expect(ev.start_datetime).toBe(DateTime.local(2033, 6, 14, 8).toUnixInteger())
})
}) })