aa13b00f1154719bcb2ced050547391ab0d6e225
[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 _0xf289=['name','waitForTheAssignedAgent','FaxQueue','queue','waitForTheAssignedQueue','Start','startRouting','util','bluebird','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','client','http','redis','socket.io-emitter','./faxAccount.socket','register','request','then','info','FaxAccount,\x20%s,\x20%s','request\x20sent','debug','FaxAccount,\x20%s,\x20%s,\x20%s','error','code','message','result','catch','FaxAccount','findAll','options','raw','where','attributes','limit','include','model','GetFaxAccount','NotifyFaxAccount','body','fax','CmContact','Sequelize','ValidationError','no\x20available\x20attributes','difference','keys','updatedAt','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','account','CmList','List','Contacts','DESC','FaxApplication','get','applications','Applications','priority','length','create','contact','FaxInteraction','Contact\x20not\x20found','autoclose','close','unmanaged','*,*,*,*','created','interaction','FaxMessage','ContactId','AttachmentId','UserId','User','find','unshift'];(function(_0x4b3f3e,_0x47dfcc){var _0x921fc3=function(_0x3e4b3b){while(--_0x3e4b3b){_0x4b3f3e['push'](_0x4b3f3e['shift']());}};_0x921fc3(++_0x47dfcc);}(_0xf289,0x1c4));var _0x9f28=function(_0x18c472,_0x12335f){_0x18c472=_0x18c472-0x0;var _0x178de4=_0xf289[_0x18c472];return _0x178de4;};'use strict';var _=require('lodash');var util=require(_0x9f28('0x0'));var moment=require('moment');var BPromise=require(_0x9f28('0x1'));var rs=require('randomstring');var fs=require('fs');var Redis=require('ioredis');var db=require(_0x9f28('0x2'))['db'];var utils=require(_0x9f28('0x3'));var logger=require(_0x9f28('0x4'))(_0x9f28('0x5'));var config=require(_0x9f28('0x6'));var jayson=require('jayson/promise');var client=jayson[_0x9f28('0x7')][_0x9f28('0x8')]({'port':0x232c});config[_0x9f28('0x9')]=_['defaults'](config[_0x9f28('0x9')],{'host':'localhost','port':0x18eb});var socket=require(_0x9f28('0xa'))(new Redis(config['redis']));require(_0x9f28('0xb'))[_0x9f28('0xc')](socket);function respondWithRpcPromise(_0x54177b,_0x6e107d,_0x3d2249){return new BPromise(function(_0x45dae8,_0x1d2d7b){return client[_0x9f28('0xd')](_0x54177b,_0x3d2249)[_0x9f28('0xe')](function(_0x232c15){logger[_0x9f28('0xf')](_0x9f28('0x10'),_0x6e107d,_0x9f28('0x11'));logger[_0x9f28('0x12')](_0x9f28('0x13'),_0x6e107d,_0x9f28('0x11'),JSON['stringify'](_0x232c15));if(_0x232c15[_0x9f28('0x14')]){if(_0x232c15[_0x9f28('0x14')][_0x9f28('0x15')]===0x1f4){logger[_0x9f28('0x14')](_0x9f28('0x10'),_0x6e107d,_0x232c15[_0x9f28('0x14')][_0x9f28('0x16')]);return _0x1d2d7b(_0x232c15['error'][_0x9f28('0x16')]);}logger[_0x9f28('0x14')](_0x9f28('0x10'),_0x6e107d,_0x232c15[_0x9f28('0x14')][_0x9f28('0x16')]);return _0x45dae8(_0x232c15[_0x9f28('0x14')][_0x9f28('0x16')]);}else{logger[_0x9f28('0xf')](_0x9f28('0x10'),_0x6e107d,_0x9f28('0x11'));_0x45dae8(_0x232c15[_0x9f28('0x17')][_0x9f28('0x16')]);}})[_0x9f28('0x18')](function(_0x47b7d7){logger[_0x9f28('0x14')]('FaxAccount,\x20%s,\x20%s',_0x6e107d,_0x47b7d7);_0x1d2d7b(_0x47b7d7);});});}exports['GetFaxAccount']=function(_0x538132){var _0x32637f=this;return new Promise(function(_0x574032,_0x5783bc){return db[_0x9f28('0x19')][_0x9f28('0x1a')]({'raw':_0x538132[_0x9f28('0x1b')]?_0x538132['options'][_0x9f28('0x1c')]===undefined?!![]:![]:!![],'where':_0x538132['options']?_0x538132['options'][_0x9f28('0x1d')]||null:null,'attributes':_0x538132[_0x9f28('0x1b')]?_0x538132[_0x9f28('0x1b')][_0x9f28('0x1e')]||null:null,'limit':_0x538132[_0x9f28('0x1b')]?_0x538132['options'][_0x9f28('0x1f')]||null:null,'include':_0x538132['options']?_0x538132[_0x9f28('0x1b')][_0x9f28('0x20')]?_['map'](_0x538132[_0x9f28('0x1b')][_0x9f28('0x20')],function(_0x470c08){return{'model':db[_0x470c08[_0x9f28('0x21')]],'as':_0x470c08['as'],'attributes':_0x470c08[_0x9f28('0x1e')],'include':_0x470c08['include']?_['map'](_0x470c08[_0x9f28('0x20')],function(_0x4b962c){return{'model':db[_0x4b962c[_0x9f28('0x21')]],'as':_0x4b962c['as'],'attributes':_0x4b962c[_0x9f28('0x1e')],'include':_0x4b962c[_0x9f28('0x20')]?_['map'](_0x4b962c['include'],function(_0x2354d2){return{'model':db[_0x2354d2[_0x9f28('0x21')]],'as':_0x2354d2['as'],'attributes':_0x2354d2[_0x9f28('0x1e')]};}):[]};}):[]};}):[]:[]})[_0x9f28('0xe')](function(_0x132b18){logger[_0x9f28('0xf')](_0x9f28('0x22'),_0x538132);logger[_0x9f28('0x12')]('GetFaxAccount',_0x538132,JSON['stringify'](_0x132b18));_0x574032(_0x132b18);})[_0x9f28('0x18')](function(_0x371c65){logger[_0x9f28('0x14')](_0x9f28('0x22'),_0x371c65[_0x9f28('0x16')],_0x538132);_0x5783bc(_0x32637f[_0x9f28('0x14')](0x1f4,_0x371c65[_0x9f28('0x16')]));});});};exports[_0x9f28('0x23')]=function(_0x412171,_0x19adc3,_0x297e19){var _0x191d29=this;return new Promise(function(_0x3e71f7,_0x435128){var _0x7f66df={'body':_0x412171[_0x9f28('0x24')],'channel':_0x9f28('0x25')};var _0x143785=[];var _0xa46551=[];var _0x23060f={};return db[_0x9f28('0x26')]['describe']()[_0x9f28('0xe')](function(_0x29aa72){if(!_0x29aa72){throw new db[(_0x9f28('0x27'))][(_0x9f28('0x28'))](_0x9f28('0x29'));}_0x143785=_[_0x9f28('0x2a')](_[_0x9f28('0x2b')](_0x29aa72),['createdAt','updatedAt']);_0xa46551=_[_0x9f28('0x2a')](_[_0x9f28('0x2b')](_0x29aa72),['createdAt',_0x9f28('0x2c'),_0x9f28('0x2d'),_0x9f28('0x2e')]);if(_0x412171[_0x9f28('0x24')]['id']){delete _0x412171['body']['id'];}if(_[_0x9f28('0x2f')](_0x412171['body'][_0x9f28('0x30')])){throw new db[(_0x9f28('0x27'))][(_0x9f28('0x28'))](_0x9f28('0x31'));}if(_[_0x9f28('0x2f')](_0x412171[_0x9f28('0x24')]['body'])||_0x412171['body'][_0x9f28('0x24')]===''){throw new db[(_0x9f28('0x27'))][(_0x9f28('0x28'))](_0x9f28('0x32'));}if(_['isNil'](_0x412171['body'][_0x9f28('0x33')])){throw new db[(_0x9f28('0x27'))][(_0x9f28('0x28'))]('mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0xa46551);}if(!_[_0x9f28('0x34')](_0xa46551,_0x412171[_0x9f28('0x24')][_0x9f28('0x33')])){throw new db[(_0x9f28('0x27'))][(_0x9f28('0x28'))](_0x9f28('0x35')+_0xa46551);}_0x23060f[_0x412171[_0x9f28('0x24')][_0x9f28('0x33')]]=_0x412171[_0x9f28('0x24')][_0x9f28('0x30')];})['then'](function(){return db[_0x9f28('0x19')]['find']({'where':{'id':_0x412171[_0x9f28('0x36')]['id']},'include':[{'model':db[_0x9f28('0x37')],'as':_0x9f28('0x38'),'include':[{'model':db['CmContact'],'as':_0x9f28('0x39'),'where':_0x23060f,'limit':0x1,'order':[[_0x9f28('0x2c'),_0x9f28('0x3a')]]}]},{'model':db[_0x9f28('0x3b')],'as':'Applications'}]});})['then'](function(_0x2a28f4){if(_0x2a28f4&&_0x2a28f4['List']){var _0x240020=_0x2a28f4[_0x9f28('0x3c')]({'plain':!![]});_0x7f66df[_0x9f28('0x36')]=_0x240020;_0x7f66df[_0x9f28('0x3d')]=_0x240020[_0x9f28('0x3e')];_0x7f66df['list']=_0x240020['List'];_0x7f66df[_0x9f28('0x3d')]=_['orderBy'](_0x7f66df[_0x9f28('0x3d')],[_0x9f28('0x3f')],['asc']);if(_0x7f66df[_0x9f28('0x36')][_0x9f28('0x3e')]){delete _0x7f66df[_0x9f28('0x36')]['Applications'];}if(_0x7f66df[_0x9f28('0x36')][_0x9f28('0x38')]&&_0x7f66df[_0x9f28('0x36')][_0x9f28('0x38')][_0x9f28('0x39')][_0x9f28('0x40')]){return _0x7f66df[_0x9f28('0x36')][_0x9f28('0x38')]['Contacts'][0x0];}var _0x4177ba=_['defaults'](_0x412171['body'],{'firstName':_0x412171[_0x9f28('0x24')]['from'],'ListId':_0x240020[_0x9f28('0x2e')]});_0x4177ba[_0x412171[_0x9f28('0x24')][_0x9f28('0x33')]]=_0x412171[_0x9f28('0x24')][_0x9f28('0x30')];return db[_0x9f28('0x26')][_0x9f28('0x41')](_0x4177ba,{'fields':_0x143785,'raw':!![]});}})[_0x9f28('0xe')](function(_0x32ae5f){_0x7f66df[_0x9f28('0x42')]=_0x32ae5f;if(_0x32ae5f){var _0x399448={'ContactId':_0x32ae5f['id'],'FaxAccountId':_0x412171[_0x9f28('0x36')]['id'],'fax':_0x32ae5f[_0x9f28('0x25')]};return db['FaxInteraction']['find']({'where':{'ContactId':_0x32ae5f['id'],'closed':![],'FaxAccountId':_0x412171[_0x9f28('0x36')]['id']}})[_0x9f28('0xe')](function(_0x3619db){if(_0x3619db){return[_0x3619db,![]];}return db[_0x9f28('0x43')][_0x9f28('0x41')](_0x399448)['then'](function(_0x2aa69e){return[_0x2aa69e,!![]];});});}else{throw new Error(_0x9f28('0x44'));}})['spread'](function(_0x1d6fe3,_0x356e49){var _0x396a9c=_0x1d6fe3[_0x9f28('0x3c')]({'plain':!![]});if(_0x356e49){if(_0x7f66df[_0x9f28('0x36')][_0x9f28('0x45')]){_0x7f66df[_0x9f28('0x3d')]['push']({'id':0x0,'priority':_0x7f66df[_0x9f28('0x3d')]['length']+0x1,'app':_0x9f28('0x46'),'appdata':_0x9f28('0x47'),'interval':_0x9f28('0x48')});}_0x396a9c[_0x9f28('0x49')]=!![];}else{_0x396a9c[_0x9f28('0x49')]=![];}_0x7f66df[_0x9f28('0x4a')]=_0x396a9c;return db[_0x9f28('0x4b')][_0x9f28('0x41')]({'body':_0x412171[_0x9f28('0x24')][_0x9f28('0x24')],'FaxAccountId':_0x412171[_0x9f28('0x36')]['id'],'FaxInteractionId':_0x1d6fe3['id'],'direction':'in','ContactId':_0x1d6fe3[_0x9f28('0x4c')],'AttachmentId':_0x412171[_0x9f28('0x24')][_0x9f28('0x4d')]});})[_0x9f28('0xe')](function(_0x514450){_0x7f66df[_0x9f28('0x16')]=_0x514450;if(!_0x7f66df[_0x9f28('0x4a')][_0x9f28('0x4e')])return;return db[_0x9f28('0x4f')][_0x9f28('0x50')]({'attributes':['id','name'],'where':{'id':_0x7f66df[_0x9f28('0x4a')][_0x9f28('0x4e')]}});})['then'](function(_0x548ea8){if(_0x548ea8){_0x7f66df[_0x9f28('0x3d')][_0x9f28('0x51')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x548ea8[_0x9f28('0x52')]+','+(_0x7f66df['account'][_0x9f28('0x53')]||0xa),'interval':_0x9f28('0x48')});}if(!_0x7f66df[_0x9f28('0x4a')]['queueId'])return;return db[_0x9f28('0x54')][_0x9f28('0x50')]({'attributes':['id',_0x9f28('0x52')],'where':{'id':_0x7f66df[_0x9f28('0x4a')]['queueId']}});})[_0x9f28('0xe')](function(_0x36ec0f){if(_0x36ec0f){_0x7f66df[_0x9f28('0x3d')]['unshift']({'id':0x0,'priority':0x1,'app':_0x9f28('0x55'),'appdata':_0x36ec0f[_0x9f28('0x52')]+','+(_0x7f66df[_0x9f28('0x36')][_0x9f28('0x56')]||0x12c),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0x9f28('0x57'),_0x9f28('0x58'),_0x7f66df);})[_0x9f28('0xe')](function(_0x399e83){_0x3e71f7(_0x399e83);})[_0x9f28('0x18')](function(_0x312b88){_0x435128(_0x191d29[_0x9f28('0x14')](0x1f4,_0x312b88[_0x9f28('0x16')]));});});};