a975b3765d6c1ed23c8414e72a85bd33a449e01f
[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 _0x62b6=['unshift','waitForTheAssignedAgent','*,*,*,*','Start','startRouting','lodash','util','moment','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','redis','defaults','localhost','socket.io-emitter','request','then','info','FaxAccount,\x20%s,\x20%s','request\x20sent','debug','FaxAccount,\x20%s,\x20%s,\x20%s','error','code','message','GetFaxAccount','FaxAccount','findAll','options','raw','where','attributes','include','map','model','catch','NotifyFaxAccount','body','fax','describe','Sequelize','ValidationError','keys','createdAt','updatedAt','difference','ListId','isNil','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','from','find','account','CmContact','Contacts','DESC','FaxApplication','Applications','get','applications','priority','asc','List','length','create','contact','FaxInteraction','Contact\x20not\x20found','spread','autoclose','push','close','unmanaged','created','interaction','FaxMessage','ContactId','AttachmentId','UserId','User','name'];(function(_0x2a7d7a,_0x106de5){var _0x53f535=function(_0x42f550){while(--_0x42f550){_0x2a7d7a['push'](_0x2a7d7a['shift']());}};_0x53f535(++_0x106de5);}(_0x62b6,0x171));var _0x662b=function(_0x333b92,_0x5c7551){_0x333b92=_0x333b92-0x0;var _0x117e0e=_0x62b6[_0x333b92];return _0x117e0e;};'use strict';var _=require(_0x662b('0x0'));var util=require(_0x662b('0x1'));var moment=require(_0x662b('0x2'));var BPromise=require('bluebird');var rs=require(_0x662b('0x3'));var fs=require('fs');var Redis=require(_0x662b('0x4'));var db=require(_0x662b('0x5'))['db'];var utils=require(_0x662b('0x6'));var logger=require(_0x662b('0x7'))(_0x662b('0x8'));var config=require(_0x662b('0x9'));var jayson=require(_0x662b('0xa'));var client=jayson[_0x662b('0xb')][_0x662b('0xc')]({'port':0x232c});config[_0x662b('0xd')]=_[_0x662b('0xe')](config[_0x662b('0xd')],{'host':_0x662b('0xf'),'port':0x18eb});var socket=require(_0x662b('0x10'))(new Redis(config[_0x662b('0xd')]));require('./faxAccount.socket')['register'](socket);function respondWithRpcPromise(_0x1805db,_0x302b38,_0x3aeb4f){return new BPromise(function(_0x57d919,_0x5f1fed){return client[_0x662b('0x11')](_0x1805db,_0x3aeb4f)[_0x662b('0x12')](function(_0x48f014){logger[_0x662b('0x13')](_0x662b('0x14'),_0x302b38,_0x662b('0x15'));logger[_0x662b('0x16')](_0x662b('0x17'),_0x302b38,_0x662b('0x15'),JSON['stringify'](_0x48f014));if(_0x48f014[_0x662b('0x18')]){if(_0x48f014[_0x662b('0x18')][_0x662b('0x19')]===0x1f4){logger[_0x662b('0x18')]('FaxAccount,\x20%s,\x20%s',_0x302b38,_0x48f014[_0x662b('0x18')][_0x662b('0x1a')]);return _0x5f1fed(_0x48f014[_0x662b('0x18')][_0x662b('0x1a')]);}logger['error'](_0x662b('0x14'),_0x302b38,_0x48f014[_0x662b('0x18')][_0x662b('0x1a')]);return _0x57d919(_0x48f014[_0x662b('0x18')][_0x662b('0x1a')]);}else{logger['info'](_0x662b('0x14'),_0x302b38,_0x662b('0x15'));_0x57d919(_0x48f014['result'][_0x662b('0x1a')]);}})['catch'](function(_0x1ed923){logger['error'](_0x662b('0x14'),_0x302b38,_0x1ed923);_0x5f1fed(_0x1ed923);});});}exports[_0x662b('0x1b')]=function(_0x333f98){var _0x68cd41=this;return new Promise(function(_0x10cdc4,_0x13b4ab){return db[_0x662b('0x1c')][_0x662b('0x1d')]({'raw':_0x333f98[_0x662b('0x1e')]?_0x333f98[_0x662b('0x1e')][_0x662b('0x1f')]===undefined?!![]:![]:!![],'where':_0x333f98['options']?_0x333f98[_0x662b('0x1e')][_0x662b('0x20')]||null:null,'attributes':_0x333f98['options']?_0x333f98[_0x662b('0x1e')][_0x662b('0x21')]||null:null,'limit':_0x333f98[_0x662b('0x1e')]?_0x333f98['options']['limit']||null:null,'include':_0x333f98[_0x662b('0x1e')]?_0x333f98[_0x662b('0x1e')][_0x662b('0x22')]?_[_0x662b('0x23')](_0x333f98['options']['include'],function(_0x3dd088){return{'model':db[_0x3dd088[_0x662b('0x24')]],'as':_0x3dd088['as'],'attributes':_0x3dd088[_0x662b('0x21')],'include':_0x3dd088[_0x662b('0x22')]?_['map'](_0x3dd088[_0x662b('0x22')],function(_0x4dbe18){return{'model':db[_0x4dbe18[_0x662b('0x24')]],'as':_0x4dbe18['as'],'attributes':_0x4dbe18['attributes'],'include':_0x4dbe18[_0x662b('0x22')]?_[_0x662b('0x23')](_0x4dbe18['include'],function(_0x223d10){return{'model':db[_0x223d10[_0x662b('0x24')]],'as':_0x223d10['as'],'attributes':_0x223d10['attributes']};}):[]};}):[]};}):[]:[]})[_0x662b('0x12')](function(_0x324447){logger[_0x662b('0x13')](_0x662b('0x1b'),_0x333f98);logger[_0x662b('0x16')](_0x662b('0x1b'),_0x333f98,JSON['stringify'](_0x324447));_0x10cdc4(_0x324447);})[_0x662b('0x25')](function(_0x2bffc5){logger[_0x662b('0x18')]('GetFaxAccount',_0x2bffc5[_0x662b('0x1a')],_0x333f98);_0x13b4ab(_0x68cd41[_0x662b('0x18')](0x1f4,_0x2bffc5[_0x662b('0x1a')]));});});};exports[_0x662b('0x26')]=function(_0x7078c6,_0x227c07,_0x5c2390){var _0x17f7ce=this;return new Promise(function(_0xba2326,_0x3afca5){var _0x1730e9={'body':_0x7078c6[_0x662b('0x27')],'channel':_0x662b('0x28')};var _0x3c8955=[];var _0x19a674=[];var _0x231e7f={};return db['CmContact'][_0x662b('0x29')]()['then'](function(_0x55a92c){if(!_0x55a92c){throw new db[(_0x662b('0x2a'))][(_0x662b('0x2b'))]('no\x20available\x20attributes');}_0x3c8955=_['difference'](_[_0x662b('0x2c')](_0x55a92c),[_0x662b('0x2d'),_0x662b('0x2e')]);_0x19a674=_[_0x662b('0x2f')](_[_0x662b('0x2c')](_0x55a92c),[_0x662b('0x2d'),'updatedAt','CompanyId',_0x662b('0x30')]);if(_0x7078c6['body']['id']){delete _0x7078c6[_0x662b('0x27')]['id'];}if(_[_0x662b('0x31')](_0x7078c6[_0x662b('0x27')]['from'])){throw new db[(_0x662b('0x2a'))][(_0x662b('0x2b'))](_0x662b('0x32'));}if(_['isNil'](_0x7078c6[_0x662b('0x27')][_0x662b('0x27')])||_0x7078c6[_0x662b('0x27')][_0x662b('0x27')]===''){throw new db[(_0x662b('0x2a'))]['ValidationError'](_0x662b('0x33'));}if(_[_0x662b('0x31')](_0x7078c6[_0x662b('0x27')][_0x662b('0x34')])){throw new db[(_0x662b('0x2a'))]['ValidationError']('mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x19a674);}if(!_[_0x662b('0x35')](_0x19a674,_0x7078c6['body'][_0x662b('0x34')])){throw new db[(_0x662b('0x2a'))][(_0x662b('0x2b'))](_0x662b('0x36')+_0x19a674);}_0x231e7f[_0x7078c6[_0x662b('0x27')][_0x662b('0x34')]]=_0x7078c6[_0x662b('0x27')][_0x662b('0x37')];})[_0x662b('0x12')](function(){return db[_0x662b('0x1c')][_0x662b('0x38')]({'where':{'id':_0x7078c6[_0x662b('0x39')]['id']},'include':[{'model':db['CmList'],'as':'List','include':[{'model':db[_0x662b('0x3a')],'as':_0x662b('0x3b'),'where':_0x231e7f,'limit':0x1,'order':[[_0x662b('0x2e'),_0x662b('0x3c')]]}]},{'model':db[_0x662b('0x3d')],'as':_0x662b('0x3e')}]});})[_0x662b('0x12')](function(_0x1e7d75){if(_0x1e7d75&&_0x1e7d75['List']){var _0x277723=_0x1e7d75[_0x662b('0x3f')]({'plain':!![]});_0x1730e9[_0x662b('0x39')]=_0x277723;_0x1730e9[_0x662b('0x40')]=_0x277723[_0x662b('0x3e')];_0x1730e9['list']=_0x277723['List'];_0x1730e9['applications']=_['orderBy'](_0x1730e9['applications'],[_0x662b('0x41')],[_0x662b('0x42')]);if(_0x1730e9[_0x662b('0x39')][_0x662b('0x3e')]){delete _0x1730e9[_0x662b('0x39')][_0x662b('0x3e')];}if(_0x1730e9[_0x662b('0x39')]['List']&&_0x1730e9[_0x662b('0x39')][_0x662b('0x43')][_0x662b('0x3b')][_0x662b('0x44')]){return _0x1730e9[_0x662b('0x39')][_0x662b('0x43')]['Contacts'][0x0];}var _0x2f2568=_[_0x662b('0xe')](_0x7078c6['body'],{'firstName':_0x7078c6['body']['from'],'ListId':_0x277723[_0x662b('0x30')]});_0x2f2568[_0x7078c6[_0x662b('0x27')][_0x662b('0x34')]]=_0x7078c6['body']['from'];return db[_0x662b('0x3a')][_0x662b('0x45')](_0x2f2568,{'fields':_0x3c8955,'raw':!![]});}})[_0x662b('0x12')](function(_0x3e7b36){_0x1730e9[_0x662b('0x46')]=_0x3e7b36;if(_0x3e7b36){var _0x2cb9f5={'ContactId':_0x3e7b36['id'],'FaxAccountId':_0x7078c6['account']['id'],'fax':_0x3e7b36[_0x662b('0x28')]};return db[_0x662b('0x47')][_0x662b('0x38')]({'where':{'ContactId':_0x3e7b36['id'],'closed':![],'FaxAccountId':_0x7078c6[_0x662b('0x39')]['id']}})[_0x662b('0x12')](function(_0x2b3aa1){if(_0x2b3aa1){return[_0x2b3aa1,![]];}return db[_0x662b('0x47')][_0x662b('0x45')](_0x2cb9f5)[_0x662b('0x12')](function(_0xaf1d14){return[_0xaf1d14,!![]];});});}else{throw new Error(_0x662b('0x48'));}})[_0x662b('0x49')](function(_0x369357,_0x39eef8){var _0x1efb4d=_0x369357[_0x662b('0x3f')]({'plain':!![]});if(_0x39eef8){if(_0x1730e9['account'][_0x662b('0x4a')]){_0x1730e9[_0x662b('0x40')][_0x662b('0x4b')]({'id':0x0,'priority':_0x1730e9[_0x662b('0x40')][_0x662b('0x44')]+0x1,'app':_0x662b('0x4c'),'appdata':_0x662b('0x4d'),'interval':'*,*,*,*'});}_0x1efb4d[_0x662b('0x4e')]=!![];}else{_0x1efb4d['created']=![];}_0x1730e9[_0x662b('0x4f')]=_0x1efb4d;return db[_0x662b('0x50')][_0x662b('0x45')]({'body':_0x7078c6[_0x662b('0x27')][_0x662b('0x27')],'FaxAccountId':_0x7078c6[_0x662b('0x39')]['id'],'FaxInteractionId':_0x369357['id'],'direction':'in','ContactId':_0x369357[_0x662b('0x51')],'AttachmentId':_0x7078c6[_0x662b('0x27')][_0x662b('0x52')]});})[_0x662b('0x12')](function(_0x49816b){_0x1730e9[_0x662b('0x1a')]=_0x49816b;if(_0x1730e9['interaction'][_0x662b('0x53')]){return db[_0x662b('0x54')]['find']({'attributes':['id',_0x662b('0x55')],'where':{'id':_0x1730e9[_0x662b('0x4f')][_0x662b('0x53')]}})[_0x662b('0x12')](function(_0x2207f8){if(_0x2207f8){_0x1730e9[_0x662b('0x40')][_0x662b('0x56')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x2207f8[_0x662b('0x55')]+','+(_0x1730e9[_0x662b('0x39')][_0x662b('0x57')]||0xa),'interval':_0x662b('0x58')});}return respondWithRpcPromise(_0x662b('0x59'),_0x662b('0x5a'),_0x1730e9);});}return respondWithRpcPromise(_0x662b('0x59'),_0x662b('0x5a'),_0x1730e9);})[_0x662b('0x12')](function(_0x1949af){_0xba2326(_0x1949af);})['catch'](function(_0x4663b2){_0x3afca5(_0x17f7ce[_0x662b('0x18')](0x1f4,_0x4663b2[_0x662b('0x1a')]));});});};