Built motion from commit 8356fca3.|2.6.19
[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 _0xcbc6=['asc','length','ListId','contact','FaxInteraction','Contact\x20not\x20found','spread','autoclose','push','close','unmanaged','created','interaction','FaxMessage','create','ContactId','AttachmentId','name','UserId','agent','waitForTheAssignedAgent','*,*,*,*','queueId','unshift','queue','waitForTheAssignedQueue','startRouting','lodash','bluebird','randomstring','ioredis','../../config/utils','../../config/logger','rpc','jayson/promise','http','redis','localhost','socket.io-emitter','./faxAccount.socket','register','request','then','info','FaxAccount,\x20%s,\x20%s','FaxAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','GetFaxAccount','FaxAccount','findAll','raw','options','where','include','map','model','attributes','debug','catch','NotifyFaxAccount','body','fax','CmContact','describe','ValidationError','no\x20available\x20attributes','keys','createdAt','difference','updatedAt','isNil','from','Sequelize','from\x20is\x20mandatory','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','find','account','CmList','List','Contacts','Applications','get','applications','list','priority'];(function(_0x55e54c,_0x3697bf){var _0x33ec5f=function(_0xc47d5f){while(--_0xc47d5f){_0x55e54c['push'](_0x55e54c['shift']());}};_0x33ec5f(++_0x3697bf);}(_0xcbc6,0x12f));var _0x6cbc=function(_0x29f6f8,_0x3bd3e4){_0x29f6f8=_0x29f6f8-0x0;var _0x6c4589=_0xcbc6[_0x29f6f8];return _0x6c4589;};'use strict';var _=require(_0x6cbc('0x0'));var util=require('util');var moment=require('moment');var BPromise=require(_0x6cbc('0x1'));var rs=require(_0x6cbc('0x2'));var fs=require('fs');var Redis=require(_0x6cbc('0x3'));var db=require('../../mysqldb')['db'];var utils=require(_0x6cbc('0x4'));var logger=require(_0x6cbc('0x5'))(_0x6cbc('0x6'));var config=require('../../config/environment');var jayson=require(_0x6cbc('0x7'));var client=jayson['client'][_0x6cbc('0x8')]({'port':0x232c});config[_0x6cbc('0x9')]=_['defaults'](config[_0x6cbc('0x9')],{'host':_0x6cbc('0xa'),'port':0x18eb});var socket=require(_0x6cbc('0xb'))(new Redis(config[_0x6cbc('0x9')]));require(_0x6cbc('0xc'))[_0x6cbc('0xd')](socket);function respondWithRpcPromise(_0x195311,_0x221804,_0x13bfe2){return new BPromise(function(_0x2b69eb,_0x1f0c45){return client[_0x6cbc('0xe')](_0x195311,_0x13bfe2)[_0x6cbc('0xf')](function(_0x2fa8b1){logger[_0x6cbc('0x10')](_0x6cbc('0x11'),_0x221804,'request\x20sent');logger['debug'](_0x6cbc('0x12'),_0x221804,_0x6cbc('0x13'),JSON[_0x6cbc('0x14')](_0x2fa8b1));if(_0x2fa8b1[_0x6cbc('0x15')]){if(_0x2fa8b1[_0x6cbc('0x15')][_0x6cbc('0x16')]===0x1f4){logger[_0x6cbc('0x15')](_0x6cbc('0x11'),_0x221804,_0x2fa8b1['error'][_0x6cbc('0x17')]);return _0x1f0c45(_0x2fa8b1['error'][_0x6cbc('0x17')]);}logger[_0x6cbc('0x15')](_0x6cbc('0x11'),_0x221804,_0x2fa8b1['error'][_0x6cbc('0x17')]);return _0x2b69eb(_0x2fa8b1[_0x6cbc('0x15')][_0x6cbc('0x17')]);}else{logger[_0x6cbc('0x10')](_0x6cbc('0x11'),_0x221804,_0x6cbc('0x13'));_0x2b69eb(_0x2fa8b1[_0x6cbc('0x18')]['message']);}})['catch'](function(_0xa9c9fe){logger['error'](_0x6cbc('0x11'),_0x221804,_0xa9c9fe);_0x1f0c45(_0xa9c9fe);});});}exports[_0x6cbc('0x19')]=function(_0x57c596){var _0x5f4186=this;return new Promise(function(_0x697197,_0x40894a){return db[_0x6cbc('0x1a')][_0x6cbc('0x1b')]({'raw':_0x57c596['options']?_0x57c596['options'][_0x6cbc('0x1c')]===undefined?!![]:![]:!![],'where':_0x57c596['options']?_0x57c596[_0x6cbc('0x1d')][_0x6cbc('0x1e')]||null:null,'attributes':_0x57c596[_0x6cbc('0x1d')]?_0x57c596[_0x6cbc('0x1d')]['attributes']||null:null,'limit':_0x57c596[_0x6cbc('0x1d')]?_0x57c596[_0x6cbc('0x1d')]['limit']||null:null,'include':_0x57c596[_0x6cbc('0x1d')]?_0x57c596[_0x6cbc('0x1d')][_0x6cbc('0x1f')]?_[_0x6cbc('0x20')](_0x57c596[_0x6cbc('0x1d')][_0x6cbc('0x1f')],function(_0x2dff63){return{'model':db[_0x2dff63[_0x6cbc('0x21')]],'as':_0x2dff63['as'],'attributes':_0x2dff63[_0x6cbc('0x22')],'include':_0x2dff63[_0x6cbc('0x1f')]?_[_0x6cbc('0x20')](_0x2dff63[_0x6cbc('0x1f')],function(_0x488b52){return{'model':db[_0x488b52[_0x6cbc('0x21')]],'as':_0x488b52['as'],'attributes':_0x488b52[_0x6cbc('0x22')],'include':_0x488b52['include']?_[_0x6cbc('0x20')](_0x488b52[_0x6cbc('0x1f')],function(_0x7d7a41){return{'model':db[_0x7d7a41[_0x6cbc('0x21')]],'as':_0x7d7a41['as'],'attributes':_0x7d7a41[_0x6cbc('0x22')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x3d6063){logger[_0x6cbc('0x10')]('GetFaxAccount',_0x57c596);logger[_0x6cbc('0x23')](_0x6cbc('0x19'),_0x57c596,JSON[_0x6cbc('0x14')](_0x3d6063));_0x697197(_0x3d6063);})[_0x6cbc('0x24')](function(_0x1fb561){logger[_0x6cbc('0x15')]('GetFaxAccount',_0x1fb561['message'],_0x57c596);_0x40894a(_0x5f4186[_0x6cbc('0x15')](0x1f4,_0x1fb561[_0x6cbc('0x17')]));});});};exports[_0x6cbc('0x25')]=function(_0x55fd1a,_0x35e223,_0x4ff68e){var _0x615d88=this;return new Promise(function(_0x47d638,_0x523c07){var _0x4e985a={'body':_0x55fd1a[_0x6cbc('0x26')],'channel':_0x6cbc('0x27')};var _0x2301fa=[];var _0x49fa1a=[];var _0x3edcae={};return db[_0x6cbc('0x28')][_0x6cbc('0x29')]()['then'](function(_0x30c71d){if(!_0x30c71d){throw new db['Sequelize'][(_0x6cbc('0x2a'))](_0x6cbc('0x2b'));}_0x2301fa=_['difference'](_[_0x6cbc('0x2c')](_0x30c71d),[_0x6cbc('0x2d'),'updatedAt']);_0x49fa1a=_[_0x6cbc('0x2e')](_[_0x6cbc('0x2c')](_0x30c71d),[_0x6cbc('0x2d'),_0x6cbc('0x2f'),'CompanyId','ListId']);if(_0x55fd1a['body']['id']){delete _0x55fd1a[_0x6cbc('0x26')]['id'];}if(_[_0x6cbc('0x30')](_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x31')])){throw new db[(_0x6cbc('0x32'))]['ValidationError'](_0x6cbc('0x33'));}if(_[_0x6cbc('0x30')](_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x26')])||_0x55fd1a['body']['body']===''){throw new db[(_0x6cbc('0x32'))][(_0x6cbc('0x2a'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0x6cbc('0x30')](_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x34')])){throw new db[(_0x6cbc('0x32'))][(_0x6cbc('0x2a'))](_0x6cbc('0x35')+_0x49fa1a);}if(!_[_0x6cbc('0x36')](_0x49fa1a,_0x55fd1a['body'][_0x6cbc('0x34')])){throw new db[(_0x6cbc('0x32'))][(_0x6cbc('0x2a'))]('mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x49fa1a);}_0x3edcae[_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x34')]]=_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x31')];})[_0x6cbc('0xf')](function(){return db[_0x6cbc('0x1a')][_0x6cbc('0x37')]({'where':{'id':_0x55fd1a[_0x6cbc('0x38')]['id']},'include':[{'model':db[_0x6cbc('0x39')],'as':_0x6cbc('0x3a'),'include':[{'model':db[_0x6cbc('0x28')],'as':_0x6cbc('0x3b'),'where':_0x3edcae,'limit':0x1,'order':[[_0x6cbc('0x2f'),'DESC']]}]},{'model':db['FaxApplication'],'as':_0x6cbc('0x3c')}]});})[_0x6cbc('0xf')](function(_0x49f4a8){if(_0x49f4a8&&_0x49f4a8[_0x6cbc('0x3a')]){var _0x38eac1=_0x49f4a8[_0x6cbc('0x3d')]({'plain':!![]});_0x4e985a['account']=_0x38eac1;_0x4e985a[_0x6cbc('0x3e')]=_0x38eac1[_0x6cbc('0x3c')];_0x4e985a[_0x6cbc('0x3f')]=_0x38eac1[_0x6cbc('0x3a')];_0x4e985a['applications']=_['orderBy'](_0x4e985a[_0x6cbc('0x3e')],[_0x6cbc('0x40')],[_0x6cbc('0x41')]);if(_0x4e985a[_0x6cbc('0x38')][_0x6cbc('0x3c')]){delete _0x4e985a[_0x6cbc('0x38')][_0x6cbc('0x3c')];}if(_0x4e985a[_0x6cbc('0x38')][_0x6cbc('0x3a')]&&_0x4e985a[_0x6cbc('0x38')][_0x6cbc('0x3a')][_0x6cbc('0x3b')][_0x6cbc('0x42')]){return _0x4e985a[_0x6cbc('0x38')][_0x6cbc('0x3a')]['Contacts'][0x0];}var _0x49c097=_['defaults'](_0x55fd1a[_0x6cbc('0x26')],{'firstName':_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x31')],'ListId':_0x38eac1[_0x6cbc('0x43')]});_0x49c097[_0x55fd1a['body'][_0x6cbc('0x34')]]=_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x31')];return db['CmContact']['create'](_0x49c097,{'fields':_0x2301fa,'raw':!![]});}})[_0x6cbc('0xf')](function(_0x249f81){_0x4e985a[_0x6cbc('0x44')]=_0x249f81;if(_0x249f81){var _0x2eb263={'ContactId':_0x249f81['id'],'FaxAccountId':_0x55fd1a[_0x6cbc('0x38')]['id'],'fax':_0x249f81[_0x6cbc('0x27')]};return db[_0x6cbc('0x45')][_0x6cbc('0x37')]({'where':{'ContactId':_0x249f81['id'],'closed':![],'FaxAccountId':_0x55fd1a[_0x6cbc('0x38')]['id']}})['then'](function(_0x1b0abd){if(_0x1b0abd){return[_0x1b0abd,![]];}return db['FaxInteraction']['create'](_0x2eb263)[_0x6cbc('0xf')](function(_0x7a9e){return[_0x7a9e,!![]];});});}else{throw new Error(_0x6cbc('0x46'));}})[_0x6cbc('0x47')](function(_0x2b1cae,_0x4ac795){var _0x23d6b1=_0x2b1cae[_0x6cbc('0x3d')]({'plain':!![]});if(_0x4ac795){if(_0x4e985a['account'][_0x6cbc('0x48')]){_0x4e985a[_0x6cbc('0x3e')][_0x6cbc('0x49')]({'id':0x0,'priority':_0x4e985a[_0x6cbc('0x3e')][_0x6cbc('0x42')]+0x1,'app':_0x6cbc('0x4a'),'appdata':_0x6cbc('0x4b'),'interval':'*,*,*,*'});}_0x23d6b1[_0x6cbc('0x4c')]=!![];}else{_0x23d6b1[_0x6cbc('0x4c')]=![];}_0x4e985a[_0x6cbc('0x4d')]=_0x23d6b1;return db[_0x6cbc('0x4e')][_0x6cbc('0x4f')]({'body':_0x55fd1a[_0x6cbc('0x26')][_0x6cbc('0x26')],'FaxAccountId':_0x55fd1a[_0x6cbc('0x38')]['id'],'FaxInteractionId':_0x2b1cae['id'],'direction':'in','ContactId':_0x2b1cae[_0x6cbc('0x50')],'AttachmentId':_0x55fd1a['body'][_0x6cbc('0x51')]});})[_0x6cbc('0xf')](function(_0x266ab0){_0x4e985a['message']=_0x266ab0;if(!_0x4e985a[_0x6cbc('0x4d')]['UserId'])return;return db['User']['find']({'attributes':['id',_0x6cbc('0x52')],'where':{'id':_0x4e985a[_0x6cbc('0x4d')][_0x6cbc('0x53')]}});})['then'](function(_0x185958){if(_0x185958){_0x4e985a[_0x6cbc('0x3e')]['unshift']({'id':0x0,'priority':0x0,'app':_0x6cbc('0x54'),'appdata':_0x185958['name']+','+(_0x4e985a['account'][_0x6cbc('0x55')]||0xa),'interval':_0x6cbc('0x56')});}if(!_0x4e985a[_0x6cbc('0x4d')][_0x6cbc('0x57')])return;return db['FaxQueue'][_0x6cbc('0x37')]({'attributes':['id',_0x6cbc('0x52')],'where':{'id':_0x4e985a[_0x6cbc('0x4d')][_0x6cbc('0x57')]}});})[_0x6cbc('0xf')](function(_0xe39039){if(_0xe39039){_0x4e985a['applications'][_0x6cbc('0x58')]({'id':0x0,'priority':0x1,'app':_0x6cbc('0x59'),'appdata':_0xe39039[_0x6cbc('0x52')]+','+(_0x4e985a[_0x6cbc('0x38')][_0x6cbc('0x5a')]||0x12c),'interval':_0x6cbc('0x56')});}return respondWithRpcPromise('Start',_0x6cbc('0x5b'),_0x4e985a);})[_0x6cbc('0xf')](function(_0x19404d){_0x47d638(_0x19404d);})[_0x6cbc('0x24')](function(_0x19a5a5){_0x523c07(_0x615d88[_0x6cbc('0x15')](0x1f4,_0x19a5a5[_0x6cbc('0x17')]));});});};