Built motion from commit e8da49ee.|2.6.33
[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 _0x3bc4=['unmanaged','*,*,*,*','created','ContactId','AttachmentId','UserId','name','interaction','agent','waitForTheAssignedAgent','FaxQueue','queueId','unshift','queue','waitForTheAssignedQueue','Start','lodash','util','moment','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','redis','defaults','socket.io-emitter','./faxAccount.socket','register','request','info','FaxAccount,\x20%s,\x20%s','debug','FaxAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','catch','GetFaxAccount','FaxAccount','options','raw','where','attributes','limit','include','map','model','body','fax','CmContact','then','Sequelize','no\x20available\x20attributes','difference','keys','createdAt','updatedAt','CompanyId','ListId','isNil','ValidationError','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','from','CmList','Contacts','DESC','FaxApplication','Applications','List','account','applications','list','asc','create','contact','FaxInteraction','find','Contact\x20not\x20found','spread','get','autoclose','push'];(function(_0x48b784,_0x2999e9){var _0x17f563=function(_0x50c7d2){while(--_0x50c7d2){_0x48b784['push'](_0x48b784['shift']());}};_0x17f563(++_0x2999e9);}(_0x3bc4,0x12a));var _0x43bc=function(_0x154192,_0x2d360f){_0x154192=_0x154192-0x0;var _0x3c2792=_0x3bc4[_0x154192];return _0x3c2792;};'use strict';var _=require(_0x43bc('0x0'));var util=require(_0x43bc('0x1'));var moment=require(_0x43bc('0x2'));var BPromise=require('bluebird');var rs=require('randomstring');var fs=require('fs');var Redis=require(_0x43bc('0x3'));var db=require(_0x43bc('0x4'))['db'];var utils=require(_0x43bc('0x5'));var logger=require(_0x43bc('0x6'))(_0x43bc('0x7'));var config=require(_0x43bc('0x8'));var jayson=require(_0x43bc('0x9'));var client=jayson[_0x43bc('0xa')][_0x43bc('0xb')]({'port':0x232c});config[_0x43bc('0xc')]=_[_0x43bc('0xd')](config[_0x43bc('0xc')],{'host':'localhost','port':0x18eb});var socket=require(_0x43bc('0xe'))(new Redis(config['redis']));require(_0x43bc('0xf'))[_0x43bc('0x10')](socket);function respondWithRpcPromise(_0x1492fa,_0x45a73e,_0x2c79e1){return new BPromise(function(_0x3aa1ed,_0x180458){return client[_0x43bc('0x11')](_0x1492fa,_0x2c79e1)['then'](function(_0x1990b0){logger[_0x43bc('0x12')](_0x43bc('0x13'),_0x45a73e,'request\x20sent');logger[_0x43bc('0x14')](_0x43bc('0x15'),_0x45a73e,_0x43bc('0x16'),JSON[_0x43bc('0x17')](_0x1990b0));if(_0x1990b0[_0x43bc('0x18')]){if(_0x1990b0[_0x43bc('0x18')][_0x43bc('0x19')]===0x1f4){logger[_0x43bc('0x18')](_0x43bc('0x13'),_0x45a73e,_0x1990b0['error']['message']);return _0x180458(_0x1990b0[_0x43bc('0x18')][_0x43bc('0x1a')]);}logger[_0x43bc('0x18')](_0x43bc('0x13'),_0x45a73e,_0x1990b0[_0x43bc('0x18')][_0x43bc('0x1a')]);return _0x3aa1ed(_0x1990b0[_0x43bc('0x18')][_0x43bc('0x1a')]);}else{logger[_0x43bc('0x12')](_0x43bc('0x13'),_0x45a73e,_0x43bc('0x16'));_0x3aa1ed(_0x1990b0['result']['message']);}})[_0x43bc('0x1b')](function(_0x957a47){logger[_0x43bc('0x18')](_0x43bc('0x13'),_0x45a73e,_0x957a47);_0x180458(_0x957a47);});});}exports[_0x43bc('0x1c')]=function(_0x3da597){var _0xc5c5ce=this;return new Promise(function(_0x501552,_0x27f7fd){return db[_0x43bc('0x1d')]['findAll']({'raw':_0x3da597[_0x43bc('0x1e')]?_0x3da597['options'][_0x43bc('0x1f')]===undefined?!![]:![]:!![],'where':_0x3da597['options']?_0x3da597[_0x43bc('0x1e')][_0x43bc('0x20')]||null:null,'attributes':_0x3da597[_0x43bc('0x1e')]?_0x3da597[_0x43bc('0x1e')][_0x43bc('0x21')]||null:null,'limit':_0x3da597[_0x43bc('0x1e')]?_0x3da597['options'][_0x43bc('0x22')]||null:null,'include':_0x3da597[_0x43bc('0x1e')]?_0x3da597[_0x43bc('0x1e')][_0x43bc('0x23')]?_[_0x43bc('0x24')](_0x3da597['options']['include'],function(_0x726dee){return{'model':db[_0x726dee[_0x43bc('0x25')]],'as':_0x726dee['as'],'attributes':_0x726dee['attributes'],'include':_0x726dee['include']?_[_0x43bc('0x24')](_0x726dee[_0x43bc('0x23')],function(_0x464f8e){return{'model':db[_0x464f8e[_0x43bc('0x25')]],'as':_0x464f8e['as'],'attributes':_0x464f8e[_0x43bc('0x21')],'include':_0x464f8e[_0x43bc('0x23')]?_['map'](_0x464f8e['include'],function(_0x3d31a6){return{'model':db[_0x3d31a6[_0x43bc('0x25')]],'as':_0x3d31a6['as'],'attributes':_0x3d31a6['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x3a86e2){logger[_0x43bc('0x12')](_0x43bc('0x1c'),_0x3da597);logger['debug']('GetFaxAccount',_0x3da597,JSON[_0x43bc('0x17')](_0x3a86e2));_0x501552(_0x3a86e2);})[_0x43bc('0x1b')](function(_0xcfcca7){logger[_0x43bc('0x18')](_0x43bc('0x1c'),_0xcfcca7['message'],_0x3da597);_0x27f7fd(_0xc5c5ce[_0x43bc('0x18')](0x1f4,_0xcfcca7[_0x43bc('0x1a')]));});});};exports['NotifyFaxAccount']=function(_0x251d07,_0x5aa868,_0x3184b0){var _0x359ec1=this;return new Promise(function(_0x45b411,_0x2a0fdb){var _0x35cc5b={'body':_0x251d07[_0x43bc('0x26')],'channel':_0x43bc('0x27')};var _0x50233b=[];var _0x537189=[];var _0x5b79e6={};return db[_0x43bc('0x28')]['describe']()[_0x43bc('0x29')](function(_0x4d7587){if(!_0x4d7587){throw new db[(_0x43bc('0x2a'))]['ValidationError'](_0x43bc('0x2b'));}_0x50233b=_[_0x43bc('0x2c')](_[_0x43bc('0x2d')](_0x4d7587),[_0x43bc('0x2e'),_0x43bc('0x2f')]);_0x537189=_[_0x43bc('0x2c')](_['keys'](_0x4d7587),['createdAt',_0x43bc('0x2f'),_0x43bc('0x30'),_0x43bc('0x31')]);if(_0x251d07[_0x43bc('0x26')]['id']){delete _0x251d07[_0x43bc('0x26')]['id'];}if(_[_0x43bc('0x32')](_0x251d07['body']['from'])){throw new db[(_0x43bc('0x2a'))][(_0x43bc('0x33'))](_0x43bc('0x34'));}if(_['isNil'](_0x251d07[_0x43bc('0x26')]['body'])||_0x251d07['body']['body']===''){throw new db[(_0x43bc('0x2a'))][(_0x43bc('0x33'))](_0x43bc('0x35'));}if(_['isNil'](_0x251d07[_0x43bc('0x26')][_0x43bc('0x36')])){throw new db['Sequelize'][(_0x43bc('0x33'))](_0x43bc('0x37')+_0x537189);}if(!_[_0x43bc('0x38')](_0x537189,_0x251d07[_0x43bc('0x26')]['mapKey'])){throw new db[(_0x43bc('0x2a'))][(_0x43bc('0x33'))](_0x43bc('0x39')+_0x537189);}_0x5b79e6[_0x251d07[_0x43bc('0x26')]['mapKey']]=_0x251d07[_0x43bc('0x26')][_0x43bc('0x3a')];})[_0x43bc('0x29')](function(){return db[_0x43bc('0x1d')]['find']({'where':{'id':_0x251d07['account']['id']},'include':[{'model':db[_0x43bc('0x3b')],'as':'List','include':[{'model':db[_0x43bc('0x28')],'as':_0x43bc('0x3c'),'where':_0x5b79e6,'limit':0x1,'order':[['updatedAt',_0x43bc('0x3d')]]}]},{'model':db[_0x43bc('0x3e')],'as':_0x43bc('0x3f')}]});})[_0x43bc('0x29')](function(_0x57cb85){if(_0x57cb85&&_0x57cb85[_0x43bc('0x40')]){var _0x5ae83a=_0x57cb85['get']({'plain':!![]});_0x35cc5b[_0x43bc('0x41')]=_0x5ae83a;_0x35cc5b[_0x43bc('0x42')]=_0x5ae83a[_0x43bc('0x3f')];_0x35cc5b[_0x43bc('0x43')]=_0x5ae83a[_0x43bc('0x40')];_0x35cc5b[_0x43bc('0x42')]=_['orderBy'](_0x35cc5b[_0x43bc('0x42')],['priority'],[_0x43bc('0x44')]);if(_0x35cc5b[_0x43bc('0x41')][_0x43bc('0x3f')]){delete _0x35cc5b[_0x43bc('0x41')][_0x43bc('0x3f')];}if(_0x35cc5b['account']['List']&&_0x35cc5b['account'][_0x43bc('0x40')][_0x43bc('0x3c')]['length']){return _0x35cc5b[_0x43bc('0x41')][_0x43bc('0x40')][_0x43bc('0x3c')][0x0];}var _0x210b23=_[_0x43bc('0xd')](_0x251d07[_0x43bc('0x26')],{'firstName':_0x251d07[_0x43bc('0x26')][_0x43bc('0x3a')],'ListId':_0x5ae83a['ListId']});_0x210b23[_0x251d07[_0x43bc('0x26')][_0x43bc('0x36')]]=_0x251d07['body'][_0x43bc('0x3a')];return db[_0x43bc('0x28')][_0x43bc('0x45')](_0x210b23,{'fields':_0x50233b,'raw':!![]});}})[_0x43bc('0x29')](function(_0x15cc63){_0x35cc5b[_0x43bc('0x46')]=_0x15cc63;if(_0x15cc63){var _0x4708e1={'ContactId':_0x15cc63['id'],'FaxAccountId':_0x251d07['account']['id'],'fax':_0x15cc63[_0x43bc('0x27')]};return db[_0x43bc('0x47')][_0x43bc('0x48')]({'where':{'ContactId':_0x15cc63['id'],'closed':![],'FaxAccountId':_0x251d07[_0x43bc('0x41')]['id']}})[_0x43bc('0x29')](function(_0x3e1965){if(_0x3e1965){return[_0x3e1965,![]];}return db[_0x43bc('0x47')]['create'](_0x4708e1)[_0x43bc('0x29')](function(_0x382519){return[_0x382519,!![]];});});}else{throw new Error(_0x43bc('0x49'));}})[_0x43bc('0x4a')](function(_0x3b461f,_0x2e8c25){var _0x118fa7=_0x3b461f[_0x43bc('0x4b')]({'plain':!![]});if(_0x2e8c25){if(_0x35cc5b[_0x43bc('0x41')][_0x43bc('0x4c')]){_0x35cc5b['applications'][_0x43bc('0x4d')]({'id':0x0,'priority':_0x35cc5b[_0x43bc('0x42')]['length']+0x1,'app':'close','appdata':_0x43bc('0x4e'),'interval':_0x43bc('0x4f')});}_0x118fa7['created']=!![];}else{_0x118fa7[_0x43bc('0x50')]=![];}_0x35cc5b['interaction']=_0x118fa7;return db['FaxMessage'][_0x43bc('0x45')]({'body':_0x251d07[_0x43bc('0x26')][_0x43bc('0x26')],'FaxAccountId':_0x251d07['account']['id'],'FaxInteractionId':_0x3b461f['id'],'direction':'in','ContactId':_0x3b461f[_0x43bc('0x51')],'AttachmentId':_0x251d07[_0x43bc('0x26')][_0x43bc('0x52')]});})[_0x43bc('0x29')](function(_0x1b5595){_0x35cc5b['message']=_0x1b5595;if(!_0x35cc5b['interaction'][_0x43bc('0x53')])return;return db['User'][_0x43bc('0x48')]({'attributes':['id',_0x43bc('0x54')],'where':{'id':_0x35cc5b[_0x43bc('0x55')]['UserId']}});})[_0x43bc('0x29')](function(_0x58e355){if(_0x58e355){_0x35cc5b[_0x43bc('0x42')]['unshift']({'id':0x0,'priority':0x0,'app':_0x43bc('0x56'),'appdata':_0x58e355['name']+','+(_0x35cc5b[_0x43bc('0x41')][_0x43bc('0x57')]||0xa),'interval':_0x43bc('0x4f')});}if(!_0x35cc5b[_0x43bc('0x55')]['queueId'])return;return db[_0x43bc('0x58')][_0x43bc('0x48')]({'attributes':['id',_0x43bc('0x54')],'where':{'id':_0x35cc5b['interaction'][_0x43bc('0x59')]}});})['then'](function(_0x5afc1b){if(_0x5afc1b){_0x35cc5b[_0x43bc('0x42')][_0x43bc('0x5a')]({'id':0x0,'priority':0x1,'app':_0x43bc('0x5b'),'appdata':_0x5afc1b['name']+','+(_0x35cc5b[_0x43bc('0x41')][_0x43bc('0x5c')]||0x12c),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0x43bc('0x5d'),'startRouting',_0x35cc5b);})[_0x43bc('0x29')](function(_0x1d1bff){_0x45b411(_0x1d1bff);})['catch'](function(_0x27df9f){_0x2a0fdb(_0x359ec1[_0x43bc('0x18')](0x1f4,_0x27df9f[_0x43bc('0x1a')]));});});};