00bc9ee650e72903ef1c675754065219aabe1e7a
[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 _0xfc93=['createdAt','updatedAt','CompanyId','from','isNil','includes','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','find','CmList','List','Contacts','DESC','account','Applications','list','applications','orderBy','asc','length','create','FaxInteraction','Contact\x20not\x20found','spread','get','autoclose','push','close','unmanaged','created','interaction','FaxMessage','AttachmentId','User','name','UserId','unshift','waitForTheAssignedAgent','*,*,*,*','queueId','FaxQueue','queue','waitForTheAssignedQueue','startRouting','util','moment','randomstring','ioredis','../../mysqldb','../../config/utils','rpc','../../config/environment','jayson/promise','http','redis','defaults','localhost','socket.io-emitter','./faxAccount.socket','register','request','then','info','FaxAccount,\x20%s,\x20%s','request\x20sent','debug','error','code','message','catch','GetFaxAccount','findAll','options','where','attributes','limit','include','map','model','stringify','NotifyFaxAccount','body','fax','CmContact','Sequelize','ValidationError','no\x20available\x20attributes','difference','keys'];(function(_0x32590b,_0x176741){var _0x3b5b22=function(_0x195586){while(--_0x195586){_0x32590b['push'](_0x32590b['shift']());}};_0x3b5b22(++_0x176741);}(_0xfc93,0x1e9));var _0x3fc9=function(_0x241dfe,_0x554285){_0x241dfe=_0x241dfe-0x0;var _0x2f5c06=_0xfc93[_0x241dfe];return _0x2f5c06;};'use strict';var _=require('lodash');var util=require(_0x3fc9('0x0'));var moment=require(_0x3fc9('0x1'));var BPromise=require('bluebird');var rs=require(_0x3fc9('0x2'));var fs=require('fs');var Redis=require(_0x3fc9('0x3'));var db=require(_0x3fc9('0x4'))['db'];var utils=require(_0x3fc9('0x5'));var logger=require('../../config/logger')(_0x3fc9('0x6'));var config=require(_0x3fc9('0x7'));var jayson=require(_0x3fc9('0x8'));var client=jayson['client'][_0x3fc9('0x9')]({'port':0x232c});config[_0x3fc9('0xa')]=_[_0x3fc9('0xb')](config[_0x3fc9('0xa')],{'host':_0x3fc9('0xc'),'port':0x18eb});var socket=require(_0x3fc9('0xd'))(new Redis(config[_0x3fc9('0xa')]));require(_0x3fc9('0xe'))[_0x3fc9('0xf')](socket);function respondWithRpcPromise(_0x5a1257,_0x265de4,_0x56b795){return new BPromise(function(_0xdbcfea,_0x14ccd2){return client[_0x3fc9('0x10')](_0x5a1257,_0x56b795)[_0x3fc9('0x11')](function(_0x472be1){logger[_0x3fc9('0x12')](_0x3fc9('0x13'),_0x265de4,_0x3fc9('0x14'));logger[_0x3fc9('0x15')]('FaxAccount,\x20%s,\x20%s,\x20%s',_0x265de4,_0x3fc9('0x14'),JSON['stringify'](_0x472be1));if(_0x472be1[_0x3fc9('0x16')]){if(_0x472be1[_0x3fc9('0x16')][_0x3fc9('0x17')]===0x1f4){logger[_0x3fc9('0x16')](_0x3fc9('0x13'),_0x265de4,_0x472be1[_0x3fc9('0x16')][_0x3fc9('0x18')]);return _0x14ccd2(_0x472be1[_0x3fc9('0x16')][_0x3fc9('0x18')]);}logger[_0x3fc9('0x16')]('FaxAccount,\x20%s,\x20%s',_0x265de4,_0x472be1[_0x3fc9('0x16')][_0x3fc9('0x18')]);return _0xdbcfea(_0x472be1[_0x3fc9('0x16')]['message']);}else{logger['info'](_0x3fc9('0x13'),_0x265de4,_0x3fc9('0x14'));_0xdbcfea(_0x472be1['result'][_0x3fc9('0x18')]);}})[_0x3fc9('0x19')](function(_0x78acb2){logger[_0x3fc9('0x16')](_0x3fc9('0x13'),_0x265de4,_0x78acb2);_0x14ccd2(_0x78acb2);});});}exports[_0x3fc9('0x1a')]=function(_0x7acb40){var _0x19056e=this;return new Promise(function(_0x62f3d4,_0x197a77){return db['FaxAccount'][_0x3fc9('0x1b')]({'raw':_0x7acb40[_0x3fc9('0x1c')]?_0x7acb40[_0x3fc9('0x1c')]['raw']===undefined?!![]:![]:!![],'where':_0x7acb40[_0x3fc9('0x1c')]?_0x7acb40[_0x3fc9('0x1c')][_0x3fc9('0x1d')]||null:null,'attributes':_0x7acb40['options']?_0x7acb40[_0x3fc9('0x1c')][_0x3fc9('0x1e')]||null:null,'limit':_0x7acb40[_0x3fc9('0x1c')]?_0x7acb40[_0x3fc9('0x1c')][_0x3fc9('0x1f')]||null:null,'include':_0x7acb40[_0x3fc9('0x1c')]?_0x7acb40[_0x3fc9('0x1c')]['include']?_['map'](_0x7acb40[_0x3fc9('0x1c')][_0x3fc9('0x20')],function(_0x793347){return{'model':db[_0x793347['model']],'as':_0x793347['as'],'attributes':_0x793347['attributes'],'include':_0x793347[_0x3fc9('0x20')]?_[_0x3fc9('0x21')](_0x793347[_0x3fc9('0x20')],function(_0x3acf5b){return{'model':db[_0x3acf5b[_0x3fc9('0x22')]],'as':_0x3acf5b['as'],'attributes':_0x3acf5b[_0x3fc9('0x1e')],'include':_0x3acf5b[_0x3fc9('0x20')]?_[_0x3fc9('0x21')](_0x3acf5b['include'],function(_0x4d5774){return{'model':db[_0x4d5774[_0x3fc9('0x22')]],'as':_0x4d5774['as'],'attributes':_0x4d5774['attributes']};}):[]};}):[]};}):[]:[]})[_0x3fc9('0x11')](function(_0x268e96){logger[_0x3fc9('0x12')](_0x3fc9('0x1a'),_0x7acb40);logger['debug'](_0x3fc9('0x1a'),_0x7acb40,JSON[_0x3fc9('0x23')](_0x268e96));_0x62f3d4(_0x268e96);})['catch'](function(_0x5f02e5){logger['error'](_0x3fc9('0x1a'),_0x5f02e5[_0x3fc9('0x18')],_0x7acb40);_0x197a77(_0x19056e[_0x3fc9('0x16')](0x1f4,_0x5f02e5[_0x3fc9('0x18')]));});});};exports[_0x3fc9('0x24')]=function(_0x4b6328,_0x4b0d49,_0x1dab3f){var _0x78793a=this;return new Promise(function(_0x1f7f45,_0x4a3c05){var _0x325666={'body':_0x4b6328[_0x3fc9('0x25')],'channel':_0x3fc9('0x26')};var _0x485a8d=[];var _0xe701de=[];var _0xd03e73={};return db[_0x3fc9('0x27')]['describe']()['then'](function(_0x256f6a){if(!_0x256f6a){throw new db[(_0x3fc9('0x28'))][(_0x3fc9('0x29'))](_0x3fc9('0x2a'));}_0x485a8d=_[_0x3fc9('0x2b')](_[_0x3fc9('0x2c')](_0x256f6a),[_0x3fc9('0x2d'),_0x3fc9('0x2e')]);_0xe701de=_[_0x3fc9('0x2b')](_[_0x3fc9('0x2c')](_0x256f6a),[_0x3fc9('0x2d'),_0x3fc9('0x2e'),_0x3fc9('0x2f'),'ListId']);if(_0x4b6328[_0x3fc9('0x25')]['id']){delete _0x4b6328[_0x3fc9('0x25')]['id'];}if(_['isNil'](_0x4b6328[_0x3fc9('0x25')][_0x3fc9('0x30')])){throw new db[(_0x3fc9('0x28'))][(_0x3fc9('0x29'))]('from\x20is\x20mandatory');}if(_[_0x3fc9('0x31')](_0x4b6328[_0x3fc9('0x25')][_0x3fc9('0x25')])||_0x4b6328[_0x3fc9('0x25')][_0x3fc9('0x25')]===''){throw new db[(_0x3fc9('0x28'))][(_0x3fc9('0x29'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0x3fc9('0x31')](_0x4b6328['body']['mapKey'])){throw new db[(_0x3fc9('0x28'))][(_0x3fc9('0x29'))]('mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0xe701de);}if(!_[_0x3fc9('0x32')](_0xe701de,_0x4b6328['body'][_0x3fc9('0x33')])){throw new db[(_0x3fc9('0x28'))][(_0x3fc9('0x29'))](_0x3fc9('0x34')+_0xe701de);}_0xd03e73[_0x4b6328['body'][_0x3fc9('0x33')]]=_0x4b6328[_0x3fc9('0x25')][_0x3fc9('0x30')];})[_0x3fc9('0x11')](function(){return db['FaxAccount'][_0x3fc9('0x35')]({'where':{'id':_0x4b6328['account']['id']},'include':[{'model':db[_0x3fc9('0x36')],'as':_0x3fc9('0x37'),'include':[{'model':db[_0x3fc9('0x27')],'as':_0x3fc9('0x38'),'where':_0xd03e73,'limit':0x1,'order':[[_0x3fc9('0x2e'),_0x3fc9('0x39')]]}]},{'model':db['FaxApplication'],'as':'Applications'}]});})[_0x3fc9('0x11')](function(_0x41fa41){if(_0x41fa41&&_0x41fa41[_0x3fc9('0x37')]){var _0x21abd4=_0x41fa41['get']({'plain':!![]});_0x325666[_0x3fc9('0x3a')]=_0x21abd4;_0x325666['applications']=_0x21abd4[_0x3fc9('0x3b')];_0x325666[_0x3fc9('0x3c')]=_0x21abd4['List'];_0x325666[_0x3fc9('0x3d')]=_[_0x3fc9('0x3e')](_0x325666[_0x3fc9('0x3d')],['priority'],[_0x3fc9('0x3f')]);if(_0x325666[_0x3fc9('0x3a')]['Applications']){delete _0x325666['account'][_0x3fc9('0x3b')];}if(_0x325666[_0x3fc9('0x3a')]['List']&&_0x325666[_0x3fc9('0x3a')][_0x3fc9('0x37')][_0x3fc9('0x38')][_0x3fc9('0x40')]){return _0x325666[_0x3fc9('0x3a')][_0x3fc9('0x37')][_0x3fc9('0x38')][0x0];}var _0x41748f=_[_0x3fc9('0xb')](_0x4b6328['body'],{'firstName':_0x4b6328[_0x3fc9('0x25')][_0x3fc9('0x30')],'ListId':_0x21abd4['ListId']});_0x41748f[_0x4b6328[_0x3fc9('0x25')][_0x3fc9('0x33')]]=_0x4b6328[_0x3fc9('0x25')]['from'];return db[_0x3fc9('0x27')][_0x3fc9('0x41')](_0x41748f,{'fields':_0x485a8d,'raw':!![]});}})[_0x3fc9('0x11')](function(_0x2b28bb){_0x325666['contact']=_0x2b28bb;if(_0x2b28bb){var _0x55c28d={'ContactId':_0x2b28bb['id'],'FaxAccountId':_0x4b6328[_0x3fc9('0x3a')]['id'],'fax':_0x2b28bb[_0x3fc9('0x26')]};return db[_0x3fc9('0x42')][_0x3fc9('0x35')]({'where':{'ContactId':_0x2b28bb['id'],'closed':![],'FaxAccountId':_0x4b6328['account']['id']}})['then'](function(_0x1ef919){if(_0x1ef919){return[_0x1ef919,![]];}return db[_0x3fc9('0x42')]['create'](_0x55c28d)[_0x3fc9('0x11')](function(_0x26d603){return[_0x26d603,!![]];});});}else{throw new Error(_0x3fc9('0x43'));}})[_0x3fc9('0x44')](function(_0x55417b,_0x3edb12){var _0x445675=_0x55417b[_0x3fc9('0x45')]({'plain':!![]});if(_0x3edb12){if(_0x325666[_0x3fc9('0x3a')][_0x3fc9('0x46')]){_0x325666[_0x3fc9('0x3d')][_0x3fc9('0x47')]({'id':0x0,'priority':_0x325666['applications'][_0x3fc9('0x40')]+0x1,'app':_0x3fc9('0x48'),'appdata':_0x3fc9('0x49'),'interval':'*,*,*,*'});}_0x445675[_0x3fc9('0x4a')]=!![];}else{_0x445675[_0x3fc9('0x4a')]=![];}_0x325666[_0x3fc9('0x4b')]=_0x445675;return db[_0x3fc9('0x4c')][_0x3fc9('0x41')]({'body':_0x4b6328[_0x3fc9('0x25')][_0x3fc9('0x25')],'FaxAccountId':_0x4b6328[_0x3fc9('0x3a')]['id'],'FaxInteractionId':_0x55417b['id'],'direction':'in','ContactId':_0x55417b['ContactId'],'AttachmentId':_0x4b6328['body'][_0x3fc9('0x4d')]});})[_0x3fc9('0x11')](function(_0x10a75a){_0x325666[_0x3fc9('0x18')]=_0x10a75a;if(!_0x325666['interaction']['UserId'])return;return db[_0x3fc9('0x4e')][_0x3fc9('0x35')]({'attributes':['id',_0x3fc9('0x4f')],'where':{'id':_0x325666['interaction'][_0x3fc9('0x50')]}});})[_0x3fc9('0x11')](function(_0x1736a2){if(_0x1736a2){_0x325666[_0x3fc9('0x3d')][_0x3fc9('0x51')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x1736a2[_0x3fc9('0x4f')]+','+(_0x325666['account'][_0x3fc9('0x52')]||0xa),'interval':_0x3fc9('0x53')});}if(!_0x325666[_0x3fc9('0x4b')][_0x3fc9('0x54')])return;return db[_0x3fc9('0x55')]['find']({'attributes':['id',_0x3fc9('0x4f')],'where':{'id':_0x325666[_0x3fc9('0x4b')][_0x3fc9('0x54')]}});})[_0x3fc9('0x11')](function(_0x3d1167){if(_0x3d1167){_0x325666['applications'][_0x3fc9('0x51')]({'id':0x0,'priority':0x1,'app':_0x3fc9('0x56'),'appdata':_0x3d1167['name']+','+(_0x325666[_0x3fc9('0x3a')][_0x3fc9('0x57')]||0x12c),'interval':_0x3fc9('0x53')});}return respondWithRpcPromise('Start',_0x3fc9('0x58'),_0x325666);})[_0x3fc9('0x11')](function(_0x23a9be){_0x1f7f45(_0x23a9be);})['catch'](function(_0x3377e1){_0x4a3c05(_0x78793a['error'](0x1f4,_0x3377e1['message']));});});};