522baf90d3dee47f2fbd805be6c16b68b5bbfa2c
[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 _0xc8d4=['client','redis','localhost','socket.io-emitter','request','then','info','FaxAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','FaxAccount,\x20%s,\x20%s','message','result','catch','GetFaxAccount','FaxAccount','findAll','raw','options','where','limit','map','include','model','attributes','debug','NotifyFaxAccount','body','fax','CmContact','describe','difference','keys','createdAt','updatedAt','isNil','ValidationError','from\x20is\x20mandatory','Sequelize','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','mapKey','from','find','account','CmList','List','FaxApplication','Applications','get','applications','list','orderBy','priority','asc','Contacts','length','defaults','ListId','create','FaxInteraction','Contact\x20not\x20found','spread','autoclose','*,*,*,*','created','interaction','FaxMessage','ContactId','AttachmentId','UserId','User','name','unshift','waitForTheAssignedAgent','queueId','FaxQueue','queue','waitForTheAssignedQueue','Start','startRouting','bluebird','randomstring','ioredis','../../config/utils','rpc','jayson/promise'];(function(_0x361c77,_0x566db7){var _0x29b089=function(_0x31e866){while(--_0x31e866){_0x361c77['push'](_0x361c77['shift']());}};_0x29b089(++_0x566db7);}(_0xc8d4,0x108));var _0x4c8d=function(_0x254eec,_0x70b3dc){_0x254eec=_0x254eec-0x0;var _0x3ba075=_0xc8d4[_0x254eec];return _0x3ba075;};'use strict';var _=require('lodash');var util=require('util');var moment=require('moment');var BPromise=require(_0x4c8d('0x0'));var rs=require(_0x4c8d('0x1'));var fs=require('fs');var Redis=require(_0x4c8d('0x2'));var db=require('../../mysqldb')['db'];var utils=require(_0x4c8d('0x3'));var logger=require('../../config/logger')(_0x4c8d('0x4'));var config=require('../../config/environment');var jayson=require(_0x4c8d('0x5'));var client=jayson[_0x4c8d('0x6')]['http']({'port':0x232c});config[_0x4c8d('0x7')]=_['defaults'](config[_0x4c8d('0x7')],{'host':_0x4c8d('0x8'),'port':0x18eb});var socket=require(_0x4c8d('0x9'))(new Redis(config[_0x4c8d('0x7')]));require('./faxAccount.socket')['register'](socket);function respondWithRpcPromise(_0x504c2e,_0x465efb,_0x511026){return new BPromise(function(_0x20e3a9,_0x4f796f){return client[_0x4c8d('0xa')](_0x504c2e,_0x511026)[_0x4c8d('0xb')](function(_0x4d6c60){logger[_0x4c8d('0xc')]('FaxAccount,\x20%s,\x20%s',_0x465efb,'request\x20sent');logger['debug'](_0x4c8d('0xd'),_0x465efb,_0x4c8d('0xe'),JSON[_0x4c8d('0xf')](_0x4d6c60));if(_0x4d6c60[_0x4c8d('0x10')]){if(_0x4d6c60[_0x4c8d('0x10')][_0x4c8d('0x11')]===0x1f4){logger[_0x4c8d('0x10')](_0x4c8d('0x12'),_0x465efb,_0x4d6c60[_0x4c8d('0x10')][_0x4c8d('0x13')]);return _0x4f796f(_0x4d6c60[_0x4c8d('0x10')][_0x4c8d('0x13')]);}logger[_0x4c8d('0x10')](_0x4c8d('0x12'),_0x465efb,_0x4d6c60[_0x4c8d('0x10')][_0x4c8d('0x13')]);return _0x20e3a9(_0x4d6c60[_0x4c8d('0x10')]['message']);}else{logger[_0x4c8d('0xc')]('FaxAccount,\x20%s,\x20%s',_0x465efb,_0x4c8d('0xe'));_0x20e3a9(_0x4d6c60[_0x4c8d('0x14')]['message']);}})[_0x4c8d('0x15')](function(_0x1359a1){logger[_0x4c8d('0x10')](_0x4c8d('0x12'),_0x465efb,_0x1359a1);_0x4f796f(_0x1359a1);});});}exports[_0x4c8d('0x16')]=function(_0xff6376){var _0x24fe3e=this;return new Promise(function(_0x407d76,_0x5a36ee){return db[_0x4c8d('0x17')][_0x4c8d('0x18')]({'raw':_0xff6376['options']?_0xff6376['options'][_0x4c8d('0x19')]===undefined?!![]:![]:!![],'where':_0xff6376[_0x4c8d('0x1a')]?_0xff6376['options'][_0x4c8d('0x1b')]||null:null,'attributes':_0xff6376[_0x4c8d('0x1a')]?_0xff6376[_0x4c8d('0x1a')]['attributes']||null:null,'limit':_0xff6376['options']?_0xff6376[_0x4c8d('0x1a')][_0x4c8d('0x1c')]||null:null,'include':_0xff6376[_0x4c8d('0x1a')]?_0xff6376['options']['include']?_[_0x4c8d('0x1d')](_0xff6376[_0x4c8d('0x1a')][_0x4c8d('0x1e')],function(_0x562aca){return{'model':db[_0x562aca[_0x4c8d('0x1f')]],'as':_0x562aca['as'],'attributes':_0x562aca[_0x4c8d('0x20')],'include':_0x562aca[_0x4c8d('0x1e')]?_[_0x4c8d('0x1d')](_0x562aca['include'],function(_0x43be0d){return{'model':db[_0x43be0d[_0x4c8d('0x1f')]],'as':_0x43be0d['as'],'attributes':_0x43be0d[_0x4c8d('0x20')],'include':_0x43be0d[_0x4c8d('0x1e')]?_['map'](_0x43be0d[_0x4c8d('0x1e')],function(_0x46c347){return{'model':db[_0x46c347[_0x4c8d('0x1f')]],'as':_0x46c347['as'],'attributes':_0x46c347[_0x4c8d('0x20')]};}):[]};}):[]};}):[]:[]})[_0x4c8d('0xb')](function(_0x311a89){logger[_0x4c8d('0xc')](_0x4c8d('0x16'),_0xff6376);logger[_0x4c8d('0x21')](_0x4c8d('0x16'),_0xff6376,JSON[_0x4c8d('0xf')](_0x311a89));_0x407d76(_0x311a89);})['catch'](function(_0x134447){logger[_0x4c8d('0x10')](_0x4c8d('0x16'),_0x134447['message'],_0xff6376);_0x5a36ee(_0x24fe3e[_0x4c8d('0x10')](0x1f4,_0x134447['message']));});});};exports[_0x4c8d('0x22')]=function(_0x53fa61,_0x5abcf7,_0x3d3018){var _0x22586d=this;return new Promise(function(_0x160abb,_0x58685e){var _0x440a05={'body':_0x53fa61[_0x4c8d('0x23')],'channel':_0x4c8d('0x24')};var _0x5b14d3=[];var _0x24e851=[];var _0x57827b={};return db[_0x4c8d('0x25')][_0x4c8d('0x26')]()[_0x4c8d('0xb')](function(_0x54f684){if(!_0x54f684){throw new db['Sequelize']['ValidationError']('no\x20available\x20attributes');}_0x5b14d3=_[_0x4c8d('0x27')](_[_0x4c8d('0x28')](_0x54f684),[_0x4c8d('0x29'),_0x4c8d('0x2a')]);_0x24e851=_['difference'](_[_0x4c8d('0x28')](_0x54f684),[_0x4c8d('0x29'),'updatedAt','CompanyId','ListId']);if(_0x53fa61[_0x4c8d('0x23')]['id']){delete _0x53fa61[_0x4c8d('0x23')]['id'];}if(_[_0x4c8d('0x2b')](_0x53fa61['body']['from'])){throw new db['Sequelize'][(_0x4c8d('0x2c'))](_0x4c8d('0x2d'));}if(_[_0x4c8d('0x2b')](_0x53fa61[_0x4c8d('0x23')][_0x4c8d('0x23')])||_0x53fa61[_0x4c8d('0x23')][_0x4c8d('0x23')]===''){throw new db[(_0x4c8d('0x2e'))][(_0x4c8d('0x2c'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0x4c8d('0x2b')](_0x53fa61['body']['mapKey'])){throw new db[(_0x4c8d('0x2e'))][(_0x4c8d('0x2c'))](_0x4c8d('0x2f')+_0x24e851);}if(!_[_0x4c8d('0x30')](_0x24e851,_0x53fa61[_0x4c8d('0x23')]['mapKey'])){throw new db[(_0x4c8d('0x2e'))]['ValidationError'](_0x4c8d('0x31')+_0x24e851);}_0x57827b[_0x53fa61['body'][_0x4c8d('0x32')]]=_0x53fa61[_0x4c8d('0x23')][_0x4c8d('0x33')];})[_0x4c8d('0xb')](function(){return db['FaxAccount'][_0x4c8d('0x34')]({'where':{'id':_0x53fa61[_0x4c8d('0x35')]['id']},'include':[{'model':db[_0x4c8d('0x36')],'as':_0x4c8d('0x37'),'include':[{'model':db['CmContact'],'as':'Contacts','where':_0x57827b,'limit':0x1,'order':[[_0x4c8d('0x2a'),'DESC']]}]},{'model':db[_0x4c8d('0x38')],'as':_0x4c8d('0x39')}]});})[_0x4c8d('0xb')](function(_0xfddfcb){if(_0xfddfcb&&_0xfddfcb['List']){var _0x5224dc=_0xfddfcb[_0x4c8d('0x3a')]({'plain':!![]});_0x440a05['account']=_0x5224dc;_0x440a05[_0x4c8d('0x3b')]=_0x5224dc[_0x4c8d('0x39')];_0x440a05[_0x4c8d('0x3c')]=_0x5224dc[_0x4c8d('0x37')];_0x440a05[_0x4c8d('0x3b')]=_[_0x4c8d('0x3d')](_0x440a05[_0x4c8d('0x3b')],[_0x4c8d('0x3e')],[_0x4c8d('0x3f')]);if(_0x440a05[_0x4c8d('0x35')][_0x4c8d('0x39')]){delete _0x440a05[_0x4c8d('0x35')][_0x4c8d('0x39')];}if(_0x440a05[_0x4c8d('0x35')][_0x4c8d('0x37')]&&_0x440a05[_0x4c8d('0x35')][_0x4c8d('0x37')][_0x4c8d('0x40')][_0x4c8d('0x41')]){return _0x440a05['account'][_0x4c8d('0x37')][_0x4c8d('0x40')][0x0];}var _0x4d97f4=_[_0x4c8d('0x42')](_0x53fa61[_0x4c8d('0x23')],{'firstName':_0x53fa61[_0x4c8d('0x23')]['from'],'ListId':_0x5224dc[_0x4c8d('0x43')]});_0x4d97f4[_0x53fa61['body']['mapKey']]=_0x53fa61[_0x4c8d('0x23')][_0x4c8d('0x33')];return db['CmContact'][_0x4c8d('0x44')](_0x4d97f4,{'fields':_0x5b14d3,'raw':!![]});}})[_0x4c8d('0xb')](function(_0x55112b){_0x440a05['contact']=_0x55112b;if(_0x55112b){var _0x1eac77={'ContactId':_0x55112b['id'],'FaxAccountId':_0x53fa61[_0x4c8d('0x35')]['id'],'fax':_0x55112b[_0x4c8d('0x24')]};return db[_0x4c8d('0x45')][_0x4c8d('0x34')]({'where':{'ContactId':_0x55112b['id'],'closed':![],'FaxAccountId':_0x53fa61['account']['id']}})[_0x4c8d('0xb')](function(_0x308f57){if(_0x308f57){return[_0x308f57,![]];}return db[_0x4c8d('0x45')][_0x4c8d('0x44')](_0x1eac77)[_0x4c8d('0xb')](function(_0x1a027a){return[_0x1a027a,!![]];});});}else{throw new Error(_0x4c8d('0x46'));}})[_0x4c8d('0x47')](function(_0x10dee5,_0x551d5b){var _0x58ab0d=_0x10dee5[_0x4c8d('0x3a')]({'plain':!![]});if(_0x551d5b){if(_0x440a05['account'][_0x4c8d('0x48')]){_0x440a05[_0x4c8d('0x3b')]['push']({'id':0x0,'priority':_0x440a05['applications']['length']+0x1,'app':'close','appdata':'unmanaged','interval':_0x4c8d('0x49')});}_0x58ab0d[_0x4c8d('0x4a')]=!![];}else{_0x58ab0d[_0x4c8d('0x4a')]=![];}_0x440a05[_0x4c8d('0x4b')]=_0x58ab0d;return db[_0x4c8d('0x4c')][_0x4c8d('0x44')]({'body':_0x53fa61[_0x4c8d('0x23')]['body'],'FaxAccountId':_0x53fa61[_0x4c8d('0x35')]['id'],'FaxInteractionId':_0x10dee5['id'],'direction':'in','ContactId':_0x10dee5[_0x4c8d('0x4d')],'AttachmentId':_0x53fa61['body'][_0x4c8d('0x4e')]});})['then'](function(_0x329d99){_0x440a05['message']=_0x329d99;if(!_0x440a05[_0x4c8d('0x4b')][_0x4c8d('0x4f')])return;return db[_0x4c8d('0x50')][_0x4c8d('0x34')]({'attributes':['id',_0x4c8d('0x51')],'where':{'id':_0x440a05[_0x4c8d('0x4b')][_0x4c8d('0x4f')]}});})[_0x4c8d('0xb')](function(_0xbf7281){if(_0xbf7281){_0x440a05[_0x4c8d('0x3b')][_0x4c8d('0x52')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0xbf7281[_0x4c8d('0x51')]+','+(_0x440a05[_0x4c8d('0x35')][_0x4c8d('0x53')]||0xa),'interval':'*,*,*,*'});}if(!_0x440a05['interaction'][_0x4c8d('0x54')])return;return db[_0x4c8d('0x55')][_0x4c8d('0x34')]({'attributes':['id',_0x4c8d('0x51')],'where':{'id':_0x440a05['interaction']['queueId']}});})[_0x4c8d('0xb')](function(_0x409fab){if(_0x409fab){_0x440a05[_0x4c8d('0x3b')]['unshift']({'id':0x0,'priority':0x1,'app':_0x4c8d('0x56'),'appdata':_0x409fab[_0x4c8d('0x51')]+','+(_0x440a05[_0x4c8d('0x35')][_0x4c8d('0x57')]||0x12c),'interval':_0x4c8d('0x49')});}return respondWithRpcPromise(_0x4c8d('0x58'),_0x4c8d('0x59'),_0x440a05);})[_0x4c8d('0xb')](function(_0x16e8ff){_0x160abb(_0x16e8ff);})['catch'](function(_0x3b4a1c){_0x58685e(_0x22586d[_0x4c8d('0x10')](0x1f4,_0x3b4a1c[_0x4c8d('0x13')]));});});};