From 87b2832c33dba4c1093e5ddd75a443f2ad7cbdd4 Mon Sep 17 00:00:00 2001 From: Cyril Beslay Date: Tue, 17 Dec 2024 09:35:08 +0100 Subject: [PATCH] feat(scenes): Add does not contain for Calendar action --- front/src/config/i18n/de.json | 1 + front/src/config/i18n/en.json | 1 + front/src/config/i18n/fr.json | 1 + .../actions/CalendarIsEventRunning.jsx | 3 ++ .../calendar.findCurrentlyRunningEvent.js | 13 +++++++ server/models/scene.js | 1 + .../test/lib/calendar/calendar.event.test.js | 39 +++++++++++++++++++ 7 files changed, 59 insertions(+) diff --git a/front/src/config/i18n/de.json b/front/src/config/i18n/de.json index e5d4902a58..e23106481e 100644 --- a/front/src/config/i18n/de.json +++ b/front/src/config/i18n/de.json @@ -2223,6 +2223,7 @@ "nameLabel": "Wenn der Name", "isExactly": "genau ist", "contains": "enthält", + "doesNotContain": "enthält nicht", "startsWith": "beginnt mit", "endsWith": "endet mit", "hasAnyName": "einen beliebigen Namen hat", diff --git a/front/src/config/i18n/en.json b/front/src/config/i18n/en.json index 7de43c0f2a..45597d520e 100644 --- a/front/src/config/i18n/en.json +++ b/front/src/config/i18n/en.json @@ -2223,6 +2223,7 @@ "nameLabel": "If the name", "isExactly": "is exactly", "contains": "contains", + "doesNotContain": "does not contain", "startsWith": "starts with", "endsWith": "ends with", "hasAnyName": "has any name", diff --git a/front/src/config/i18n/fr.json b/front/src/config/i18n/fr.json index 6674e794d6..ab36d39457 100644 --- a/front/src/config/i18n/fr.json +++ b/front/src/config/i18n/fr.json @@ -2223,6 +2223,7 @@ "nameLabel": "Si le nom", "isExactly": "est exactement", "contains": "contient", + "doesNotContain": "ne contient pas", "startsWith": "commence par", "endsWith": "finit par", "hasAnyName": "a n'importe quel nom", diff --git a/front/src/routes/scene/edit-scene/actions/CalendarIsEventRunning.jsx b/front/src/routes/scene/edit-scene/actions/CalendarIsEventRunning.jsx index d4d662165e..44c6f15ca9 100644 --- a/front/src/routes/scene/edit-scene/actions/CalendarIsEventRunning.jsx +++ b/front/src/routes/scene/edit-scene/actions/CalendarIsEventRunning.jsx @@ -217,6 +217,9 @@ class CheckTime extends Component { + diff --git a/server/lib/calendar/calendar.findCurrentlyRunningEvent.js b/server/lib/calendar/calendar.findCurrentlyRunningEvent.js index 52f8ef3aa3..aa842a639c 100644 --- a/server/lib/calendar/calendar.findCurrentlyRunningEvent.js +++ b/server/lib/calendar/calendar.findCurrentlyRunningEvent.js @@ -43,6 +43,7 @@ async function findCurrentlyRunningEvent(calendars, calendarEventNameComparator, switch (calendarEventNameComparator) { // do nothing in that case case 'has-any-name': + case 'does-not-contains': break; case 'is-exactly': // @ts-ignore @@ -70,6 +71,18 @@ async function findCurrentlyRunningEvent(calendars, calendarEventNameComparator, // we search in the database if we find events that match our request const eventsMatching = await db.CalendarEvent.findAll(queryParams); + // manage special does not contain condition + if (calendarEventNameComparator === 'does-not-contain') { + const eventMatchingDoesNotContain = eventsMatching.filter((event) => !event.name.includes(calendarEventName)); + const eventNotMatchingDoesNotContain = eventsMatching.filter((event) => event.name.includes(calendarEventName)); + // If at least an event is found not containing the word, we want to stop the scene + if (eventMatchingDoesNotContain.length > 0) { + return []; + } + // else we return the other events + return eventNotMatchingDoesNotContain.map((event) => event.get({ plain: true })); + } + return eventsMatching.map((event) => event.get({ plain: true })); } diff --git a/server/models/scene.js b/server/models/scene.js index 7c91a90c7b..b05267d90b 100644 --- a/server/models/scene.js +++ b/server/models/scene.js @@ -33,6 +33,7 @@ const actionSchema = Joi.array().items( calendar_event_name_comparator: Joi.string().valid( 'is-exactly', 'contains', + 'does-not-contain', 'starts-with', 'ends-with', 'has-any-name', diff --git a/server/test/lib/calendar/calendar.event.test.js b/server/test/lib/calendar/calendar.event.test.js index a4d1bc40a3..4c24e861fb 100644 --- a/server/test/lib/calendar/calendar.event.test.js +++ b/server/test/lib/calendar/calendar.event.test.js @@ -302,6 +302,45 @@ describe('calendar.findCurrentlyRunningEvent', () => { const eventsId = events.map((e) => e.id); expect(eventsId).deep.equal(['a2b57b0a-7148-4961-8540-e493104bfd7c']); }); + it('should find event in calendar - does not contain', async () => { + await calendar.createEvent('test-calendar', { + id: 'a2b57b0a-7148-4961-8540-e493104bfd7c', + name: 'my test event', + start: startDate, + end: endDate, + }); + const events = await calendar.findCurrentlyRunningEvent(['test-calendar'], 'does-not-contain', 'random'); + const eventsId = events.map((e) => e.id); + expect(eventsId).deep.equal([]); + }); + it('should find no event in calendar - does not contain', async () => { + await calendar.createEvent('test-calendar', { + id: 'a2b57b0a-7148-4961-8540-e493104bfd7c', + name: 'random event', + start: startDate, + end: endDate, + }); + const events = await calendar.findCurrentlyRunningEvent(['test-calendar'], 'does-not-contain', 'random'); + const eventsId = events.map((e) => e.id); + expect(eventsId).deep.equal(['a2b57b0a-7148-4961-8540-e493104bfd7c']); + }); + it('should find one event in calendar but return nothing - does not contain', async () => { + await calendar.createEvent('test-calendar', { + id: 'a2b57b0a-7148-4961-8540-e493104bfd7c', + name: 'my test event', + start: startDate, + end: endDate, + }); + await calendar.createEvent('test-calendar', { + id: 'a2b57b0a-7148-4961-8540-e493104bfd7d', + name: 'random event', + start: startDate, + end: endDate, + }); + const events = await calendar.findCurrentlyRunningEvent(['test-calendar'], 'does-not-contain', 'random'); + const eventsId = events.map((e) => e.id); + expect(eventsId).deep.equal([]); + }); it('should find event in calendar - starts-with', async () => { await calendar.createEvent('test-calendar', { id: 'a2b57b0a-7148-4961-8540-e493104bfd7c',