52a2c5324d035cc72c359ee7014d16f0fcef5a71
[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 _0x29f9=['includes','find','account','CmList','List','CmContact','FaxApplication','Applications','get','applications','list','orderBy','asc','length','Contacts','defaults','create','contact','FaxInteraction','Contact\x20not\x20found','spread','autoclose','close','unmanaged','created','FaxMessage','ContactId','AttachmentId','interaction','UserId','User','name','unshift','agent','*,*,*,*','Start','startRouting','moment','ioredis','../../config/utils','../../config/logger','rpc','../../config/environment','client','redis','localhost','socket.io-emitter','./faxAccount.socket','register','request','FaxAccount,\x20%s,\x20%s','request\x20sent','FaxAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','info','result','catch','FaxAccount','findAll','options','attributes','limit','include','map','model','then','debug','GetFaxAccount','NotifyFaxAccount','body','fax','describe','Sequelize','ValidationError','difference','keys','createdAt','updatedAt','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'];(function(_0xbc346f,_0x3db5c0){var _0x9f9260=function(_0x37e6b9){while(--_0x37e6b9){_0xbc346f['push'](_0xbc346f['shift']());}};_0x9f9260(++_0x3db5c0);}(_0x29f9,0x1e2));var _0x929f=function(_0x31a138,_0xc2d1b6){_0x31a138=_0x31a138-0x0;var _0x16cbd4=_0x29f9[_0x31a138];return _0x16cbd4;};'use strict';var _=require('lodash');var util=require('util');var moment=require(_0x929f('0x0'));var BPromise=require('bluebird');var rs=require('randomstring');var fs=require('fs');var Redis=require(_0x929f('0x1'));var db=require('../../mysqldb')['db'];var utils=require(_0x929f('0x2'));var logger=require(_0x929f('0x3'))(_0x929f('0x4'));var config=require(_0x929f('0x5'));var jayson=require('jayson/promise');var client=jayson[_0x929f('0x6')]['http']({'port':0x232c});config[_0x929f('0x7')]=_['defaults'](config[_0x929f('0x7')],{'host':_0x929f('0x8'),'port':0x18eb});var socket=require(_0x929f('0x9'))(new Redis(config[_0x929f('0x7')]));require(_0x929f('0xa'))[_0x929f('0xb')](socket);function respondWithRpcPromise(_0x1dad52,_0x42e3f6,_0x182dec){return new BPromise(function(_0x21ad0b,_0x500a1e){return client[_0x929f('0xc')](_0x1dad52,_0x182dec)['then'](function(_0x59fbd2){logger['info'](_0x929f('0xd'),_0x42e3f6,_0x929f('0xe'));logger['debug'](_0x929f('0xf'),_0x42e3f6,_0x929f('0xe'),JSON[_0x929f('0x10')](_0x59fbd2));if(_0x59fbd2['error']){if(_0x59fbd2[_0x929f('0x11')][_0x929f('0x12')]===0x1f4){logger[_0x929f('0x11')](_0x929f('0xd'),_0x42e3f6,_0x59fbd2[_0x929f('0x11')][_0x929f('0x13')]);return _0x500a1e(_0x59fbd2[_0x929f('0x11')]['message']);}logger['error']('FaxAccount,\x20%s,\x20%s',_0x42e3f6,_0x59fbd2[_0x929f('0x11')][_0x929f('0x13')]);return _0x21ad0b(_0x59fbd2[_0x929f('0x11')][_0x929f('0x13')]);}else{logger[_0x929f('0x14')](_0x929f('0xd'),_0x42e3f6,'request\x20sent');_0x21ad0b(_0x59fbd2[_0x929f('0x15')][_0x929f('0x13')]);}})[_0x929f('0x16')](function(_0xe87bd2){logger['error']('FaxAccount,\x20%s,\x20%s',_0x42e3f6,_0xe87bd2);_0x500a1e(_0xe87bd2);});});}exports['GetFaxAccount']=function(_0x54ff46){var _0x242836=this;return new Promise(function(_0x4a288b,_0x1b9ae4){return db[_0x929f('0x17')][_0x929f('0x18')]({'raw':_0x54ff46[_0x929f('0x19')]?_0x54ff46[_0x929f('0x19')]['raw']===undefined?!![]:![]:!![],'where':_0x54ff46['options']?_0x54ff46['options']['where']||null:null,'attributes':_0x54ff46['options']?_0x54ff46[_0x929f('0x19')][_0x929f('0x1a')]||null:null,'limit':_0x54ff46['options']?_0x54ff46['options'][_0x929f('0x1b')]||null:null,'include':_0x54ff46['options']?_0x54ff46['options'][_0x929f('0x1c')]?_[_0x929f('0x1d')](_0x54ff46[_0x929f('0x19')][_0x929f('0x1c')],function(_0x20b306){return{'model':db[_0x20b306['model']],'as':_0x20b306['as'],'attributes':_0x20b306[_0x929f('0x1a')],'include':_0x20b306[_0x929f('0x1c')]?_['map'](_0x20b306['include'],function(_0x569c60){return{'model':db[_0x569c60[_0x929f('0x1e')]],'as':_0x569c60['as'],'attributes':_0x569c60[_0x929f('0x1a')],'include':_0x569c60['include']?_['map'](_0x569c60[_0x929f('0x1c')],function(_0x5ae2d8){return{'model':db[_0x5ae2d8['model']],'as':_0x5ae2d8['as'],'attributes':_0x5ae2d8[_0x929f('0x1a')]};}):[]};}):[]};}):[]:[]})[_0x929f('0x1f')](function(_0x33f298){logger[_0x929f('0x14')]('GetFaxAccount',_0x54ff46);logger[_0x929f('0x20')](_0x929f('0x21'),_0x54ff46,JSON[_0x929f('0x10')](_0x33f298));_0x4a288b(_0x33f298);})[_0x929f('0x16')](function(_0x2fef74){logger[_0x929f('0x11')](_0x929f('0x21'),_0x2fef74[_0x929f('0x13')],_0x54ff46);_0x1b9ae4(_0x242836[_0x929f('0x11')](0x1f4,_0x2fef74['message']));});});};exports[_0x929f('0x22')]=function(_0x4f53fe,_0x36cae9,_0x2890b2){var _0x2967a3=this;return new Promise(function(_0x3e9a91,_0x2fa8da){var _0x3bd1fc={'body':_0x4f53fe[_0x929f('0x23')],'channel':_0x929f('0x24')};var _0x1dea6f=[];var _0x33648c=[];var _0x4051a0={};return db['CmContact'][_0x929f('0x25')]()[_0x929f('0x1f')](function(_0x7b3873){if(!_0x7b3873){throw new db[(_0x929f('0x26'))][(_0x929f('0x27'))]('no\x20available\x20attributes');}_0x1dea6f=_[_0x929f('0x28')](_[_0x929f('0x29')](_0x7b3873),[_0x929f('0x2a'),_0x929f('0x2b')]);_0x33648c=_[_0x929f('0x28')](_[_0x929f('0x29')](_0x7b3873),[_0x929f('0x2a'),_0x929f('0x2b'),_0x929f('0x2c'),_0x929f('0x2d')]);if(_0x4f53fe[_0x929f('0x23')]['id']){delete _0x4f53fe[_0x929f('0x23')]['id'];}if(_[_0x929f('0x2e')](_0x4f53fe['body'][_0x929f('0x2f')])){throw new db[(_0x929f('0x26'))][(_0x929f('0x27'))](_0x929f('0x30'));}if(_[_0x929f('0x2e')](_0x4f53fe[_0x929f('0x23')]['body'])||_0x4f53fe['body'][_0x929f('0x23')]===''){throw new db[(_0x929f('0x26'))][(_0x929f('0x27'))](_0x929f('0x31'));}if(_[_0x929f('0x2e')](_0x4f53fe[_0x929f('0x23')][_0x929f('0x32')])){throw new db['Sequelize'][(_0x929f('0x27'))](_0x929f('0x33')+_0x33648c);}if(!_[_0x929f('0x34')](_0x33648c,_0x4f53fe[_0x929f('0x23')][_0x929f('0x32')])){throw new db[(_0x929f('0x26'))][(_0x929f('0x27'))]('mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x33648c);}_0x4051a0[_0x4f53fe['body'][_0x929f('0x32')]]=_0x4f53fe[_0x929f('0x23')]['from'];})['then'](function(){return db[_0x929f('0x17')][_0x929f('0x35')]({'where':{'id':_0x4f53fe[_0x929f('0x36')]['id']},'include':[{'model':db[_0x929f('0x37')],'as':_0x929f('0x38'),'include':[{'model':db[_0x929f('0x39')],'as':'Contacts','where':_0x4051a0,'limit':0x1,'order':[['updatedAt','DESC']]}]},{'model':db[_0x929f('0x3a')],'as':_0x929f('0x3b')}]});})['then'](function(_0x2bd600){if(_0x2bd600&&_0x2bd600[_0x929f('0x38')]){var _0x28e3a9=_0x2bd600[_0x929f('0x3c')]({'plain':!![]});_0x3bd1fc[_0x929f('0x36')]=_0x28e3a9;_0x3bd1fc[_0x929f('0x3d')]=_0x28e3a9['Applications'];_0x3bd1fc[_0x929f('0x3e')]=_0x28e3a9[_0x929f('0x38')];_0x3bd1fc[_0x929f('0x3d')]=_[_0x929f('0x3f')](_0x3bd1fc[_0x929f('0x3d')],['priority'],[_0x929f('0x40')]);if(_0x3bd1fc[_0x929f('0x36')]['Applications']){delete _0x3bd1fc[_0x929f('0x36')][_0x929f('0x3b')];}if(_0x3bd1fc['account'][_0x929f('0x38')]&&_0x3bd1fc[_0x929f('0x36')][_0x929f('0x38')]['Contacts'][_0x929f('0x41')]){return _0x3bd1fc[_0x929f('0x36')][_0x929f('0x38')][_0x929f('0x42')][0x0];}var _0x1bf736=_[_0x929f('0x43')](_0x4f53fe[_0x929f('0x23')],{'firstName':_0x4f53fe[_0x929f('0x23')]['from'],'ListId':_0x28e3a9[_0x929f('0x2d')]});_0x1bf736[_0x4f53fe[_0x929f('0x23')][_0x929f('0x32')]]=_0x4f53fe['body'][_0x929f('0x2f')];return db[_0x929f('0x39')][_0x929f('0x44')](_0x1bf736,{'fields':_0x1dea6f,'raw':!![]});}})['then'](function(_0x35c835){_0x3bd1fc[_0x929f('0x45')]=_0x35c835;if(_0x35c835){var _0x1ee3b0={'ContactId':_0x35c835['id'],'FaxAccountId':_0x4f53fe[_0x929f('0x36')]['id'],'fax':_0x35c835[_0x929f('0x24')]};return db['FaxInteraction']['find']({'where':{'ContactId':_0x35c835['id'],'closed':![],'FaxAccountId':_0x4f53fe[_0x929f('0x36')]['id']}})[_0x929f('0x1f')](function(_0x131965){if(_0x131965){return[_0x131965,![]];}return db[_0x929f('0x46')][_0x929f('0x44')](_0x1ee3b0)[_0x929f('0x1f')](function(_0x54dab7){return[_0x54dab7,!![]];});});}else{throw new Error(_0x929f('0x47'));}})[_0x929f('0x48')](function(_0x4891b4,_0x590e0c){var _0x102992=_0x4891b4[_0x929f('0x3c')]({'plain':!![]});if(_0x590e0c){if(_0x3bd1fc['account'][_0x929f('0x49')]){_0x3bd1fc[_0x929f('0x3d')]['push']({'id':0x0,'priority':_0x3bd1fc['applications'][_0x929f('0x41')]+0x1,'app':_0x929f('0x4a'),'appdata':_0x929f('0x4b'),'interval':'*,*,*,*'});}_0x102992['created']=!![];}else{_0x102992[_0x929f('0x4c')]=![];}_0x3bd1fc['interaction']=_0x102992;return db[_0x929f('0x4d')][_0x929f('0x44')]({'body':_0x4f53fe[_0x929f('0x23')][_0x929f('0x23')],'FaxAccountId':_0x4f53fe[_0x929f('0x36')]['id'],'FaxInteractionId':_0x4891b4['id'],'direction':'in','ContactId':_0x4891b4[_0x929f('0x4e')],'AttachmentId':_0x4f53fe['body'][_0x929f('0x4f')]});})['then'](function(_0x5de920){_0x3bd1fc[_0x929f('0x13')]=_0x5de920;if(_0x3bd1fc[_0x929f('0x50')][_0x929f('0x51')]){return db[_0x929f('0x52')]['find']({'attributes':['id',_0x929f('0x53')],'where':{'id':_0x3bd1fc['interaction'][_0x929f('0x51')]}})['then'](function(_0x1227f1){if(_0x1227f1){_0x3bd1fc[_0x929f('0x3d')][_0x929f('0x54')]({'id':0x0,'priority':0x0,'app':_0x929f('0x55'),'appdata':_0x1227f1[_0x929f('0x53')]+','+(_0x3bd1fc[_0x929f('0x36')]['waitForTheAssignedAgent']||0xa),'interval':_0x929f('0x56')});}return respondWithRpcPromise(_0x929f('0x57'),_0x929f('0x58'),_0x3bd1fc);});}return respondWithRpcPromise(_0x929f('0x57'),_0x929f('0x58'),_0x3bd1fc);})[_0x929f('0x1f')](function(_0x4cb6ef){_0x3e9a91(_0x4cb6ef);})[_0x929f('0x16')](function(_0x5cc00b){_0x2fa8da(_0x2967a3[_0x929f('0x11')](0x1f4,_0x5cc00b[_0x929f('0x13')]));});});};