Built motion from commit f67b3b02.|2.6.31
[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 _0x200f=['DESC','FaxApplication','get','list','applications','priority','asc','account','Applications','contact','fax','FaxInteraction','create','spread','autoclose','push','length','unmanaged','*,*,*,*','created','interaction','ContactId','AttachmentId','UserId','User','name','unshift','agent','waitForTheAssignedAgent','queueId','FaxQueue','queue','waitForTheAssignedQueue','startRouting','util','moment','bluebird','randomstring','../../mysqldb','../../config/utils','rpc','jayson/promise','http','redis','defaults','register','request','then','info','FaxAccount,\x20%s,\x20%s','request\x20sent','FaxAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','catch','GetFaxAccount','FaxAccount','findAll','options','raw','where','attributes','limit','include','map','model','debug','NotifyFaxAccount','body','describe','Sequelize','ValidationError','no\x20available\x20attributes','difference','keys','updatedAt','createdAt','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','find','List','CmContact','Contacts'];(function(_0x565c49,_0x54acd0){var _0x20ca34=function(_0x42f7e1){while(--_0x42f7e1){_0x565c49['push'](_0x565c49['shift']());}};_0x20ca34(++_0x54acd0);}(_0x200f,0x136));var _0xf200=function(_0x4e9f1a,_0x35e3f3){_0x4e9f1a=_0x4e9f1a-0x0;var _0x441f20=_0x200f[_0x4e9f1a];return _0x441f20;};'use strict';var _=require('lodash');var util=require(_0xf200('0x0'));var moment=require(_0xf200('0x1'));var BPromise=require(_0xf200('0x2'));var rs=require(_0xf200('0x3'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0xf200('0x4'))['db'];var utils=require(_0xf200('0x5'));var logger=require('../../config/logger')(_0xf200('0x6'));var config=require('../../config/environment');var jayson=require(_0xf200('0x7'));var client=jayson['client'][_0xf200('0x8')]({'port':0x232c});config[_0xf200('0x9')]=_[_0xf200('0xa')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xf200('0x9')]));require('./faxAccount.socket')[_0xf200('0xb')](socket);function respondWithRpcPromise(_0x183789,_0x358a71,_0x18f9f1){return new BPromise(function(_0x3ac70e,_0x61f5d5){return client[_0xf200('0xc')](_0x183789,_0x18f9f1)[_0xf200('0xd')](function(_0x4c089a){logger[_0xf200('0xe')](_0xf200('0xf'),_0x358a71,_0xf200('0x10'));logger['debug'](_0xf200('0x11'),_0x358a71,'request\x20sent',JSON[_0xf200('0x12')](_0x4c089a));if(_0x4c089a[_0xf200('0x13')]){if(_0x4c089a[_0xf200('0x13')][_0xf200('0x14')]===0x1f4){logger['error']('FaxAccount,\x20%s,\x20%s',_0x358a71,_0x4c089a[_0xf200('0x13')][_0xf200('0x15')]);return _0x61f5d5(_0x4c089a[_0xf200('0x13')][_0xf200('0x15')]);}logger[_0xf200('0x13')](_0xf200('0xf'),_0x358a71,_0x4c089a[_0xf200('0x13')][_0xf200('0x15')]);return _0x3ac70e(_0x4c089a[_0xf200('0x13')][_0xf200('0x15')]);}else{logger['info'](_0xf200('0xf'),_0x358a71,_0xf200('0x10'));_0x3ac70e(_0x4c089a['result'][_0xf200('0x15')]);}})[_0xf200('0x16')](function(_0x96d1ec){logger[_0xf200('0x13')]('FaxAccount,\x20%s,\x20%s',_0x358a71,_0x96d1ec);_0x61f5d5(_0x96d1ec);});});}exports[_0xf200('0x17')]=function(_0x51a0be){var _0x347f64=this;return new Promise(function(_0x902c28,_0x1888da){return db[_0xf200('0x18')][_0xf200('0x19')]({'raw':_0x51a0be[_0xf200('0x1a')]?_0x51a0be[_0xf200('0x1a')][_0xf200('0x1b')]===undefined?!![]:![]:!![],'where':_0x51a0be['options']?_0x51a0be['options'][_0xf200('0x1c')]||null:null,'attributes':_0x51a0be[_0xf200('0x1a')]?_0x51a0be[_0xf200('0x1a')][_0xf200('0x1d')]||null:null,'limit':_0x51a0be[_0xf200('0x1a')]?_0x51a0be['options'][_0xf200('0x1e')]||null:null,'include':_0x51a0be[_0xf200('0x1a')]?_0x51a0be[_0xf200('0x1a')][_0xf200('0x1f')]?_[_0xf200('0x20')](_0x51a0be[_0xf200('0x1a')]['include'],function(_0x190347){return{'model':db[_0x190347[_0xf200('0x21')]],'as':_0x190347['as'],'attributes':_0x190347[_0xf200('0x1d')],'include':_0x190347[_0xf200('0x1f')]?_['map'](_0x190347[_0xf200('0x1f')],function(_0x13aa44){return{'model':db[_0x13aa44[_0xf200('0x21')]],'as':_0x13aa44['as'],'attributes':_0x13aa44[_0xf200('0x1d')],'include':_0x13aa44[_0xf200('0x1f')]?_['map'](_0x13aa44[_0xf200('0x1f')],function(_0x38075c){return{'model':db[_0x38075c[_0xf200('0x21')]],'as':_0x38075c['as'],'attributes':_0x38075c[_0xf200('0x1d')]};}):[]};}):[]};}):[]:[]})[_0xf200('0xd')](function(_0x3d0071){logger['info']('GetFaxAccount',_0x51a0be);logger[_0xf200('0x22')](_0xf200('0x17'),_0x51a0be,JSON[_0xf200('0x12')](_0x3d0071));_0x902c28(_0x3d0071);})['catch'](function(_0x37d91e){logger[_0xf200('0x13')]('GetFaxAccount',_0x37d91e[_0xf200('0x15')],_0x51a0be);_0x1888da(_0x347f64[_0xf200('0x13')](0x1f4,_0x37d91e[_0xf200('0x15')]));});});};exports[_0xf200('0x23')]=function(_0x2bc469,_0xf3ae31,_0x4502e4){var _0x576b03=this;return new Promise(function(_0x4c1f2b,_0x3f7f03){var _0x38a9fb={'body':_0x2bc469[_0xf200('0x24')],'channel':'fax'};var _0x1eedb3=[];var _0x174b0d=[];var _0x2e37a7={};return db['CmContact'][_0xf200('0x25')]()[_0xf200('0xd')](function(_0x3cfebc){if(!_0x3cfebc){throw new db[(_0xf200('0x26'))][(_0xf200('0x27'))](_0xf200('0x28'));}_0x1eedb3=_[_0xf200('0x29')](_[_0xf200('0x2a')](_0x3cfebc),['createdAt',_0xf200('0x2b')]);_0x174b0d=_['difference'](_[_0xf200('0x2a')](_0x3cfebc),[_0xf200('0x2c'),'updatedAt',_0xf200('0x2d'),_0xf200('0x2e')]);if(_0x2bc469[_0xf200('0x24')]['id']){delete _0x2bc469[_0xf200('0x24')]['id'];}if(_[_0xf200('0x2f')](_0x2bc469[_0xf200('0x24')][_0xf200('0x30')])){throw new db[(_0xf200('0x26'))]['ValidationError'](_0xf200('0x31'));}if(_['isNil'](_0x2bc469['body'][_0xf200('0x24')])||_0x2bc469[_0xf200('0x24')][_0xf200('0x24')]===''){throw new db[(_0xf200('0x26'))][(_0xf200('0x27'))](_0xf200('0x32'));}if(_['isNil'](_0x2bc469[_0xf200('0x24')][_0xf200('0x33')])){throw new db[(_0xf200('0x26'))][(_0xf200('0x27'))]('mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x174b0d);}if(!_[_0xf200('0x34')](_0x174b0d,_0x2bc469[_0xf200('0x24')][_0xf200('0x33')])){throw new db[(_0xf200('0x26'))]['ValidationError'](_0xf200('0x35')+_0x174b0d);}_0x2e37a7[_0x2bc469[_0xf200('0x24')]['mapKey']]=_0x2bc469[_0xf200('0x24')]['from'];})['then'](function(){return db[_0xf200('0x18')][_0xf200('0x36')]({'where':{'id':_0x2bc469['account']['id']},'include':[{'model':db['CmList'],'as':_0xf200('0x37'),'include':[{'model':db[_0xf200('0x38')],'as':_0xf200('0x39'),'where':_0x2e37a7,'limit':0x1,'order':[[_0xf200('0x2b'),_0xf200('0x3a')]]}]},{'model':db[_0xf200('0x3b')],'as':'Applications'}]});})['then'](function(_0x45d8bb){if(_0x45d8bb&&_0x45d8bb[_0xf200('0x37')]){var _0x50be6f=_0x45d8bb[_0xf200('0x3c')]({'plain':!![]});_0x38a9fb['account']=_0x50be6f;_0x38a9fb['applications']=_0x50be6f['Applications'];_0x38a9fb[_0xf200('0x3d')]=_0x50be6f[_0xf200('0x37')];_0x38a9fb['applications']=_['orderBy'](_0x38a9fb[_0xf200('0x3e')],[_0xf200('0x3f')],[_0xf200('0x40')]);if(_0x38a9fb['account']['Applications']){delete _0x38a9fb[_0xf200('0x41')][_0xf200('0x42')];}if(_0x38a9fb['account'][_0xf200('0x37')]&&_0x38a9fb['account'][_0xf200('0x37')][_0xf200('0x39')]['length']){return _0x38a9fb[_0xf200('0x41')][_0xf200('0x37')]['Contacts'][0x0];}var _0x3096da=_[_0xf200('0xa')](_0x2bc469[_0xf200('0x24')],{'firstName':_0x2bc469[_0xf200('0x24')][_0xf200('0x30')],'ListId':_0x50be6f[_0xf200('0x2e')]});_0x3096da[_0x2bc469[_0xf200('0x24')][_0xf200('0x33')]]=_0x2bc469[_0xf200('0x24')][_0xf200('0x30')];return db[_0xf200('0x38')]['create'](_0x3096da,{'fields':_0x1eedb3,'raw':!![]});}})[_0xf200('0xd')](function(_0xd53359){_0x38a9fb[_0xf200('0x43')]=_0xd53359;if(_0xd53359){var _0x271dad={'ContactId':_0xd53359['id'],'FaxAccountId':_0x2bc469['account']['id'],'fax':_0xd53359[_0xf200('0x44')]};return db[_0xf200('0x45')][_0xf200('0x36')]({'where':{'ContactId':_0xd53359['id'],'closed':![],'FaxAccountId':_0x2bc469[_0xf200('0x41')]['id']}})[_0xf200('0xd')](function(_0x4e49ed){if(_0x4e49ed){return[_0x4e49ed,![]];}return db['FaxInteraction'][_0xf200('0x46')](_0x271dad)[_0xf200('0xd')](function(_0x3a9949){return[_0x3a9949,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0xf200('0x47')](function(_0x25fdb2,_0x37a1a7){var _0x3f330d=_0x25fdb2['get']({'plain':!![]});if(_0x37a1a7){if(_0x38a9fb[_0xf200('0x41')][_0xf200('0x48')]){_0x38a9fb[_0xf200('0x3e')][_0xf200('0x49')]({'id':0x0,'priority':_0x38a9fb[_0xf200('0x3e')][_0xf200('0x4a')]+0x1,'app':'close','appdata':_0xf200('0x4b'),'interval':_0xf200('0x4c')});}_0x3f330d[_0xf200('0x4d')]=!![];}else{_0x3f330d[_0xf200('0x4d')]=![];}_0x38a9fb[_0xf200('0x4e')]=_0x3f330d;return db['FaxMessage']['create']({'body':_0x2bc469[_0xf200('0x24')]['body'],'FaxAccountId':_0x2bc469[_0xf200('0x41')]['id'],'FaxInteractionId':_0x25fdb2['id'],'direction':'in','ContactId':_0x25fdb2[_0xf200('0x4f')],'AttachmentId':_0x2bc469[_0xf200('0x24')][_0xf200('0x50')]});})[_0xf200('0xd')](function(_0x223e41){_0x38a9fb[_0xf200('0x15')]=_0x223e41;if(!_0x38a9fb[_0xf200('0x4e')][_0xf200('0x51')])return;return db[_0xf200('0x52')]['find']({'attributes':['id',_0xf200('0x53')],'where':{'id':_0x38a9fb[_0xf200('0x4e')][_0xf200('0x51')]}});})[_0xf200('0xd')](function(_0x498ddd){if(_0x498ddd){_0x38a9fb['applications'][_0xf200('0x54')]({'id':0x0,'priority':0x0,'app':_0xf200('0x55'),'appdata':_0x498ddd[_0xf200('0x53')]+','+(_0x38a9fb[_0xf200('0x41')][_0xf200('0x56')]||0xa),'interval':_0xf200('0x4c')});}if(!_0x38a9fb['interaction'][_0xf200('0x57')])return;return db[_0xf200('0x58')][_0xf200('0x36')]({'attributes':['id',_0xf200('0x53')],'where':{'id':_0x38a9fb[_0xf200('0x4e')][_0xf200('0x57')]}});})[_0xf200('0xd')](function(_0x1e4798){if(_0x1e4798){_0x38a9fb[_0xf200('0x3e')]['unshift']({'id':0x0,'priority':0x1,'app':_0xf200('0x59'),'appdata':_0x1e4798[_0xf200('0x53')]+','+(_0x38a9fb[_0xf200('0x41')][_0xf200('0x5a')]||0x12c),'interval':'*,*,*,*'});}return respondWithRpcPromise('Start',_0xf200('0x5b'),_0x38a9fb);})['then'](function(_0x275faa){_0x4c1f2b(_0x275faa);})[_0xf200('0x16')](function(_0x4a1b24){_0x3f7f03(_0x576b03[_0xf200('0x13')](0x1f4,_0x4a1b24[_0xf200('0x15')]));});});};