4b54fb1f0c9a1e2adc6f0f45a7bd97c099624820
[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 _0x82e1=['map','include','model','catch','NotifyFaxAccount','fax','CmContact','describe','Sequelize','ValidationError','no\x20available\x20attributes','difference','keys','createdAt','updatedAt','CompanyId','ListId','body','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','find','List','Contacts','DESC','FaxApplication','Applications','account','applications','list','orderBy','asc','length','create','contact','Contact\x20not\x20found','spread','push','close','unmanaged','*,*,*,*','created','FaxMessage','ContactId','AttachmentId','UserId','User','name','interaction','startRouting','Start','lodash','util','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','../../config/environment','jayson/promise','client','http','redis','defaults','localhost','socket.io-emitter','register','request','then','info','FaxAccount,\x20%s,\x20%s','request\x20sent','debug','FaxAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','GetFaxAccount','FaxAccount','findAll','options','raw','where','attributes'];(function(_0x210a15,_0x285ad6){var _0x1d9495=function(_0x3af4cf){while(--_0x3af4cf){_0x210a15['push'](_0x210a15['shift']());}};_0x1d9495(++_0x285ad6);}(_0x82e1,0x14f));var _0x182e=function(_0x177346,_0x5e4024){_0x177346=_0x177346-0x0;var _0x155669=_0x82e1[_0x177346];return _0x155669;};'use strict';var _=require(_0x182e('0x0'));var util=require(_0x182e('0x1'));var moment=require(_0x182e('0x2'));var BPromise=require(_0x182e('0x3'));var rs=require(_0x182e('0x4'));var fs=require('fs');var Redis=require(_0x182e('0x5'));var db=require(_0x182e('0x6'))['db'];var utils=require(_0x182e('0x7'));var logger=require(_0x182e('0x8'))('rpc');var config=require(_0x182e('0x9'));var jayson=require(_0x182e('0xa'));var client=jayson[_0x182e('0xb')][_0x182e('0xc')]({'port':0x232c});config[_0x182e('0xd')]=_[_0x182e('0xe')](config[_0x182e('0xd')],{'host':_0x182e('0xf'),'port':0x18eb});var socket=require(_0x182e('0x10'))(new Redis(config[_0x182e('0xd')]));require('./faxAccount.socket')[_0x182e('0x11')](socket);function respondWithRpcPromise(_0x104770,_0x4a5ce9,_0x40bb55){return new BPromise(function(_0x2dc2a0,_0x41f02f){return client[_0x182e('0x12')](_0x104770,_0x40bb55)[_0x182e('0x13')](function(_0x2ac6f4){logger[_0x182e('0x14')](_0x182e('0x15'),_0x4a5ce9,_0x182e('0x16'));logger[_0x182e('0x17')](_0x182e('0x18'),_0x4a5ce9,_0x182e('0x16'),JSON[_0x182e('0x19')](_0x2ac6f4));if(_0x2ac6f4['error']){if(_0x2ac6f4[_0x182e('0x1a')][_0x182e('0x1b')]===0x1f4){logger[_0x182e('0x1a')](_0x182e('0x15'),_0x4a5ce9,_0x2ac6f4[_0x182e('0x1a')][_0x182e('0x1c')]);return _0x41f02f(_0x2ac6f4[_0x182e('0x1a')][_0x182e('0x1c')]);}logger[_0x182e('0x1a')](_0x182e('0x15'),_0x4a5ce9,_0x2ac6f4[_0x182e('0x1a')][_0x182e('0x1c')]);return _0x2dc2a0(_0x2ac6f4['error'][_0x182e('0x1c')]);}else{logger['info'](_0x182e('0x15'),_0x4a5ce9,'request\x20sent');_0x2dc2a0(_0x2ac6f4[_0x182e('0x1d')][_0x182e('0x1c')]);}})['catch'](function(_0x20d7fa){logger[_0x182e('0x1a')]('FaxAccount,\x20%s,\x20%s',_0x4a5ce9,_0x20d7fa);_0x41f02f(_0x20d7fa);});});}exports[_0x182e('0x1e')]=function(_0x328cf2){var _0x307de5=this;return new Promise(function(_0x45a982,_0x3f034e){return db[_0x182e('0x1f')][_0x182e('0x20')]({'raw':_0x328cf2[_0x182e('0x21')]?_0x328cf2[_0x182e('0x21')][_0x182e('0x22')]===undefined?!![]:![]:!![],'where':_0x328cf2[_0x182e('0x21')]?_0x328cf2[_0x182e('0x21')][_0x182e('0x23')]||null:null,'attributes':_0x328cf2[_0x182e('0x21')]?_0x328cf2[_0x182e('0x21')][_0x182e('0x24')]||null:null,'limit':_0x328cf2[_0x182e('0x21')]?_0x328cf2[_0x182e('0x21')]['limit']||null:null,'include':_0x328cf2[_0x182e('0x21')]?_0x328cf2['options']['include']?_[_0x182e('0x25')](_0x328cf2[_0x182e('0x21')][_0x182e('0x26')],function(_0x1b7c76){return{'model':db[_0x1b7c76[_0x182e('0x27')]],'as':_0x1b7c76['as'],'attributes':_0x1b7c76[_0x182e('0x24')],'include':_0x1b7c76[_0x182e('0x26')]?_['map'](_0x1b7c76[_0x182e('0x26')],function(_0x3be1a6){return{'model':db[_0x3be1a6[_0x182e('0x27')]],'as':_0x3be1a6['as'],'attributes':_0x3be1a6[_0x182e('0x24')],'include':_0x3be1a6[_0x182e('0x26')]?_['map'](_0x3be1a6[_0x182e('0x26')],function(_0x289a60){return{'model':db[_0x289a60[_0x182e('0x27')]],'as':_0x289a60['as'],'attributes':_0x289a60[_0x182e('0x24')]};}):[]};}):[]};}):[]:[]})[_0x182e('0x13')](function(_0x3b83fc){logger[_0x182e('0x14')](_0x182e('0x1e'),_0x328cf2);logger[_0x182e('0x17')](_0x182e('0x1e'),_0x328cf2,JSON[_0x182e('0x19')](_0x3b83fc));_0x45a982(_0x3b83fc);})[_0x182e('0x28')](function(_0x3be8a2){logger[_0x182e('0x1a')](_0x182e('0x1e'),_0x3be8a2[_0x182e('0x1c')],_0x328cf2);_0x3f034e(_0x307de5[_0x182e('0x1a')](0x1f4,_0x3be8a2[_0x182e('0x1c')]));});});};exports[_0x182e('0x29')]=function(_0x557b0b,_0x3e64da,_0x1f1908){var _0x21f42e=this;return new Promise(function(_0x2794dd,_0x2aee55){var _0x45d690={'body':_0x557b0b['body'],'channel':_0x182e('0x2a')};var _0x5ba573=[];var _0x283db3=[];var _0x5a82a3={};return db[_0x182e('0x2b')][_0x182e('0x2c')]()[_0x182e('0x13')](function(_0x26677f){if(!_0x26677f){throw new db[(_0x182e('0x2d'))][(_0x182e('0x2e'))](_0x182e('0x2f'));}_0x5ba573=_[_0x182e('0x30')](_[_0x182e('0x31')](_0x26677f),[_0x182e('0x32'),_0x182e('0x33')]);_0x283db3=_[_0x182e('0x30')](_[_0x182e('0x31')](_0x26677f),[_0x182e('0x32'),_0x182e('0x33'),_0x182e('0x34'),_0x182e('0x35')]);if(_0x557b0b[_0x182e('0x36')]['id']){delete _0x557b0b[_0x182e('0x36')]['id'];}if(_[_0x182e('0x37')](_0x557b0b['body'][_0x182e('0x38')])){throw new db['Sequelize']['ValidationError'](_0x182e('0x39'));}if(_[_0x182e('0x37')](_0x557b0b[_0x182e('0x36')][_0x182e('0x36')])||_0x557b0b['body'][_0x182e('0x36')]===''){throw new db[(_0x182e('0x2d'))]['ValidationError'](_0x182e('0x3a'));}if(_['isNil'](_0x557b0b['body'][_0x182e('0x3b')])){throw new db[(_0x182e('0x2d'))]['ValidationError'](_0x182e('0x3c')+_0x283db3);}if(!_[_0x182e('0x3d')](_0x283db3,_0x557b0b['body']['mapKey'])){throw new db[(_0x182e('0x2d'))][(_0x182e('0x2e'))](_0x182e('0x3e')+_0x283db3);}_0x5a82a3[_0x557b0b[_0x182e('0x36')][_0x182e('0x3b')]]=_0x557b0b[_0x182e('0x36')][_0x182e('0x38')];})[_0x182e('0x13')](function(){return db[_0x182e('0x1f')][_0x182e('0x3f')]({'where':{'id':_0x557b0b['account']['id']},'include':[{'model':db['CmList'],'as':_0x182e('0x40'),'include':[{'model':db[_0x182e('0x2b')],'as':_0x182e('0x41'),'where':_0x5a82a3,'limit':0x1,'order':[[_0x182e('0x33'),_0x182e('0x42')]]}]},{'model':db[_0x182e('0x43')],'as':_0x182e('0x44')}]});})[_0x182e('0x13')](function(_0x3ab34f){if(_0x3ab34f&&_0x3ab34f[_0x182e('0x40')]){var _0x294c48=_0x3ab34f['get']({'plain':!![]});_0x45d690[_0x182e('0x45')]=_0x294c48;_0x45d690[_0x182e('0x46')]=_0x294c48['Applications'];_0x45d690[_0x182e('0x47')]=_0x294c48['List'];_0x45d690[_0x182e('0x46')]=_[_0x182e('0x48')](_0x45d690[_0x182e('0x46')],['priority'],[_0x182e('0x49')]);if(_0x45d690[_0x182e('0x45')]['Applications']){delete _0x45d690[_0x182e('0x45')]['Applications'];}if(_0x45d690[_0x182e('0x45')][_0x182e('0x40')]&&_0x45d690[_0x182e('0x45')][_0x182e('0x40')][_0x182e('0x41')][_0x182e('0x4a')]){return _0x45d690['account'][_0x182e('0x40')][_0x182e('0x41')][0x0];}var _0x896cef=_[_0x182e('0xe')](_0x557b0b[_0x182e('0x36')],{'firstName':_0x557b0b[_0x182e('0x36')][_0x182e('0x38')],'ListId':_0x294c48['ListId']});_0x896cef[_0x557b0b[_0x182e('0x36')]['mapKey']]=_0x557b0b['body']['from'];return db[_0x182e('0x2b')][_0x182e('0x4b')](_0x896cef,{'fields':_0x5ba573,'raw':!![]});}})['then'](function(_0x3c86ba){_0x45d690[_0x182e('0x4c')]=_0x3c86ba;if(_0x3c86ba){var _0x37c0f0={'ContactId':_0x3c86ba['id'],'FaxAccountId':_0x557b0b[_0x182e('0x45')]['id'],'fax':_0x3c86ba[_0x182e('0x2a')]};return db['FaxInteraction'][_0x182e('0x3f')]({'where':{'ContactId':_0x3c86ba['id'],'closed':![],'FaxAccountId':_0x557b0b['account']['id']}})[_0x182e('0x13')](function(_0x44e7f3){if(_0x44e7f3){return[_0x44e7f3,![]];}return db['FaxInteraction'][_0x182e('0x4b')](_0x37c0f0)[_0x182e('0x13')](function(_0x53853){return[_0x53853,!![]];});});}else{throw new Error(_0x182e('0x4d'));}})[_0x182e('0x4e')](function(_0x5819f6,_0x2358b0){var _0xeae6c0=_0x5819f6['get']({'plain':!![]});if(_0x2358b0){if(_0x45d690[_0x182e('0x45')]['autoclose']){_0x45d690['applications'][_0x182e('0x4f')]({'id':0x0,'priority':_0x45d690[_0x182e('0x46')][_0x182e('0x4a')]+0x1,'app':_0x182e('0x50'),'appdata':_0x182e('0x51'),'interval':_0x182e('0x52')});}_0xeae6c0[_0x182e('0x53')]=!![];}else{_0xeae6c0[_0x182e('0x53')]=![];}_0x45d690['interaction']=_0xeae6c0;return db[_0x182e('0x54')]['create']({'body':_0x557b0b[_0x182e('0x36')]['body'],'FaxAccountId':_0x557b0b['account']['id'],'FaxInteractionId':_0x5819f6['id'],'direction':'in','ContactId':_0x5819f6[_0x182e('0x55')],'AttachmentId':_0x557b0b[_0x182e('0x36')][_0x182e('0x56')]});})[_0x182e('0x13')](function(_0x3a52fc){_0x45d690[_0x182e('0x1c')]=_0x3a52fc;if(_0x45d690['interaction'][_0x182e('0x57')]){return db[_0x182e('0x58')]['find']({'attributes':['id',_0x182e('0x59')],'where':{'id':_0x45d690[_0x182e('0x5a')][_0x182e('0x57')]}})[_0x182e('0x13')](function(_0x5dc4c1){if(_0x5dc4c1){_0x45d690['applications']['unshift']({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x5dc4c1['name']+','+(_0x45d690['account']['waitForTheAssignedAgent']||0xa),'interval':'*,*,*,*'});}return respondWithRpcPromise('Start',_0x182e('0x5b'),_0x45d690);});}return respondWithRpcPromise(_0x182e('0x5c'),_0x182e('0x5b'),_0x45d690);})[_0x182e('0x13')](function(_0x26779c){_0x2794dd(_0x26779c);})[_0x182e('0x28')](function(_0x54cf80){_0x2aee55(_0x21f42e[_0x182e('0x1a')](0x1f4,_0x54cf80[_0x182e('0x1c')]));});});};