3e7f273dbc5988e71c23ecb3138a3f1f851f5a53
[motion2.git] / server / api / openchannelMessage / openchannelMessage.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xf40b=['jayson/promise','client','http','ValidationError','The\x20password\x20must\x20be\x20at\x20least\x208\x20characters\x20long\x20and\x20have\x201\x20lowercase\x20character,\x201\x20uppercase\x20character,\x201\x20number\x20and\x201\x20special\x20character\x20~!@#$%^&-_=+[{]}.','request','then','OpenchannelMessage,\x20%s,\x20%s','debug','OpenchannelMessage,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','message','info','status','end','json','count','offset','limit','Content-Range','apply','save','update','destroy','sendStatus','stack','name','send','index','map','OpenchannelMessage','rawAttributes','fieldName','type','key','keys','query','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','findAll','rows','catch','show','params','includeAll','include','find','create','body','describe','../../config/logger','openchannel-interactions','openchannel','read','readAt','format','UserId','user','CmContact','Contact','get','OpenchannelInteraction','isNil','read1stAt','interaction','[OPENCHANNELMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','null','acceptMessage','interface','channel','event','accept','EventManager','RejectMessage','rejectMessage','SIP/%s','rejectmessage','html-pdf','eml-format','rimraf','zip-dir','moment','mustache','util','path','ejs','crypto','jsforce','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','api','../../config/utils','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register'];(function(_0x380af2,_0x36cf66){var _0x1789dc=function(_0x37b604){while(--_0x37b604){_0x380af2['push'](_0x380af2['shift']());}};_0x1789dc(++_0x36cf66);}(_0xf40b,0x14e));var _0xbf40=function(_0xbe0d54,_0x5c3362){_0xbe0d54=_0xbe0d54-0x0;var _0x13e619=_0xf40b[_0xbe0d54];return _0x13e619;};'use strict';var pdf=require(_0xbf40('0x0'));var emlformat=require(_0xbf40('0x1'));var rimraf=require(_0xbf40('0x2'));var zipdir=require(_0xbf40('0x3'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xbf40('0x4'));var BPromise=require('bluebird');var Mustache=require(_0xbf40('0x5'));var util=require(_0xbf40('0x6'));var path=require(_0xbf40('0x7'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xbf40('0x8'));var fs=require('fs');var _=require('lodash');var squel=require('squel');var crypto=require(_0xbf40('0x9'));var jsforce=require(_0xbf40('0xa'));var deskjs=require('desk.js');var toCsv=require(_0xbf40('0xb'));var querystring=require(_0xbf40('0xc'));var Papa=require(_0xbf40('0xd'));var Redis=require(_0xbf40('0xe'));var authService=require(_0xbf40('0xf'));var qs=require(_0xbf40('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0xbf40('0x11'));var utils=require(_0xbf40('0x12'));var config=require(_0xbf40('0x13'));var db=require(_0xbf40('0x14'))['db'];config[_0xbf40('0x15')]=_[_0xbf40('0x16')](config['redis'],{'host':_0xbf40('0x17'),'port':0x18eb});var socket=require(_0xbf40('0x18'))(new Redis(config[_0xbf40('0x15')]));require('./openchannelMessage.socket')[_0xbf40('0x19')](socket);var jayson=require(_0xbf40('0x1a'));var client=jayson['client']['http']({'port':0x232c});var client9002=jayson[_0xbf40('0x1b')][_0xbf40('0x1c')]({'port':0x232a});function checkPasswordPattern(_0x581928,_0xda65f,_0x9528ad){if(_0x9528ad){var _0x57bfb8=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x57bfb8['test'](_0xda65f)){return _0x581928;}else{throw new db['Sequelize'][(_0xbf40('0x1d'))](_0xbf40('0x1e'));}}else{return _0x581928;}}function respondWithRpcPromise(_0x3a047a,_0x329d31,_0x58388c,_0xd5d3a1){return new BPromise(function(_0x48cbe8,_0x269b7e){var _0x3ac4e7=_0xd5d3a1||client;return _0x3ac4e7[_0xbf40('0x1f')](_0x3a047a,_0x58388c)[_0xbf40('0x20')](function(_0x5f2aa0){logger['info'](_0xbf40('0x21'),_0x329d31,'request\x20sent');logger[_0xbf40('0x22')](_0xbf40('0x23'),_0x329d31,_0xbf40('0x24'),JSON[_0xbf40('0x25')](_0x5f2aa0));if(_0x5f2aa0[_0xbf40('0x26')]){if(_0x5f2aa0[_0xbf40('0x26')]['code']===0x1f4){logger[_0xbf40('0x26')](_0xbf40('0x21'),_0x329d31,_0x5f2aa0[_0xbf40('0x26')][_0xbf40('0x27')]);return _0x269b7e(_0x5f2aa0['error']['message']);}logger[_0xbf40('0x26')](_0xbf40('0x21'),_0x329d31,_0x5f2aa0[_0xbf40('0x26')][_0xbf40('0x27')]);return _0x48cbe8(_0x5f2aa0['error'][_0xbf40('0x27')]);}else{logger[_0xbf40('0x28')]('OpenchannelMessage,\x20%s,\x20%s',_0x329d31,_0xbf40('0x24'));_0x48cbe8(_0x5f2aa0['result'][_0xbf40('0x27')]);}})['catch'](function(_0x562c7c){logger[_0xbf40('0x26')](_0xbf40('0x21'),_0x329d31,_0x562c7c);_0x269b7e(_0x562c7c);});});}function respondWithStatusCode(_0x154f13,_0x33f4c4){_0x33f4c4=_0x33f4c4||0xcc;return function(_0x4e589a){if(_0x4e589a){return _0x154f13['sendStatus'](_0x33f4c4);}return _0x154f13[_0xbf40('0x29')](_0x33f4c4)[_0xbf40('0x2a')]();};}function respondWithResult(_0x19f6ba,_0x2085e1){_0x2085e1=_0x2085e1||0xc8;return function(_0x52c8df){if(_0x52c8df){return _0x19f6ba[_0xbf40('0x29')](_0x2085e1)[_0xbf40('0x2b')](_0x52c8df);}};}function respondWithFilteredResult(_0x26ea4a,_0x320335){return function(_0x49cf65){if(_0x49cf65){var _0x2772ea=_0x49cf65[_0xbf40('0x2c')],_0x2d0124=_0x320335[_0xbf40('0x2d')],_0x4b6551=_0x320335[_0xbf40('0x2d')]+_0x320335[_0xbf40('0x2e')],_0x1d3be2;if(_0x4b6551>=_0x2772ea){_0x4b6551=_0x2772ea;_0x1d3be2=0xc8;}else{_0x1d3be2=0xce;}_0x26ea4a[_0xbf40('0x29')](_0x1d3be2);return _0x26ea4a['set'](_0xbf40('0x2f'),_0x2d0124+'-'+_0x4b6551+'/'+_0x2772ea)[_0xbf40('0x2b')](_0x49cf65);}return null;};}function patchUpdates(_0x3d9ec0){return function(_0x29d28c){try{jsonpatch[_0xbf40('0x30')](_0x29d28c,_0x3d9ec0,!![]);}catch(_0x41d44c){return BPromise['reject'](_0x41d44c);}return _0x29d28c[_0xbf40('0x31')]();};}function saveUpdates(_0xb4bdba,_0x46fe06){return function(_0x196275){if(_0x196275){return _0x196275[_0xbf40('0x32')](_0xb4bdba)['then'](function(_0xbc96a4){return _0xbc96a4;});}return null;};}function removeEntity(_0x293af8,_0x5a4168){return function(_0x5c3e7c){if(_0x5c3e7c){return _0x5c3e7c[_0xbf40('0x33')]()[_0xbf40('0x20')](function(){_0x293af8[_0xbf40('0x29')](0xcc)[_0xbf40('0x2a')]();});}};}function handleEntityNotFound(_0xa1d782,_0x157e2c){return function(_0x1b110c){if(!_0x1b110c){_0xa1d782[_0xbf40('0x34')](0x194);}return _0x1b110c;};}function handleError(_0x34eda5,_0x1e5076){_0x1e5076=_0x1e5076||0x1f4;return function(_0x4855f3){logger[_0xbf40('0x26')](_0x4855f3[_0xbf40('0x35')]);if(_0x4855f3[_0xbf40('0x36')]){delete _0x4855f3['name'];}_0x34eda5[_0xbf40('0x29')](_0x1e5076)[_0xbf40('0x37')](_0x4855f3);};}exports[_0xbf40('0x38')]=function(_0x3907eb,_0x1b6e6d){var _0x5f496e={},_0x4aae71={},_0x3cc2ee={'count':0x0,'rows':[]};var _0x117b61=_[_0xbf40('0x39')](db[_0xbf40('0x3a')][_0xbf40('0x3b')],function(_0x2519c3){return{'name':_0x2519c3[_0xbf40('0x3c')],'type':_0x2519c3[_0xbf40('0x3d')][_0xbf40('0x3e')]};});_0x4aae71['model']=_[_0xbf40('0x39')](_0x117b61,'name');_0x4aae71['query']=_[_0xbf40('0x3f')](_0x3907eb[_0xbf40('0x40')]);_0x4aae71[_0xbf40('0x41')]=_[_0xbf40('0x42')](_0x4aae71[_0xbf40('0x43')],_0x4aae71[_0xbf40('0x40')]);_0x5f496e[_0xbf40('0x44')]=_[_0xbf40('0x42')](_0x4aae71[_0xbf40('0x43')],qs[_0xbf40('0x45')](_0x3907eb[_0xbf40('0x40')][_0xbf40('0x45')]));_0x5f496e[_0xbf40('0x44')]=_0x5f496e[_0xbf40('0x44')][_0xbf40('0x46')]?_0x5f496e[_0xbf40('0x44')]:_0x4aae71[_0xbf40('0x43')];if(!_0x3907eb[_0xbf40('0x40')][_0xbf40('0x47')](_0xbf40('0x48'))){_0x5f496e[_0xbf40('0x2e')]=qs[_0xbf40('0x2e')](_0x3907eb['query'][_0xbf40('0x2e')]);_0x5f496e[_0xbf40('0x2d')]=qs[_0xbf40('0x2d')](_0x3907eb[_0xbf40('0x40')][_0xbf40('0x2d')]);}_0x5f496e[_0xbf40('0x49')]=qs[_0xbf40('0x4a')](_0x3907eb[_0xbf40('0x40')][_0xbf40('0x4a')]);_0x5f496e[_0xbf40('0x4b')]=qs[_0xbf40('0x41')](_[_0xbf40('0x4c')](_0x3907eb[_0xbf40('0x40')],_0x4aae71['filters']),_0x117b61);if(_0x3907eb['query'][_0xbf40('0x4d')]){_0x5f496e[_0xbf40('0x4b')]=_[_0xbf40('0x4e')](_0x5f496e[_0xbf40('0x4b')],{'$or':_[_0xbf40('0x39')](_0x117b61,function(_0x32ea7e){if(_0x32ea7e['type']!==_0xbf40('0x4f')){var _0x48661b={};_0x48661b[_0x32ea7e[_0xbf40('0x36')]]={'$like':'%'+_0x3907eb[_0xbf40('0x40')][_0xbf40('0x4d')]+'%'};return _0x48661b;}})});}_0x5f496e=_[_0xbf40('0x4e')]({},_0x5f496e,_0x3907eb[_0xbf40('0x50')]);var _0x36751a={'where':_0x5f496e['where']};return db['OpenchannelMessage'][_0xbf40('0x2c')](_0x36751a)[_0xbf40('0x20')](function(_0x5dc2f7){_0x3cc2ee[_0xbf40('0x2c')]=_0x5dc2f7;if(_0x3907eb[_0xbf40('0x40')]['includeAll']){_0x5f496e['include']=[{'all':!![]}];}return db[_0xbf40('0x3a')][_0xbf40('0x51')](_0x5f496e);})[_0xbf40('0x20')](function(_0x2bdb57){_0x3cc2ee[_0xbf40('0x52')]=_0x2bdb57;return _0x3cc2ee;})[_0xbf40('0x20')](respondWithFilteredResult(_0x1b6e6d,_0x5f496e))[_0xbf40('0x53')](handleError(_0x1b6e6d,null));};exports[_0xbf40('0x54')]=function(_0xe75ddb,_0x57aa06){var _0x23bda2={'raw':!![],'where':{'id':_0xe75ddb[_0xbf40('0x55')]['id']}},_0x358f24={};_0x358f24['model']=_['keys'](db[_0xbf40('0x3a')][_0xbf40('0x3b')]);_0x358f24[_0xbf40('0x40')]=_[_0xbf40('0x3f')](_0xe75ddb[_0xbf40('0x40')]);_0x358f24[_0xbf40('0x41')]=_['intersection'](_0x358f24[_0xbf40('0x43')],_0x358f24[_0xbf40('0x40')]);_0x23bda2[_0xbf40('0x44')]=_[_0xbf40('0x42')](_0x358f24[_0xbf40('0x43')],qs['fields'](_0xe75ddb[_0xbf40('0x40')][_0xbf40('0x45')]));_0x23bda2['attributes']=_0x23bda2[_0xbf40('0x44')][_0xbf40('0x46')]?_0x23bda2[_0xbf40('0x44')]:_0x358f24[_0xbf40('0x43')];if(_0xe75ddb[_0xbf40('0x40')][_0xbf40('0x56')]){_0x23bda2[_0xbf40('0x57')]=[{'all':!![]}];}_0x23bda2=_[_0xbf40('0x4e')]({},_0x23bda2,_0xe75ddb[_0xbf40('0x50')]);return db[_0xbf40('0x3a')][_0xbf40('0x58')](_0x23bda2)['then'](handleEntityNotFound(_0x57aa06,null))[_0xbf40('0x20')](respondWithResult(_0x57aa06,null))[_0xbf40('0x53')](handleError(_0x57aa06,null));};exports[_0xbf40('0x59')]=function(_0x39bd93,_0x2d519b){return db[_0xbf40('0x3a')]['create'](_0x39bd93[_0xbf40('0x5a')],{})[_0xbf40('0x20')](respondWithResult(_0x2d519b,0xc9))[_0xbf40('0x53')](handleError(_0x2d519b,null));};exports[_0xbf40('0x32')]=function(_0x52248,_0x45b04f){if(_0x52248[_0xbf40('0x5a')]['id']){delete _0x52248[_0xbf40('0x5a')]['id'];}return db[_0xbf40('0x3a')][_0xbf40('0x58')]({'where':{'id':_0x52248[_0xbf40('0x55')]['id']}})['then'](handleEntityNotFound(_0x45b04f,null))[_0xbf40('0x20')](saveUpdates(_0x52248[_0xbf40('0x5a')],null))[_0xbf40('0x20')](respondWithResult(_0x45b04f,null))[_0xbf40('0x53')](handleError(_0x45b04f,null));};exports['destroy']=function(_0x25bc4e,_0x3c603c){return db[_0xbf40('0x3a')]['find']({'where':{'id':_0x25bc4e['params']['id']}})[_0xbf40('0x20')](handleEntityNotFound(_0x3c603c,null))[_0xbf40('0x20')](removeEntity(_0x3c603c,null))[_0xbf40('0x53')](handleError(_0x3c603c,null));};exports['describe']=function(_0x3162ba,_0x189a4b){return db[_0xbf40('0x3a')][_0xbf40('0x5b')]()['then'](respondWithResult(_0x189a4b,null))[_0xbf40('0x53')](handleError(_0x189a4b,null));};var interaction_log=require(_0xbf40('0x5c'))(_0xbf40('0x5d'));exports['accept']=function(_0x548724,_0x357b6e,_0x9714c6){var _0xb1d76f={'channel':_0xbf40('0x5e')};if(_0x548724[_0xbf40('0x5a')]['id']){delete _0x548724[_0xbf40('0x5a')]['id'];}_0x548724[_0xbf40('0x5a')][_0xbf40('0x5f')]=!![];_0x548724[_0xbf40('0x5a')][_0xbf40('0x60')]=moment()[_0xbf40('0x61')]('YYYY-MM-DD\x20HH:mm:ss');_0x548724['body'][_0xbf40('0x62')]=_0x548724['body'][_0xbf40('0x62')]||_0x548724[_0xbf40('0x63')]['id'];return db[_0xbf40('0x3a')]['find']({'where':{'id':_0x548724[_0xbf40('0x55')]['id'],'UserId':null},'include':[{'model':db[_0xbf40('0x64')],'as':_0xbf40('0x65')}]})[_0xbf40('0x20')](handleEntityNotFound(_0x357b6e,null))['then'](saveUpdates(_0x548724[_0xbf40('0x5a')],null))[_0xbf40('0x20')](function(_0x38e8d1){if(_0x38e8d1){_0xb1d76f[_0xbf40('0x27')]=_0x38e8d1[_0xbf40('0x66')]({'plain':!![]});return db[_0xbf40('0x67')][_0xbf40('0x58')]({'where':{'id':_0x38e8d1['OpenchannelInteractionId']}});}return null;})[_0xbf40('0x20')](handleEntityNotFound(_0x357b6e,null))[_0xbf40('0x20')](function(_0x530823){if(_0x530823){return _0x530823['update']({'UserId':_0x548724['body'][_0xbf40('0x62')],'read1stAt':_[_0xbf40('0x68')](_0x530823[_0xbf40('0x69')])?moment()['format']('YYYY-MM-DD\x20HH:mm:ss'):undefined});}return null;})[_0xbf40('0x20')](function(_0x195d0d){if(_0x195d0d){_0xb1d76f[_0xbf40('0x6a')]=_0x195d0d[_0xbf40('0x66')]({'plain':!![]});interaction_log[_0xbf40('0x28')](_0xbf40('0x6b'),_0x548724[_0xbf40('0x63')]['id'],_0x548724['user']['name'],_0x548724[_0xbf40('0x63')][_0xbf40('0x6c')],_0xb1d76f[_0xbf40('0x6a')]['id'],_0x548724[_0xbf40('0x5a')]?JSON[_0xbf40('0x25')](_0x548724[_0xbf40('0x5a')]):_0xbf40('0x6d'));return respondWithRpcPromise('AcceptMessage',_0xbf40('0x6e'),_0xb1d76f);}return null;})[_0xbf40('0x20')](function(_0x2eb7e0){if(_0x2eb7e0){if(_0x548724[_0xbf40('0x5a')]['manual']){_0xb1d76f[_0xbf40('0x27')][_0xbf40('0x6f')]=util[_0xbf40('0x61')]('SIP/%s',_0x548724[_0xbf40('0x63')][_0xbf40('0x36')]);_0xb1d76f[_0xbf40('0x27')][_0xbf40('0x70')]=_0xb1d76f['channel'];_0xb1d76f['message'][_0xbf40('0x71')]=_0xbf40('0x72');return respondWithRpcPromise('EventManager',_0xbf40('0x73'),{'event':'acceptmessage','message':_0xb1d76f[_0xbf40('0x27')]},client9002)[_0xbf40('0x20')](function(){return _0xb1d76f;});}return _0xb1d76f;}return null;})[_0xbf40('0x20')](respondWithResult(_0x357b6e,null))[_0xbf40('0x53')](handleError(_0x357b6e,null));};var interaction_log=require('../../config/logger')(_0xbf40('0x5d'));exports['reject']=function(_0x77d42a,_0x17df33,_0xe998b9){var _0x282c04={'agent':{},'channel':_0xbf40('0x5e')};if(_0x77d42a['body']['id']){delete _0x77d42a[_0xbf40('0x5a')]['id'];}_0x77d42a[_0xbf40('0x5a')][_0xbf40('0x62')]=_0x77d42a[_0xbf40('0x5a')][_0xbf40('0x62')]||_0x77d42a[_0xbf40('0x63')]['id'];_0x282c04['agent']['id']=_0x77d42a[_0xbf40('0x5a')][_0xbf40('0x62')];return db['OpenchannelMessage']['find']({'where':{'id':_0x77d42a[_0xbf40('0x55')]['id'],'UserId':null}})[_0xbf40('0x20')](handleEntityNotFound(_0x17df33,null))['then'](function(_0x42abed){if(_0x42abed){_0x282c04[_0xbf40('0x27')]=_0x42abed[_0xbf40('0x66')]({'plain':!![]});return db['OpenchannelInteraction'][_0xbf40('0x58')]({'where':{'id':_0x42abed['OpenchannelInteractionId']}});}return null;})[_0xbf40('0x20')](handleEntityNotFound(_0x17df33,null))['then'](function(_0x63c96b){if(_0x63c96b){_0x282c04[_0xbf40('0x6a')]=_0x63c96b[_0xbf40('0x66')]({'plain':!![]});interaction_log[_0xbf40('0x28')]('[OPENCHANNELMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x77d42a[_0xbf40('0x63')]['id'],_0x77d42a[_0xbf40('0x63')][_0xbf40('0x36')],_0x77d42a[_0xbf40('0x63')][_0xbf40('0x6c')],_0x282c04[_0xbf40('0x6a')]['id'],_0x77d42a[_0xbf40('0x5a')]?JSON[_0xbf40('0x25')](_0x77d42a[_0xbf40('0x5a')]):_0xbf40('0x6d'));return respondWithRpcPromise(_0xbf40('0x74'),_0xbf40('0x75'),_0x282c04);}return null;})[_0xbf40('0x20')](function(_0x45d51e){if(_0x45d51e){_0x45d51e[_0xbf40('0x27')][_0xbf40('0x6f')]=util[_0xbf40('0x61')](_0xbf40('0x76'),_0x77d42a[_0xbf40('0x63')][_0xbf40('0x36')]);_0x45d51e[_0xbf40('0x27')]['channel']=_0x282c04[_0xbf40('0x70')];_0x45d51e['message'][_0xbf40('0x71')]='reject';respondWithRpcPromise(_0xbf40('0x73'),'EventManager',{'event':_0xbf40('0x77'),'message':_0x45d51e[_0xbf40('0x27')]},client9002);return _0x282c04;}return null;})['then'](respondWithResult(_0x17df33,null))[_0xbf40('0x53')](handleError(_0x17df33,null));};