f530479017a0ce1a3f9e67877f544c0bf4121f54
[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 _0x93ef=['autoclose','push','close','unmanaged','*,*,*,*','created','FaxMessage','AttachmentId','User','find','name','interaction','UserId','unshift','agent','waitForTheAssignedAgent','FaxQueue','queueId','queue','waitForTheAssignedQueue','Start','startRouting','lodash','util','moment','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','../../config/environment','jayson/promise','client','http','redis','localhost','register','request','then','info','FaxAccount,\x20%s,\x20%s','request\x20sent','FaxAccount,\x20%s,\x20%s,\x20%s','error','code','message','result','catch','FaxAccount','findAll','options','raw','where','limit','include','map','model','attributes','GetFaxAccount','debug','stringify','body','fax','describe','ValidationError','difference','keys','createdAt','updatedAt','CompanyId','ListId','from','from\x20is\x20mandatory','Sequelize','body\x20is\x20mandatory\x20and\x20not\x20empty','isNil','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','account','CmList','List','CmContact','Contacts','DESC','FaxApplication','Applications','get','applications','list','asc','length','defaults','create','contact','FaxInteraction','Contact\x20not\x20found'];(function(_0x122ef9,_0x451d2a){var _0x42fba0=function(_0x17c9d3){while(--_0x17c9d3){_0x122ef9['push'](_0x122ef9['shift']());}};_0x42fba0(++_0x451d2a);}(_0x93ef,0xd8));var _0xf93e=function(_0x38f3a9,_0x67f305){_0x38f3a9=_0x38f3a9-0x0;var _0x1623e6=_0x93ef[_0x38f3a9];return _0x1623e6;};'use strict';var _=require(_0xf93e('0x0'));var util=require(_0xf93e('0x1'));var moment=require(_0xf93e('0x2'));var BPromise=require('bluebird');var rs=require(_0xf93e('0x3'));var fs=require('fs');var Redis=require(_0xf93e('0x4'));var db=require(_0xf93e('0x5'))['db'];var utils=require(_0xf93e('0x6'));var logger=require(_0xf93e('0x7'))('rpc');var config=require(_0xf93e('0x8'));var jayson=require(_0xf93e('0x9'));var client=jayson[_0xf93e('0xa')][_0xf93e('0xb')]({'port':0x232c});config[_0xf93e('0xc')]=_['defaults'](config[_0xf93e('0xc')],{'host':_0xf93e('0xd'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xf93e('0xc')]));require('./faxAccount.socket')[_0xf93e('0xe')](socket);function respondWithRpcPromise(_0x11e242,_0x333b4e,_0x459820){return new BPromise(function(_0x1b0a1f,_0x9e3b2){return client[_0xf93e('0xf')](_0x11e242,_0x459820)[_0xf93e('0x10')](function(_0xa0804a){logger[_0xf93e('0x11')](_0xf93e('0x12'),_0x333b4e,_0xf93e('0x13'));logger['debug'](_0xf93e('0x14'),_0x333b4e,'request\x20sent',JSON['stringify'](_0xa0804a));if(_0xa0804a['error']){if(_0xa0804a[_0xf93e('0x15')][_0xf93e('0x16')]===0x1f4){logger[_0xf93e('0x15')](_0xf93e('0x12'),_0x333b4e,_0xa0804a['error'][_0xf93e('0x17')]);return _0x9e3b2(_0xa0804a[_0xf93e('0x15')][_0xf93e('0x17')]);}logger[_0xf93e('0x15')]('FaxAccount,\x20%s,\x20%s',_0x333b4e,_0xa0804a['error'][_0xf93e('0x17')]);return _0x1b0a1f(_0xa0804a[_0xf93e('0x15')][_0xf93e('0x17')]);}else{logger['info'](_0xf93e('0x12'),_0x333b4e,'request\x20sent');_0x1b0a1f(_0xa0804a[_0xf93e('0x18')][_0xf93e('0x17')]);}})[_0xf93e('0x19')](function(_0x15ed6b){logger[_0xf93e('0x15')](_0xf93e('0x12'),_0x333b4e,_0x15ed6b);_0x9e3b2(_0x15ed6b);});});}exports['GetFaxAccount']=function(_0x35a93b){var _0x1a559b=this;return new Promise(function(_0x187836,_0x2acdd3){return db[_0xf93e('0x1a')][_0xf93e('0x1b')]({'raw':_0x35a93b['options']?_0x35a93b[_0xf93e('0x1c')][_0xf93e('0x1d')]===undefined?!![]:![]:!![],'where':_0x35a93b[_0xf93e('0x1c')]?_0x35a93b['options'][_0xf93e('0x1e')]||null:null,'attributes':_0x35a93b[_0xf93e('0x1c')]?_0x35a93b['options']['attributes']||null:null,'limit':_0x35a93b[_0xf93e('0x1c')]?_0x35a93b[_0xf93e('0x1c')][_0xf93e('0x1f')]||null:null,'include':_0x35a93b[_0xf93e('0x1c')]?_0x35a93b[_0xf93e('0x1c')][_0xf93e('0x20')]?_[_0xf93e('0x21')](_0x35a93b[_0xf93e('0x1c')][_0xf93e('0x20')],function(_0x56abf4){return{'model':db[_0x56abf4[_0xf93e('0x22')]],'as':_0x56abf4['as'],'attributes':_0x56abf4[_0xf93e('0x23')],'include':_0x56abf4[_0xf93e('0x20')]?_[_0xf93e('0x21')](_0x56abf4['include'],function(_0x5b75bb){return{'model':db[_0x5b75bb['model']],'as':_0x5b75bb['as'],'attributes':_0x5b75bb['attributes'],'include':_0x5b75bb[_0xf93e('0x20')]?_[_0xf93e('0x21')](_0x5b75bb['include'],function(_0x183b37){return{'model':db[_0x183b37['model']],'as':_0x183b37['as'],'attributes':_0x183b37[_0xf93e('0x23')]};}):[]};}):[]};}):[]:[]})[_0xf93e('0x10')](function(_0x32b90f){logger[_0xf93e('0x11')](_0xf93e('0x24'),_0x35a93b);logger[_0xf93e('0x25')](_0xf93e('0x24'),_0x35a93b,JSON[_0xf93e('0x26')](_0x32b90f));_0x187836(_0x32b90f);})[_0xf93e('0x19')](function(_0x2f7856){logger[_0xf93e('0x15')](_0xf93e('0x24'),_0x2f7856[_0xf93e('0x17')],_0x35a93b);_0x2acdd3(_0x1a559b[_0xf93e('0x15')](0x1f4,_0x2f7856[_0xf93e('0x17')]));});});};exports['NotifyFaxAccount']=function(_0x5d2696,_0x2d4438,_0x136d08){var _0xe1e315=this;return new Promise(function(_0x119407,_0x5756c1){var _0x520b8f={'body':_0x5d2696[_0xf93e('0x27')],'channel':_0xf93e('0x28')};var _0x295ed0=[];var _0x3ca58b=[];var _0x4d59f8={};return db['CmContact'][_0xf93e('0x29')]()[_0xf93e('0x10')](function(_0x1e6790){if(!_0x1e6790){throw new db['Sequelize'][(_0xf93e('0x2a'))]('no\x20available\x20attributes');}_0x295ed0=_[_0xf93e('0x2b')](_[_0xf93e('0x2c')](_0x1e6790),[_0xf93e('0x2d'),_0xf93e('0x2e')]);_0x3ca58b=_[_0xf93e('0x2b')](_[_0xf93e('0x2c')](_0x1e6790),[_0xf93e('0x2d'),'updatedAt',_0xf93e('0x2f'),_0xf93e('0x30')]);if(_0x5d2696['body']['id']){delete _0x5d2696['body']['id'];}if(_['isNil'](_0x5d2696[_0xf93e('0x27')][_0xf93e('0x31')])){throw new db['Sequelize'][(_0xf93e('0x2a'))](_0xf93e('0x32'));}if(_['isNil'](_0x5d2696[_0xf93e('0x27')]['body'])||_0x5d2696[_0xf93e('0x27')][_0xf93e('0x27')]===''){throw new db[(_0xf93e('0x33'))]['ValidationError'](_0xf93e('0x34'));}if(_[_0xf93e('0x35')](_0x5d2696['body'][_0xf93e('0x36')])){throw new db[(_0xf93e('0x33'))][(_0xf93e('0x2a'))](_0xf93e('0x37')+_0x3ca58b);}if(!_[_0xf93e('0x38')](_0x3ca58b,_0x5d2696[_0xf93e('0x27')][_0xf93e('0x36')])){throw new db[(_0xf93e('0x33'))][(_0xf93e('0x2a'))]('mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x3ca58b);}_0x4d59f8[_0x5d2696['body'][_0xf93e('0x36')]]=_0x5d2696[_0xf93e('0x27')][_0xf93e('0x31')];})['then'](function(){return db['FaxAccount']['find']({'where':{'id':_0x5d2696[_0xf93e('0x39')]['id']},'include':[{'model':db[_0xf93e('0x3a')],'as':_0xf93e('0x3b'),'include':[{'model':db[_0xf93e('0x3c')],'as':_0xf93e('0x3d'),'where':_0x4d59f8,'limit':0x1,'order':[[_0xf93e('0x2e'),_0xf93e('0x3e')]]}]},{'model':db[_0xf93e('0x3f')],'as':_0xf93e('0x40')}]});})['then'](function(_0x3f9957){if(_0x3f9957&&_0x3f9957['List']){var _0x1e9171=_0x3f9957[_0xf93e('0x41')]({'plain':!![]});_0x520b8f['account']=_0x1e9171;_0x520b8f[_0xf93e('0x42')]=_0x1e9171[_0xf93e('0x40')];_0x520b8f[_0xf93e('0x43')]=_0x1e9171[_0xf93e('0x3b')];_0x520b8f[_0xf93e('0x42')]=_['orderBy'](_0x520b8f[_0xf93e('0x42')],['priority'],[_0xf93e('0x44')]);if(_0x520b8f[_0xf93e('0x39')][_0xf93e('0x40')]){delete _0x520b8f[_0xf93e('0x39')][_0xf93e('0x40')];}if(_0x520b8f['account']['List']&&_0x520b8f['account'][_0xf93e('0x3b')][_0xf93e('0x3d')][_0xf93e('0x45')]){return _0x520b8f[_0xf93e('0x39')][_0xf93e('0x3b')][_0xf93e('0x3d')][0x0];}var _0x5a3c01=_[_0xf93e('0x46')](_0x5d2696[_0xf93e('0x27')],{'firstName':_0x5d2696['body']['from'],'ListId':_0x1e9171[_0xf93e('0x30')]});_0x5a3c01[_0x5d2696[_0xf93e('0x27')][_0xf93e('0x36')]]=_0x5d2696[_0xf93e('0x27')][_0xf93e('0x31')];return db[_0xf93e('0x3c')][_0xf93e('0x47')](_0x5a3c01,{'fields':_0x295ed0,'raw':!![]});}})[_0xf93e('0x10')](function(_0x3def7d){_0x520b8f[_0xf93e('0x48')]=_0x3def7d;if(_0x3def7d){var _0x363aa9={'ContactId':_0x3def7d['id'],'FaxAccountId':_0x5d2696[_0xf93e('0x39')]['id'],'fax':_0x3def7d[_0xf93e('0x28')]};return db['FaxInteraction']['find']({'where':{'ContactId':_0x3def7d['id'],'closed':![],'FaxAccountId':_0x5d2696[_0xf93e('0x39')]['id']}})[_0xf93e('0x10')](function(_0x2dc18b){if(_0x2dc18b){return[_0x2dc18b,![]];}return db[_0xf93e('0x49')][_0xf93e('0x47')](_0x363aa9)['then'](function(_0x2aa4ef){return[_0x2aa4ef,!![]];});});}else{throw new Error(_0xf93e('0x4a'));}})['spread'](function(_0x171952,_0x5caf5a){var _0x49b17c=_0x171952[_0xf93e('0x41')]({'plain':!![]});if(_0x5caf5a){if(_0x520b8f[_0xf93e('0x39')][_0xf93e('0x4b')]){_0x520b8f['applications'][_0xf93e('0x4c')]({'id':0x0,'priority':_0x520b8f['applications']['length']+0x1,'app':_0xf93e('0x4d'),'appdata':_0xf93e('0x4e'),'interval':_0xf93e('0x4f')});}_0x49b17c[_0xf93e('0x50')]=!![];}else{_0x49b17c[_0xf93e('0x50')]=![];}_0x520b8f['interaction']=_0x49b17c;return db[_0xf93e('0x51')][_0xf93e('0x47')]({'body':_0x5d2696[_0xf93e('0x27')][_0xf93e('0x27')],'FaxAccountId':_0x5d2696[_0xf93e('0x39')]['id'],'FaxInteractionId':_0x171952['id'],'direction':'in','ContactId':_0x171952['ContactId'],'AttachmentId':_0x5d2696[_0xf93e('0x27')][_0xf93e('0x52')]});})[_0xf93e('0x10')](function(_0x193090){_0x520b8f['message']=_0x193090;if(!_0x520b8f['interaction']['UserId'])return;return db[_0xf93e('0x53')][_0xf93e('0x54')]({'attributes':['id',_0xf93e('0x55')],'where':{'id':_0x520b8f[_0xf93e('0x56')][_0xf93e('0x57')]}});})[_0xf93e('0x10')](function(_0x4afd19){if(_0x4afd19){_0x520b8f[_0xf93e('0x42')][_0xf93e('0x58')]({'id':0x0,'priority':0x0,'app':_0xf93e('0x59'),'appdata':_0x4afd19[_0xf93e('0x55')]+','+(_0x520b8f[_0xf93e('0x39')][_0xf93e('0x5a')]||0xa),'interval':_0xf93e('0x4f')});}if(!_0x520b8f[_0xf93e('0x56')]['queueId'])return;return db[_0xf93e('0x5b')]['find']({'attributes':['id',_0xf93e('0x55')],'where':{'id':_0x520b8f['interaction'][_0xf93e('0x5c')]}});})[_0xf93e('0x10')](function(_0xdd07c9){if(_0xdd07c9){_0x520b8f[_0xf93e('0x42')][_0xf93e('0x58')]({'id':0x0,'priority':0x1,'app':_0xf93e('0x5d'),'appdata':_0xdd07c9[_0xf93e('0x55')]+','+(_0x520b8f['account'][_0xf93e('0x5e')]||0x12c),'interval':_0xf93e('0x4f')});}return respondWithRpcPromise(_0xf93e('0x5f'),_0xf93e('0x60'),_0x520b8f);})[_0xf93e('0x10')](function(_0x5bc44f){_0x119407(_0x5bc44f);})[_0xf93e('0x19')](function(_0x1ddaf6){_0x5756c1(_0xe1e315['error'](0x1f4,_0x1ddaf6[_0xf93e('0x17')]));});});};