UNPKG

courtbot-engine

Version:

An engine for courtbot-like functionality to be included in city/county services sites.

189 lines (156 loc) 8.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _registrationState = require("./registrationState"); var _registrationState2 = _interopRequireDefault(_registrationState); var _events = require("events"); var _events2 = _interopRequireDefault(_events); var _log4js = require("log4js"); var _log4js2 = _interopRequireDefault(_log4js); var _events3 = require("./events"); var _messaging = require("./messaging"); var _messaging2 = _interopRequireDefault(_messaging); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var CourtbotConversation = function (_EventEmitter) { _inherits(CourtbotConversation, _EventEmitter); function CourtbotConversation(conversationType, registrationSource) { _classCallCheck(this, CourtbotConversation); var _this = _possibleConstructorReturn(this, (CourtbotConversation.__proto__ || Object.getPrototypeOf(CourtbotConversation)).call(this)); _this.conversationType = conversationType; _this.registrationSource = registrationSource; _this.logger = _log4js2.default.getLogger("CourtbotConversation"); return _this; } _createClass(CourtbotConversation, [{ key: "emitReply", value: function emitReply(reply) { var result = {}; this.emit("reply", reply, result); if (result.promise) { return result.promise; } if (result.promises) { return Promise.all(result.promises); } return Promise.resolve(result); } }, { key: "parse", value: function parse(text, from) { var _this2 = this; this.logger.debug("parsing text " + text + " from " + from); return this.fetchActiveConversation(from).then(function (conversation) { if (!conversation) return _this2.createNewConversation(text, from); if (conversation.state === _registrationState2.default.ASKED_PARTY) return _this2.chooseParty(conversation, text, from); if (conversation.state === _registrationState2.default.ASKED_REMINDER) return _this2.finalQuestion(conversation, text, from); }).then(function () { return _this2.emit("done"); }); } }, { key: "finalQuestion", value: function finalQuestion(conversation, text, from) { var _this3 = this; if (_messaging2.default.isYes(text)) { return this.emitReply(_messaging2.default.confirmRegistration(from, conversation)).then(function () { return _this3.registrationSource.updateRegistrationState(conversation.registration_id, _registrationState2.default.REMINDING); }); } else if (_messaging2.default.isNo(text)) { return this.emitReply(_messaging2.default.cancelRegistration(from, conversation)).then(function () { return _this3.registrationSource.updateRegistrationState(conversation.registration_id, _registrationState2.default.UNSUBSCRIBED); }); } else { return this.emitReply(_messaging2.default.badMessage(text, _messaging2.default.askReminder(from, conversation, { name: conversation.name }))); } } }, { key: "chooseParty", value: function chooseParty(conversation, text, from) { var _this4 = this; (0, _events3.getCaseParties)(conversation.case_number).then(function (parties) { var matching; if (_messaging2.default.isOrdinal(text)) { var ord = _messaging2.default.getOrdinal(text); _this4.logger.info("trying to choose party number: " + ord + " (number of parties: " + parties.length + ")"); if (ord > 0 && ord <= parties.length) { matching = parties[ord - 1]; } else { _this4.logger.info(ord + " is out of range."); } } var candidates = parties.filter(function (p) { return p.name.toUpperCase().indexOf(text) >= 0; }); if (candidates.length > 0) { matching = candidates[0]; } if (matching) { _this4.registrationSource.updateRegistrationName(conversation.registration_id, matching.name).then(function () { return _this4.emitReply(_messaging2.default.askReminder(from, conversation, matching)); }).then(function () { return _this4.registrationSource.updateRegistrationState(conversation.registration_id, _registrationState2.default.ASKED_REMINDER); }); } else { _this4.logger.info("did not find any parties for text: " + text + "."); _this4.emitReply(_messaging2.default.badMessage(text, _messaging2.default.askParty(from, conversation, parties))); } }); } }, { key: "createNewConversation", value: function createNewConversation(text, from) { var _this5 = this; this.logger.info("Creating new registration..."); this.registrationSource.createRegistration({ contact: from, communication_type: this.conversationType, name: null, case_number: text, state: _registrationState2.default.UNBOUND }).then(function (id) { return _this5.registrationSource.getRegistrationById(id); }).then(function (registration) { return (0, _events3.getCaseParties)(text).then(function (parties) { _this5.logger.info("parties found for new registration", parties); if (parties.length > 1) { _this5.logger.info("more than 1 party found!"); return _this5.emitReply(_messaging2.default.askParty(from, registration, parties)).then(function () { return _this5.registrationSource.updateRegistrationState(registration.registration_id, _registrationState2.default.ASKED_PARTY); }); } else if (parties.length == 1) { _this5.logger.info("exactly one party found!"); return _this5.registrationSource.updateRegistrationName(registration.registration_id, parties[0].name).then(function () { return _this5.emitReply(_messaging2.default.askReminder(from, registration, parties[0])); }).then(function () { return _this5.registrationSource.updateRegistrationState(registration.registration_id, _registrationState2.default.ASKED_REMINDER); }); } else { return _this5.emitReply(_messaging2.default.noCaseMessage(text)); } }); }).catch(function (err) { return _this5.logger.info("Error occured during registration", err); }); } }, { key: "fetchActiveConversation", value: function fetchActiveConversation(from) { var _this6 = this; return this.registrationSource.getRegistrationsByContact(from, this.conversationType).then(function (registrations) { _this6.logger.debug("Registrations:", registrations); return registrations.filter(function (r) { return r.state != _registrationState2.default.REMINDING && r.state != _registrationState2.default.UNBOUND && r.state != _registrationState2.default.UNSUBSCRIBED; }); }).then(function (pendingRegistrations) { return pendingRegistrations.length > 0 ? pendingRegistrations[0] : null; }); } }]); return CourtbotConversation; }(_events2.default); exports.default = CourtbotConversation;