7bd059ac3c6b3084f9717c126aa1bab4e1c6c691
[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 _0x573c=['localhost','socket.io-emitter','register','request','then','info','request\x20sent','debug','FaxAccount,\x20%s,\x20%s,\x20%s','error','code','FaxAccount,\x20%s,\x20%s','message','catch','FaxAccount','options','raw','attributes','limit','include','map','model','GetFaxAccount','stringify','body','fax','CmContact','ValidationError','no\x20available\x20attributes','difference','keys','createdAt','updatedAt','ListId','isNil','Sequelize','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','List','Contacts','DESC','Applications','get','account','list','applications','orderBy','asc','from','create','contact','FaxInteraction','find','Contact\x20not\x20found','autoclose','length','close','*,*,*,*','created','interaction','ContactId','AttachmentId','UserId','User','unshift','agent','name','waitForTheAssignedAgent','Start','startRouting','util','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','jayson/promise','client','http','redis'];(function(_0x550882,_0x417b5c){var _0x4bbdbd=function(_0xd0d42c){while(--_0xd0d42c){_0x550882['push'](_0x550882['shift']());}};_0x4bbdbd(++_0x417b5c);}(_0x573c,0x1a1));var _0xc573=function(_0x38a68a,_0x1d8296){_0x38a68a=_0x38a68a-0x0;var _0x5d6a6e=_0x573c[_0x38a68a];return _0x5d6a6e;};'use strict';var _=require('lodash');var util=require(_0xc573('0x0'));var moment=require(_0xc573('0x1'));var BPromise=require(_0xc573('0x2'));var rs=require(_0xc573('0x3'));var fs=require('fs');var Redis=require(_0xc573('0x4'));var db=require(_0xc573('0x5'))['db'];var utils=require(_0xc573('0x6'));var logger=require(_0xc573('0x7'))(_0xc573('0x8'));var config=require('../../config/environment');var jayson=require(_0xc573('0x9'));var client=jayson[_0xc573('0xa')][_0xc573('0xb')]({'port':0x232c});config['redis']=_['defaults'](config[_0xc573('0xc')],{'host':_0xc573('0xd'),'port':0x18eb});var socket=require(_0xc573('0xe'))(new Redis(config[_0xc573('0xc')]));require('./faxAccount.socket')[_0xc573('0xf')](socket);function respondWithRpcPromise(_0x4f8b27,_0xe11f77,_0x5d64b6){return new BPromise(function(_0x21f0e7,_0x14bbf2){return client[_0xc573('0x10')](_0x4f8b27,_0x5d64b6)[_0xc573('0x11')](function(_0x3690fa){logger[_0xc573('0x12')]('FaxAccount,\x20%s,\x20%s',_0xe11f77,_0xc573('0x13'));logger[_0xc573('0x14')](_0xc573('0x15'),_0xe11f77,_0xc573('0x13'),JSON['stringify'](_0x3690fa));if(_0x3690fa[_0xc573('0x16')]){if(_0x3690fa['error'][_0xc573('0x17')]===0x1f4){logger['error'](_0xc573('0x18'),_0xe11f77,_0x3690fa[_0xc573('0x16')][_0xc573('0x19')]);return _0x14bbf2(_0x3690fa['error'][_0xc573('0x19')]);}logger['error'](_0xc573('0x18'),_0xe11f77,_0x3690fa[_0xc573('0x16')][_0xc573('0x19')]);return _0x21f0e7(_0x3690fa[_0xc573('0x16')]['message']);}else{logger[_0xc573('0x12')](_0xc573('0x18'),_0xe11f77,'request\x20sent');_0x21f0e7(_0x3690fa['result'][_0xc573('0x19')]);}})[_0xc573('0x1a')](function(_0x30dab2){logger[_0xc573('0x16')](_0xc573('0x18'),_0xe11f77,_0x30dab2);_0x14bbf2(_0x30dab2);});});}exports['GetFaxAccount']=function(_0x5ef1ff){var _0x12eafd=this;return new Promise(function(_0x4bc627,_0x5c804b){return db[_0xc573('0x1b')]['findAll']({'raw':_0x5ef1ff[_0xc573('0x1c')]?_0x5ef1ff[_0xc573('0x1c')][_0xc573('0x1d')]===undefined?!![]:![]:!![],'where':_0x5ef1ff[_0xc573('0x1c')]?_0x5ef1ff['options']['where']||null:null,'attributes':_0x5ef1ff[_0xc573('0x1c')]?_0x5ef1ff['options'][_0xc573('0x1e')]||null:null,'limit':_0x5ef1ff[_0xc573('0x1c')]?_0x5ef1ff['options'][_0xc573('0x1f')]||null:null,'include':_0x5ef1ff['options']?_0x5ef1ff[_0xc573('0x1c')][_0xc573('0x20')]?_[_0xc573('0x21')](_0x5ef1ff[_0xc573('0x1c')][_0xc573('0x20')],function(_0x14e2ee){return{'model':db[_0x14e2ee[_0xc573('0x22')]],'as':_0x14e2ee['as'],'attributes':_0x14e2ee[_0xc573('0x1e')],'include':_0x14e2ee[_0xc573('0x20')]?_['map'](_0x14e2ee[_0xc573('0x20')],function(_0x5489cd){return{'model':db[_0x5489cd[_0xc573('0x22')]],'as':_0x5489cd['as'],'attributes':_0x5489cd[_0xc573('0x1e')],'include':_0x5489cd[_0xc573('0x20')]?_[_0xc573('0x21')](_0x5489cd['include'],function(_0x4ce045){return{'model':db[_0x4ce045['model']],'as':_0x4ce045['as'],'attributes':_0x4ce045[_0xc573('0x1e')]};}):[]};}):[]};}):[]:[]})[_0xc573('0x11')](function(_0x2021d3){logger[_0xc573('0x12')](_0xc573('0x23'),_0x5ef1ff);logger[_0xc573('0x14')](_0xc573('0x23'),_0x5ef1ff,JSON[_0xc573('0x24')](_0x2021d3));_0x4bc627(_0x2021d3);})['catch'](function(_0x209f0e){logger[_0xc573('0x16')](_0xc573('0x23'),_0x209f0e['message'],_0x5ef1ff);_0x5c804b(_0x12eafd['error'](0x1f4,_0x209f0e['message']));});});};exports['NotifyFaxAccount']=function(_0x21e079,_0x1bc19b,_0x21dc00){var _0x4d7d3a=this;return new Promise(function(_0x273e32,_0x479943){var _0x1570b2={'body':_0x21e079[_0xc573('0x25')],'channel':_0xc573('0x26')};var _0x58022f=[];var _0x52ffa7=[];var _0x5bcce9={};return db[_0xc573('0x27')]['describe']()[_0xc573('0x11')](function(_0x3d7df1){if(!_0x3d7df1){throw new db['Sequelize'][(_0xc573('0x28'))](_0xc573('0x29'));}_0x58022f=_[_0xc573('0x2a')](_[_0xc573('0x2b')](_0x3d7df1),[_0xc573('0x2c'),'updatedAt']);_0x52ffa7=_[_0xc573('0x2a')](_[_0xc573('0x2b')](_0x3d7df1),['createdAt',_0xc573('0x2d'),'CompanyId',_0xc573('0x2e')]);if(_0x21e079[_0xc573('0x25')]['id']){delete _0x21e079[_0xc573('0x25')]['id'];}if(_[_0xc573('0x2f')](_0x21e079[_0xc573('0x25')]['from'])){throw new db['Sequelize'][(_0xc573('0x28'))]('from\x20is\x20mandatory');}if(_[_0xc573('0x2f')](_0x21e079[_0xc573('0x25')]['body'])||_0x21e079[_0xc573('0x25')]['body']===''){throw new db[(_0xc573('0x30'))][(_0xc573('0x28'))](_0xc573('0x31'));}if(_['isNil'](_0x21e079[_0xc573('0x25')][_0xc573('0x32')])){throw new db[(_0xc573('0x30'))][(_0xc573('0x28'))](_0xc573('0x33')+_0x52ffa7);}if(!_['includes'](_0x52ffa7,_0x21e079[_0xc573('0x25')][_0xc573('0x32')])){throw new db[(_0xc573('0x30'))][(_0xc573('0x28'))](_0xc573('0x34')+_0x52ffa7);}_0x5bcce9[_0x21e079[_0xc573('0x25')][_0xc573('0x32')]]=_0x21e079[_0xc573('0x25')]['from'];})['then'](function(){return db[_0xc573('0x1b')]['find']({'where':{'id':_0x21e079['account']['id']},'include':[{'model':db[_0xc573('0x35')],'as':_0xc573('0x36'),'include':[{'model':db[_0xc573('0x27')],'as':_0xc573('0x37'),'where':_0x5bcce9,'limit':0x1,'order':[[_0xc573('0x2d'),_0xc573('0x38')]]}]},{'model':db['FaxApplication'],'as':_0xc573('0x39')}]});})[_0xc573('0x11')](function(_0x557c15){if(_0x557c15&&_0x557c15[_0xc573('0x36')]){var _0x96f099=_0x557c15[_0xc573('0x3a')]({'plain':!![]});_0x1570b2[_0xc573('0x3b')]=_0x96f099;_0x1570b2['applications']=_0x96f099[_0xc573('0x39')];_0x1570b2[_0xc573('0x3c')]=_0x96f099['List'];_0x1570b2[_0xc573('0x3d')]=_[_0xc573('0x3e')](_0x1570b2[_0xc573('0x3d')],['priority'],[_0xc573('0x3f')]);if(_0x1570b2['account'][_0xc573('0x39')]){delete _0x1570b2[_0xc573('0x3b')][_0xc573('0x39')];}if(_0x1570b2[_0xc573('0x3b')]['List']&&_0x1570b2[_0xc573('0x3b')][_0xc573('0x36')][_0xc573('0x37')]['length']){return _0x1570b2[_0xc573('0x3b')][_0xc573('0x36')][_0xc573('0x37')][0x0];}var _0x5af50a=_['defaults'](_0x21e079[_0xc573('0x25')],{'firstName':_0x21e079['body']['from'],'ListId':_0x96f099[_0xc573('0x2e')]});_0x5af50a[_0x21e079['body']['mapKey']]=_0x21e079[_0xc573('0x25')][_0xc573('0x40')];return db[_0xc573('0x27')][_0xc573('0x41')](_0x5af50a,{'fields':_0x58022f,'raw':!![]});}})['then'](function(_0x119285){_0x1570b2[_0xc573('0x42')]=_0x119285;if(_0x119285){var _0x37fafa={'ContactId':_0x119285['id'],'FaxAccountId':_0x21e079['account']['id'],'fax':_0x119285[_0xc573('0x26')]};return db[_0xc573('0x43')][_0xc573('0x44')]({'where':{'ContactId':_0x119285['id'],'closed':![],'FaxAccountId':_0x21e079['account']['id']}})['then'](function(_0x59ec82){if(_0x59ec82){return[_0x59ec82,![]];}return db[_0xc573('0x43')][_0xc573('0x41')](_0x37fafa)['then'](function(_0x18f8aa){return[_0x18f8aa,!![]];});});}else{throw new Error(_0xc573('0x45'));}})['spread'](function(_0x518ddc,_0x5e9cc2){var _0x4a6ba0=_0x518ddc[_0xc573('0x3a')]({'plain':!![]});if(_0x5e9cc2){if(_0x1570b2['account'][_0xc573('0x46')]){_0x1570b2['applications']['push']({'id':0x0,'priority':_0x1570b2['applications'][_0xc573('0x47')]+0x1,'app':_0xc573('0x48'),'appdata':'unmanaged','interval':_0xc573('0x49')});}_0x4a6ba0['created']=!![];}else{_0x4a6ba0[_0xc573('0x4a')]=![];}_0x1570b2[_0xc573('0x4b')]=_0x4a6ba0;return db['FaxMessage'][_0xc573('0x41')]({'body':_0x21e079['body'][_0xc573('0x25')],'FaxAccountId':_0x21e079[_0xc573('0x3b')]['id'],'FaxInteractionId':_0x518ddc['id'],'direction':'in','ContactId':_0x518ddc[_0xc573('0x4c')],'AttachmentId':_0x21e079[_0xc573('0x25')][_0xc573('0x4d')]});})[_0xc573('0x11')](function(_0x16b085){_0x1570b2[_0xc573('0x19')]=_0x16b085;if(_0x1570b2['interaction'][_0xc573('0x4e')]){return db[_0xc573('0x4f')][_0xc573('0x44')]({'attributes':['id','name'],'where':{'id':_0x1570b2[_0xc573('0x4b')][_0xc573('0x4e')]}})[_0xc573('0x11')](function(_0x5a8c49){if(_0x5a8c49){_0x1570b2[_0xc573('0x3d')][_0xc573('0x50')]({'id':0x0,'priority':0x0,'app':_0xc573('0x51'),'appdata':_0x5a8c49[_0xc573('0x52')]+','+(_0x1570b2['account'][_0xc573('0x53')]||0xa),'interval':_0xc573('0x49')});}return respondWithRpcPromise(_0xc573('0x54'),_0xc573('0x55'),_0x1570b2);});}return respondWithRpcPromise('Start',_0xc573('0x55'),_0x1570b2);})[_0xc573('0x11')](function(_0x3ceb1b){_0x273e32(_0x3ceb1b);})['catch'](function(_0x979743){_0x479943(_0x4d7d3a[_0xc573('0x16')](0x1f4,_0x979743['message']));});});};