85f07f0275af12a483e5751395a802d3c6df6886
[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 _0xdbbd=['list','orderBy','priority','asc','length','ListId','create','contact','FaxInteraction','Contact\x20not\x20found','spread','get','close','unmanaged','created','interaction','FaxMessage','ContactId','AttachmentId','UserId','User','name','*,*,*,*','startRouting','util','moment','bluebird','ioredis','../../mysqldb','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','redis','defaults','socket.io-emitter','register','request','then','FaxAccount,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','info','catch','GetFaxAccount','findAll','options','where','attributes','model','include','debug','NotifyFaxAccount','body','fax','CmContact','describe','difference','createdAt','updatedAt','keys','Sequelize','ValidationError','from\x20is\x20mandatory','isNil','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','mapKey','from','FaxAccount','account','CmList','List','Contacts','DESC','Applications','applications'];(function(_0x463f6f,_0x19b8f6){var _0x5c908d=function(_0x35c275){while(--_0x35c275){_0x463f6f['push'](_0x463f6f['shift']());}};_0x5c908d(++_0x19b8f6);}(_0xdbbd,0x1bc));var _0xddbb=function(_0x40bffc,_0x3a45d7){_0x40bffc=_0x40bffc-0x0;var _0x3007e9=_0xdbbd[_0x40bffc];return _0x3007e9;};'use strict';var _=require('lodash');var util=require(_0xddbb('0x0'));var moment=require(_0xddbb('0x1'));var BPromise=require(_0xddbb('0x2'));var rs=require('randomstring');var fs=require('fs');var Redis=require(_0xddbb('0x3'));var db=require(_0xddbb('0x4'))['db'];var utils=require('../../config/utils');var logger=require(_0xddbb('0x5'))(_0xddbb('0x6'));var config=require(_0xddbb('0x7'));var jayson=require(_0xddbb('0x8'));var client=jayson[_0xddbb('0x9')][_0xddbb('0xa')]({'port':0x232c});config[_0xddbb('0xb')]=_[_0xddbb('0xc')](config[_0xddbb('0xb')],{'host':'localhost','port':0x18eb});var socket=require(_0xddbb('0xd'))(new Redis(config[_0xddbb('0xb')]));require('./faxAccount.socket')[_0xddbb('0xe')](socket);function respondWithRpcPromise(_0xa1063c,_0x1bb728,_0x398a75){return new BPromise(function(_0xc67008,_0x7c7f70){return client[_0xddbb('0xf')](_0xa1063c,_0x398a75)[_0xddbb('0x10')](function(_0x4ec365){logger['info'](_0xddbb('0x11'),_0x1bb728,_0xddbb('0x12'));logger['debug']('FaxAccount,\x20%s,\x20%s,\x20%s',_0x1bb728,'request\x20sent',JSON[_0xddbb('0x13')](_0x4ec365));if(_0x4ec365[_0xddbb('0x14')]){if(_0x4ec365[_0xddbb('0x14')][_0xddbb('0x15')]===0x1f4){logger[_0xddbb('0x14')](_0xddbb('0x11'),_0x1bb728,_0x4ec365['error']['message']);return _0x7c7f70(_0x4ec365[_0xddbb('0x14')][_0xddbb('0x16')]);}logger[_0xddbb('0x14')]('FaxAccount,\x20%s,\x20%s',_0x1bb728,_0x4ec365['error'][_0xddbb('0x16')]);return _0xc67008(_0x4ec365[_0xddbb('0x14')][_0xddbb('0x16')]);}else{logger[_0xddbb('0x17')](_0xddbb('0x11'),_0x1bb728,'request\x20sent');_0xc67008(_0x4ec365['result']['message']);}})[_0xddbb('0x18')](function(_0xa4b5f){logger['error']('FaxAccount,\x20%s,\x20%s',_0x1bb728,_0xa4b5f);_0x7c7f70(_0xa4b5f);});});}exports[_0xddbb('0x19')]=function(_0x57b841){var _0x1d5104=this;return new Promise(function(_0x34bc41,_0x15698a){return db['FaxAccount'][_0xddbb('0x1a')]({'raw':_0x57b841[_0xddbb('0x1b')]?_0x57b841[_0xddbb('0x1b')]['raw']===undefined?!![]:![]:!![],'where':_0x57b841[_0xddbb('0x1b')]?_0x57b841['options'][_0xddbb('0x1c')]||null:null,'attributes':_0x57b841[_0xddbb('0x1b')]?_0x57b841[_0xddbb('0x1b')][_0xddbb('0x1d')]||null:null,'limit':_0x57b841[_0xddbb('0x1b')]?_0x57b841[_0xddbb('0x1b')]['limit']||null:null,'include':_0x57b841[_0xddbb('0x1b')]?_0x57b841[_0xddbb('0x1b')]['include']?_['map'](_0x57b841['options']['include'],function(_0x51067d){return{'model':db[_0x51067d[_0xddbb('0x1e')]],'as':_0x51067d['as'],'attributes':_0x51067d[_0xddbb('0x1d')],'include':_0x51067d[_0xddbb('0x1f')]?_['map'](_0x51067d[_0xddbb('0x1f')],function(_0x2c8a33){return{'model':db[_0x2c8a33[_0xddbb('0x1e')]],'as':_0x2c8a33['as'],'attributes':_0x2c8a33[_0xddbb('0x1d')],'include':_0x2c8a33['include']?_['map'](_0x2c8a33[_0xddbb('0x1f')],function(_0x47d0f2){return{'model':db[_0x47d0f2['model']],'as':_0x47d0f2['as'],'attributes':_0x47d0f2[_0xddbb('0x1d')]};}):[]};}):[]};}):[]:[]})[_0xddbb('0x10')](function(_0x31ab8e){logger[_0xddbb('0x17')]('GetFaxAccount',_0x57b841);logger[_0xddbb('0x20')](_0xddbb('0x19'),_0x57b841,JSON[_0xddbb('0x13')](_0x31ab8e));_0x34bc41(_0x31ab8e);})[_0xddbb('0x18')](function(_0x4f61ee){logger[_0xddbb('0x14')](_0xddbb('0x19'),_0x4f61ee['message'],_0x57b841);_0x15698a(_0x1d5104['error'](0x1f4,_0x4f61ee['message']));});});};exports[_0xddbb('0x21')]=function(_0x18bba4,_0x34f487,_0x1c6bff){var _0x194a8f=this;return new Promise(function(_0x30f933,_0x15a8dd){var _0xb2cbee={'body':_0x18bba4[_0xddbb('0x22')],'channel':_0xddbb('0x23')};var _0x3a331c=[];var _0x5ebd5d=[];var _0x221851={};return db[_0xddbb('0x24')][_0xddbb('0x25')]()['then'](function(_0x1c2bd4){if(!_0x1c2bd4){throw new db['Sequelize']['ValidationError']('no\x20available\x20attributes');}_0x3a331c=_[_0xddbb('0x26')](_['keys'](_0x1c2bd4),[_0xddbb('0x27'),_0xddbb('0x28')]);_0x5ebd5d=_[_0xddbb('0x26')](_[_0xddbb('0x29')](_0x1c2bd4),['createdAt','updatedAt','CompanyId','ListId']);if(_0x18bba4[_0xddbb('0x22')]['id']){delete _0x18bba4['body']['id'];}if(_['isNil'](_0x18bba4[_0xddbb('0x22')]['from'])){throw new db[(_0xddbb('0x2a'))][(_0xddbb('0x2b'))](_0xddbb('0x2c'));}if(_[_0xddbb('0x2d')](_0x18bba4[_0xddbb('0x22')][_0xddbb('0x22')])||_0x18bba4[_0xddbb('0x22')][_0xddbb('0x22')]===''){throw new db[(_0xddbb('0x2a'))][(_0xddbb('0x2b'))](_0xddbb('0x2e'));}if(_['isNil'](_0x18bba4[_0xddbb('0x22')]['mapKey'])){throw new db['Sequelize'][(_0xddbb('0x2b'))](_0xddbb('0x2f')+_0x5ebd5d);}if(!_[_0xddbb('0x30')](_0x5ebd5d,_0x18bba4['body']['mapKey'])){throw new db['Sequelize']['ValidationError'](_0xddbb('0x31')+_0x5ebd5d);}_0x221851[_0x18bba4['body'][_0xddbb('0x32')]]=_0x18bba4[_0xddbb('0x22')][_0xddbb('0x33')];})[_0xddbb('0x10')](function(){return db[_0xddbb('0x34')]['find']({'where':{'id':_0x18bba4[_0xddbb('0x35')]['id']},'include':[{'model':db[_0xddbb('0x36')],'as':_0xddbb('0x37'),'include':[{'model':db[_0xddbb('0x24')],'as':_0xddbb('0x38'),'where':_0x221851,'limit':0x1,'order':[[_0xddbb('0x28'),_0xddbb('0x39')]]}]},{'model':db['FaxApplication'],'as':_0xddbb('0x3a')}]});})['then'](function(_0x151ac6){if(_0x151ac6&&_0x151ac6['List']){var _0x31c9e2=_0x151ac6['get']({'plain':!![]});_0xb2cbee[_0xddbb('0x35')]=_0x31c9e2;_0xb2cbee[_0xddbb('0x3b')]=_0x31c9e2[_0xddbb('0x3a')];_0xb2cbee[_0xddbb('0x3c')]=_0x31c9e2[_0xddbb('0x37')];_0xb2cbee[_0xddbb('0x3b')]=_[_0xddbb('0x3d')](_0xb2cbee[_0xddbb('0x3b')],[_0xddbb('0x3e')],[_0xddbb('0x3f')]);if(_0xb2cbee[_0xddbb('0x35')][_0xddbb('0x3a')]){delete _0xb2cbee[_0xddbb('0x35')]['Applications'];}if(_0xb2cbee['account'][_0xddbb('0x37')]&&_0xb2cbee[_0xddbb('0x35')][_0xddbb('0x37')][_0xddbb('0x38')][_0xddbb('0x40')]){return _0xb2cbee[_0xddbb('0x35')][_0xddbb('0x37')][_0xddbb('0x38')][0x0];}var _0x550031=_[_0xddbb('0xc')](_0x18bba4[_0xddbb('0x22')],{'firstName':_0x18bba4['body'][_0xddbb('0x33')],'ListId':_0x31c9e2[_0xddbb('0x41')]});_0x550031[_0x18bba4[_0xddbb('0x22')]['mapKey']]=_0x18bba4[_0xddbb('0x22')][_0xddbb('0x33')];return db['CmContact'][_0xddbb('0x42')](_0x550031,{'fields':_0x3a331c,'raw':!![]});}})[_0xddbb('0x10')](function(_0x2c3c80){_0xb2cbee[_0xddbb('0x43')]=_0x2c3c80;if(_0x2c3c80){var _0x163bc5={'ContactId':_0x2c3c80['id'],'FaxAccountId':_0x18bba4[_0xddbb('0x35')]['id'],'fax':_0x2c3c80[_0xddbb('0x23')]};return db[_0xddbb('0x44')]['find']({'where':{'ContactId':_0x2c3c80['id'],'closed':![],'FaxAccountId':_0x18bba4[_0xddbb('0x35')]['id']}})[_0xddbb('0x10')](function(_0x510821){if(_0x510821){return[_0x510821,![]];}return db[_0xddbb('0x44')][_0xddbb('0x42')](_0x163bc5)[_0xddbb('0x10')](function(_0x128b29){return[_0x128b29,!![]];});});}else{throw new Error(_0xddbb('0x45'));}})[_0xddbb('0x46')](function(_0x50d739,_0x1d109b){var _0xb4d880=_0x50d739[_0xddbb('0x47')]({'plain':!![]});if(_0x1d109b){if(_0xb2cbee[_0xddbb('0x35')]['autoclose']){_0xb2cbee[_0xddbb('0x3b')]['push']({'id':0x0,'priority':_0xb2cbee[_0xddbb('0x3b')][_0xddbb('0x40')]+0x1,'app':_0xddbb('0x48'),'appdata':_0xddbb('0x49'),'interval':'*,*,*,*'});}_0xb4d880[_0xddbb('0x4a')]=!![];}else{_0xb4d880[_0xddbb('0x4a')]=![];}_0xb2cbee[_0xddbb('0x4b')]=_0xb4d880;return db[_0xddbb('0x4c')]['create']({'body':_0x18bba4[_0xddbb('0x22')][_0xddbb('0x22')],'FaxAccountId':_0x18bba4['account']['id'],'FaxInteractionId':_0x50d739['id'],'direction':'in','ContactId':_0x50d739[_0xddbb('0x4d')],'AttachmentId':_0x18bba4[_0xddbb('0x22')][_0xddbb('0x4e')]});})['then'](function(_0x3b19e7){_0xb2cbee[_0xddbb('0x16')]=_0x3b19e7;if(_0xb2cbee['interaction'][_0xddbb('0x4f')]){return db[_0xddbb('0x50')]['find']({'attributes':['id',_0xddbb('0x51')],'where':{'id':_0xb2cbee['interaction'][_0xddbb('0x4f')]}})[_0xddbb('0x10')](function(_0x21ed32){if(_0x21ed32){_0xb2cbee[_0xddbb('0x3b')]['unshift']({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x21ed32[_0xddbb('0x51')]+','+(_0xb2cbee[_0xddbb('0x35')]['waitForTheAssignedAgent']||0xa),'interval':_0xddbb('0x52')});}return respondWithRpcPromise('Start',_0xddbb('0x53'),_0xb2cbee);});}return respondWithRpcPromise('Start',_0xddbb('0x53'),_0xb2cbee);})[_0xddbb('0x10')](function(_0x1fc72b){_0x30f933(_0x1fc72b);})[_0xddbb('0x18')](function(_0x9b49f5){_0x15a8dd(_0x194a8f[_0xddbb('0x14')](0x1f4,_0x9b49f5[_0xddbb('0x16')]));});});};