Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variables in calendar.eventIsRunning condition in scene #1450

Merged
merged 4 commits into from Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion front/src/routes/calendar/index.js
Expand Up @@ -64,7 +64,6 @@ class Map extends Component {

render(props, {}) {
const noCalendarConnected = props.calendars && props.calendars.length === 0;
console.log(noCalendarConnected, props.calendars);
return (
<div class="page">
<div class="page-main">
Expand Down
2 changes: 2 additions & 0 deletions front/src/routes/scene/edit-scene/ActionCard.jsx
Expand Up @@ -251,6 +251,8 @@ const ActionCard = ({ children, ...props }) => (
columnIndex={props.columnIndex}
index={props.index}
updateActionProperty={props.updateActionProperty}
variables={props.variables}
setVariables={props.setVariables}
/>
)}
</div>
Expand Down
Expand Up @@ -6,6 +6,8 @@ import { Text, Localizer } from 'preact-i18n';
import cx from 'classnames';
import get from 'get-value';

import withIntlAsProp from '../../../../utils/withIntlAsProp';

import style from './CalendarIsEventRunning.css';

const isNullOrUndefined = variable => variable === null || variable === undefined;
Expand Down Expand Up @@ -66,6 +68,14 @@ class CheckTime extends Component {
);
};

initVariables = action => {
if (action.stop_scene_if_event_found === false) {
this.setVariables();
} else {
this.removeVariables();
}
};

refreshSelectedOptions = action => {
const selectedCalendarsOptions = [];
if (action.calendars && this.state.calendarsOptions) {
Expand All @@ -76,6 +86,10 @@ class CheckTime extends Component {
}
});
}
if (get(this.props, 'action.stop_scene_if_event_found') !== action.stop_scene_if_event_found) {
this.initVariables(action);
}

this.setState({ selectedCalendarsOptions });
};

Expand All @@ -99,9 +113,53 @@ class CheckTime extends Component {
}
};

setVariables = () => {
const { columnIndex, index } = this.props;
const EVENT_NAME_VARIABLE = get(this.props.intl.dictionary, 'editScene.variables.calendar.eventName');
const EVENT_LOCATION_VARIABLE = get(this.props.intl.dictionary, 'editScene.variables.calendar.eventLocation');
const EVENT_START_VARIABLE = get(this.props.intl.dictionary, 'editScene.variables.calendar.eventStart');
const EVENT_END_VARIABLE = get(this.props.intl.dictionary, 'editScene.variables.calendar.eventEnd');
this.props.setVariables(columnIndex, index, [
{
name: 'calendarEvent.name',
type: 'calendar',
ready: true,
label: EVENT_NAME_VARIABLE,
data: {}
},
{
name: 'calendarEvent.location',
type: 'calendar',
ready: true,
label: EVENT_LOCATION_VARIABLE,
data: {}
},
{
name: 'calendarEvent.start',
type: 'calendar',
ready: true,
label: EVENT_START_VARIABLE,
data: {}
},
{
name: 'calendarEvent.end',
type: 'calendar',
ready: true,
label: EVENT_END_VARIABLE,
data: {}
}
]);
};

removeVariables = () => {
const { columnIndex, index } = this.props;
this.props.setVariables(columnIndex, index, []);
};

componentDidMount() {
this.initActionIfNeeded();
this.getCalendars();
this.initVariables(this.props.action);
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -218,4 +276,4 @@ class CheckTime extends Component {
}
}

export default connect('user,httpClient', {})(CheckTime);
export default connect('user,httpClient', {})(withIntlAsProp(CheckTime));
22 changes: 21 additions & 1 deletion server/lib/scene/scene.actions.js
Expand Up @@ -271,7 +271,7 @@ const actionsFunc = {
await self.house.userLeft(action.house, action.user);
}
},
[ACTIONS.CALENDAR.IS_EVENT_RUNNING]: async (self, action) => {
[ACTIONS.CALENDAR.IS_EVENT_RUNNING]: async (self, action, scope, columnIndex, rowIndex) => {
// find if one event match the condition
const events = await self.calendar.findCurrentlyRunningEvent(
action.calendars,
Expand All @@ -288,6 +288,26 @@ const actionsFunc = {
if (!atLeastOneEventFound && action.stop_scene_if_event_not_found === true) {
throw new AbortScene('EVENT_NOT_FOUND');
}

// set variable
if (atLeastOneEventFound) {
const eventRaw = events[0];
const eventFormatted = {
name: eventRaw.name,
location: eventRaw.location,
start: dayjs(eventRaw.start)
.tz(self.timezone)
.locale(eventRaw.calendar.creator.language)
.format('LLL'),
end: dayjs(eventRaw.end)
.tz(self.timezone)
.locale(eventRaw.calendar.creator.language)
.format('LLL'),
};
set(scope, `${columnIndex}.${rowIndex}`, {
calendarEvent: eventFormatted,
});
}
},
};

Expand Down
2 changes: 1 addition & 1 deletion server/lib/scene/scene.checkCalendarTriggers.js
Expand Up @@ -114,7 +114,7 @@ async function checkCalendarTriggers() {
.tz(this.timezone)
.locale(eventRaw.calendar.creator.language)
.format('LLL'),
end: dayjs(eventRaw.start)
end: dayjs(eventRaw.end)
.tz(this.timezone)
.locale(eventRaw.calendar.creator.language)
.format('LLL'),
Expand Down
@@ -1,7 +1,9 @@
const { assert, fake, useFakeTimers } = require('sinon');
const chaiAssert = require('chai').assert;
const { expect } = require('chai');
const dayjs = require('dayjs');
const EventEmitter = require('events');

const { ACTIONS } = require('../../../../utils/constants');
const { AbortScene } = require('../../../../utils/coreErrors');
const { executeActions } = require('../../../../lib/scene/scene.executeActions');
Expand Down Expand Up @@ -36,11 +38,12 @@ describe('scene.action.isEventRunning', () => {
await calendar.createEvent('test-calendar', {
id: 'a2b57b0a-7148-4961-8540-e493104bfd7c',
name: 'my test event',
location: 'school',
start: startDate,
end: endDate,
});
await executeActions(
{ stateManager, event, message, calendar },
{ stateManager, event, message, calendar, timezone: 'Europe/Paris' },
[
[
{
Expand All @@ -62,6 +65,24 @@ describe('scene.action.isEventRunning', () => {
scope,
);
assert.calledWith(message.sendToUser, 'pepper', 'hello');
expect(scope).to.deep.equal({
'0': {
'0': {
calendarEvent: {
name: 'my test event',
location: 'school',
start: dayjs(startDate)
.tz('Europe/Paris')
.locale('en')
.format('LLL'),
end: dayjs(endDate)
.tz('Europe/Paris')
.locale('en')
.format('LLL'),
},
},
},
});
});
it('should execute condition is-event-running, and not send message because scene should stop', async () => {
const stateManager = new StateManager(event);
Expand Down
2 changes: 1 addition & 1 deletion server/test/lib/scene/scene.checkCalendarTriggers.test.js
Expand Up @@ -75,7 +75,7 @@ describe('scene.checkCalendarTriggers', () => {
.tz('Europe/Paris')
.locale('en')
.format('LLL'),
end: dayjs(startDate)
end: dayjs(endDate)
.tz('Europe/Paris')
.locale('en')
.format('LLL'),
Expand Down