Built motion from commit f6d7e26c.|2.6.14
[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 _0x15de=['message','result','catch','GetFaxAccount','findAll','options','raw','where','limit','include','model','attributes','map','info','NotifyFaxAccount','body','fax','CmContact','Sequelize','ValidationError','no\x20available\x20attributes','keys','updatedAt','difference','createdAt','ListId','isNil','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','from','FaxAccount','find','CmList','Contacts','FaxApplication','List','get','account','applications','list','orderBy','asc','Applications','length','create','FaxInteraction','Contact\x20not\x20found','spread','autoclose','push','close','*,*,*,*','created','interaction','ContactId','AttachmentId','UserId','User','name','unshift','agent','waitForTheAssignedAgent','Start','startRouting','lodash','util','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','http','redis','defaults','localhost','register','request','then','FaxAccount,\x20%s,\x20%s','debug','FaxAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error'];(function(_0x2b63a0,_0x14fc48){var _0x4f0434=function(_0x8cba9e){while(--_0x8cba9e){_0x2b63a0['push'](_0x2b63a0['shift']());}};_0x4f0434(++_0x14fc48);}(_0x15de,0xf8));var _0xe15d=function(_0x5ff517,_0x3e4ee0){_0x5ff517=_0x5ff517-0x0;var _0x19a245=_0x15de[_0x5ff517];return _0x19a245;};'use strict';var _=require(_0xe15d('0x0'));var util=require(_0xe15d('0x1'));var moment=require(_0xe15d('0x2'));var BPromise=require(_0xe15d('0x3'));var rs=require(_0xe15d('0x4'));var fs=require('fs');var Redis=require(_0xe15d('0x5'));var db=require(_0xe15d('0x6'))['db'];var utils=require(_0xe15d('0x7'));var logger=require(_0xe15d('0x8'))(_0xe15d('0x9'));var config=require(_0xe15d('0xa'));var jayson=require(_0xe15d('0xb'));var client=jayson['client'][_0xe15d('0xc')]({'port':0x232c});config[_0xe15d('0xd')]=_[_0xe15d('0xe')](config[_0xe15d('0xd')],{'host':_0xe15d('0xf'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xe15d('0xd')]));require('./faxAccount.socket')[_0xe15d('0x10')](socket);function respondWithRpcPromise(_0x57996b,_0x1ca3d5,_0x518649){return new BPromise(function(_0x3b4a3a,_0x38d831){return client[_0xe15d('0x11')](_0x57996b,_0x518649)[_0xe15d('0x12')](function(_0x5f4bde){logger['info'](_0xe15d('0x13'),_0x1ca3d5,'request\x20sent');logger[_0xe15d('0x14')](_0xe15d('0x15'),_0x1ca3d5,_0xe15d('0x16'),JSON[_0xe15d('0x17')](_0x5f4bde));if(_0x5f4bde[_0xe15d('0x18')]){if(_0x5f4bde[_0xe15d('0x18')]['code']===0x1f4){logger[_0xe15d('0x18')](_0xe15d('0x13'),_0x1ca3d5,_0x5f4bde['error']['message']);return _0x38d831(_0x5f4bde['error'][_0xe15d('0x19')]);}logger['error'](_0xe15d('0x13'),_0x1ca3d5,_0x5f4bde[_0xe15d('0x18')][_0xe15d('0x19')]);return _0x3b4a3a(_0x5f4bde[_0xe15d('0x18')][_0xe15d('0x19')]);}else{logger['info']('FaxAccount,\x20%s,\x20%s',_0x1ca3d5,_0xe15d('0x16'));_0x3b4a3a(_0x5f4bde[_0xe15d('0x1a')][_0xe15d('0x19')]);}})[_0xe15d('0x1b')](function(_0x2e6bf1){logger[_0xe15d('0x18')](_0xe15d('0x13'),_0x1ca3d5,_0x2e6bf1);_0x38d831(_0x2e6bf1);});});}exports[_0xe15d('0x1c')]=function(_0x3c9146){var _0x2f04f6=this;return new Promise(function(_0x66d8b9,_0x2ff60e){return db['FaxAccount'][_0xe15d('0x1d')]({'raw':_0x3c9146[_0xe15d('0x1e')]?_0x3c9146[_0xe15d('0x1e')][_0xe15d('0x1f')]===undefined?!![]:![]:!![],'where':_0x3c9146[_0xe15d('0x1e')]?_0x3c9146[_0xe15d('0x1e')][_0xe15d('0x20')]||null:null,'attributes':_0x3c9146[_0xe15d('0x1e')]?_0x3c9146[_0xe15d('0x1e')]['attributes']||null:null,'limit':_0x3c9146[_0xe15d('0x1e')]?_0x3c9146[_0xe15d('0x1e')][_0xe15d('0x21')]||null:null,'include':_0x3c9146[_0xe15d('0x1e')]?_0x3c9146[_0xe15d('0x1e')][_0xe15d('0x22')]?_['map'](_0x3c9146[_0xe15d('0x1e')][_0xe15d('0x22')],function(_0x8cc27a){return{'model':db[_0x8cc27a[_0xe15d('0x23')]],'as':_0x8cc27a['as'],'attributes':_0x8cc27a[_0xe15d('0x24')],'include':_0x8cc27a[_0xe15d('0x22')]?_[_0xe15d('0x25')](_0x8cc27a['include'],function(_0x2f0add){return{'model':db[_0x2f0add[_0xe15d('0x23')]],'as':_0x2f0add['as'],'attributes':_0x2f0add['attributes'],'include':_0x2f0add[_0xe15d('0x22')]?_[_0xe15d('0x25')](_0x2f0add[_0xe15d('0x22')],function(_0x26be2c){return{'model':db[_0x26be2c['model']],'as':_0x26be2c['as'],'attributes':_0x26be2c['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x351b9b){logger[_0xe15d('0x26')](_0xe15d('0x1c'),_0x3c9146);logger['debug'](_0xe15d('0x1c'),_0x3c9146,JSON[_0xe15d('0x17')](_0x351b9b));_0x66d8b9(_0x351b9b);})[_0xe15d('0x1b')](function(_0x133b92){logger[_0xe15d('0x18')](_0xe15d('0x1c'),_0x133b92[_0xe15d('0x19')],_0x3c9146);_0x2ff60e(_0x2f04f6[_0xe15d('0x18')](0x1f4,_0x133b92['message']));});});};exports[_0xe15d('0x27')]=function(_0x5f322a,_0x2ba7c8,_0x27e8d6){var _0x2afca0=this;return new Promise(function(_0x267d31,_0x430985){var _0x1e07da={'body':_0x5f322a[_0xe15d('0x28')],'channel':_0xe15d('0x29')};var _0x694de3=[];var _0xbda8ca=[];var _0x590c2c={};return db[_0xe15d('0x2a')]['describe']()[_0xe15d('0x12')](function(_0x5d49d1){if(!_0x5d49d1){throw new db[(_0xe15d('0x2b'))][(_0xe15d('0x2c'))](_0xe15d('0x2d'));}_0x694de3=_['difference'](_[_0xe15d('0x2e')](_0x5d49d1),['createdAt',_0xe15d('0x2f')]);_0xbda8ca=_[_0xe15d('0x30')](_[_0xe15d('0x2e')](_0x5d49d1),[_0xe15d('0x31'),_0xe15d('0x2f'),'CompanyId',_0xe15d('0x32')]);if(_0x5f322a[_0xe15d('0x28')]['id']){delete _0x5f322a[_0xe15d('0x28')]['id'];}if(_[_0xe15d('0x33')](_0x5f322a[_0xe15d('0x28')]['from'])){throw new db[(_0xe15d('0x2b'))][(_0xe15d('0x2c'))]('from\x20is\x20mandatory');}if(_[_0xe15d('0x33')](_0x5f322a[_0xe15d('0x28')][_0xe15d('0x28')])||_0x5f322a[_0xe15d('0x28')]['body']===''){throw new db['Sequelize'][(_0xe15d('0x2c'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0xe15d('0x33')](_0x5f322a['body'][_0xe15d('0x34')])){throw new db[(_0xe15d('0x2b'))][(_0xe15d('0x2c'))](_0xe15d('0x35')+_0xbda8ca);}if(!_[_0xe15d('0x36')](_0xbda8ca,_0x5f322a[_0xe15d('0x28')]['mapKey'])){throw new db[(_0xe15d('0x2b'))][(_0xe15d('0x2c'))](_0xe15d('0x37')+_0xbda8ca);}_0x590c2c[_0x5f322a[_0xe15d('0x28')][_0xe15d('0x34')]]=_0x5f322a[_0xe15d('0x28')][_0xe15d('0x38')];})[_0xe15d('0x12')](function(){return db[_0xe15d('0x39')][_0xe15d('0x3a')]({'where':{'id':_0x5f322a['account']['id']},'include':[{'model':db[_0xe15d('0x3b')],'as':'List','include':[{'model':db[_0xe15d('0x2a')],'as':_0xe15d('0x3c'),'where':_0x590c2c,'limit':0x1,'order':[[_0xe15d('0x2f'),'DESC']]}]},{'model':db[_0xe15d('0x3d')],'as':'Applications'}]});})['then'](function(_0x6db00c){if(_0x6db00c&&_0x6db00c[_0xe15d('0x3e')]){var _0x129a77=_0x6db00c[_0xe15d('0x3f')]({'plain':!![]});_0x1e07da[_0xe15d('0x40')]=_0x129a77;_0x1e07da[_0xe15d('0x41')]=_0x129a77['Applications'];_0x1e07da[_0xe15d('0x42')]=_0x129a77[_0xe15d('0x3e')];_0x1e07da[_0xe15d('0x41')]=_[_0xe15d('0x43')](_0x1e07da[_0xe15d('0x41')],['priority'],[_0xe15d('0x44')]);if(_0x1e07da[_0xe15d('0x40')][_0xe15d('0x45')]){delete _0x1e07da[_0xe15d('0x40')][_0xe15d('0x45')];}if(_0x1e07da['account'][_0xe15d('0x3e')]&&_0x1e07da[_0xe15d('0x40')]['List'][_0xe15d('0x3c')][_0xe15d('0x46')]){return _0x1e07da[_0xe15d('0x40')][_0xe15d('0x3e')]['Contacts'][0x0];}var _0x345796=_[_0xe15d('0xe')](_0x5f322a[_0xe15d('0x28')],{'firstName':_0x5f322a[_0xe15d('0x28')]['from'],'ListId':_0x129a77[_0xe15d('0x32')]});_0x345796[_0x5f322a[_0xe15d('0x28')][_0xe15d('0x34')]]=_0x5f322a['body'][_0xe15d('0x38')];return db[_0xe15d('0x2a')][_0xe15d('0x47')](_0x345796,{'fields':_0x694de3,'raw':!![]});}})[_0xe15d('0x12')](function(_0x1459fc){_0x1e07da['contact']=_0x1459fc;if(_0x1459fc){var _0x3f333e={'ContactId':_0x1459fc['id'],'FaxAccountId':_0x5f322a['account']['id'],'fax':_0x1459fc['fax']};return db[_0xe15d('0x48')][_0xe15d('0x3a')]({'where':{'ContactId':_0x1459fc['id'],'closed':![],'FaxAccountId':_0x5f322a[_0xe15d('0x40')]['id']}})[_0xe15d('0x12')](function(_0x34eb97){if(_0x34eb97){return[_0x34eb97,![]];}return db[_0xe15d('0x48')]['create'](_0x3f333e)[_0xe15d('0x12')](function(_0x55808d){return[_0x55808d,!![]];});});}else{throw new Error(_0xe15d('0x49'));}})[_0xe15d('0x4a')](function(_0x1697fc,_0x315ea6){var _0x52fef2=_0x1697fc[_0xe15d('0x3f')]({'plain':!![]});if(_0x315ea6){if(_0x1e07da[_0xe15d('0x40')][_0xe15d('0x4b')]){_0x1e07da[_0xe15d('0x41')][_0xe15d('0x4c')]({'id':0x0,'priority':_0x1e07da[_0xe15d('0x41')][_0xe15d('0x46')]+0x1,'app':_0xe15d('0x4d'),'appdata':'unmanaged','interval':_0xe15d('0x4e')});}_0x52fef2['created']=!![];}else{_0x52fef2[_0xe15d('0x4f')]=![];}_0x1e07da[_0xe15d('0x50')]=_0x52fef2;return db['FaxMessage']['create']({'body':_0x5f322a[_0xe15d('0x28')][_0xe15d('0x28')],'FaxAccountId':_0x5f322a[_0xe15d('0x40')]['id'],'FaxInteractionId':_0x1697fc['id'],'direction':'in','ContactId':_0x1697fc[_0xe15d('0x51')],'AttachmentId':_0x5f322a[_0xe15d('0x28')][_0xe15d('0x52')]});})[_0xe15d('0x12')](function(_0x25d090){_0x1e07da[_0xe15d('0x19')]=_0x25d090;if(_0x1e07da[_0xe15d('0x50')][_0xe15d('0x53')]){return db[_0xe15d('0x54')]['find']({'attributes':['id',_0xe15d('0x55')],'where':{'id':_0x1e07da[_0xe15d('0x50')][_0xe15d('0x53')]}})[_0xe15d('0x12')](function(_0x224da9){if(_0x224da9){_0x1e07da[_0xe15d('0x41')][_0xe15d('0x56')]({'id':0x0,'priority':0x0,'app':_0xe15d('0x57'),'appdata':_0x224da9[_0xe15d('0x55')]+','+(_0x1e07da[_0xe15d('0x40')][_0xe15d('0x58')]||0xa),'interval':_0xe15d('0x4e')});}return respondWithRpcPromise(_0xe15d('0x59'),_0xe15d('0x5a'),_0x1e07da);});}return respondWithRpcPromise('Start',_0xe15d('0x5a'),_0x1e07da);})[_0xe15d('0x12')](function(_0x27a93e){_0x267d31(_0x27a93e);})[_0xe15d('0x1b')](function(_0x3250ad){_0x430985(_0x2afca0['error'](0x1f4,_0x3250ad['message']));});});};