f368938a0be630d3245e541d528caf2a4e1f33d0
[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 _0x58f9=['util','moment','bluebird','ioredis','../../mysqldb','../../config/utils','rpc','../../config/environment','jayson/promise','client','redis','socket.io-emitter','request','then','FaxAccount,\x20%s,\x20%s','request\x20sent','debug','FaxAccount,\x20%s,\x20%s,\x20%s','error','message','result','catch','FaxAccount','options','where','attributes','include','model','map','info','GetFaxAccount','stringify','NotifyFaxAccount','body','fax','CmContact','ValidationError','no\x20available\x20attributes','difference','keys','createdAt','isNil','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','Sequelize','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','from','find','account','Contacts','updatedAt','DESC','FaxApplication','List','get','applications','Applications','list','orderBy','asc','length','defaults','ListId','create','FaxInteraction','Contact\x20not\x20found','spread','autoclose','push','unmanaged','FaxMessage','ContactId','interaction','User','UserId','unshift','agent','waitForTheAssignedAgent','*,*,*,*','Start','startRouting','lodash'];(function(_0x2dafbe,_0x5d04de){var _0x58a45a=function(_0x50ac96){while(--_0x50ac96){_0x2dafbe['push'](_0x2dafbe['shift']());}};_0x58a45a(++_0x5d04de);}(_0x58f9,0x1a3));var _0x958f=function(_0x57d9c0,_0x4628ab){_0x57d9c0=_0x57d9c0-0x0;var _0x9c8eec=_0x58f9[_0x57d9c0];return _0x9c8eec;};'use strict';var _=require(_0x958f('0x0'));var util=require(_0x958f('0x1'));var moment=require(_0x958f('0x2'));var BPromise=require(_0x958f('0x3'));var rs=require('randomstring');var fs=require('fs');var Redis=require(_0x958f('0x4'));var db=require(_0x958f('0x5'))['db'];var utils=require(_0x958f('0x6'));var logger=require('../../config/logger')(_0x958f('0x7'));var config=require(_0x958f('0x8'));var jayson=require(_0x958f('0x9'));var client=jayson[_0x958f('0xa')]['http']({'port':0x232c});config[_0x958f('0xb')]=_['defaults'](config[_0x958f('0xb')],{'host':'localhost','port':0x18eb});var socket=require(_0x958f('0xc'))(new Redis(config[_0x958f('0xb')]));require('./faxAccount.socket')['register'](socket);function respondWithRpcPromise(_0x41ab00,_0x48f8df,_0x1cf93b){return new BPromise(function(_0x23fda0,_0x53c3a9){return client[_0x958f('0xd')](_0x41ab00,_0x1cf93b)[_0x958f('0xe')](function(_0x1f95e2){logger['info'](_0x958f('0xf'),_0x48f8df,_0x958f('0x10'));logger[_0x958f('0x11')](_0x958f('0x12'),_0x48f8df,_0x958f('0x10'),JSON['stringify'](_0x1f95e2));if(_0x1f95e2[_0x958f('0x13')]){if(_0x1f95e2['error']['code']===0x1f4){logger[_0x958f('0x13')]('FaxAccount,\x20%s,\x20%s',_0x48f8df,_0x1f95e2[_0x958f('0x13')][_0x958f('0x14')]);return _0x53c3a9(_0x1f95e2[_0x958f('0x13')][_0x958f('0x14')]);}logger['error'](_0x958f('0xf'),_0x48f8df,_0x1f95e2[_0x958f('0x13')][_0x958f('0x14')]);return _0x23fda0(_0x1f95e2[_0x958f('0x13')][_0x958f('0x14')]);}else{logger['info'](_0x958f('0xf'),_0x48f8df,_0x958f('0x10'));_0x23fda0(_0x1f95e2[_0x958f('0x15')][_0x958f('0x14')]);}})[_0x958f('0x16')](function(_0x5ba978){logger[_0x958f('0x13')](_0x958f('0xf'),_0x48f8df,_0x5ba978);_0x53c3a9(_0x5ba978);});});}exports['GetFaxAccount']=function(_0x38049b){var _0x12699e=this;return new Promise(function(_0x3716de,_0x39cd74){return db[_0x958f('0x17')]['findAll']({'raw':_0x38049b[_0x958f('0x18')]?_0x38049b[_0x958f('0x18')]['raw']===undefined?!![]:![]:!![],'where':_0x38049b['options']?_0x38049b[_0x958f('0x18')][_0x958f('0x19')]||null:null,'attributes':_0x38049b[_0x958f('0x18')]?_0x38049b[_0x958f('0x18')][_0x958f('0x1a')]||null:null,'limit':_0x38049b[_0x958f('0x18')]?_0x38049b[_0x958f('0x18')]['limit']||null:null,'include':_0x38049b[_0x958f('0x18')]?_0x38049b[_0x958f('0x18')][_0x958f('0x1b')]?_['map'](_0x38049b[_0x958f('0x18')][_0x958f('0x1b')],function(_0x3df818){return{'model':db[_0x3df818[_0x958f('0x1c')]],'as':_0x3df818['as'],'attributes':_0x3df818['attributes'],'include':_0x3df818[_0x958f('0x1b')]?_['map'](_0x3df818['include'],function(_0x60b97b){return{'model':db[_0x60b97b['model']],'as':_0x60b97b['as'],'attributes':_0x60b97b[_0x958f('0x1a')],'include':_0x60b97b[_0x958f('0x1b')]?_[_0x958f('0x1d')](_0x60b97b[_0x958f('0x1b')],function(_0x1cc7d9){return{'model':db[_0x1cc7d9[_0x958f('0x1c')]],'as':_0x1cc7d9['as'],'attributes':_0x1cc7d9[_0x958f('0x1a')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x7f0ed0){logger[_0x958f('0x1e')]('GetFaxAccount',_0x38049b);logger[_0x958f('0x11')](_0x958f('0x1f'),_0x38049b,JSON[_0x958f('0x20')](_0x7f0ed0));_0x3716de(_0x7f0ed0);})[_0x958f('0x16')](function(_0xd93385){logger[_0x958f('0x13')]('GetFaxAccount',_0xd93385['message'],_0x38049b);_0x39cd74(_0x12699e[_0x958f('0x13')](0x1f4,_0xd93385[_0x958f('0x14')]));});});};exports[_0x958f('0x21')]=function(_0x5bc62f,_0x1804d4,_0x3f5bd0){var _0x5341ad=this;return new Promise(function(_0x26cef0,_0x8717ae){var _0x3ea76f={'body':_0x5bc62f[_0x958f('0x22')],'channel':_0x958f('0x23')};var _0x25fd98=[];var _0x4f6716=[];var _0x456788={};return db[_0x958f('0x24')]['describe']()[_0x958f('0xe')](function(_0x2ca62b){if(!_0x2ca62b){throw new db['Sequelize'][(_0x958f('0x25'))](_0x958f('0x26'));}_0x25fd98=_[_0x958f('0x27')](_[_0x958f('0x28')](_0x2ca62b),['createdAt','updatedAt']);_0x4f6716=_['difference'](_[_0x958f('0x28')](_0x2ca62b),[_0x958f('0x29'),'updatedAt','CompanyId','ListId']);if(_0x5bc62f[_0x958f('0x22')]['id']){delete _0x5bc62f[_0x958f('0x22')]['id'];}if(_[_0x958f('0x2a')](_0x5bc62f[_0x958f('0x22')]['from'])){throw new db['Sequelize'][(_0x958f('0x25'))](_0x958f('0x2b'));}if(_[_0x958f('0x2a')](_0x5bc62f[_0x958f('0x22')]['body'])||_0x5bc62f['body'][_0x958f('0x22')]===''){throw new db['Sequelize'][(_0x958f('0x25'))](_0x958f('0x2c'));}if(_['isNil'](_0x5bc62f[_0x958f('0x22')][_0x958f('0x2d')])){throw new db['Sequelize'][(_0x958f('0x25'))](_0x958f('0x2e')+_0x4f6716);}if(!_['includes'](_0x4f6716,_0x5bc62f[_0x958f('0x22')]['mapKey'])){throw new db[(_0x958f('0x2f'))][(_0x958f('0x25'))](_0x958f('0x30')+_0x4f6716);}_0x456788[_0x5bc62f[_0x958f('0x22')][_0x958f('0x2d')]]=_0x5bc62f[_0x958f('0x22')][_0x958f('0x31')];})[_0x958f('0xe')](function(){return db['FaxAccount'][_0x958f('0x32')]({'where':{'id':_0x5bc62f[_0x958f('0x33')]['id']},'include':[{'model':db['CmList'],'as':'List','include':[{'model':db[_0x958f('0x24')],'as':_0x958f('0x34'),'where':_0x456788,'limit':0x1,'order':[[_0x958f('0x35'),_0x958f('0x36')]]}]},{'model':db[_0x958f('0x37')],'as':'Applications'}]});})['then'](function(_0x52c5cf){if(_0x52c5cf&&_0x52c5cf[_0x958f('0x38')]){var _0x336245=_0x52c5cf[_0x958f('0x39')]({'plain':!![]});_0x3ea76f[_0x958f('0x33')]=_0x336245;_0x3ea76f[_0x958f('0x3a')]=_0x336245[_0x958f('0x3b')];_0x3ea76f[_0x958f('0x3c')]=_0x336245['List'];_0x3ea76f['applications']=_[_0x958f('0x3d')](_0x3ea76f[_0x958f('0x3a')],['priority'],[_0x958f('0x3e')]);if(_0x3ea76f[_0x958f('0x33')]['Applications']){delete _0x3ea76f[_0x958f('0x33')][_0x958f('0x3b')];}if(_0x3ea76f[_0x958f('0x33')][_0x958f('0x38')]&&_0x3ea76f[_0x958f('0x33')][_0x958f('0x38')][_0x958f('0x34')][_0x958f('0x3f')]){return _0x3ea76f['account'][_0x958f('0x38')][_0x958f('0x34')][0x0];}var _0x11e839=_[_0x958f('0x40')](_0x5bc62f['body'],{'firstName':_0x5bc62f['body'][_0x958f('0x31')],'ListId':_0x336245[_0x958f('0x41')]});_0x11e839[_0x5bc62f['body'][_0x958f('0x2d')]]=_0x5bc62f[_0x958f('0x22')][_0x958f('0x31')];return db['CmContact'][_0x958f('0x42')](_0x11e839,{'fields':_0x25fd98,'raw':!![]});}})[_0x958f('0xe')](function(_0x3eb25e){_0x3ea76f['contact']=_0x3eb25e;if(_0x3eb25e){var _0x4fa4e4={'ContactId':_0x3eb25e['id'],'FaxAccountId':_0x5bc62f[_0x958f('0x33')]['id'],'fax':_0x3eb25e[_0x958f('0x23')]};return db[_0x958f('0x43')]['find']({'where':{'ContactId':_0x3eb25e['id'],'closed':![],'FaxAccountId':_0x5bc62f[_0x958f('0x33')]['id']}})[_0x958f('0xe')](function(_0x363422){if(_0x363422){return[_0x363422,![]];}return db[_0x958f('0x43')][_0x958f('0x42')](_0x4fa4e4)[_0x958f('0xe')](function(_0x130b22){return[_0x130b22,!![]];});});}else{throw new Error(_0x958f('0x44'));}})[_0x958f('0x45')](function(_0x43202d,_0xa953e){var _0xb37c96=_0x43202d['get']({'plain':!![]});if(_0xa953e){if(_0x3ea76f['account'][_0x958f('0x46')]){_0x3ea76f[_0x958f('0x3a')][_0x958f('0x47')]({'id':0x0,'priority':_0x3ea76f['applications'][_0x958f('0x3f')]+0x1,'app':'close','appdata':_0x958f('0x48'),'interval':'*,*,*,*'});}_0xb37c96['created']=!![];}else{_0xb37c96['created']=![];}_0x3ea76f['interaction']=_0xb37c96;return db[_0x958f('0x49')][_0x958f('0x42')]({'body':_0x5bc62f[_0x958f('0x22')][_0x958f('0x22')],'FaxAccountId':_0x5bc62f[_0x958f('0x33')]['id'],'FaxInteractionId':_0x43202d['id'],'direction':'in','ContactId':_0x43202d[_0x958f('0x4a')],'AttachmentId':_0x5bc62f['body']['AttachmentId']});})[_0x958f('0xe')](function(_0x4836cd){_0x3ea76f[_0x958f('0x14')]=_0x4836cd;if(_0x3ea76f[_0x958f('0x4b')]['UserId']){return db[_0x958f('0x4c')][_0x958f('0x32')]({'attributes':['id','name'],'where':{'id':_0x3ea76f[_0x958f('0x4b')][_0x958f('0x4d')]}})[_0x958f('0xe')](function(_0x81a5d9){if(_0x81a5d9){_0x3ea76f[_0x958f('0x3a')][_0x958f('0x4e')]({'id':0x0,'priority':0x0,'app':_0x958f('0x4f'),'appdata':_0x81a5d9['name']+','+(_0x3ea76f[_0x958f('0x33')][_0x958f('0x50')]||0xa),'interval':_0x958f('0x51')});}return respondWithRpcPromise(_0x958f('0x52'),_0x958f('0x53'),_0x3ea76f);});}return respondWithRpcPromise(_0x958f('0x52'),_0x958f('0x53'),_0x3ea76f);})[_0x958f('0xe')](function(_0xf6a41f){_0x26cef0(_0xf6a41f);})['catch'](function(_0x22787c){_0x8717ae(_0x5341ad[_0x958f('0x13')](0x1f4,_0x22787c[_0x958f('0x14')]));});});};