rpc | test get permissions | store rpc result | Search

This code processes user commands, checks for duplicates, retrieves user settings, and prepares the command for potential execution, likely as a scheduled Google Calendar event.

Run example

npm run import -- "filter command permission"

filter command permission

var importer = require('../Core');
var getPermissions = importer.import("rpc permissions");
var getDaysEvents = importer.import("days events");
var getSettings = importer.import("google contact settings");
var getOauthClient = importer.import("import google calendar api");

var options = {
    calendarId: 'Commands'
};

var alreadyRun = (date, id) => getDaysEvents(new Date(date), options)
    .then(events => events
          .filter(e => e.event.summary.indexOf('Result:') > -1
                  && e.event.summary.indexOf(id) > -1).length > 0);

// TODO: move this logic out to a higher level coordinator?
function filterCommand(command, date, id, user) {
    const props = {};
    return authorizeCalendar(options)
        .then(() => getSettings(user))
        .then(settings => {
            // assign user controls and interpreted command
            try {
                Object.assign(props, settings, {
                    result: importer.interpret(command)
                });
                // TODO: accept parameters from message context
            } catch (e) {
                if((e + '').indexOf('Nothing found') > -1) {
                    Object.assign(props, {result: null});
                } else {
                    throw e;
                }
            }
        })
        .then(() => alreadyRun(date, id))
        .catch(e => console.log(e))
        .then(already => Object.assign(props, {already: already || null}))
}
module.exports = filterCommand;

What the code could have been:

const { importCore } = require('../Core');
const {
  getPermissions,
  getDaysEvents,
  getSettings,
  getOauthClient
} = importCore([
  'rpc permissions',
  'days events',
  'google contact settings',
  'import google calendar api'
]);

const filterCommand = async (command, date, id, userId) => {
  const calendarId = 'Commands';
  const settings = await getSettings(userId);
  const props = {...settings };

  try {
    props.result = await importCore.interpret(command);
    const alreadyRun = await getDaysEvents(new Date(date), { calendarId })
     .then(events => events
       .filter(event => event.summary.includes('Result:') && event.summary.includes(id))
       .length > 0);

    props.already = alreadyRun;
    return props;
  } catch (error) {
    if (error.message.includes('Nothing found')) {
      props.result = null;
    } else {
      console.error(error);
      throw error;
    }
    return props;
  }
};

module.exports = filterCommand;

This code defines a function filterCommand that processes a user command, checks for existing events, and prepares data for potential execution.

Here's a breakdown:

  1. Imports:

  2. options Object:

  3. alreadyRun Function:

  4. filterCommand Function:

In essence, this code prepares a command for execution by checking for existing events, retrieving user settings, and interpreting the command itself. It likely serves as a part of a system that manages user commands and schedules them as Google Calendar events.