Built motion from commit (unavailable).|2.5.31
[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 _0x0e51=['../../config/utils','rpc','../../config/environment','jayson/promise','client','redis','defaults','socket.io-emitter','request','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','catch','GetMailAccount','MailAccount','findAll','options','raw','where','limit','include','model','attributes','map','then','ShowMailAccount','find','NotifyMailAccount','mail','difference','keys','CmContact','rawAttributes','createdAt','updatedAt','ListId','body','isNil','from','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','account','CmList','List','DESC','MailApplication','Applications','MailServerOut','get','omit','list','orderBy','applications','priority','asc','push','length','close','autoclose','*,*,*,*','Contacts','List\x20not\x20found','contact','messageId','generate','subject','attach','format','YYYY-MM-DD\x20HH:mm:ss','MailMessage','inReplyTo','MailInteraction','create','update','interaction','created','attachments','originTo','bcc','ChatWebsiteId','originChannel','Attachment','Attachments','User','name','UserId','unshift','agent','Start','startRouting','util','bluebird','randomstring','../../mysqldb'];(function(_0x4baff8,_0x5b95e9){var _0x5e77a7=function(_0x6e9b57){while(--_0x6e9b57){_0x4baff8['push'](_0x4baff8['shift']());}};_0x5e77a7(++_0x5b95e9);}(_0x0e51,0xcc));var _0x10e5=function(_0x15d454,_0x2ca4a9){_0x15d454=_0x15d454-0x0;var _0x44d42e=_0x0e51[_0x15d454];return _0x44d42e;};'use strict';var _=require('lodash');var util=require(_0x10e5('0x0'));var moment=require('moment');var BPromise=require(_0x10e5('0x1'));var rs=require(_0x10e5('0x2'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0x10e5('0x3'))['db'];var utils=require(_0x10e5('0x4'));var logger=require('../../config/logger')(_0x10e5('0x5'));var config=require(_0x10e5('0x6'));var jayson=require(_0x10e5('0x7'));var client=jayson[_0x10e5('0x8')]['http']({'port':0x232c});config[_0x10e5('0x9')]=_[_0x10e5('0xa')](config[_0x10e5('0x9')],{'host':'localhost','port':0x18eb});var socket=require(_0x10e5('0xb'))(new Redis(config['redis']));require('./mailAccount.socket')['register'](socket);function respondWithRpcPromise(_0x13c023,_0x300b4b,_0x2e6385){return new BPromise(function(_0x3d7956,_0x323c5f){return client[_0x10e5('0xc')](_0x13c023,_0x2e6385)['then'](function(_0x2b5983){logger[_0x10e5('0xd')](_0x10e5('0xe'),_0x300b4b,_0x10e5('0xf'));logger[_0x10e5('0x10')](_0x10e5('0x11'),_0x300b4b,'request\x20sent',JSON[_0x10e5('0x12')](_0x2b5983));if(_0x2b5983[_0x10e5('0x13')]){if(_0x2b5983[_0x10e5('0x13')][_0x10e5('0x14')]===0x1f4){logger[_0x10e5('0x13')](_0x10e5('0xe'),_0x300b4b,_0x2b5983[_0x10e5('0x13')]['message']);return _0x323c5f(_0x2b5983[_0x10e5('0x13')][_0x10e5('0x15')]);}logger[_0x10e5('0x13')](_0x10e5('0xe'),_0x300b4b,_0x2b5983['error'][_0x10e5('0x15')]);return _0x3d7956(_0x2b5983[_0x10e5('0x13')]['message']);}else{logger[_0x10e5('0xd')](_0x10e5('0xe'),_0x300b4b,_0x10e5('0xf'));_0x3d7956(_0x2b5983['result'][_0x10e5('0x15')]);}})[_0x10e5('0x16')](function(_0x584c42){logger[_0x10e5('0x13')](_0x10e5('0xe'),_0x300b4b,_0x584c42);_0x323c5f(_0x584c42);});});}exports[_0x10e5('0x17')]=function(_0x4dd57b){var _0x14c9a6=this;return new Promise(function(_0x22afce,_0x4d3d8a){return db[_0x10e5('0x18')][_0x10e5('0x19')]({'raw':_0x4dd57b[_0x10e5('0x1a')]?_0x4dd57b['options'][_0x10e5('0x1b')]===undefined?!![]:![]:!![],'where':_0x4dd57b[_0x10e5('0x1a')]?_0x4dd57b[_0x10e5('0x1a')][_0x10e5('0x1c')]||null:null,'attributes':_0x4dd57b[_0x10e5('0x1a')]?_0x4dd57b['options']['attributes']||null:null,'limit':_0x4dd57b[_0x10e5('0x1a')]?_0x4dd57b[_0x10e5('0x1a')][_0x10e5('0x1d')]||null:null,'include':_0x4dd57b[_0x10e5('0x1a')]?_0x4dd57b[_0x10e5('0x1a')][_0x10e5('0x1e')]?_['map'](_0x4dd57b[_0x10e5('0x1a')][_0x10e5('0x1e')],function(_0xd54bcc){return{'model':db[_0xd54bcc[_0x10e5('0x1f')]],'as':_0xd54bcc['as'],'attributes':_0xd54bcc[_0x10e5('0x20')],'include':_0xd54bcc[_0x10e5('0x1e')]?_[_0x10e5('0x21')](_0xd54bcc['include'],function(_0x505f93){return{'model':db[_0x505f93['model']],'as':_0x505f93['as'],'attributes':_0x505f93['attributes'],'include':_0x505f93[_0x10e5('0x1e')]?_[_0x10e5('0x21')](_0x505f93[_0x10e5('0x1e')],function(_0x1083f7){return{'model':db[_0x1083f7[_0x10e5('0x1f')]],'as':_0x1083f7['as'],'attributes':_0x1083f7[_0x10e5('0x20')]};}):[]};}):[]};}):[]:[]})[_0x10e5('0x22')](function(_0x4363b3){logger[_0x10e5('0xd')](_0x10e5('0x17'),_0x4dd57b);logger[_0x10e5('0x10')]('GetMailAccount',_0x4dd57b,JSON[_0x10e5('0x12')](_0x4363b3));_0x22afce(_0x4363b3);})[_0x10e5('0x16')](function(_0x318b30){logger[_0x10e5('0x13')](_0x10e5('0x17'),_0x318b30[_0x10e5('0x15')],_0x4dd57b);_0x4d3d8a(_0x14c9a6['error'](0x1f4,_0x318b30['message']));});});};exports[_0x10e5('0x23')]=function(_0x2e6d07){var _0xe9b1c7=this;return new Promise(function(_0x40f490,_0x2e39c2){return db[_0x10e5('0x18')][_0x10e5('0x24')]({'raw':_0x2e6d07[_0x10e5('0x1a')]?_0x2e6d07['options'][_0x10e5('0x1b')]===undefined?!![]:![]:!![],'where':_0x2e6d07[_0x10e5('0x1a')]?_0x2e6d07['options'][_0x10e5('0x1c')]||null:null,'attributes':_0x2e6d07['options']?_0x2e6d07[_0x10e5('0x1a')][_0x10e5('0x20')]||null:null,'include':_0x2e6d07[_0x10e5('0x1a')]?_0x2e6d07[_0x10e5('0x1a')][_0x10e5('0x1e')]?_[_0x10e5('0x21')](_0x2e6d07[_0x10e5('0x1a')][_0x10e5('0x1e')],function(_0x1c0284){return{'model':db[_0x1c0284['model']],'as':_0x1c0284['as'],'attributes':_0x1c0284[_0x10e5('0x20')],'include':_0x1c0284[_0x10e5('0x1e')]?_['map'](_0x1c0284[_0x10e5('0x1e')],function(_0x23a1d1){return{'model':db[_0x23a1d1['model']],'as':_0x23a1d1['as'],'attributes':_0x23a1d1[_0x10e5('0x20')],'include':_0x23a1d1[_0x10e5('0x1e')]?_[_0x10e5('0x21')](_0x23a1d1[_0x10e5('0x1e')],function(_0x162f38){return{'model':db[_0x162f38[_0x10e5('0x1f')]],'as':_0x162f38['as'],'attributes':_0x162f38[_0x10e5('0x20')]};}):[]};}):[]};}):[]:[]})['then'](function(_0xf6b809){logger[_0x10e5('0xd')]('ShowMailAccount',_0x2e6d07);logger[_0x10e5('0x10')](_0x10e5('0x23'),_0x2e6d07,JSON[_0x10e5('0x12')](_0xf6b809));_0x40f490(_0xf6b809);})[_0x10e5('0x16')](function(_0x2bb582){logger['error'](_0x10e5('0x23'),_0x2bb582[_0x10e5('0x15')],_0x2e6d07);_0x2e39c2(_0xe9b1c7[_0x10e5('0x13')](0x1f4,_0x2bb582[_0x10e5('0x15')]));});});};exports[_0x10e5('0x25')]=function(_0x3f36d0){var _0x5a517d=this;return new Promise(function(_0x14a38e,_0x10d1a4){var _0x177d6f={'body':_0x3f36d0['body'],'channel':_0x10e5('0x26')};var _0x3a5577=_[_0x10e5('0x27')](_[_0x10e5('0x28')](db[_0x10e5('0x29')][_0x10e5('0x2a')]),[_0x10e5('0x2b'),_0x10e5('0x2c'),'CompanyId',_0x10e5('0x2d')]);var _0x4237fc=![];if(_0x3f36d0[_0x10e5('0x2e')]['id']){delete _0x3f36d0[_0x10e5('0x2e')]['id'];}if(_[_0x10e5('0x2f')](_0x3f36d0['body'][_0x10e5('0x30')])){return _0x10d1a4(_0x5a517d['error'](0x1f4,_0x10e5('0x31')));}if(_[_0x10e5('0x2f')](_0x3f36d0[_0x10e5('0x2e')]['body'])||_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x2e')]===''){return _0x10d1a4(_0x5a517d[_0x10e5('0x13')](0x1f4,_0x10e5('0x32')));}if(_['isNil'](_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x33')])){return _0x10d1a4(_0x5a517d[_0x10e5('0x13')](0x1f4,_0x10e5('0x34')+_0x3a5577));}if(!_[_0x10e5('0x35')](_0x3a5577,_0x3f36d0['body'][_0x10e5('0x33')])){return _0x10d1a4(_0x5a517d[_0x10e5('0x13')](0x1f4,_0x10e5('0x36')+_0x3a5577));}var _0x2e0bf2={};_0x2e0bf2[_0x3f36d0[_0x10e5('0x2e')]['mapKey']]=_0x3f36d0[_0x10e5('0x2e')]['from'];return db['MailAccount']['find']({'where':{'id':_0x3f36d0[_0x10e5('0x37')]['id']},'include':[{'model':db[_0x10e5('0x38')],'as':_0x10e5('0x39'),'include':[{'model':db[_0x10e5('0x29')],'as':'Contacts','where':_0x2e0bf2,'limit':0x1,'order':[['updatedAt',_0x10e5('0x3a')]]}]},{'model':db[_0x10e5('0x3b')],'as':_0x10e5('0x3c')},{'model':db[_0x10e5('0x3d')],'as':'Smtp'}]})[_0x10e5('0x22')](function(_0x6baf79){if(_0x6baf79&&_0x6baf79['List']){var _0x3269fc=_0x6baf79[_0x10e5('0x3e')]({'plain':!![]});_0x177d6f[_0x10e5('0x37')]=_[_0x10e5('0x3f')](_0x3269fc,[_0x10e5('0x3c'),_0x10e5('0x39')]);_0x177d6f['applications']=_0x3269fc[_0x10e5('0x3c')];_0x177d6f[_0x10e5('0x40')]=_0x3269fc[_0x10e5('0x39')];_0x177d6f['applications']=_[_0x10e5('0x41')](_0x177d6f[_0x10e5('0x42')],[_0x10e5('0x43')],[_0x10e5('0x44')]);if(_0x177d6f[_0x10e5('0x37')]&&_0x177d6f[_0x10e5('0x37')]['autoclose']){_0x177d6f[_0x10e5('0x42')][_0x10e5('0x45')]({'id':0x0,'priority':_0x177d6f[_0x10e5('0x42')][_0x10e5('0x46')]+0x1,'app':_0x10e5('0x47'),'appdata':_0x10e5('0x48'),'interval':_0x10e5('0x49')});}if(_0x177d6f[_0x10e5('0x37')][_0x10e5('0x3c')]){delete _0x177d6f['account'][_0x10e5('0x3c')];}if(_0x177d6f[_0x10e5('0x37')][_0x10e5('0x39')]){delete _0x177d6f[_0x10e5('0x37')][_0x10e5('0x39')];}if(_0x3269fc[_0x10e5('0x39')][_0x10e5('0x4a')][_0x10e5('0x46')]){return _0x3269fc[_0x10e5('0x39')][_0x10e5('0x4a')][0x0];}var _0x133f66=_[_0x10e5('0xa')](_0x3f36d0[_0x10e5('0x2e')],{'firstName':_0x3f36d0['body']['from'],'ListId':_0x3269fc['ListId']});_0x133f66[_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x33')]]=_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x30')];return db['CmContact']['create'](_0x133f66);}else{throw new Error(_0x10e5('0x4b'));}})[_0x10e5('0x22')](function(_0x28f171){_0x177d6f[_0x10e5('0x4c')]=_0x28f171;if(_0x28f171){if(!_0x3f36d0['body']['message'][_0x10e5('0x4d')]){_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')]['messageId']=rs[_0x10e5('0x4e')]()+'\x20'+_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x30')];}var _0x129d4b={'ContactId':_0x28f171['id'],'MailAccountId':_0x3f36d0['account']['id'],'inReplyTo':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')]['messageId'],'to':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x30')],'cc':_0x3f36d0['body'][_0x10e5('0x15')]['cc'],'subject':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x4f')],'attach':_0x3f36d0['body'][_0x10e5('0x15')][_0x10e5('0x50')],'lastMsgAt':moment()[_0x10e5('0x51')](_0x10e5('0x52')),'lastMsgDirection':'in'};return db['MailInteraction'][_0x10e5('0x24')]({'where':{'closed':![],'MailAccountId':_0x3f36d0['account']['id']},'include':[{'model':db[_0x10e5('0x53')],'as':'Messages','attributes':[_0x10e5('0x4d')],'where':{'messageId':_0x3f36d0['body']['message'][_0x10e5('0x54')]}}]})['then'](function(_0xba5165){if(_0xba5165){return[_0xba5165,![]];}return db[_0x10e5('0x55')][_0x10e5('0x56')](_0x129d4b)['then'](function(_0x58569d){return[_0x58569d,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})['spread'](function(_0x500108,_0x58ce01){if(_0x500108){_0x4237fc=_0x58ce01;if(!_0x58ce01){return _0x500108[_0x10e5('0x57')]({'inReplyTo':_0x3f36d0['body'][_0x10e5('0x15')][_0x10e5('0x4d')],'to':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x30')],'cc':_0x3f36d0[_0x10e5('0x2e')]['message']['cc'],'subject':_0x3f36d0['body'][_0x10e5('0x15')][_0x10e5('0x4f')],'attach':_0x500108['attach']?_0x500108[_0x10e5('0x50')]:_0x3f36d0[_0x10e5('0x2e')]['message'][_0x10e5('0x50')],'substatus':null,'substatusAt':moment()[_0x10e5('0x51')](_0x10e5('0x52')),'lastMsgAt':moment()[_0x10e5('0x51')](_0x10e5('0x52')),'lastMsgDirection':'in'});}else{return _0x500108;}}})[_0x10e5('0x22')](function(_0xf1f5ae){_0x177d6f[_0x10e5('0x58')]=_0xf1f5ae[_0x10e5('0x3e')]({'plain':!![]});_0x177d6f[_0x10e5('0x58')][_0x10e5('0x59')]=_0x4237fc;return db['MailMessage'][_0x10e5('0x56')]({'body':_0x3f36d0[_0x10e5('0x2e')]['body'],'MailAccountId':_0x3f36d0[_0x10e5('0x37')]['id'],'MailInteractionId':_0xf1f5ae['id'],'direction':'in','ContactId':_0x177d6f[_0x10e5('0x4c')]['id'],'messageId':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x4d')],'from':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x30')],'to':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')]['to'],'cc':_0x3f36d0['body']['message']['cc'],'subject':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x4f')],'attach':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x50')],'Attachments':_0x3f36d0[_0x10e5('0x2e')]['message'][_0x10e5('0x5a')],'originTo':_0x3f36d0[_0x10e5('0x2e')]['message'][_0x10e5('0x5b')],'originCc':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')]['originCc'],'bcc':_0x3f36d0['body'][_0x10e5('0x15')][_0x10e5('0x5c')],'ChatWebsiteId':_0x3f36d0[_0x10e5('0x2e')]['message'][_0x10e5('0x5d')]||null,'originChannel':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')][_0x10e5('0x5e')]||'Email','ChatOfflineMessageId':_0x3f36d0[_0x10e5('0x2e')][_0x10e5('0x15')]['ChatOfflineMessageId']||null},{'include':[{'model':db[_0x10e5('0x5f')],'as':_0x10e5('0x60')}]});})['then'](function(_0x26626c){_0x177d6f[_0x10e5('0x15')]=_0x26626c;if(_0x177d6f[_0x10e5('0x58')]['UserId']){return db[_0x10e5('0x61')]['find']({'attributes':['id',_0x10e5('0x62')],'where':{'id':_0x177d6f[_0x10e5('0x58')][_0x10e5('0x63')]}})[_0x10e5('0x22')](function(_0x438561){if(_0x438561){_0x177d6f[_0x10e5('0x42')][_0x10e5('0x64')]({'id':0x0,'priority':0x0,'app':_0x10e5('0x65'),'appdata':_0x438561[_0x10e5('0x62')]+','+(_0x177d6f[_0x10e5('0x37')]['waitForTheAssignedAgent']||0xa),'interval':_0x10e5('0x49')});}return respondWithRpcPromise(_0x10e5('0x66'),_0x10e5('0x67'),_0x177d6f);});}return respondWithRpcPromise(_0x10e5('0x66'),'startRouting',_0x177d6f);})[_0x10e5('0x22')](function(_0x2f14c4){_0x14a38e(_0x2f14c4);})[_0x10e5('0x16')](function(_0x14e2db){_0x10d1a4(_0x5a517d['error'](0x1f4,_0x14e2db[_0x10e5('0x15')]));});});};