e26ef426ad93f6e73ea11cf294454be8154cecf9
[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 _0x2887=['Sequelize','ValidationError','no\x20available\x20attributes','difference','keys','updatedAt','createdAt','ListId','from','from\x20is\x20mandatory','isNil','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','find','account','List','DESC','FaxApplication','Applications','applications','list','priority','asc','Contacts','length','create','contact','fax','FaxInteraction','Contact\x20not\x20found','spread','get','autoclose','push','*,*,*,*','created','interaction','ContactId','AttachmentId','User','unshift','agent','name','Start','startRouting','catch','lodash','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/logger','../../config/environment','jayson/promise','http','redis','defaults','socket.io-emitter','./faxAccount.socket','register','request','info','debug','FaxAccount,\x20%s,\x20%s,\x20%s','stringify','error','FaxAccount,\x20%s,\x20%s','message','request\x20sent','result','FaxAccount','options','where','limit','include','model','attributes','map','GetFaxAccount','NotifyFaxAccount','body','CmContact','describe','then'];(function(_0x8f4c15,_0x571506){var _0x3d8837=function(_0x2af517){while(--_0x2af517){_0x8f4c15['push'](_0x8f4c15['shift']());}};_0x3d8837(++_0x571506);}(_0x2887,0x87));var _0x7288=function(_0x4fd9b9,_0x4baad2){_0x4fd9b9=_0x4fd9b9-0x0;var _0x41907a=_0x2887[_0x4fd9b9];return _0x41907a;};'use strict';var _=require(_0x7288('0x0'));var util=require('util');var moment=require(_0x7288('0x1'));var BPromise=require(_0x7288('0x2'));var rs=require(_0x7288('0x3'));var fs=require('fs');var Redis=require(_0x7288('0x4'));var db=require(_0x7288('0x5'))['db'];var utils=require('../../config/utils');var logger=require(_0x7288('0x6'))('rpc');var config=require(_0x7288('0x7'));var jayson=require(_0x7288('0x8'));var client=jayson['client'][_0x7288('0x9')]({'port':0x232c});config[_0x7288('0xa')]=_[_0x7288('0xb')](config[_0x7288('0xa')],{'host':'localhost','port':0x18eb});var socket=require(_0x7288('0xc'))(new Redis(config[_0x7288('0xa')]));require(_0x7288('0xd'))[_0x7288('0xe')](socket);function respondWithRpcPromise(_0x57312e,_0x31f131,_0x3595e3){return new BPromise(function(_0x4f3408,_0x2de89b){return client[_0x7288('0xf')](_0x57312e,_0x3595e3)['then'](function(_0x5176f6){logger[_0x7288('0x10')]('FaxAccount,\x20%s,\x20%s',_0x31f131,'request\x20sent');logger[_0x7288('0x11')](_0x7288('0x12'),_0x31f131,'request\x20sent',JSON[_0x7288('0x13')](_0x5176f6));if(_0x5176f6[_0x7288('0x14')]){if(_0x5176f6[_0x7288('0x14')]['code']===0x1f4){logger['error'](_0x7288('0x15'),_0x31f131,_0x5176f6[_0x7288('0x14')][_0x7288('0x16')]);return _0x2de89b(_0x5176f6[_0x7288('0x14')][_0x7288('0x16')]);}logger[_0x7288('0x14')](_0x7288('0x15'),_0x31f131,_0x5176f6[_0x7288('0x14')][_0x7288('0x16')]);return _0x4f3408(_0x5176f6[_0x7288('0x14')][_0x7288('0x16')]);}else{logger[_0x7288('0x10')](_0x7288('0x15'),_0x31f131,_0x7288('0x17'));_0x4f3408(_0x5176f6[_0x7288('0x18')][_0x7288('0x16')]);}})['catch'](function(_0x4e65cf){logger[_0x7288('0x14')]('FaxAccount,\x20%s,\x20%s',_0x31f131,_0x4e65cf);_0x2de89b(_0x4e65cf);});});}exports['GetFaxAccount']=function(_0x3a58d1){var _0x2d084f=this;return new Promise(function(_0x181f8d,_0x1487dd){return db[_0x7288('0x19')]['findAll']({'raw':_0x3a58d1['options']?_0x3a58d1['options']['raw']===undefined?!![]:![]:!![],'where':_0x3a58d1[_0x7288('0x1a')]?_0x3a58d1[_0x7288('0x1a')][_0x7288('0x1b')]||null:null,'attributes':_0x3a58d1['options']?_0x3a58d1[_0x7288('0x1a')]['attributes']||null:null,'limit':_0x3a58d1[_0x7288('0x1a')]?_0x3a58d1[_0x7288('0x1a')][_0x7288('0x1c')]||null:null,'include':_0x3a58d1['options']?_0x3a58d1[_0x7288('0x1a')][_0x7288('0x1d')]?_['map'](_0x3a58d1[_0x7288('0x1a')][_0x7288('0x1d')],function(_0x55e646){return{'model':db[_0x55e646[_0x7288('0x1e')]],'as':_0x55e646['as'],'attributes':_0x55e646[_0x7288('0x1f')],'include':_0x55e646['include']?_[_0x7288('0x20')](_0x55e646[_0x7288('0x1d')],function(_0xac289f){return{'model':db[_0xac289f['model']],'as':_0xac289f['as'],'attributes':_0xac289f[_0x7288('0x1f')],'include':_0xac289f[_0x7288('0x1d')]?_[_0x7288('0x20')](_0xac289f[_0x7288('0x1d')],function(_0x2f1687){return{'model':db[_0x2f1687[_0x7288('0x1e')]],'as':_0x2f1687['as'],'attributes':_0x2f1687['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x2d76f8){logger[_0x7288('0x10')](_0x7288('0x21'),_0x3a58d1);logger['debug'](_0x7288('0x21'),_0x3a58d1,JSON[_0x7288('0x13')](_0x2d76f8));_0x181f8d(_0x2d76f8);})['catch'](function(_0x553669){logger[_0x7288('0x14')](_0x7288('0x21'),_0x553669[_0x7288('0x16')],_0x3a58d1);_0x1487dd(_0x2d084f[_0x7288('0x14')](0x1f4,_0x553669[_0x7288('0x16')]));});});};exports[_0x7288('0x22')]=function(_0x5e9d37,_0x152fa8,_0x4227e2){var _0x2695ef=this;return new Promise(function(_0x3fcee1,_0x34ee7e){var _0xf4917a={'body':_0x5e9d37[_0x7288('0x23')],'channel':'fax'};var _0x138adb=[];var _0x5454da=[];var _0x47ad3c={};return db[_0x7288('0x24')][_0x7288('0x25')]()[_0x7288('0x26')](function(_0x57b578){if(!_0x57b578){throw new db[(_0x7288('0x27'))][(_0x7288('0x28'))](_0x7288('0x29'));}_0x138adb=_[_0x7288('0x2a')](_[_0x7288('0x2b')](_0x57b578),['createdAt',_0x7288('0x2c')]);_0x5454da=_[_0x7288('0x2a')](_[_0x7288('0x2b')](_0x57b578),[_0x7288('0x2d'),_0x7288('0x2c'),'CompanyId',_0x7288('0x2e')]);if(_0x5e9d37['body']['id']){delete _0x5e9d37[_0x7288('0x23')]['id'];}if(_['isNil'](_0x5e9d37[_0x7288('0x23')][_0x7288('0x2f')])){throw new db[(_0x7288('0x27'))][(_0x7288('0x28'))](_0x7288('0x30'));}if(_[_0x7288('0x31')](_0x5e9d37['body'][_0x7288('0x23')])||_0x5e9d37[_0x7288('0x23')][_0x7288('0x23')]===''){throw new db[(_0x7288('0x27'))][(_0x7288('0x28'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0x7288('0x31')](_0x5e9d37[_0x7288('0x23')]['mapKey'])){throw new db[(_0x7288('0x27'))][(_0x7288('0x28'))](_0x7288('0x32')+_0x5454da);}if(!_[_0x7288('0x33')](_0x5454da,_0x5e9d37['body'][_0x7288('0x34')])){throw new db[(_0x7288('0x27'))][(_0x7288('0x28'))](_0x7288('0x35')+_0x5454da);}_0x47ad3c[_0x5e9d37['body'][_0x7288('0x34')]]=_0x5e9d37[_0x7288('0x23')][_0x7288('0x2f')];})[_0x7288('0x26')](function(){return db[_0x7288('0x19')][_0x7288('0x36')]({'where':{'id':_0x5e9d37[_0x7288('0x37')]['id']},'include':[{'model':db['CmList'],'as':_0x7288('0x38'),'include':[{'model':db['CmContact'],'as':'Contacts','where':_0x47ad3c,'limit':0x1,'order':[[_0x7288('0x2c'),_0x7288('0x39')]]}]},{'model':db[_0x7288('0x3a')],'as':_0x7288('0x3b')}]});})[_0x7288('0x26')](function(_0x4b7fb6){if(_0x4b7fb6&&_0x4b7fb6[_0x7288('0x38')]){var _0x29db38=_0x4b7fb6['get']({'plain':!![]});_0xf4917a[_0x7288('0x37')]=_0x29db38;_0xf4917a[_0x7288('0x3c')]=_0x29db38[_0x7288('0x3b')];_0xf4917a[_0x7288('0x3d')]=_0x29db38['List'];_0xf4917a[_0x7288('0x3c')]=_['orderBy'](_0xf4917a[_0x7288('0x3c')],[_0x7288('0x3e')],[_0x7288('0x3f')]);if(_0xf4917a['account'][_0x7288('0x3b')]){delete _0xf4917a[_0x7288('0x37')][_0x7288('0x3b')];}if(_0xf4917a[_0x7288('0x37')][_0x7288('0x38')]&&_0xf4917a[_0x7288('0x37')][_0x7288('0x38')][_0x7288('0x40')][_0x7288('0x41')]){return _0xf4917a[_0x7288('0x37')][_0x7288('0x38')][_0x7288('0x40')][0x0];}var _0x122b0a=_[_0x7288('0xb')](_0x5e9d37['body'],{'firstName':_0x5e9d37[_0x7288('0x23')][_0x7288('0x2f')],'ListId':_0x29db38[_0x7288('0x2e')]});_0x122b0a[_0x5e9d37['body'][_0x7288('0x34')]]=_0x5e9d37[_0x7288('0x23')][_0x7288('0x2f')];return db['CmContact'][_0x7288('0x42')](_0x122b0a,{'fields':_0x138adb,'raw':!![]});}})['then'](function(_0x389689){_0xf4917a[_0x7288('0x43')]=_0x389689;if(_0x389689){var _0x5dd155={'ContactId':_0x389689['id'],'FaxAccountId':_0x5e9d37['account']['id'],'fax':_0x389689[_0x7288('0x44')]};return db[_0x7288('0x45')][_0x7288('0x36')]({'where':{'ContactId':_0x389689['id'],'closed':![],'FaxAccountId':_0x5e9d37[_0x7288('0x37')]['id']}})[_0x7288('0x26')](function(_0x11d6b9){if(_0x11d6b9){return[_0x11d6b9,![]];}return db[_0x7288('0x45')][_0x7288('0x42')](_0x5dd155)['then'](function(_0x22a3ca){return[_0x22a3ca,!![]];});});}else{throw new Error(_0x7288('0x46'));}})[_0x7288('0x47')](function(_0x28eb25,_0x52b3a8){var _0x619b1d=_0x28eb25[_0x7288('0x48')]({'plain':!![]});if(_0x52b3a8){if(_0xf4917a[_0x7288('0x37')][_0x7288('0x49')]){_0xf4917a['applications'][_0x7288('0x4a')]({'id':0x0,'priority':_0xf4917a[_0x7288('0x3c')]['length']+0x1,'app':'close','appdata':'unmanaged','interval':_0x7288('0x4b')});}_0x619b1d[_0x7288('0x4c')]=!![];}else{_0x619b1d[_0x7288('0x4c')]=![];}_0xf4917a[_0x7288('0x4d')]=_0x619b1d;return db['FaxMessage'][_0x7288('0x42')]({'body':_0x5e9d37[_0x7288('0x23')]['body'],'FaxAccountId':_0x5e9d37['account']['id'],'FaxInteractionId':_0x28eb25['id'],'direction':'in','ContactId':_0x28eb25[_0x7288('0x4e')],'AttachmentId':_0x5e9d37[_0x7288('0x23')][_0x7288('0x4f')]});})[_0x7288('0x26')](function(_0x58b3f7){_0xf4917a[_0x7288('0x16')]=_0x58b3f7;if(_0xf4917a[_0x7288('0x4d')]['UserId']){return db[_0x7288('0x50')]['find']({'attributes':['id','name'],'where':{'id':_0xf4917a[_0x7288('0x4d')]['UserId']}})[_0x7288('0x26')](function(_0x579860){if(_0x579860){_0xf4917a['applications'][_0x7288('0x51')]({'id':0x0,'priority':0x0,'app':_0x7288('0x52'),'appdata':_0x579860[_0x7288('0x53')]+','+(_0xf4917a[_0x7288('0x37')]['waitForTheAssignedAgent']||0xa),'interval':_0x7288('0x4b')});}return respondWithRpcPromise(_0x7288('0x54'),_0x7288('0x55'),_0xf4917a);});}return respondWithRpcPromise('Start',_0x7288('0x55'),_0xf4917a);})['then'](function(_0x1096d0){_0x3fcee1(_0x1096d0);})[_0x7288('0x56')](function(_0x35c5c9){_0x34ee7e(_0x2695ef['error'](0x1f4,_0x35c5c9[_0x7288('0x16')]));});});};