Built motion from commit e1760111.|2.6.18
[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 _0x6bf3=['from\x20is\x20mandatory','isNil','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','find','CmList','List','Contacts','DESC','FaxApplication','Applications','get','account','applications','orderBy','priority','asc','length','create','contact','Contact\x20not\x20found','spread','push','close','unmanaged','created','interaction','AttachmentId','User','name','unshift','agent','waitForTheAssignedAgent','*,*,*,*','startRouting','Start','lodash','util','ioredis','../../config/utils','../../config/logger','rpc','../../config/environment','client','http','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','message','result','GetFaxAccount','findAll','options','attributes','limit','include','map','stringify','catch','body','fax','CmContact','describe','Sequelize','ValidationError','no\x20available\x20attributes','difference','createdAt','updatedAt','CompanyId','ListId','from'];(function(_0x138adb,_0x193fee){var _0x1b27f2=function(_0x5e97a2){while(--_0x5e97a2){_0x138adb['push'](_0x138adb['shift']());}};_0x1b27f2(++_0x193fee);}(_0x6bf3,0x1c9));var _0x36bf=function(_0x4734a0,_0x2fd639){_0x4734a0=_0x4734a0-0x0;var _0x4d02b0=_0x6bf3[_0x4734a0];return _0x4d02b0;};'use strict';var _=require(_0x36bf('0x0'));var util=require(_0x36bf('0x1'));var moment=require('moment');var BPromise=require('bluebird');var rs=require('randomstring');var fs=require('fs');var Redis=require(_0x36bf('0x2'));var db=require('../../mysqldb')['db'];var utils=require(_0x36bf('0x3'));var logger=require(_0x36bf('0x4'))(_0x36bf('0x5'));var config=require(_0x36bf('0x6'));var jayson=require('jayson/promise');var client=jayson[_0x36bf('0x7')][_0x36bf('0x8')]({'port':0x232c});config[_0x36bf('0x9')]=_[_0x36bf('0xa')](config[_0x36bf('0x9')],{'host':_0x36bf('0xb'),'port':0x18eb});var socket=require(_0x36bf('0xc'))(new Redis(config[_0x36bf('0x9')]));require(_0x36bf('0xd'))[_0x36bf('0xe')](socket);function respondWithRpcPromise(_0x8ef6bf,_0x5301cd,_0x2ebffc){return new BPromise(function(_0x196a8f,_0x5a1599){return client[_0x36bf('0xf')](_0x8ef6bf,_0x2ebffc)[_0x36bf('0x10')](function(_0x1f7855){logger[_0x36bf('0x11')](_0x36bf('0x12'),_0x5301cd,'request\x20sent');logger[_0x36bf('0x13')](_0x36bf('0x14'),_0x5301cd,_0x36bf('0x15'),JSON['stringify'](_0x1f7855));if(_0x1f7855[_0x36bf('0x16')]){if(_0x1f7855['error']['code']===0x1f4){logger[_0x36bf('0x16')](_0x36bf('0x12'),_0x5301cd,_0x1f7855['error'][_0x36bf('0x17')]);return _0x5a1599(_0x1f7855[_0x36bf('0x16')][_0x36bf('0x17')]);}logger[_0x36bf('0x16')](_0x36bf('0x12'),_0x5301cd,_0x1f7855[_0x36bf('0x16')][_0x36bf('0x17')]);return _0x196a8f(_0x1f7855[_0x36bf('0x16')][_0x36bf('0x17')]);}else{logger[_0x36bf('0x11')](_0x36bf('0x12'),_0x5301cd,'request\x20sent');_0x196a8f(_0x1f7855[_0x36bf('0x18')][_0x36bf('0x17')]);}})['catch'](function(_0x567d41){logger[_0x36bf('0x16')](_0x36bf('0x12'),_0x5301cd,_0x567d41);_0x5a1599(_0x567d41);});});}exports[_0x36bf('0x19')]=function(_0x2943b0){var _0x415213=this;return new Promise(function(_0x3eab27,_0x28c80c){return db['FaxAccount'][_0x36bf('0x1a')]({'raw':_0x2943b0['options']?_0x2943b0[_0x36bf('0x1b')]['raw']===undefined?!![]:![]:!![],'where':_0x2943b0['options']?_0x2943b0['options']['where']||null:null,'attributes':_0x2943b0[_0x36bf('0x1b')]?_0x2943b0[_0x36bf('0x1b')][_0x36bf('0x1c')]||null:null,'limit':_0x2943b0[_0x36bf('0x1b')]?_0x2943b0[_0x36bf('0x1b')][_0x36bf('0x1d')]||null:null,'include':_0x2943b0['options']?_0x2943b0[_0x36bf('0x1b')][_0x36bf('0x1e')]?_[_0x36bf('0x1f')](_0x2943b0[_0x36bf('0x1b')]['include'],function(_0x207c00){return{'model':db[_0x207c00['model']],'as':_0x207c00['as'],'attributes':_0x207c00[_0x36bf('0x1c')],'include':_0x207c00[_0x36bf('0x1e')]?_[_0x36bf('0x1f')](_0x207c00[_0x36bf('0x1e')],function(_0x5c4cf5){return{'model':db[_0x5c4cf5['model']],'as':_0x5c4cf5['as'],'attributes':_0x5c4cf5[_0x36bf('0x1c')],'include':_0x5c4cf5['include']?_[_0x36bf('0x1f')](_0x5c4cf5['include'],function(_0x5b8ea1){return{'model':db[_0x5b8ea1['model']],'as':_0x5b8ea1['as'],'attributes':_0x5b8ea1[_0x36bf('0x1c')]};}):[]};}):[]};}):[]:[]})[_0x36bf('0x10')](function(_0x2d1b40){logger[_0x36bf('0x11')]('GetFaxAccount',_0x2943b0);logger['debug'](_0x36bf('0x19'),_0x2943b0,JSON[_0x36bf('0x20')](_0x2d1b40));_0x3eab27(_0x2d1b40);})[_0x36bf('0x21')](function(_0x284cc7){logger[_0x36bf('0x16')](_0x36bf('0x19'),_0x284cc7['message'],_0x2943b0);_0x28c80c(_0x415213[_0x36bf('0x16')](0x1f4,_0x284cc7[_0x36bf('0x17')]));});});};exports['NotifyFaxAccount']=function(_0x29c938,_0x1c79ad,_0x5e0121){var _0x12d5c0=this;return new Promise(function(_0xf376bf,_0x16dda3){var _0x43607d={'body':_0x29c938[_0x36bf('0x22')],'channel':_0x36bf('0x23')};var _0x4179ab=[];var _0x3334cc=[];var _0x510cab={};return db[_0x36bf('0x24')][_0x36bf('0x25')]()[_0x36bf('0x10')](function(_0x10d3af){if(!_0x10d3af){throw new db[(_0x36bf('0x26'))][(_0x36bf('0x27'))](_0x36bf('0x28'));}_0x4179ab=_[_0x36bf('0x29')](_['keys'](_0x10d3af),[_0x36bf('0x2a'),_0x36bf('0x2b')]);_0x3334cc=_['difference'](_['keys'](_0x10d3af),[_0x36bf('0x2a'),_0x36bf('0x2b'),_0x36bf('0x2c'),_0x36bf('0x2d')]);if(_0x29c938[_0x36bf('0x22')]['id']){delete _0x29c938[_0x36bf('0x22')]['id'];}if(_['isNil'](_0x29c938[_0x36bf('0x22')][_0x36bf('0x2e')])){throw new db['Sequelize'][(_0x36bf('0x27'))](_0x36bf('0x2f'));}if(_[_0x36bf('0x30')](_0x29c938[_0x36bf('0x22')][_0x36bf('0x22')])||_0x29c938[_0x36bf('0x22')][_0x36bf('0x22')]===''){throw new db[(_0x36bf('0x26'))][(_0x36bf('0x27'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_['isNil'](_0x29c938[_0x36bf('0x22')]['mapKey'])){throw new db['Sequelize'][(_0x36bf('0x27'))](_0x36bf('0x31')+_0x3334cc);}if(!_[_0x36bf('0x32')](_0x3334cc,_0x29c938['body'][_0x36bf('0x33')])){throw new db[(_0x36bf('0x26'))][(_0x36bf('0x27'))]('mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x3334cc);}_0x510cab[_0x29c938[_0x36bf('0x22')][_0x36bf('0x33')]]=_0x29c938[_0x36bf('0x22')][_0x36bf('0x2e')];})[_0x36bf('0x10')](function(){return db['FaxAccount'][_0x36bf('0x34')]({'where':{'id':_0x29c938['account']['id']},'include':[{'model':db[_0x36bf('0x35')],'as':_0x36bf('0x36'),'include':[{'model':db['CmContact'],'as':_0x36bf('0x37'),'where':_0x510cab,'limit':0x1,'order':[[_0x36bf('0x2b'),_0x36bf('0x38')]]}]},{'model':db[_0x36bf('0x39')],'as':_0x36bf('0x3a')}]});})[_0x36bf('0x10')](function(_0x2e3340){if(_0x2e3340&&_0x2e3340[_0x36bf('0x36')]){var _0x29bab8=_0x2e3340[_0x36bf('0x3b')]({'plain':!![]});_0x43607d[_0x36bf('0x3c')]=_0x29bab8;_0x43607d[_0x36bf('0x3d')]=_0x29bab8[_0x36bf('0x3a')];_0x43607d['list']=_0x29bab8['List'];_0x43607d[_0x36bf('0x3d')]=_[_0x36bf('0x3e')](_0x43607d[_0x36bf('0x3d')],[_0x36bf('0x3f')],[_0x36bf('0x40')]);if(_0x43607d[_0x36bf('0x3c')][_0x36bf('0x3a')]){delete _0x43607d[_0x36bf('0x3c')]['Applications'];}if(_0x43607d[_0x36bf('0x3c')][_0x36bf('0x36')]&&_0x43607d[_0x36bf('0x3c')][_0x36bf('0x36')][_0x36bf('0x37')][_0x36bf('0x41')]){return _0x43607d['account'][_0x36bf('0x36')][_0x36bf('0x37')][0x0];}var _0x10408f=_[_0x36bf('0xa')](_0x29c938['body'],{'firstName':_0x29c938['body']['from'],'ListId':_0x29bab8[_0x36bf('0x2d')]});_0x10408f[_0x29c938[_0x36bf('0x22')][_0x36bf('0x33')]]=_0x29c938['body'][_0x36bf('0x2e')];return db[_0x36bf('0x24')][_0x36bf('0x42')](_0x10408f,{'fields':_0x4179ab,'raw':!![]});}})[_0x36bf('0x10')](function(_0x105870){_0x43607d[_0x36bf('0x43')]=_0x105870;if(_0x105870){var _0x384a33={'ContactId':_0x105870['id'],'FaxAccountId':_0x29c938[_0x36bf('0x3c')]['id'],'fax':_0x105870[_0x36bf('0x23')]};return db['FaxInteraction'][_0x36bf('0x34')]({'where':{'ContactId':_0x105870['id'],'closed':![],'FaxAccountId':_0x29c938[_0x36bf('0x3c')]['id']}})[_0x36bf('0x10')](function(_0x3188b7){if(_0x3188b7){return[_0x3188b7,![]];}return db['FaxInteraction'][_0x36bf('0x42')](_0x384a33)[_0x36bf('0x10')](function(_0x4a4f23){return[_0x4a4f23,!![]];});});}else{throw new Error(_0x36bf('0x44'));}})[_0x36bf('0x45')](function(_0x17eb65,_0x10f0d5){var _0x45ddba=_0x17eb65[_0x36bf('0x3b')]({'plain':!![]});if(_0x10f0d5){if(_0x43607d['account']['autoclose']){_0x43607d[_0x36bf('0x3d')][_0x36bf('0x46')]({'id':0x0,'priority':_0x43607d['applications'][_0x36bf('0x41')]+0x1,'app':_0x36bf('0x47'),'appdata':_0x36bf('0x48'),'interval':'*,*,*,*'});}_0x45ddba[_0x36bf('0x49')]=!![];}else{_0x45ddba[_0x36bf('0x49')]=![];}_0x43607d[_0x36bf('0x4a')]=_0x45ddba;return db['FaxMessage'][_0x36bf('0x42')]({'body':_0x29c938[_0x36bf('0x22')]['body'],'FaxAccountId':_0x29c938['account']['id'],'FaxInteractionId':_0x17eb65['id'],'direction':'in','ContactId':_0x17eb65['ContactId'],'AttachmentId':_0x29c938[_0x36bf('0x22')][_0x36bf('0x4b')]});})['then'](function(_0x590d17){_0x43607d[_0x36bf('0x17')]=_0x590d17;if(_0x43607d['interaction']['UserId']){return db[_0x36bf('0x4c')]['find']({'attributes':['id',_0x36bf('0x4d')],'where':{'id':_0x43607d[_0x36bf('0x4a')]['UserId']}})[_0x36bf('0x10')](function(_0x7852d4){if(_0x7852d4){_0x43607d[_0x36bf('0x3d')][_0x36bf('0x4e')]({'id':0x0,'priority':0x0,'app':_0x36bf('0x4f'),'appdata':_0x7852d4[_0x36bf('0x4d')]+','+(_0x43607d[_0x36bf('0x3c')][_0x36bf('0x50')]||0xa),'interval':_0x36bf('0x51')});}return respondWithRpcPromise('Start',_0x36bf('0x52'),_0x43607d);});}return respondWithRpcPromise(_0x36bf('0x53'),_0x36bf('0x52'),_0x43607d);})[_0x36bf('0x10')](function(_0x457554){_0xf376bf(_0x457554);})[_0x36bf('0x21')](function(_0x6b2427){_0x16dda3(_0x12d5c0['error'](0x1f4,_0x6b2427[_0x36bf('0x17')]));});});};