From 892f560634fdb1ca3270cd16935bafe741a03bb8 Mon Sep 17 00:00:00 2001 From: lesion Date: Sun, 18 Jun 2023 21:56:48 +0200 Subject: [PATCH] test and fix some recurrent events --- server/api/controller/event.js | 8 ++++--- server/helpers.js | 7 +++++- tests/recurrent.test.js | 39 +++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/server/api/controller/event.js b/server/api/controller/event.js index 4863c331..d664141d 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -690,7 +690,7 @@ const eventController = { const parentStartDatetime = DateTime.fromSeconds(e.start_datetime) // 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 startAt = cursor @@ -711,6 +711,8 @@ const eventController = { cursor = cursor.plus({ days: 7 * Number(frequency[0]) }) } } else if (frequency === '1m') { + + // day n.X each month if (type === 'ordinal') { cursor = cursor.set({ day: parentStartDatetime.day }) @@ -718,10 +720,10 @@ const eventController = { cursor = cursor.plus({ months: 1 }) } } else { // weekday - // get weekday + // get recurrent freq details cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday) - if (cursor< startAt) { + if (cursor < startAt) { cursor = cursor.plus({ months: 1 }) cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday) } diff --git a/server/helpers.js b/server/helpers.js index bdadf7cf..7af98bbc 100644 --- a/server/helpers.js +++ b/server/helpers.js @@ -259,7 +259,7 @@ module.exports = { } else { cursor = date.startOf('month') // 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({ hour: date.hour, minute: date.minute, second: 0 }) @@ -293,5 +293,10 @@ module.exports = { } else { res.sendStatus(403) } + }, + + queryParamToBool (value) { + return ((value+'').toLowerCase() === 'true') } + } diff --git a/tests/recurrent.test.js b/tests/recurrent.test.js index 478c6d3b..1d05aafa 100644 --- a/tests/recurrent.test.js +++ b/tests/recurrent.test.js @@ -143,9 +143,9 @@ describe('Recurrent events', () => { const eventController = require('../server/api/controller/event') const { Event } = require('../server/api/models/models') - // each week starting from past + // each week let ret = await Event.create({ - title: 'each last monday starting from past', + title: 'each last monday starting', is_visible: true, recurrent: { frequency: '1m', type: -1 }, 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) expect(ev.start_datetime).toBe(DateTime.local(2033, 4, 24, 8).toUnixInteger()) - // 24 April 2033 08:00 -> 1m -> 29 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, 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()) + + }) + })