Skip to content

Commit

Permalink
feat(scenes): Add does not contain for Calendar action
Browse files Browse the repository at this point in the history
  • Loading branch information
cicoub13 committed Jan 27, 2025
1 parent c56d552 commit 87b2832
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions front/src/config/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions front/src/config/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ class CheckTime extends Component {
<option value="contains">
<Text id="editScene.triggersCard.calendarEventIsComing.contains" />
</option>
<option value="does-not-contain">
<Text id="editScene.triggersCard.calendarEventIsComing.doesNotContain" />
</option>
<option value="starts-with">
<Text id="editScene.triggersCard.calendarEventIsComing.startsWith" />
</option>
Expand Down
13 changes: 13 additions & 0 deletions server/lib/calendar/calendar.findCurrentlyRunningEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }));
}

Expand Down
1 change: 1 addition & 0 deletions server/models/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
39 changes: 39 additions & 0 deletions server/test/lib/calendar/calendar.event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 87b2832

Please sign in to comment.