Built motion from commit (unavailable).|2.5.11
[motion2.git] / server / api / faxMessage / faxMessage.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 _0x10d8=['api','../../config/utils','../../config/environment','../../mysqldb','redis','socket.io-emitter','register','jayson/promise','client','http','request','then','info','FaxMessage,\x20%s,\x20%s','request\x20sent','debug','stringify','error','message','result','catch','sendStatus','status','end','json','count','offset','set','apply','destroy','stack','index','map','FaxMessage','rawAttributes','fieldName','key','model','name','keys','query','filters','attributes','fields','hasOwnProperty','nolimit','limit','order','sort','where','pick','filter','merge','type','includeAll','include','findAll','rows','show','params','intersection','options','update','body','find','describe','fax-interactions','accept','fax','read','readAt','format','YYYY-MM-DD\x20HH:mm:ss','UserId','user','CmContact','get','isNil','read1stAt','interaction','[FAXMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','null','acceptMessage','interface','channel','event','EventManager','acceptmessage','reject','agent','FaxInteraction','FaxInteractionId','[FAXMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','RejectMessage','rejectMessage','SIP/%s','rejectmessage','create','findOne','SendFax','FaxAccountId','basename','root','server','files','attachments','Attachment','join','html-pdf','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','sox','to-csv','ejs','lodash','squel','crypto','jsforce','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger'];(function(_0x283284,_0x5cf98d){var _0xe6cd06=function(_0x5da4ac){while(--_0x5da4ac){_0x283284['push'](_0x283284['shift']());}};_0xe6cd06(++_0x5cf98d);}(_0x10d8,0xee));var _0x810d=function(_0x2af28f,_0x2357b8){_0x2af28f=_0x2af28f-0x0;var _0x175020=_0x10d8[_0x2af28f];return _0x175020;};'use strict';var pdf=require(_0x810d('0x0'));var emlformat=require(_0x810d('0x1'));var rimraf=require(_0x810d('0x2'));var zipdir=require('zip-dir');var jsonpatch=require(_0x810d('0x3'));var rp=require(_0x810d('0x4'));var moment=require(_0x810d('0x5'));var BPromise=require(_0x810d('0x6'));var Mustache=require(_0x810d('0x7'));var util=require('util');var path=require('path');var sox=require(_0x810d('0x8'));var csv=require(_0x810d('0x9'));var ejs=require(_0x810d('0xa'));var fs=require('fs');var _=require(_0x810d('0xb'));var squel=require(_0x810d('0xc'));var crypto=require(_0x810d('0xd'));var jsforce=require(_0x810d('0xe'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0x810d('0xf'));var authService=require(_0x810d('0x10'));var qs=require(_0x810d('0x11'));var as=require(_0x810d('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x810d('0x13'))(_0x810d('0x14'));var utils=require(_0x810d('0x15'));var config=require(_0x810d('0x16'));var licenseUtil=require('../../config/license/util');var db=require(_0x810d('0x17'))['db'];config['redis']=_['defaults'](config[_0x810d('0x18')],{'host':'localhost','port':0x18eb});var socket=require(_0x810d('0x19'))(new Redis(config[_0x810d('0x18')]));require('./faxMessage.socket')[_0x810d('0x1a')](socket);var jayson=require(_0x810d('0x1b'));var client=jayson[_0x810d('0x1c')][_0x810d('0x1d')]({'port':0x232c});var client9002=jayson[_0x810d('0x1c')]['http']({'port':0x232a});function respondWithRpcPromise(_0x522d6d,_0x13b6b8,_0x5e8328,_0x2a4907){return new BPromise(function(_0x290563,_0x31b3e6){var _0x181d0a=_0x2a4907||client;return _0x181d0a[_0x810d('0x1e')](_0x522d6d,_0x5e8328)[_0x810d('0x1f')](function(_0x576238){logger[_0x810d('0x20')](_0x810d('0x21'),_0x13b6b8,_0x810d('0x22'));logger[_0x810d('0x23')]('FaxMessage,\x20%s,\x20%s,\x20%s',_0x13b6b8,_0x810d('0x22'),JSON[_0x810d('0x24')](_0x576238));if(_0x576238[_0x810d('0x25')]){if(_0x576238[_0x810d('0x25')]['code']===0x1f4){logger[_0x810d('0x25')](_0x810d('0x21'),_0x13b6b8,_0x576238[_0x810d('0x25')][_0x810d('0x26')]);return _0x31b3e6(_0x576238[_0x810d('0x25')][_0x810d('0x26')]);}logger[_0x810d('0x25')](_0x810d('0x21'),_0x13b6b8,_0x576238['error'][_0x810d('0x26')]);return _0x290563(_0x576238[_0x810d('0x25')][_0x810d('0x26')]);}else{logger[_0x810d('0x20')](_0x810d('0x21'),_0x13b6b8,_0x810d('0x22'));_0x290563(_0x576238[_0x810d('0x27')]['message']);}})[_0x810d('0x28')](function(_0x314652){logger['error']('FaxMessage,\x20%s,\x20%s',_0x13b6b8,_0x314652);_0x31b3e6(_0x314652);});});}function respondWithStatusCode(_0xb7ecbc,_0x38638a){_0x38638a=_0x38638a||0xcc;return function(_0x5a6409){if(_0x5a6409){return _0xb7ecbc[_0x810d('0x29')](_0x38638a);}return _0xb7ecbc[_0x810d('0x2a')](_0x38638a)[_0x810d('0x2b')]();};}function respondWithResult(_0x5135eb,_0x263bfb){_0x263bfb=_0x263bfb||0xc8;return function(_0x47f4aa){if(_0x47f4aa){return _0x5135eb[_0x810d('0x2a')](_0x263bfb)[_0x810d('0x2c')](_0x47f4aa);}};}function respondWithFilteredResult(_0x58c2fa,_0x1aa205){return function(_0x53f5f9){if(_0x53f5f9){var _0x12a6d8=_0x53f5f9[_0x810d('0x2d')],_0x5c9108=_0x1aa205[_0x810d('0x2e')],_0x3464f0=_0x1aa205['offset']+_0x1aa205['limit'],_0x1d789a;if(_0x3464f0>=_0x12a6d8){_0x3464f0=_0x12a6d8;_0x1d789a=0xc8;}else{_0x1d789a=0xce;}_0x58c2fa[_0x810d('0x2a')](_0x1d789a);return _0x58c2fa[_0x810d('0x2f')]('Content-Range',_0x5c9108+'-'+_0x3464f0+'/'+_0x12a6d8)[_0x810d('0x2c')](_0x53f5f9);}return null;};}function patchUpdates(_0xb939ed){return function(_0x3a5f81){try{jsonpatch[_0x810d('0x30')](_0x3a5f81,_0xb939ed,!![]);}catch(_0x1824a0){return BPromise['reject'](_0x1824a0);}return _0x3a5f81['save']();};}function saveUpdates(_0x1a50f3,_0x3dd99e){return function(_0x3db84d){if(_0x3db84d){return _0x3db84d['update'](_0x1a50f3)[_0x810d('0x1f')](function(_0x4b60de){return _0x4b60de;});}return null;};}function removeEntity(_0xae91fc,_0x26c715){return function(_0x10044d){if(_0x10044d){return _0x10044d[_0x810d('0x31')]()[_0x810d('0x1f')](function(){_0xae91fc[_0x810d('0x2a')](0xcc)[_0x810d('0x2b')]();});}};}function handleEntityNotFound(_0x27f622,_0x204479){return function(_0x2fa9fd){if(!_0x2fa9fd){_0x27f622['sendStatus'](0x194);}return _0x2fa9fd;};}function handleError(_0xcca3d1,_0x56662b){_0x56662b=_0x56662b||0x1f4;return function(_0x2889c0){logger[_0x810d('0x25')](_0x2889c0[_0x810d('0x32')]);if(_0x2889c0['name']){delete _0x2889c0['name'];}_0xcca3d1[_0x810d('0x2a')](_0x56662b)['send'](_0x2889c0);};}exports[_0x810d('0x33')]=function(_0x543b2d,_0x1809ff){var _0x676699={},_0x27f133={},_0x50d8c6={'count':0x0,'rows':[]};var _0x28650a=_[_0x810d('0x34')](db[_0x810d('0x35')][_0x810d('0x36')],function(_0x39fa0b){return{'name':_0x39fa0b[_0x810d('0x37')],'type':_0x39fa0b['type'][_0x810d('0x38')]};});_0x27f133[_0x810d('0x39')]=_[_0x810d('0x34')](_0x28650a,_0x810d('0x3a'));_0x27f133['query']=_[_0x810d('0x3b')](_0x543b2d[_0x810d('0x3c')]);_0x27f133[_0x810d('0x3d')]=_['intersection'](_0x27f133[_0x810d('0x39')],_0x27f133[_0x810d('0x3c')]);_0x676699[_0x810d('0x3e')]=_['intersection'](_0x27f133['model'],qs[_0x810d('0x3f')](_0x543b2d[_0x810d('0x3c')]['fields']));_0x676699[_0x810d('0x3e')]=_0x676699[_0x810d('0x3e')]['length']?_0x676699['attributes']:_0x27f133[_0x810d('0x39')];if(!_0x543b2d[_0x810d('0x3c')][_0x810d('0x40')](_0x810d('0x41'))){_0x676699[_0x810d('0x42')]=qs[_0x810d('0x42')](_0x543b2d[_0x810d('0x3c')]['limit']);_0x676699['offset']=qs[_0x810d('0x2e')](_0x543b2d[_0x810d('0x3c')][_0x810d('0x2e')]);}_0x676699[_0x810d('0x43')]=qs['sort'](_0x543b2d[_0x810d('0x3c')][_0x810d('0x44')]);_0x676699[_0x810d('0x45')]=qs[_0x810d('0x3d')](_[_0x810d('0x46')](_0x543b2d[_0x810d('0x3c')],_0x27f133['filters']),_0x28650a);if(_0x543b2d[_0x810d('0x3c')][_0x810d('0x47')]){_0x676699[_0x810d('0x45')]=_[_0x810d('0x48')](_0x676699[_0x810d('0x45')],{'$or':_[_0x810d('0x34')](_0x28650a,function(_0x3eaa86){if(_0x3eaa86[_0x810d('0x49')]!=='VIRTUAL'){var _0x2e3781={};_0x2e3781[_0x3eaa86[_0x810d('0x3a')]]={'$like':'%'+_0x543b2d['query'][_0x810d('0x47')]+'%'};return _0x2e3781;}})});}_0x676699=_[_0x810d('0x48')]({},_0x676699,_0x543b2d['options']);var _0x5136ad={'where':_0x676699['where']};return db[_0x810d('0x35')][_0x810d('0x2d')](_0x5136ad)['then'](function(_0x2f3f50){_0x50d8c6[_0x810d('0x2d')]=_0x2f3f50;if(_0x543b2d[_0x810d('0x3c')][_0x810d('0x4a')]){_0x676699[_0x810d('0x4b')]=[{'all':!![]}];}return db[_0x810d('0x35')][_0x810d('0x4c')](_0x676699);})['then'](function(_0x3d2938){_0x50d8c6[_0x810d('0x4d')]=_0x3d2938;return _0x50d8c6;})['then'](respondWithFilteredResult(_0x1809ff,_0x676699))['catch'](handleError(_0x1809ff,null));};exports[_0x810d('0x4e')]=function(_0x1e64e8,_0x4de899){var _0x2b4b80={'raw':!![],'where':{'id':_0x1e64e8[_0x810d('0x4f')]['id']}},_0x1cbede={};_0x1cbede[_0x810d('0x39')]=_['keys'](db[_0x810d('0x35')][_0x810d('0x36')]);_0x1cbede['query']=_[_0x810d('0x3b')](_0x1e64e8[_0x810d('0x3c')]);_0x1cbede[_0x810d('0x3d')]=_[_0x810d('0x50')](_0x1cbede[_0x810d('0x39')],_0x1cbede['query']);_0x2b4b80['attributes']=_[_0x810d('0x50')](_0x1cbede[_0x810d('0x39')],qs[_0x810d('0x3f')](_0x1e64e8[_0x810d('0x3c')][_0x810d('0x3f')]));_0x2b4b80['attributes']=_0x2b4b80[_0x810d('0x3e')]['length']?_0x2b4b80[_0x810d('0x3e')]:_0x1cbede['model'];if(_0x1e64e8['query']['includeAll']){_0x2b4b80[_0x810d('0x4b')]=[{'all':!![]}];}_0x2b4b80=_[_0x810d('0x48')]({},_0x2b4b80,_0x1e64e8[_0x810d('0x51')]);return db['FaxMessage']['find'](_0x2b4b80)[_0x810d('0x1f')](handleEntityNotFound(_0x4de899,null))[_0x810d('0x1f')](respondWithResult(_0x4de899,null))[_0x810d('0x28')](handleError(_0x4de899,null));};exports[_0x810d('0x52')]=function(_0x2087dc,_0x45e4dc){if(_0x2087dc[_0x810d('0x53')]['id']){delete _0x2087dc[_0x810d('0x53')]['id'];}return db[_0x810d('0x35')][_0x810d('0x54')]({'where':{'id':_0x2087dc[_0x810d('0x4f')]['id']}})[_0x810d('0x1f')](handleEntityNotFound(_0x45e4dc,null))[_0x810d('0x1f')](saveUpdates(_0x2087dc[_0x810d('0x53')],null))[_0x810d('0x1f')](respondWithResult(_0x45e4dc,null))[_0x810d('0x28')](handleError(_0x45e4dc,null));};exports[_0x810d('0x31')]=function(_0x218d1d,_0x2b8b3f){return db['FaxMessage'][_0x810d('0x54')]({'where':{'id':_0x218d1d['params']['id']}})[_0x810d('0x1f')](handleEntityNotFound(_0x2b8b3f,null))['then'](removeEntity(_0x2b8b3f,null))[_0x810d('0x28')](handleError(_0x2b8b3f,null));};exports[_0x810d('0x55')]=function(_0x249468,_0x70bd68){return db[_0x810d('0x35')][_0x810d('0x55')]()[_0x810d('0x1f')](respondWithResult(_0x70bd68,null))[_0x810d('0x28')](handleError(_0x70bd68,null));};var interaction_log=require(_0x810d('0x13'))(_0x810d('0x56'));exports[_0x810d('0x57')]=function(_0x2b5eb9,_0x11468b,_0x25482d){var _0x292bc7={'channel':_0x810d('0x58')};if(_0x2b5eb9['body']['id']){delete _0x2b5eb9[_0x810d('0x53')]['id'];}_0x2b5eb9[_0x810d('0x53')][_0x810d('0x59')]=!![];_0x2b5eb9[_0x810d('0x53')][_0x810d('0x5a')]=moment()[_0x810d('0x5b')](_0x810d('0x5c'));_0x2b5eb9[_0x810d('0x53')][_0x810d('0x5d')]=_0x2b5eb9[_0x810d('0x53')]['UserId']||_0x2b5eb9[_0x810d('0x5e')]['id'];return db['FaxMessage'][_0x810d('0x54')]({'where':{'id':_0x2b5eb9[_0x810d('0x4f')]['id'],'UserId':null},'include':[{'model':db[_0x810d('0x5f')],'as':'Contact'}]})[_0x810d('0x1f')](handleEntityNotFound(_0x11468b,null))[_0x810d('0x1f')](saveUpdates(_0x2b5eb9[_0x810d('0x53')],null))[_0x810d('0x1f')](function(_0xbe48a6){if(_0xbe48a6){_0x292bc7[_0x810d('0x26')]=_0xbe48a6[_0x810d('0x60')]({'plain':!![]});return db['FaxInteraction'][_0x810d('0x54')]({'where':{'id':_0xbe48a6['FaxInteractionId']}});}return null;})[_0x810d('0x1f')](handleEntityNotFound(_0x11468b,null))['then'](function(_0x3f46a0){if(_0x3f46a0){return _0x3f46a0[_0x810d('0x52')]({'UserId':_0x2b5eb9[_0x810d('0x53')][_0x810d('0x5d')],'read1stAt':_[_0x810d('0x61')](_0x3f46a0[_0x810d('0x62')])?moment()[_0x810d('0x5b')](_0x810d('0x5c')):undefined});}return null;})['then'](function(_0x550551){if(_0x550551){_0x292bc7[_0x810d('0x63')]=_0x550551[_0x810d('0x60')]({'plain':!![]});interaction_log['info'](_0x810d('0x64'),_0x2b5eb9[_0x810d('0x5e')]['id'],_0x2b5eb9[_0x810d('0x5e')][_0x810d('0x3a')],_0x2b5eb9[_0x810d('0x5e')][_0x810d('0x65')],_0x292bc7['interaction']['id'],_0x2b5eb9[_0x810d('0x53')]?JSON[_0x810d('0x24')](_0x2b5eb9[_0x810d('0x53')]):_0x810d('0x66'));return respondWithRpcPromise('AcceptMessage',_0x810d('0x67'),_0x292bc7);}return null;})[_0x810d('0x1f')](function(_0x11a765){if(_0x11a765){if(_0x2b5eb9[_0x810d('0x53')]['manual']){_0x292bc7['message'][_0x810d('0x68')]=util['format']('SIP/%s',_0x2b5eb9[_0x810d('0x5e')]['name']);_0x292bc7[_0x810d('0x26')][_0x810d('0x69')]=_0x292bc7['channel'];_0x292bc7['message'][_0x810d('0x6a')]='accept';return respondWithRpcPromise(_0x810d('0x6b'),_0x810d('0x6b'),{'event':_0x810d('0x6c'),'message':_0x292bc7['message']},client9002)['then'](function(){return _0x292bc7;});}return _0x292bc7;}return null;})[_0x810d('0x1f')](respondWithResult(_0x11468b,null))[_0x810d('0x28')](handleError(_0x11468b,null));};var interaction_log=require(_0x810d('0x13'))(_0x810d('0x56'));exports[_0x810d('0x6d')]=function(_0x904f8d,_0x186ebc,_0x2d108f){var _0x43fddd={'agent':{},'channel':'fax'};if(_0x904f8d[_0x810d('0x53')]['id']){delete _0x904f8d[_0x810d('0x53')]['id'];}_0x904f8d[_0x810d('0x53')][_0x810d('0x5d')]=_0x904f8d['body']['UserId']||_0x904f8d[_0x810d('0x5e')]['id'];_0x43fddd[_0x810d('0x6e')]['id']=_0x904f8d[_0x810d('0x53')][_0x810d('0x5d')];return db['FaxMessage'][_0x810d('0x54')]({'where':{'id':_0x904f8d[_0x810d('0x4f')]['id'],'UserId':null}})[_0x810d('0x1f')](handleEntityNotFound(_0x186ebc,null))['then'](function(_0x2f41ae){if(_0x2f41ae){_0x43fddd[_0x810d('0x26')]=_0x2f41ae[_0x810d('0x60')]({'plain':!![]});return db[_0x810d('0x6f')][_0x810d('0x54')]({'where':{'id':_0x2f41ae[_0x810d('0x70')]}});}return null;})['then'](handleEntityNotFound(_0x186ebc,null))['then'](function(_0x3ba140){if(_0x3ba140){_0x43fddd[_0x810d('0x63')]=_0x3ba140[_0x810d('0x60')]({'plain':!![]});interaction_log[_0x810d('0x20')](_0x810d('0x71'),_0x904f8d[_0x810d('0x5e')]['id'],_0x904f8d[_0x810d('0x5e')][_0x810d('0x3a')],_0x904f8d[_0x810d('0x5e')][_0x810d('0x65')],_0x43fddd[_0x810d('0x63')]['id'],_0x904f8d['body']?JSON[_0x810d('0x24')](_0x904f8d[_0x810d('0x53')]):_0x810d('0x66'));return respondWithRpcPromise(_0x810d('0x72'),_0x810d('0x73'),_0x43fddd);}return null;})[_0x810d('0x1f')](function(_0x5d0be0){if(_0x5d0be0){_0x5d0be0['message'][_0x810d('0x68')]=util['format'](_0x810d('0x74'),_0x904f8d[_0x810d('0x5e')][_0x810d('0x3a')]);_0x5d0be0[_0x810d('0x26')][_0x810d('0x69')]=_0x43fddd[_0x810d('0x69')];_0x5d0be0[_0x810d('0x26')][_0x810d('0x6a')]=_0x810d('0x6d');respondWithRpcPromise('EventManager',_0x810d('0x6b'),{'event':_0x810d('0x75'),'message':_0x5d0be0[_0x810d('0x26')]},client9002);return _0x43fddd;}return null;})[_0x810d('0x1f')](respondWithResult(_0x186ebc,null))[_0x810d('0x28')](handleError(_0x186ebc,null));};exports[_0x810d('0x76')]=function(_0x23d1e9,_0x2f6bb7){var _0x2d452a;return db['FaxMessage'][_0x810d('0x76')](_0x23d1e9[_0x810d('0x53')],{})[_0x810d('0x1f')](function(_0x3b5bc6){_0x2d452a=_0x3b5bc6;return db[_0x810d('0x5f')][_0x810d('0x77')]({'where':{'id':_0x2d452a['ContactId']}});})['then'](function(_0x3189be){if(_[_0x810d('0x61')](_0x3189be)){return;}else{_0x2d452a[_0x810d('0x58')]=_0x3189be[_0x810d('0x58')];return db['Attachment']['findOne']({'where':{'id':_0x2d452a['AttachmentId']}});}})[_0x810d('0x1f')](function(_0x31dc54){return respondWithRpcPromise(_0x810d('0x78'),_0x810d('0x78'),{'FaxAccountId':_[_0x810d('0x61')](_0x23d1e9[_0x810d('0x53')])?undefined:_0x23d1e9['body'][_0x810d('0x79')],'path':_['isNil'](_0x31dc54)?undefined:util[_0x810d('0x5b')]('/var/opt/motion2/server/files/attachments/%s',_0x31dc54[_0x810d('0x7a')]),'fax':_['isNil'](_0x2d452a[_0x810d('0x58')])?undefined:_0x2d452a[_0x810d('0x58')],'AttachmentId':_0x2d452a['AttachmentId']},client9002);})[_0x810d('0x1f')](respondWithResult(_0x2f6bb7,0xc9))[_0x810d('0x28')](handleError(_0x2f6bb7,null));};exports['download']=function(_0x461ba2,_0x449e7c){var _0x262cf2=path['join'](config[_0x810d('0x7b')],_0x810d('0x7c'),_0x810d('0x7d'),_0x810d('0x7e'));return db['FaxMessage'][_0x810d('0x54')]({'where':{'id':_0x461ba2[_0x810d('0x4f')]['id']},'include':[{'model':db['Attachment'],'as':_0x810d('0x7f')}]})[_0x810d('0x1f')](handleEntityNotFound(_0x449e7c,null))['then'](function(_0x47bd53){if(_0x47bd53){return _0x449e7c['download'](path[_0x810d('0x80')](_0x262cf2,_0x47bd53[_0x810d('0x7f')][_0x810d('0x7a')]));}})['catch'](handleError(_0x449e7c,null));};