5d6e416ca6a13ff667e0e05dda62a524422747e1
[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 _0x3140=['message','result','catch','GetFaxAccount','FaxAccount','findAll','options','raw','limit','include','map','attributes','model','NotifyFaxAccount','body','fax','CmContact','describe','ValidationError','no\x20available\x20attributes','difference','keys','createdAt','updatedAt','CompanyId','ListId','isNil','Sequelize','from\x20is\x20mandatory','mapKey','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','find','Contacts','DESC','FaxApplication','List','account','applications','priority','asc','Applications','length','from','create','FaxInteraction','spread','get','autoclose','unmanaged','*,*,*,*','created','ContactId','interaction','UserId','User','name','startRouting','Start','util','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','../../config/environment','jayson/promise','client','http','redis','defaults','localhost','socket.io-emitter','./faxAccount.socket','request','then','info','request\x20sent','debug','FaxAccount,\x20%s,\x20%s,\x20%s','stringify','error','FaxAccount,\x20%s,\x20%s'];(function(_0x1ae4b4,_0x537b3a){var _0x561029=function(_0x58ceff){while(--_0x58ceff){_0x1ae4b4['push'](_0x1ae4b4['shift']());}};_0x561029(++_0x537b3a);}(_0x3140,0x137));var _0x0314=function(_0x4cbde1,_0x5785ec){_0x4cbde1=_0x4cbde1-0x0;var _0x37d42=_0x3140[_0x4cbde1];return _0x37d42;};'use strict';var _=require('lodash');var util=require(_0x0314('0x0'));var moment=require('moment');var BPromise=require(_0x0314('0x1'));var rs=require(_0x0314('0x2'));var fs=require('fs');var Redis=require(_0x0314('0x3'));var db=require(_0x0314('0x4'))['db'];var utils=require(_0x0314('0x5'));var logger=require(_0x0314('0x6'))('rpc');var config=require(_0x0314('0x7'));var jayson=require(_0x0314('0x8'));var client=jayson[_0x0314('0x9')][_0x0314('0xa')]({'port':0x232c});config[_0x0314('0xb')]=_[_0x0314('0xc')](config[_0x0314('0xb')],{'host':_0x0314('0xd'),'port':0x18eb});var socket=require(_0x0314('0xe'))(new Redis(config[_0x0314('0xb')]));require(_0x0314('0xf'))['register'](socket);function respondWithRpcPromise(_0x59d54c,_0x2e4da5,_0x7fab31){return new BPromise(function(_0x1e2125,_0x170e30){return client[_0x0314('0x10')](_0x59d54c,_0x7fab31)[_0x0314('0x11')](function(_0x399b5c){logger[_0x0314('0x12')]('FaxAccount,\x20%s,\x20%s',_0x2e4da5,_0x0314('0x13'));logger[_0x0314('0x14')](_0x0314('0x15'),_0x2e4da5,'request\x20sent',JSON[_0x0314('0x16')](_0x399b5c));if(_0x399b5c[_0x0314('0x17')]){if(_0x399b5c[_0x0314('0x17')]['code']===0x1f4){logger[_0x0314('0x17')](_0x0314('0x18'),_0x2e4da5,_0x399b5c[_0x0314('0x17')][_0x0314('0x19')]);return _0x170e30(_0x399b5c['error'][_0x0314('0x19')]);}logger[_0x0314('0x17')](_0x0314('0x18'),_0x2e4da5,_0x399b5c['error']['message']);return _0x1e2125(_0x399b5c[_0x0314('0x17')]['message']);}else{logger[_0x0314('0x12')](_0x0314('0x18'),_0x2e4da5,'request\x20sent');_0x1e2125(_0x399b5c[_0x0314('0x1a')]['message']);}})[_0x0314('0x1b')](function(_0x463ed7){logger[_0x0314('0x17')](_0x0314('0x18'),_0x2e4da5,_0x463ed7);_0x170e30(_0x463ed7);});});}exports[_0x0314('0x1c')]=function(_0x3bd912){var _0x32534a=this;return new Promise(function(_0x44a9c6,_0x52b529){return db[_0x0314('0x1d')][_0x0314('0x1e')]({'raw':_0x3bd912[_0x0314('0x1f')]?_0x3bd912[_0x0314('0x1f')][_0x0314('0x20')]===undefined?!![]:![]:!![],'where':_0x3bd912[_0x0314('0x1f')]?_0x3bd912['options']['where']||null:null,'attributes':_0x3bd912[_0x0314('0x1f')]?_0x3bd912[_0x0314('0x1f')]['attributes']||null:null,'limit':_0x3bd912[_0x0314('0x1f')]?_0x3bd912['options'][_0x0314('0x21')]||null:null,'include':_0x3bd912['options']?_0x3bd912[_0x0314('0x1f')][_0x0314('0x22')]?_[_0x0314('0x23')](_0x3bd912[_0x0314('0x1f')][_0x0314('0x22')],function(_0x3948fa){return{'model':db[_0x3948fa['model']],'as':_0x3948fa['as'],'attributes':_0x3948fa[_0x0314('0x24')],'include':_0x3948fa['include']?_[_0x0314('0x23')](_0x3948fa[_0x0314('0x22')],function(_0x232c87){return{'model':db[_0x232c87[_0x0314('0x25')]],'as':_0x232c87['as'],'attributes':_0x232c87[_0x0314('0x24')],'include':_0x232c87['include']?_[_0x0314('0x23')](_0x232c87[_0x0314('0x22')],function(_0x592a0f){return{'model':db[_0x592a0f['model']],'as':_0x592a0f['as'],'attributes':_0x592a0f[_0x0314('0x24')]};}):[]};}):[]};}):[]:[]})[_0x0314('0x11')](function(_0x4383d5){logger['info'](_0x0314('0x1c'),_0x3bd912);logger[_0x0314('0x14')]('GetFaxAccount',_0x3bd912,JSON[_0x0314('0x16')](_0x4383d5));_0x44a9c6(_0x4383d5);})[_0x0314('0x1b')](function(_0x13674d){logger[_0x0314('0x17')]('GetFaxAccount',_0x13674d['message'],_0x3bd912);_0x52b529(_0x32534a[_0x0314('0x17')](0x1f4,_0x13674d[_0x0314('0x19')]));});});};exports[_0x0314('0x26')]=function(_0x7ebbe5,_0x511d21,_0x3b89f4){var _0x259a05=this;return new Promise(function(_0x388c49,_0x47949f){var _0x18364d={'body':_0x7ebbe5[_0x0314('0x27')],'channel':_0x0314('0x28')};var _0x5464a6=[];var _0x427eb1=[];var _0x567109={};return db[_0x0314('0x29')][_0x0314('0x2a')]()[_0x0314('0x11')](function(_0x442d5c){if(!_0x442d5c){throw new db['Sequelize'][(_0x0314('0x2b'))](_0x0314('0x2c'));}_0x5464a6=_[_0x0314('0x2d')](_[_0x0314('0x2e')](_0x442d5c),[_0x0314('0x2f'),_0x0314('0x30')]);_0x427eb1=_[_0x0314('0x2d')](_[_0x0314('0x2e')](_0x442d5c),[_0x0314('0x2f'),_0x0314('0x30'),_0x0314('0x31'),_0x0314('0x32')]);if(_0x7ebbe5[_0x0314('0x27')]['id']){delete _0x7ebbe5[_0x0314('0x27')]['id'];}if(_[_0x0314('0x33')](_0x7ebbe5['body']['from'])){throw new db[(_0x0314('0x34'))][(_0x0314('0x2b'))](_0x0314('0x35'));}if(_[_0x0314('0x33')](_0x7ebbe5['body'][_0x0314('0x27')])||_0x7ebbe5['body']['body']===''){throw new db['Sequelize']['ValidationError']('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0x0314('0x33')](_0x7ebbe5['body'][_0x0314('0x36')])){throw new db['Sequelize']['ValidationError']('mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x427eb1);}if(!_[_0x0314('0x37')](_0x427eb1,_0x7ebbe5['body'][_0x0314('0x36')])){throw new db[(_0x0314('0x34'))][(_0x0314('0x2b'))](_0x0314('0x38')+_0x427eb1);}_0x567109[_0x7ebbe5['body'][_0x0314('0x36')]]=_0x7ebbe5[_0x0314('0x27')]['from'];})[_0x0314('0x11')](function(){return db['FaxAccount'][_0x0314('0x39')]({'where':{'id':_0x7ebbe5['account']['id']},'include':[{'model':db['CmList'],'as':'List','include':[{'model':db[_0x0314('0x29')],'as':_0x0314('0x3a'),'where':_0x567109,'limit':0x1,'order':[[_0x0314('0x30'),_0x0314('0x3b')]]}]},{'model':db[_0x0314('0x3c')],'as':'Applications'}]});})[_0x0314('0x11')](function(_0x47676f){if(_0x47676f&&_0x47676f[_0x0314('0x3d')]){var _0x27acd1=_0x47676f['get']({'plain':!![]});_0x18364d[_0x0314('0x3e')]=_0x27acd1;_0x18364d[_0x0314('0x3f')]=_0x27acd1['Applications'];_0x18364d['list']=_0x27acd1[_0x0314('0x3d')];_0x18364d[_0x0314('0x3f')]=_['orderBy'](_0x18364d[_0x0314('0x3f')],[_0x0314('0x40')],[_0x0314('0x41')]);if(_0x18364d['account'][_0x0314('0x42')]){delete _0x18364d[_0x0314('0x3e')][_0x0314('0x42')];}if(_0x18364d[_0x0314('0x3e')]['List']&&_0x18364d[_0x0314('0x3e')][_0x0314('0x3d')][_0x0314('0x3a')][_0x0314('0x43')]){return _0x18364d['account']['List'][_0x0314('0x3a')][0x0];}var _0x45d6ad=_[_0x0314('0xc')](_0x7ebbe5[_0x0314('0x27')],{'firstName':_0x7ebbe5[_0x0314('0x27')]['from'],'ListId':_0x27acd1[_0x0314('0x32')]});_0x45d6ad[_0x7ebbe5['body']['mapKey']]=_0x7ebbe5['body'][_0x0314('0x44')];return db[_0x0314('0x29')][_0x0314('0x45')](_0x45d6ad,{'fields':_0x5464a6,'raw':!![]});}})[_0x0314('0x11')](function(_0x30bdb2){_0x18364d['contact']=_0x30bdb2;if(_0x30bdb2){var _0x103060={'ContactId':_0x30bdb2['id'],'FaxAccountId':_0x7ebbe5[_0x0314('0x3e')]['id'],'fax':_0x30bdb2[_0x0314('0x28')]};return db['FaxInteraction'][_0x0314('0x39')]({'where':{'ContactId':_0x30bdb2['id'],'closed':![],'FaxAccountId':_0x7ebbe5[_0x0314('0x3e')]['id']}})[_0x0314('0x11')](function(_0x1ddc19){if(_0x1ddc19){return[_0x1ddc19,![]];}return db[_0x0314('0x46')][_0x0314('0x45')](_0x103060)[_0x0314('0x11')](function(_0x46d48e){return[_0x46d48e,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0x0314('0x47')](function(_0x56d372,_0x5e0a5c){var _0x28749b=_0x56d372[_0x0314('0x48')]({'plain':!![]});if(_0x5e0a5c){if(_0x18364d[_0x0314('0x3e')][_0x0314('0x49')]){_0x18364d[_0x0314('0x3f')]['push']({'id':0x0,'priority':_0x18364d[_0x0314('0x3f')]['length']+0x1,'app':'close','appdata':_0x0314('0x4a'),'interval':_0x0314('0x4b')});}_0x28749b[_0x0314('0x4c')]=!![];}else{_0x28749b[_0x0314('0x4c')]=![];}_0x18364d['interaction']=_0x28749b;return db['FaxMessage'][_0x0314('0x45')]({'body':_0x7ebbe5[_0x0314('0x27')][_0x0314('0x27')],'FaxAccountId':_0x7ebbe5[_0x0314('0x3e')]['id'],'FaxInteractionId':_0x56d372['id'],'direction':'in','ContactId':_0x56d372[_0x0314('0x4d')],'AttachmentId':_0x7ebbe5['body']['AttachmentId']});})['then'](function(_0x3f8300){_0x18364d[_0x0314('0x19')]=_0x3f8300;if(_0x18364d[_0x0314('0x4e')][_0x0314('0x4f')]){return db[_0x0314('0x50')][_0x0314('0x39')]({'attributes':['id',_0x0314('0x51')],'where':{'id':_0x18364d[_0x0314('0x4e')][_0x0314('0x4f')]}})['then'](function(_0x4c367d){if(_0x4c367d){_0x18364d['applications']['unshift']({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x4c367d[_0x0314('0x51')]+','+(_0x18364d[_0x0314('0x3e')]['waitForTheAssignedAgent']||0xa),'interval':_0x0314('0x4b')});}return respondWithRpcPromise('Start',_0x0314('0x52'),_0x18364d);});}return respondWithRpcPromise(_0x0314('0x53'),_0x0314('0x52'),_0x18364d);})[_0x0314('0x11')](function(_0xddef07){_0x388c49(_0xddef07);})[_0x0314('0x1b')](function(_0x4ae315){_0x47949f(_0x259a05[_0x0314('0x17')](0x1f4,_0x4ae315[_0x0314('0x19')]));});});};