Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / api / faxAccount / faxAccount.rpc.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 _0x5ab4=['Contact\x20not\x20found','autoclose','push','close','unmanaged','*,*,*,*','created','ContactId','interaction','UserId','name','unshift','agent','waitForTheAssignedAgent','queueId','FaxQueue','waitForTheAssignedQueue','startRouting','lodash','moment','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/environment','client','http','redis','defaults','localhost','socket.io-emitter','register','request','FaxAccount,\x20%s,\x20%s','request\x20sent','FaxAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','info','result','catch','GetFaxAccount','findAll','options','raw','where','attributes','limit','include','map','model','then','debug','NotifyFaxAccount','body','fax','CmContact','describe','Sequelize','ValidationError','keys','createdAt','updatedAt','difference','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','FaxAccount','account','CmList','DESC','FaxApplication','Applications','List','get','applications','list','orderBy','priority','Contacts','create','contact','FaxInteraction','find'];(function(_0x53cf8e,_0x3d290f){var _0x459197=function(_0x67073c){while(--_0x67073c){_0x53cf8e['push'](_0x53cf8e['shift']());}};_0x459197(++_0x3d290f);}(_0x5ab4,0x129));var _0x45ab=function(_0x12d8f3,_0x14cbb5){_0x12d8f3=_0x12d8f3-0x0;var _0x2ecabc=_0x5ab4[_0x12d8f3];return _0x2ecabc;};'use strict';var _=require(_0x45ab('0x0'));var util=require('util');var moment=require(_0x45ab('0x1'));var BPromise=require('bluebird');var rs=require(_0x45ab('0x2'));var fs=require('fs');var Redis=require(_0x45ab('0x3'));var db=require(_0x45ab('0x4'))['db'];var utils=require(_0x45ab('0x5'));var logger=require('../../config/logger')('rpc');var config=require(_0x45ab('0x6'));var jayson=require('jayson/promise');var client=jayson[_0x45ab('0x7')][_0x45ab('0x8')]({'port':0x232c});config[_0x45ab('0x9')]=_[_0x45ab('0xa')](config[_0x45ab('0x9')],{'host':_0x45ab('0xb'),'port':0x18eb});var socket=require(_0x45ab('0xc'))(new Redis(config[_0x45ab('0x9')]));require('./faxAccount.socket')[_0x45ab('0xd')](socket);function respondWithRpcPromise(_0x496fa6,_0x470d8f,_0x2d5fea){return new BPromise(function(_0x301374,_0x55a4bd){return client[_0x45ab('0xe')](_0x496fa6,_0x2d5fea)['then'](function(_0x7f0a52){logger['info'](_0x45ab('0xf'),_0x470d8f,_0x45ab('0x10'));logger['debug'](_0x45ab('0x11'),_0x470d8f,'request\x20sent',JSON[_0x45ab('0x12')](_0x7f0a52));if(_0x7f0a52['error']){if(_0x7f0a52[_0x45ab('0x13')][_0x45ab('0x14')]===0x1f4){logger['error']('FaxAccount,\x20%s,\x20%s',_0x470d8f,_0x7f0a52[_0x45ab('0x13')]['message']);return _0x55a4bd(_0x7f0a52[_0x45ab('0x13')][_0x45ab('0x15')]);}logger['error'](_0x45ab('0xf'),_0x470d8f,_0x7f0a52[_0x45ab('0x13')][_0x45ab('0x15')]);return _0x301374(_0x7f0a52[_0x45ab('0x13')]['message']);}else{logger[_0x45ab('0x16')](_0x45ab('0xf'),_0x470d8f,_0x45ab('0x10'));_0x301374(_0x7f0a52[_0x45ab('0x17')][_0x45ab('0x15')]);}})[_0x45ab('0x18')](function(_0x359f99){logger[_0x45ab('0x13')](_0x45ab('0xf'),_0x470d8f,_0x359f99);_0x55a4bd(_0x359f99);});});}exports[_0x45ab('0x19')]=function(_0x13ecc9){var _0x223a0a=this;return new Promise(function(_0x2d07b8,_0x12e2c3){return db['FaxAccount'][_0x45ab('0x1a')]({'raw':_0x13ecc9[_0x45ab('0x1b')]?_0x13ecc9[_0x45ab('0x1b')][_0x45ab('0x1c')]===undefined?!![]:![]:!![],'where':_0x13ecc9[_0x45ab('0x1b')]?_0x13ecc9[_0x45ab('0x1b')][_0x45ab('0x1d')]||null:null,'attributes':_0x13ecc9[_0x45ab('0x1b')]?_0x13ecc9[_0x45ab('0x1b')][_0x45ab('0x1e')]||null:null,'limit':_0x13ecc9['options']?_0x13ecc9['options'][_0x45ab('0x1f')]||null:null,'include':_0x13ecc9[_0x45ab('0x1b')]?_0x13ecc9['options'][_0x45ab('0x20')]?_[_0x45ab('0x21')](_0x13ecc9[_0x45ab('0x1b')][_0x45ab('0x20')],function(_0x3db6c5){return{'model':db[_0x3db6c5[_0x45ab('0x22')]],'as':_0x3db6c5['as'],'attributes':_0x3db6c5[_0x45ab('0x1e')],'include':_0x3db6c5['include']?_[_0x45ab('0x21')](_0x3db6c5[_0x45ab('0x20')],function(_0x12a623){return{'model':db[_0x12a623[_0x45ab('0x22')]],'as':_0x12a623['as'],'attributes':_0x12a623[_0x45ab('0x1e')],'include':_0x12a623[_0x45ab('0x20')]?_[_0x45ab('0x21')](_0x12a623[_0x45ab('0x20')],function(_0x5483bf){return{'model':db[_0x5483bf[_0x45ab('0x22')]],'as':_0x5483bf['as'],'attributes':_0x5483bf[_0x45ab('0x1e')]};}):[]};}):[]};}):[]:[]})[_0x45ab('0x23')](function(_0x1db374){logger[_0x45ab('0x16')]('GetFaxAccount',_0x13ecc9);logger[_0x45ab('0x24')](_0x45ab('0x19'),_0x13ecc9,JSON['stringify'](_0x1db374));_0x2d07b8(_0x1db374);})['catch'](function(_0x543a1e){logger[_0x45ab('0x13')](_0x45ab('0x19'),_0x543a1e[_0x45ab('0x15')],_0x13ecc9);_0x12e2c3(_0x223a0a[_0x45ab('0x13')](0x1f4,_0x543a1e['message']));});});};exports[_0x45ab('0x25')]=function(_0x593aef,_0xf5738d,_0x295476){var _0x4ff95f=this;return new Promise(function(_0x47b1fc,_0x39ea28){var _0x4ce25b={'body':_0x593aef[_0x45ab('0x26')],'channel':_0x45ab('0x27')};var _0x26f17d=[];var _0x5790b5=[];var _0x156b57={};return db[_0x45ab('0x28')][_0x45ab('0x29')]()[_0x45ab('0x23')](function(_0x42aec5){if(!_0x42aec5){throw new db[(_0x45ab('0x2a'))][(_0x45ab('0x2b'))]('no\x20available\x20attributes');}_0x26f17d=_['difference'](_[_0x45ab('0x2c')](_0x42aec5),[_0x45ab('0x2d'),_0x45ab('0x2e')]);_0x5790b5=_[_0x45ab('0x2f')](_[_0x45ab('0x2c')](_0x42aec5),[_0x45ab('0x2d'),_0x45ab('0x2e'),_0x45ab('0x30'),_0x45ab('0x31')]);if(_0x593aef['body']['id']){delete _0x593aef[_0x45ab('0x26')]['id'];}if(_[_0x45ab('0x32')](_0x593aef['body'][_0x45ab('0x33')])){throw new db[(_0x45ab('0x2a'))]['ValidationError'](_0x45ab('0x34'));}if(_[_0x45ab('0x32')](_0x593aef[_0x45ab('0x26')]['body'])||_0x593aef[_0x45ab('0x26')]['body']===''){throw new db[(_0x45ab('0x2a'))][(_0x45ab('0x2b'))](_0x45ab('0x35'));}if(_[_0x45ab('0x32')](_0x593aef[_0x45ab('0x26')]['mapKey'])){throw new db['Sequelize'][(_0x45ab('0x2b'))](_0x45ab('0x36')+_0x5790b5);}if(!_[_0x45ab('0x37')](_0x5790b5,_0x593aef[_0x45ab('0x26')][_0x45ab('0x38')])){throw new db[(_0x45ab('0x2a'))][(_0x45ab('0x2b'))](_0x45ab('0x39')+_0x5790b5);}_0x156b57[_0x593aef[_0x45ab('0x26')][_0x45ab('0x38')]]=_0x593aef[_0x45ab('0x26')][_0x45ab('0x33')];})[_0x45ab('0x23')](function(){return db[_0x45ab('0x3a')]['find']({'where':{'id':_0x593aef[_0x45ab('0x3b')]['id']},'include':[{'model':db[_0x45ab('0x3c')],'as':'List','include':[{'model':db['CmContact'],'as':'Contacts','where':_0x156b57,'limit':0x1,'order':[[_0x45ab('0x2e'),_0x45ab('0x3d')]]}]},{'model':db[_0x45ab('0x3e')],'as':_0x45ab('0x3f')}]});})[_0x45ab('0x23')](function(_0x2c2e3b){if(_0x2c2e3b&&_0x2c2e3b[_0x45ab('0x40')]){var _0x2ddf2b=_0x2c2e3b[_0x45ab('0x41')]({'plain':!![]});_0x4ce25b[_0x45ab('0x3b')]=_0x2ddf2b;_0x4ce25b[_0x45ab('0x42')]=_0x2ddf2b['Applications'];_0x4ce25b[_0x45ab('0x43')]=_0x2ddf2b[_0x45ab('0x40')];_0x4ce25b[_0x45ab('0x42')]=_[_0x45ab('0x44')](_0x4ce25b[_0x45ab('0x42')],[_0x45ab('0x45')],['asc']);if(_0x4ce25b[_0x45ab('0x3b')][_0x45ab('0x3f')]){delete _0x4ce25b[_0x45ab('0x3b')]['Applications'];}if(_0x4ce25b[_0x45ab('0x3b')][_0x45ab('0x40')]&&_0x4ce25b[_0x45ab('0x3b')][_0x45ab('0x40')]['Contacts']['length']){return _0x4ce25b[_0x45ab('0x3b')][_0x45ab('0x40')][_0x45ab('0x46')][0x0];}var _0x533223=_[_0x45ab('0xa')](_0x593aef[_0x45ab('0x26')],{'firstName':_0x593aef['body']['from'],'ListId':_0x2ddf2b[_0x45ab('0x31')]});_0x533223[_0x593aef['body'][_0x45ab('0x38')]]=_0x593aef['body'][_0x45ab('0x33')];return db[_0x45ab('0x28')][_0x45ab('0x47')](_0x533223,{'fields':_0x26f17d,'raw':!![]});}})['then'](function(_0x33c71a){_0x4ce25b[_0x45ab('0x48')]=_0x33c71a;if(_0x33c71a){var _0x53d7d8={'ContactId':_0x33c71a['id'],'FaxAccountId':_0x593aef[_0x45ab('0x3b')]['id'],'fax':_0x33c71a[_0x45ab('0x27')]};return db[_0x45ab('0x49')][_0x45ab('0x4a')]({'where':{'ContactId':_0x33c71a['id'],'closed':![],'FaxAccountId':_0x593aef[_0x45ab('0x3b')]['id']}})['then'](function(_0xb9a103){if(_0xb9a103){return[_0xb9a103,![]];}return db['FaxInteraction'][_0x45ab('0x47')](_0x53d7d8)[_0x45ab('0x23')](function(_0x331b42){return[_0x331b42,!![]];});});}else{throw new Error(_0x45ab('0x4b'));}})['spread'](function(_0x23acbc,_0x249aa2){var _0xa5f805=_0x23acbc[_0x45ab('0x41')]({'plain':!![]});if(_0x249aa2){if(_0x4ce25b[_0x45ab('0x3b')][_0x45ab('0x4c')]){_0x4ce25b[_0x45ab('0x42')][_0x45ab('0x4d')]({'id':0x0,'priority':_0x4ce25b[_0x45ab('0x42')]['length']+0x1,'app':_0x45ab('0x4e'),'appdata':_0x45ab('0x4f'),'interval':_0x45ab('0x50')});}_0xa5f805[_0x45ab('0x51')]=!![];}else{_0xa5f805['created']=![];}_0x4ce25b['interaction']=_0xa5f805;return db['FaxMessage'][_0x45ab('0x47')]({'body':_0x593aef[_0x45ab('0x26')]['body'],'FaxAccountId':_0x593aef['account']['id'],'FaxInteractionId':_0x23acbc['id'],'direction':'in','ContactId':_0x23acbc[_0x45ab('0x52')],'AttachmentId':_0x593aef[_0x45ab('0x26')]['AttachmentId']});})[_0x45ab('0x23')](function(_0x13a23c){_0x4ce25b[_0x45ab('0x15')]=_0x13a23c;if(!_0x4ce25b[_0x45ab('0x53')][_0x45ab('0x54')])return;return db['User'][_0x45ab('0x4a')]({'attributes':['id',_0x45ab('0x55')],'where':{'id':_0x4ce25b['interaction']['UserId']}});})[_0x45ab('0x23')](function(_0x27268f){if(_0x27268f){_0x4ce25b[_0x45ab('0x42')][_0x45ab('0x56')]({'id':0x0,'priority':0x0,'app':_0x45ab('0x57'),'appdata':_0x27268f['name']+','+(_0x4ce25b['account'][_0x45ab('0x58')]||0xa),'interval':_0x45ab('0x50')});}if(!_0x4ce25b[_0x45ab('0x53')][_0x45ab('0x59')])return;return db[_0x45ab('0x5a')][_0x45ab('0x4a')]({'attributes':['id',_0x45ab('0x55')],'where':{'id':_0x4ce25b[_0x45ab('0x53')][_0x45ab('0x59')]}});})[_0x45ab('0x23')](function(_0x5cd2c6){if(_0x5cd2c6){_0x4ce25b[_0x45ab('0x42')][_0x45ab('0x56')]({'id':0x0,'priority':0x1,'app':'queue','appdata':_0x5cd2c6['name']+','+(_0x4ce25b['account'][_0x45ab('0x5b')]||0x12c),'interval':_0x45ab('0x50')});}return respondWithRpcPromise('Start',_0x45ab('0x5c'),_0x4ce25b);})[_0x45ab('0x23')](function(_0x4b6075){_0x47b1fc(_0x4b6075);})[_0x45ab('0x18')](function(_0x48d9e7){_0x39ea28(_0x4ff95f[_0x45ab('0x13')](0x1f4,_0x48d9e7[_0x45ab('0x15')]));});});};