Built motion from commit (unavailable).|2.5.14
[motion2.git] / server / api / mailAccount / mailAccount.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 _0x195e=['from','account','CmList','List','Contacts','MailApplication','Applications','MailServerOut','Smtp','get','omit','list','applications','priority','asc','autoclose','push','length','close','*,*,*,*','List\x20not\x20found','contact','messageId','generate','subject','attach','format','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found','YYYY-MM-DD\x20HH:mm:ss','interaction','created','create','attachments','originCc','bcc','UserId','User','name','unshift','Start','startRouting','lodash','util','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','client','redis','defaults','localhost','socket.io-emitter','request','then','MailAccount,\x20%s,\x20%s','request\x20sent','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','info','result','catch','GetMailAccount','MailAccount','findAll','options','where','attributes','limit','include','map','model','debug','find','ShowMailAccount','NotifyMailAccount','body','mail','difference','keys','CmContact','rawAttributes','createdAt','updatedAt','ListId','isNil','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'];(function(_0xcb86e,_0x27e972){var _0x256865=function(_0x8e44e2){while(--_0x8e44e2){_0xcb86e['push'](_0xcb86e['shift']());}};_0x256865(++_0x27e972);}(_0x195e,0xfb));var _0xe195=function(_0x1f6b09,_0x352d85){_0x1f6b09=_0x1f6b09-0x0;var _0x155d71=_0x195e[_0x1f6b09];return _0x155d71;};'use strict';var _=require(_0xe195('0x0'));var util=require(_0xe195('0x1'));var moment=require(_0xe195('0x2'));var BPromise=require(_0xe195('0x3'));var rs=require(_0xe195('0x4'));var fs=require('fs');var Redis=require(_0xe195('0x5'));var db=require(_0xe195('0x6'))['db'];var utils=require(_0xe195('0x7'));var logger=require(_0xe195('0x8'))(_0xe195('0x9'));var config=require(_0xe195('0xa'));var jayson=require(_0xe195('0xb'));var client=jayson[_0xe195('0xc')]['http']({'port':0x232c});config[_0xe195('0xd')]=_[_0xe195('0xe')](config[_0xe195('0xd')],{'host':_0xe195('0xf'),'port':0x18eb});var socket=require(_0xe195('0x10'))(new Redis(config['redis']));require('./mailAccount.socket')['register'](socket);function respondWithRpcPromise(_0x3bf721,_0x3a7b4e,_0x1d8102){return new BPromise(function(_0xb606ec,_0x5bd212){return client[_0xe195('0x11')](_0x3bf721,_0x1d8102)[_0xe195('0x12')](function(_0x1bde59){logger['info'](_0xe195('0x13'),_0x3a7b4e,_0xe195('0x14'));logger['debug'](_0xe195('0x15'),_0x3a7b4e,_0xe195('0x14'),JSON[_0xe195('0x16')](_0x1bde59));if(_0x1bde59[_0xe195('0x17')]){if(_0x1bde59['error'][_0xe195('0x18')]===0x1f4){logger[_0xe195('0x17')]('MailAccount,\x20%s,\x20%s',_0x3a7b4e,_0x1bde59['error'][_0xe195('0x19')]);return _0x5bd212(_0x1bde59['error'][_0xe195('0x19')]);}logger['error']('MailAccount,\x20%s,\x20%s',_0x3a7b4e,_0x1bde59[_0xe195('0x17')][_0xe195('0x19')]);return _0xb606ec(_0x1bde59[_0xe195('0x17')][_0xe195('0x19')]);}else{logger[_0xe195('0x1a')](_0xe195('0x13'),_0x3a7b4e,_0xe195('0x14'));_0xb606ec(_0x1bde59[_0xe195('0x1b')][_0xe195('0x19')]);}})[_0xe195('0x1c')](function(_0x4db087){logger[_0xe195('0x17')](_0xe195('0x13'),_0x3a7b4e,_0x4db087);_0x5bd212(_0x4db087);});});}exports[_0xe195('0x1d')]=function(_0x2cb300){var _0x3f4326=this;return new Promise(function(_0x14b686,_0x496722){return db[_0xe195('0x1e')][_0xe195('0x1f')]({'raw':_0x2cb300[_0xe195('0x20')]?_0x2cb300[_0xe195('0x20')]['raw']===undefined?!![]:![]:!![],'where':_0x2cb300['options']?_0x2cb300['options'][_0xe195('0x21')]||null:null,'attributes':_0x2cb300[_0xe195('0x20')]?_0x2cb300[_0xe195('0x20')][_0xe195('0x22')]||null:null,'limit':_0x2cb300[_0xe195('0x20')]?_0x2cb300[_0xe195('0x20')][_0xe195('0x23')]||null:null,'include':_0x2cb300[_0xe195('0x20')]?_0x2cb300[_0xe195('0x20')][_0xe195('0x24')]?_[_0xe195('0x25')](_0x2cb300[_0xe195('0x20')][_0xe195('0x24')],function(_0x58ea75){return{'model':db[_0x58ea75[_0xe195('0x26')]],'as':_0x58ea75['as'],'attributes':_0x58ea75[_0xe195('0x22')],'include':_0x58ea75[_0xe195('0x24')]?_[_0xe195('0x25')](_0x58ea75[_0xe195('0x24')],function(_0x38be17){return{'model':db[_0x38be17[_0xe195('0x26')]],'as':_0x38be17['as'],'attributes':_0x38be17[_0xe195('0x22')],'include':_0x38be17[_0xe195('0x24')]?_[_0xe195('0x25')](_0x38be17['include'],function(_0x33a08d){return{'model':db[_0x33a08d[_0xe195('0x26')]],'as':_0x33a08d['as'],'attributes':_0x33a08d[_0xe195('0x22')]};}):[]};}):[]};}):[]:[]})[_0xe195('0x12')](function(_0x283516){logger['info']('GetMailAccount',_0x2cb300);logger[_0xe195('0x27')](_0xe195('0x1d'),_0x2cb300,JSON[_0xe195('0x16')](_0x283516));_0x14b686(_0x283516);})[_0xe195('0x1c')](function(_0x2b69a3){logger[_0xe195('0x17')]('GetMailAccount',_0x2b69a3[_0xe195('0x19')],_0x2cb300);_0x496722(_0x3f4326[_0xe195('0x17')](0x1f4,_0x2b69a3[_0xe195('0x19')]));});});};exports['ShowMailAccount']=function(_0x2b399e){var _0x5a9f50=this;return new Promise(function(_0xca299f,_0x7ec255){return db['MailAccount'][_0xe195('0x28')]({'raw':_0x2b399e[_0xe195('0x20')]?_0x2b399e[_0xe195('0x20')]['raw']===undefined?!![]:![]:!![],'where':_0x2b399e[_0xe195('0x20')]?_0x2b399e[_0xe195('0x20')][_0xe195('0x21')]||null:null,'attributes':_0x2b399e[_0xe195('0x20')]?_0x2b399e[_0xe195('0x20')][_0xe195('0x22')]||null:null,'include':_0x2b399e['options']?_0x2b399e[_0xe195('0x20')][_0xe195('0x24')]?_[_0xe195('0x25')](_0x2b399e[_0xe195('0x20')][_0xe195('0x24')],function(_0x2ce341){return{'model':db[_0x2ce341[_0xe195('0x26')]],'as':_0x2ce341['as'],'attributes':_0x2ce341['attributes'],'include':_0x2ce341[_0xe195('0x24')]?_[_0xe195('0x25')](_0x2ce341['include'],function(_0x2a3ecd){return{'model':db[_0x2a3ecd[_0xe195('0x26')]],'as':_0x2a3ecd['as'],'attributes':_0x2a3ecd['attributes'],'include':_0x2a3ecd[_0xe195('0x24')]?_['map'](_0x2a3ecd[_0xe195('0x24')],function(_0x3fc4c6){return{'model':db[_0x3fc4c6[_0xe195('0x26')]],'as':_0x3fc4c6['as'],'attributes':_0x3fc4c6[_0xe195('0x22')]};}):[]};}):[]};}):[]:[]})[_0xe195('0x12')](function(_0x423023){logger['info'](_0xe195('0x29'),_0x2b399e);logger[_0xe195('0x27')](_0xe195('0x29'),_0x2b399e,JSON['stringify'](_0x423023));_0xca299f(_0x423023);})[_0xe195('0x1c')](function(_0x4dd43a){logger['error'](_0xe195('0x29'),_0x4dd43a[_0xe195('0x19')],_0x2b399e);_0x7ec255(_0x5a9f50['error'](0x1f4,_0x4dd43a[_0xe195('0x19')]));});});};exports[_0xe195('0x2a')]=function(_0x28ac42){var _0x57f25f=this;return new Promise(function(_0x2b6bad,_0x78f2cc){var _0x4028b6={'body':_0x28ac42[_0xe195('0x2b')],'channel':_0xe195('0x2c')};var _0x206dfb=_[_0xe195('0x2d')](_[_0xe195('0x2e')](db[_0xe195('0x2f')][_0xe195('0x30')]),[_0xe195('0x31'),_0xe195('0x32'),'CompanyId',_0xe195('0x33')]);var _0x2e732a=![];if(_0x28ac42[_0xe195('0x2b')]['id']){delete _0x28ac42[_0xe195('0x2b')]['id'];}if(_[_0xe195('0x34')](_0x28ac42['body']['from'])){return _0x78f2cc(_0x57f25f['error'](0x1f4,'from\x20is\x20mandatory'));}if(_[_0xe195('0x34')](_0x28ac42['body'][_0xe195('0x2b')])||_0x28ac42['body']['body']===''){return _0x78f2cc(_0x57f25f[_0xe195('0x17')](0x1f4,_0xe195('0x35')));}if(_[_0xe195('0x34')](_0x28ac42[_0xe195('0x2b')][_0xe195('0x36')])){return _0x78f2cc(_0x57f25f[_0xe195('0x17')](0x1f4,_0xe195('0x37')+_0x206dfb));}if(!_[_0xe195('0x38')](_0x206dfb,_0x28ac42['body']['mapKey'])){return _0x78f2cc(_0x57f25f[_0xe195('0x17')](0x1f4,_0xe195('0x39')+_0x206dfb));}var _0x4873be={};_0x4873be[_0x28ac42[_0xe195('0x2b')][_0xe195('0x36')]]=_0x28ac42[_0xe195('0x2b')][_0xe195('0x3a')];return db[_0xe195('0x1e')][_0xe195('0x28')]({'where':{'id':_0x28ac42[_0xe195('0x3b')]['id']},'include':[{'model':db[_0xe195('0x3c')],'as':_0xe195('0x3d'),'include':[{'model':db[_0xe195('0x2f')],'as':_0xe195('0x3e'),'where':_0x4873be,'limit':0x1,'order':[[_0xe195('0x32'),'DESC']]}]},{'model':db[_0xe195('0x3f')],'as':_0xe195('0x40')},{'model':db[_0xe195('0x41')],'as':_0xe195('0x42')}]})[_0xe195('0x12')](function(_0x289782){if(_0x289782&&_0x289782[_0xe195('0x3d')]){var _0x30fe4b=_0x289782[_0xe195('0x43')]({'plain':!![]});_0x4028b6[_0xe195('0x3b')]=_[_0xe195('0x44')](_0x30fe4b,['Applications',_0xe195('0x3d')]);_0x4028b6['applications']=_0x30fe4b[_0xe195('0x40')];_0x4028b6[_0xe195('0x45')]=_0x30fe4b[_0xe195('0x3d')];_0x4028b6['applications']=_['orderBy'](_0x4028b6[_0xe195('0x46')],[_0xe195('0x47')],[_0xe195('0x48')]);if(_0x4028b6[_0xe195('0x3b')]&&_0x4028b6[_0xe195('0x3b')][_0xe195('0x49')]){_0x4028b6[_0xe195('0x46')][_0xe195('0x4a')]({'id':0x0,'priority':_0x4028b6[_0xe195('0x46')][_0xe195('0x4b')]+0x1,'app':_0xe195('0x4c'),'appdata':_0xe195('0x49'),'interval':_0xe195('0x4d')});}if(_0x4028b6[_0xe195('0x3b')][_0xe195('0x40')]){delete _0x4028b6[_0xe195('0x3b')][_0xe195('0x40')];}if(_0x4028b6[_0xe195('0x3b')][_0xe195('0x3d')]){delete _0x4028b6['account'][_0xe195('0x3d')];}if(_0x30fe4b[_0xe195('0x3d')][_0xe195('0x3e')][_0xe195('0x4b')]){return _0x30fe4b[_0xe195('0x3d')][_0xe195('0x3e')][0x0];}var _0x57ad8c=_['defaults'](_0x28ac42[_0xe195('0x2b')],{'firstName':_0x28ac42[_0xe195('0x2b')][_0xe195('0x3a')],'ListId':_0x30fe4b['ListId']});_0x57ad8c[_0x28ac42[_0xe195('0x2b')][_0xe195('0x36')]]=_0x28ac42['body'][_0xe195('0x3a')];return db[_0xe195('0x2f')]['create'](_0x57ad8c);}else{throw new Error(_0xe195('0x4e'));}})['then'](function(_0x368623){_0x4028b6[_0xe195('0x4f')]=_0x368623;if(_0x368623){if(!_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x50')]){_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')]['messageId']=rs[_0xe195('0x51')]()+'\x20'+_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x3a')];}var _0x2ff60c={'ContactId':_0x368623['id'],'MailAccountId':_0x28ac42[_0xe195('0x3b')]['id'],'inReplyTo':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x50')],'to':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x3a')],'cc':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')]['cc'],'subject':_0x28ac42[_0xe195('0x2b')]['message'][_0xe195('0x52')],'attach':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x53')],'lastMsgAt':moment()[_0xe195('0x54')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in','lastMsgBody':_0x28ac42[_0xe195('0x2b')][_0xe195('0x2b')]};return db[_0xe195('0x55')][_0xe195('0x28')]({'where':{'closed':![],'MailAccountId':_0x28ac42[_0xe195('0x3b')]['id']},'include':[{'model':db[_0xe195('0x56')],'as':_0xe195('0x57'),'attributes':[_0xe195('0x50')],'where':{'messageId':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x58')]}}]})[_0xe195('0x12')](function(_0xa90491){if(_0xa90491){return[_0xa90491,![]];}return db[_0xe195('0x55')]['create'](_0x2ff60c)[_0xe195('0x12')](function(_0x57aa4b){return[_0x57aa4b,!![]];});});}else{throw new Error(_0xe195('0x59'));}})['spread'](function(_0x2f60d9,_0x28f0b2){if(_0x2f60d9){_0x2e732a=_0x28f0b2;if(!_0x28f0b2){return _0x2f60d9['update']({'inReplyTo':_0x28ac42['body'][_0xe195('0x19')][_0xe195('0x50')],'to':_0x28ac42['body'][_0xe195('0x19')][_0xe195('0x3a')],'cc':_0x28ac42['body'][_0xe195('0x19')]['cc'],'subject':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x52')],'attach':_0x2f60d9['attach']?_0x2f60d9['attach']:_0x28ac42['body'][_0xe195('0x19')]['attach'],'substatus':null,'substatusAt':moment()['format'](_0xe195('0x5a')),'lastMsgAt':moment()[_0xe195('0x54')](_0xe195('0x5a')),'lastMsgDirection':'in','lastMsgBody':_0x28ac42['body']['body']});}else{return _0x2f60d9;}}})[_0xe195('0x12')](function(_0x46f1a2){_0x4028b6[_0xe195('0x5b')]=_0x46f1a2['get']({'plain':!![]});_0x4028b6[_0xe195('0x5b')][_0xe195('0x5c')]=_0x2e732a;return db[_0xe195('0x56')][_0xe195('0x5d')]({'body':_0x28ac42[_0xe195('0x2b')][_0xe195('0x2b')],'MailAccountId':_0x28ac42[_0xe195('0x3b')]['id'],'MailInteractionId':_0x46f1a2['id'],'direction':'in','ContactId':_0x4028b6[_0xe195('0x4f')]['id'],'messageId':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')]['messageId'],'from':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x3a')],'to':_0x28ac42[_0xe195('0x2b')]['message']['to'],'cc':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')]['cc'],'subject':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x52')],'attach':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')]['attach'],'Attachments':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x5e')],'originTo':_0x28ac42[_0xe195('0x2b')]['message']['originTo'],'originCc':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x5f')],'bcc':_0x28ac42[_0xe195('0x2b')][_0xe195('0x19')][_0xe195('0x60')]},{'include':[{'model':db['Attachment'],'as':'Attachments'}]});})[_0xe195('0x12')](function(_0x34a330){_0x4028b6[_0xe195('0x19')]=_0x34a330;if(_0x4028b6[_0xe195('0x5b')][_0xe195('0x61')]){return db[_0xe195('0x62')][_0xe195('0x28')]({'attributes':['id',_0xe195('0x63')],'where':{'id':_0x4028b6['interaction'][_0xe195('0x61')]}})[_0xe195('0x12')](function(_0x1bf3fa){if(_0x1bf3fa){_0x4028b6[_0xe195('0x46')][_0xe195('0x64')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x1bf3fa[_0xe195('0x63')]+','+(_0x4028b6[_0xe195('0x3b')]['waitForTheAssignedAgent']||0xa),'interval':_0xe195('0x4d')});}return respondWithRpcPromise(_0xe195('0x65'),'startRouting',_0x4028b6);});}return respondWithRpcPromise(_0xe195('0x65'),_0xe195('0x66'),_0x4028b6);})['then'](function(_0x458f8b){_0x2b6bad(_0x458f8b);})[_0xe195('0x1c')](function(_0x15d24b){_0x78f2cc(_0x57f25f[_0xe195('0x17')](0x1f4,_0x15d24b[_0xe195('0x19')]));});});};