ea7285a37792d2b691150cfd1f894f80e1807db0
[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 _0x08b7=['mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','account','CmList','List','Contacts','DESC','FaxApplication','Applications','get','list','applications','orderBy','priority','asc','length','create','FaxInteraction','find','Contact\x20not\x20found','spread','push','close','unmanaged','*,*,*,*','created','interaction','ContactId','AttachmentId','UserId','User','name','unshift','waitForTheAssignedAgent','FaxQueue','Start','startRouting','lodash','util','moment','randomstring','ioredis','../../mysqldb','../../config/utils','rpc','../../config/environment','jayson/promise','redis','defaults','localhost','socket.io-emitter','./faxAccount.socket','register','request','then','info','FaxAccount,\x20%s,\x20%s','debug','FaxAccount,\x20%s,\x20%s,\x20%s','request\x20sent','error','code','message','result','catch','FaxAccount','findAll','options','raw','where','limit','map','include','attributes','model','GetFaxAccount','body','fax','describe','ValidationError','difference','keys','createdAt','updatedAt','CompanyId','ListId','isNil','from','Sequelize','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey'];(function(_0x334841,_0x2bbc4c){var _0x5beb4b=function(_0x4bea72){while(--_0x4bea72){_0x334841['push'](_0x334841['shift']());}};_0x5beb4b(++_0x2bbc4c);}(_0x08b7,0x195));var _0x708b=function(_0x19b18f,_0x2f0a0c){_0x19b18f=_0x19b18f-0x0;var _0x9c3c37=_0x08b7[_0x19b18f];return _0x9c3c37;};'use strict';var _=require(_0x708b('0x0'));var util=require(_0x708b('0x1'));var moment=require(_0x708b('0x2'));var BPromise=require('bluebird');var rs=require(_0x708b('0x3'));var fs=require('fs');var Redis=require(_0x708b('0x4'));var db=require(_0x708b('0x5'))['db'];var utils=require(_0x708b('0x6'));var logger=require('../../config/logger')(_0x708b('0x7'));var config=require(_0x708b('0x8'));var jayson=require(_0x708b('0x9'));var client=jayson['client']['http']({'port':0x232c});config[_0x708b('0xa')]=_[_0x708b('0xb')](config['redis'],{'host':_0x708b('0xc'),'port':0x18eb});var socket=require(_0x708b('0xd'))(new Redis(config[_0x708b('0xa')]));require(_0x708b('0xe'))[_0x708b('0xf')](socket);function respondWithRpcPromise(_0xc94fc,_0x4517c2,_0x1354e3){return new BPromise(function(_0x31daea,_0x226e34){return client[_0x708b('0x10')](_0xc94fc,_0x1354e3)[_0x708b('0x11')](function(_0x342935){logger[_0x708b('0x12')](_0x708b('0x13'),_0x4517c2,'request\x20sent');logger[_0x708b('0x14')](_0x708b('0x15'),_0x4517c2,_0x708b('0x16'),JSON['stringify'](_0x342935));if(_0x342935[_0x708b('0x17')]){if(_0x342935[_0x708b('0x17')][_0x708b('0x18')]===0x1f4){logger['error']('FaxAccount,\x20%s,\x20%s',_0x4517c2,_0x342935['error'][_0x708b('0x19')]);return _0x226e34(_0x342935[_0x708b('0x17')][_0x708b('0x19')]);}logger[_0x708b('0x17')](_0x708b('0x13'),_0x4517c2,_0x342935[_0x708b('0x17')]['message']);return _0x31daea(_0x342935['error'][_0x708b('0x19')]);}else{logger['info'](_0x708b('0x13'),_0x4517c2,_0x708b('0x16'));_0x31daea(_0x342935[_0x708b('0x1a')][_0x708b('0x19')]);}})[_0x708b('0x1b')](function(_0x4b1520){logger[_0x708b('0x17')](_0x708b('0x13'),_0x4517c2,_0x4b1520);_0x226e34(_0x4b1520);});});}exports['GetFaxAccount']=function(_0x18873e){var _0x335119=this;return new Promise(function(_0x1da57a,_0x47bcc5){return db[_0x708b('0x1c')][_0x708b('0x1d')]({'raw':_0x18873e[_0x708b('0x1e')]?_0x18873e[_0x708b('0x1e')][_0x708b('0x1f')]===undefined?!![]:![]:!![],'where':_0x18873e[_0x708b('0x1e')]?_0x18873e[_0x708b('0x1e')][_0x708b('0x20')]||null:null,'attributes':_0x18873e[_0x708b('0x1e')]?_0x18873e[_0x708b('0x1e')]['attributes']||null:null,'limit':_0x18873e[_0x708b('0x1e')]?_0x18873e[_0x708b('0x1e')][_0x708b('0x21')]||null:null,'include':_0x18873e[_0x708b('0x1e')]?_0x18873e[_0x708b('0x1e')]['include']?_[_0x708b('0x22')](_0x18873e[_0x708b('0x1e')][_0x708b('0x23')],function(_0x5b5f32){return{'model':db[_0x5b5f32['model']],'as':_0x5b5f32['as'],'attributes':_0x5b5f32[_0x708b('0x24')],'include':_0x5b5f32['include']?_[_0x708b('0x22')](_0x5b5f32[_0x708b('0x23')],function(_0x159bb3){return{'model':db[_0x159bb3['model']],'as':_0x159bb3['as'],'attributes':_0x159bb3[_0x708b('0x24')],'include':_0x159bb3[_0x708b('0x23')]?_[_0x708b('0x22')](_0x159bb3[_0x708b('0x23')],function(_0x23a84a){return{'model':db[_0x23a84a[_0x708b('0x25')]],'as':_0x23a84a['as'],'attributes':_0x23a84a[_0x708b('0x24')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x9aa773){logger['info'](_0x708b('0x26'),_0x18873e);logger[_0x708b('0x14')](_0x708b('0x26'),_0x18873e,JSON['stringify'](_0x9aa773));_0x1da57a(_0x9aa773);})[_0x708b('0x1b')](function(_0x34e854){logger[_0x708b('0x17')](_0x708b('0x26'),_0x34e854[_0x708b('0x19')],_0x18873e);_0x47bcc5(_0x335119[_0x708b('0x17')](0x1f4,_0x34e854[_0x708b('0x19')]));});});};exports['NotifyFaxAccount']=function(_0x23ad87,_0x41adf0,_0x5c5cb3){var _0x56c313=this;return new Promise(function(_0x2f27eb,_0x2fedf3){var _0x43b9e8={'body':_0x23ad87[_0x708b('0x27')],'channel':_0x708b('0x28')};var _0x54e8b0=[];var _0xb474dd=[];var _0x3a2f82={};return db['CmContact'][_0x708b('0x29')]()[_0x708b('0x11')](function(_0x39da0f){if(!_0x39da0f){throw new db['Sequelize'][(_0x708b('0x2a'))]('no\x20available\x20attributes');}_0x54e8b0=_[_0x708b('0x2b')](_[_0x708b('0x2c')](_0x39da0f),[_0x708b('0x2d'),'updatedAt']);_0xb474dd=_[_0x708b('0x2b')](_[_0x708b('0x2c')](_0x39da0f),[_0x708b('0x2d'),_0x708b('0x2e'),_0x708b('0x2f'),_0x708b('0x30')]);if(_0x23ad87['body']['id']){delete _0x23ad87[_0x708b('0x27')]['id'];}if(_[_0x708b('0x31')](_0x23ad87[_0x708b('0x27')][_0x708b('0x32')])){throw new db[(_0x708b('0x33'))][(_0x708b('0x2a'))](_0x708b('0x34'));}if(_[_0x708b('0x31')](_0x23ad87[_0x708b('0x27')][_0x708b('0x27')])||_0x23ad87[_0x708b('0x27')][_0x708b('0x27')]===''){throw new db[(_0x708b('0x33'))][(_0x708b('0x2a'))](_0x708b('0x35'));}if(_['isNil'](_0x23ad87['body'][_0x708b('0x36')])){throw new db['Sequelize'][(_0x708b('0x2a'))](_0x708b('0x37')+_0xb474dd);}if(!_['includes'](_0xb474dd,_0x23ad87[_0x708b('0x27')]['mapKey'])){throw new db[(_0x708b('0x33'))]['ValidationError'](_0x708b('0x38')+_0xb474dd);}_0x3a2f82[_0x23ad87[_0x708b('0x27')]['mapKey']]=_0x23ad87[_0x708b('0x27')][_0x708b('0x32')];})[_0x708b('0x11')](function(){return db[_0x708b('0x1c')]['find']({'where':{'id':_0x23ad87[_0x708b('0x39')]['id']},'include':[{'model':db[_0x708b('0x3a')],'as':_0x708b('0x3b'),'include':[{'model':db['CmContact'],'as':_0x708b('0x3c'),'where':_0x3a2f82,'limit':0x1,'order':[[_0x708b('0x2e'),_0x708b('0x3d')]]}]},{'model':db[_0x708b('0x3e')],'as':_0x708b('0x3f')}]});})[_0x708b('0x11')](function(_0xcd3072){if(_0xcd3072&&_0xcd3072[_0x708b('0x3b')]){var _0x3f7532=_0xcd3072[_0x708b('0x40')]({'plain':!![]});_0x43b9e8[_0x708b('0x39')]=_0x3f7532;_0x43b9e8['applications']=_0x3f7532['Applications'];_0x43b9e8[_0x708b('0x41')]=_0x3f7532[_0x708b('0x3b')];_0x43b9e8[_0x708b('0x42')]=_[_0x708b('0x43')](_0x43b9e8['applications'],[_0x708b('0x44')],[_0x708b('0x45')]);if(_0x43b9e8[_0x708b('0x39')][_0x708b('0x3f')]){delete _0x43b9e8[_0x708b('0x39')][_0x708b('0x3f')];}if(_0x43b9e8[_0x708b('0x39')][_0x708b('0x3b')]&&_0x43b9e8[_0x708b('0x39')][_0x708b('0x3b')][_0x708b('0x3c')][_0x708b('0x46')]){return _0x43b9e8[_0x708b('0x39')][_0x708b('0x3b')][_0x708b('0x3c')][0x0];}var _0x6eb3a3=_[_0x708b('0xb')](_0x23ad87[_0x708b('0x27')],{'firstName':_0x23ad87[_0x708b('0x27')][_0x708b('0x32')],'ListId':_0x3f7532[_0x708b('0x30')]});_0x6eb3a3[_0x23ad87[_0x708b('0x27')][_0x708b('0x36')]]=_0x23ad87[_0x708b('0x27')][_0x708b('0x32')];return db['CmContact'][_0x708b('0x47')](_0x6eb3a3,{'fields':_0x54e8b0,'raw':!![]});}})[_0x708b('0x11')](function(_0x2418f4){_0x43b9e8['contact']=_0x2418f4;if(_0x2418f4){var _0x5c6b8f={'ContactId':_0x2418f4['id'],'FaxAccountId':_0x23ad87['account']['id'],'fax':_0x2418f4[_0x708b('0x28')]};return db[_0x708b('0x48')][_0x708b('0x49')]({'where':{'ContactId':_0x2418f4['id'],'closed':![],'FaxAccountId':_0x23ad87[_0x708b('0x39')]['id']}})['then'](function(_0x450a00){if(_0x450a00){return[_0x450a00,![]];}return db[_0x708b('0x48')][_0x708b('0x47')](_0x5c6b8f)[_0x708b('0x11')](function(_0x3c07a7){return[_0x3c07a7,!![]];});});}else{throw new Error(_0x708b('0x4a'));}})[_0x708b('0x4b')](function(_0x3b6d0b,_0x5dc774){var _0xdebad0=_0x3b6d0b[_0x708b('0x40')]({'plain':!![]});if(_0x5dc774){if(_0x43b9e8[_0x708b('0x39')]['autoclose']){_0x43b9e8['applications'][_0x708b('0x4c')]({'id':0x0,'priority':_0x43b9e8[_0x708b('0x42')][_0x708b('0x46')]+0x1,'app':_0x708b('0x4d'),'appdata':_0x708b('0x4e'),'interval':_0x708b('0x4f')});}_0xdebad0[_0x708b('0x50')]=!![];}else{_0xdebad0[_0x708b('0x50')]=![];}_0x43b9e8[_0x708b('0x51')]=_0xdebad0;return db['FaxMessage']['create']({'body':_0x23ad87['body'][_0x708b('0x27')],'FaxAccountId':_0x23ad87[_0x708b('0x39')]['id'],'FaxInteractionId':_0x3b6d0b['id'],'direction':'in','ContactId':_0x3b6d0b[_0x708b('0x52')],'AttachmentId':_0x23ad87[_0x708b('0x27')][_0x708b('0x53')]});})['then'](function(_0x46d383){_0x43b9e8['message']=_0x46d383;if(!_0x43b9e8[_0x708b('0x51')][_0x708b('0x54')])return;return db[_0x708b('0x55')][_0x708b('0x49')]({'attributes':['id',_0x708b('0x56')],'where':{'id':_0x43b9e8[_0x708b('0x51')][_0x708b('0x54')]}});})['then'](function(_0x26f41d){if(_0x26f41d){_0x43b9e8[_0x708b('0x42')][_0x708b('0x57')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x26f41d[_0x708b('0x56')]+','+(_0x43b9e8[_0x708b('0x39')][_0x708b('0x58')]||0xa),'interval':_0x708b('0x4f')});}if(!_0x43b9e8[_0x708b('0x51')]['queueId'])return;return db[_0x708b('0x59')]['find']({'attributes':['id',_0x708b('0x56')],'where':{'id':_0x43b9e8[_0x708b('0x51')]['queueId']}});})[_0x708b('0x11')](function(_0x48bbe5){if(_0x48bbe5){_0x43b9e8[_0x708b('0x42')][_0x708b('0x57')]({'id':0x0,'priority':0x1,'app':'queue','appdata':_0x48bbe5[_0x708b('0x56')]+','+(_0x43b9e8[_0x708b('0x39')]['waitForTheAssignedQueue']||0x12c),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0x708b('0x5a'),_0x708b('0x5b'),_0x43b9e8);})[_0x708b('0x11')](function(_0x1e0b5f){_0x2f27eb(_0x1e0b5f);})[_0x708b('0x1b')](function(_0x15b9d1){_0x2fedf3(_0x56c313[_0x708b('0x17')](0x1f4,_0x15b9d1[_0x708b('0x19')]));});});};