UNPKG

courtbot-engine-data-courtbook

Version:
115 lines (99 loc) 5.19 kB
"use strict"; var _courtbookApi = require("./courtbook-api"); var _courtbookApi2 = _interopRequireDefault(_courtbookApi); var _courtbotEngine = require("courtbot-engine"); var _log4js = require("log4js"); var _log4js2 = _interopRequireDefault(_log4js); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var logger = _log4js2.default.getLogger("courtbook"); module.exports = exports = function exports(_ref) { var courtbookUrl = _ref.courtbookUrl, oauthConfig = _ref.oauthConfig; var courtbookApi = new _courtbookApi2.default({ courtbookUrl: courtbookUrl, oauthConfig: oauthConfig }); _courtbotEngine.events.on("add-routes", function (_ref2) { var router = _ref2.router, registrationSource = _ref2.registrationSource; router.get("/courtbook/messageLog/:casenumber/:communicationtype/:contact", function (req, res) { if (!process.env.API_TOKENS || JSON.parse(process.env.API_TOKENS).filter(function (x) { return x == req.query.api_token; }).length == 0) { logger.debug("Invalid API token.", req.body); res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: false, message: "Invalid API token." })); return; } (0, _courtbotEngine.verifyContact)(req.params.contact, req.params.communicationtype).then(function (contact) { return registrationSource.getSentMessages(contact, req.params.casenumber).then(function (msgs) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: true, messages: msgs })); }); }); }); router.post("/courtbook/register", function (req, res) { if (!process.env.API_TOKENS || JSON.parse(process.env.API_TOKENS).filter(function (x) { return x == req.body.api_token; }).length == 0) { logger.debug("Invalid API token.", req.body); res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: false, message: "Invalid API token." })); return; } (0, _courtbotEngine.verifyContact)(req.body.contact, req.body.communication_type).then(function (contact) { return registrationSource.getRegistrationsByContact(contact, req.body.communication_type).then(function (registrations) { var existing = registrations.filter(function (r) { return r.name == req.body.name && r.case_number == req.body.case_number && r.state != _courtbotEngine.registrationState.UNSUBSCRIBED; }); if (existing.length > 0) { logger.debug("User has an existing registration"); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: false, message: "User has an existing registration" })); return; } return registrationSource.createRegistration({ contact: contact, communication_type: req.body.communication_type, name: req.body.name, case_number: req.body.case_number, state: _courtbotEngine.registrationState.ASKED_REMINDER }).then(function () { return (0, _courtbotEngine.sendNonReplyMessage)(contact, _courtbotEngine.messaging.remote(req.body.user, req.body.case_number, req.body.name), req.body.communication_type); }).then(function () { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: true, message: "Registration added", verifiedContact: contact })); }); }); }).catch(function (err) { logger.debug("Invalid phone number", err); res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: false, message: "Invalid phone number" })); }); }); }); _courtbotEngine.events.on("retrieve-parties", function (casenumber, result) { result.promises.push(courtbookApi.getParties(casenumber)); }); _courtbotEngine.events.on("retrieve-party-events", function (casenumber, party, result) { result.promises.push(courtbookApi.getEvents(casenumber, party)); }); };