10673d0ea191a3dc11af874b4634bd1f5761140c
[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 _0x4317=['name','agent','waitForTheAssignedAgent','queueId','MailQueue','unshift','queue','waitForTheAssignedQueue','startRouting','moment','bluebird','randomstring','../../mysqldb','../../config/logger','rpc','../../config/environment','jayson/promise','client','defaults','redis','localhost','socket.io-emitter','./mailAccount.socket','register','request','then','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','message','result','GetMailAccount','findAll','options','raw','attributes','limit','include','map','model','catch','find','where','smtp','setDataValue','ShowMailAccount','NotifyMailAccount','body','mail','keys','rawAttributes','createdAt','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','mapKey','CmList','List','CmContact','Contacts','updatedAt','Applications','MailServerOut','Smtp','CloudProvider','get','account','omit','applications','list','orderBy','priority','asc','autoclose','push','length','*,*,*,*','create','List\x20not\x20found','generate','messageId','subject','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found','spread','update','attach','interaction','created','contact','attachments','originTo','originCc','bcc','ChatWebsiteId','originChannel','Email','ChatOfflineMessageId','Attachment','Attachments','UserId','User'];(function(_0x2a19c1,_0x1a7182){var _0x1e15bf=function(_0x302dbb){while(--_0x302dbb){_0x2a19c1['push'](_0x2a19c1['shift']());}};_0x1e15bf(++_0x1a7182);}(_0x4317,0x7c));var _0x7431=function(_0x502c7d,_0x2f1285){_0x502c7d=_0x502c7d-0x0;var _0x30b7ed=_0x4317[_0x502c7d];return _0x30b7ed;};'use strict';var _=require('lodash');var moment=require(_0x7431('0x0'));var BPromise=require(_0x7431('0x1'));var rs=require(_0x7431('0x2'));var Redis=require('ioredis');var db=require(_0x7431('0x3'))['db'];var logger=require(_0x7431('0x4'))(_0x7431('0x5'));var config=require(_0x7431('0x6'));var jayson=require(_0x7431('0x7'));var client=jayson[_0x7431('0x8')]['http']({'port':0x232c});config['redis']=_[_0x7431('0x9')](config[_0x7431('0xa')],{'host':_0x7431('0xb'),'port':0x18eb});var socket=require(_0x7431('0xc'))(new Redis(config[_0x7431('0xa')]));require(_0x7431('0xd'))[_0x7431('0xe')](socket);function respondWithRpcPromise(_0x2294b8,_0xccbaee,_0xcf3ea4){return new BPromise(function(_0x9a8b79,_0x13bf7b){return client[_0x7431('0xf')](_0x2294b8,_0xcf3ea4)[_0x7431('0x10')](function(_0x216897){logger[_0x7431('0x11')](_0x7431('0x12'),_0xccbaee,_0x7431('0x13'));logger[_0x7431('0x14')](_0x7431('0x15'),_0xccbaee,_0x7431('0x13'),JSON[_0x7431('0x16')](_0x216897));if(_0x216897[_0x7431('0x17')]){if(_0x216897[_0x7431('0x17')]['code']===0x1f4){logger[_0x7431('0x17')](_0x7431('0x12'),_0xccbaee,_0x216897[_0x7431('0x17')]['message']);return _0x13bf7b(_0x216897[_0x7431('0x17')][_0x7431('0x18')]);}logger[_0x7431('0x17')]('MailAccount,\x20%s,\x20%s',_0xccbaee,_0x216897[_0x7431('0x17')]['message']);return _0x9a8b79(_0x216897['error']['message']);}else{logger[_0x7431('0x11')](_0x7431('0x12'),_0xccbaee,_0x7431('0x13'));_0x9a8b79(_0x216897[_0x7431('0x19')]['message']);}})['catch'](function(_0x167238){logger[_0x7431('0x17')](_0x7431('0x12'),_0xccbaee,_0x167238);_0x13bf7b(_0x167238);});});}exports[_0x7431('0x1a')]=function(_0x1e7fdf){var _0x409c6c=this;return new Promise(function(_0x488e1a,_0x563c0a){return db['MailAccount'][_0x7431('0x1b')]({'raw':_0x1e7fdf[_0x7431('0x1c')]?_0x1e7fdf['options'][_0x7431('0x1d')]===undefined?!![]:![]:!![],'where':_0x1e7fdf[_0x7431('0x1c')]?_0x1e7fdf[_0x7431('0x1c')]['where']||null:null,'attributes':_0x1e7fdf[_0x7431('0x1c')]?_0x1e7fdf[_0x7431('0x1c')][_0x7431('0x1e')]||null:null,'limit':_0x1e7fdf[_0x7431('0x1c')]?_0x1e7fdf['options'][_0x7431('0x1f')]||null:null,'include':_0x1e7fdf[_0x7431('0x1c')]?_0x1e7fdf[_0x7431('0x1c')][_0x7431('0x20')]?_[_0x7431('0x21')](_0x1e7fdf[_0x7431('0x1c')][_0x7431('0x20')],function(_0x183e53){return{'model':db[_0x183e53[_0x7431('0x22')]],'as':_0x183e53['as'],'attributes':_0x183e53[_0x7431('0x1e')],'include':_0x183e53[_0x7431('0x20')]?_[_0x7431('0x21')](_0x183e53['include'],function(_0x43116e){return{'model':db[_0x43116e['model']],'as':_0x43116e['as'],'attributes':_0x43116e['attributes'],'include':_0x43116e[_0x7431('0x20')]?_[_0x7431('0x21')](_0x43116e['include'],function(_0x3052fc){return{'model':db[_0x3052fc[_0x7431('0x22')]],'as':_0x3052fc['as'],'attributes':_0x3052fc[_0x7431('0x1e')]};}):[]};}):[]};}):[]:[]})[_0x7431('0x10')](function(_0x30e63f){logger[_0x7431('0x11')](_0x7431('0x1a'),_0x1e7fdf);logger[_0x7431('0x14')](_0x7431('0x1a'),_0x1e7fdf,JSON[_0x7431('0x16')](_0x30e63f));_0x488e1a(_0x30e63f);})[_0x7431('0x23')](function(_0xa63228){logger['error'](_0x7431('0x1a'),_0xa63228[_0x7431('0x18')],_0x1e7fdf);_0x563c0a(_0x409c6c[_0x7431('0x17')](0x1f4,_0xa63228[_0x7431('0x18')]));});});};exports['ShowMailAccount']=function(_0x2e3d64){var _0x27d324=this;return new Promise(function(_0x3e3fe6,_0x516dc1){return db['MailAccount'][_0x7431('0x24')]({'raw':_0x2e3d64['options']?_0x2e3d64['options'][_0x7431('0x1d')]===undefined?!![]:![]:!![],'where':_0x2e3d64['options']?_0x2e3d64[_0x7431('0x1c')][_0x7431('0x25')]||null:null,'attributes':_0x2e3d64['options']?_0x2e3d64[_0x7431('0x1c')][_0x7431('0x1e')]||null:null,'include':_0x2e3d64['options']?_0x2e3d64[_0x7431('0x1c')][_0x7431('0x20')]?_[_0x7431('0x21')](_0x2e3d64[_0x7431('0x1c')][_0x7431('0x20')],function(_0x4d2be3){return{'model':db[_0x4d2be3[_0x7431('0x22')]],'as':_0x4d2be3['as'],'attributes':_0x4d2be3['attributes'],'include':_0x4d2be3[_0x7431('0x20')]?_[_0x7431('0x21')](_0x4d2be3[_0x7431('0x20')],function(_0x524de5){return{'model':db[_0x524de5[_0x7431('0x22')]],'as':_0x524de5['as'],'attributes':_0x524de5[_0x7431('0x1e')],'include':_0x524de5[_0x7431('0x20')]?_[_0x7431('0x21')](_0x524de5[_0x7431('0x20')],function(_0x316e0c){return{'model':db[_0x316e0c[_0x7431('0x22')]],'as':_0x316e0c['as'],'attributes':_0x316e0c[_0x7431('0x1e')]};}):[]};}):[]};}):[]:[]})[_0x7431('0x10')](function(_0xfde13f){if(_0x2e3d64[_0x7431('0x1c')][_0x7431('0x26')]===!![])_0xfde13f[_0x7431('0x27')]('smtpOptions',_0xfde13f['getSmtpOptions']());logger[_0x7431('0x11')](_0x7431('0x28'),_0x2e3d64);logger['debug']('ShowMailAccount',_0x2e3d64,JSON[_0x7431('0x16')](_0xfde13f));_0x3e3fe6(_0xfde13f);})[_0x7431('0x23')](function(_0x273bb8){logger[_0x7431('0x17')](_0x7431('0x28'),_0x273bb8[_0x7431('0x18')],_0x2e3d64);_0x516dc1(_0x27d324['error'](0x1f4,_0x273bb8['message']));});});};exports[_0x7431('0x29')]=function(_0x4330e5){var _0x13d903=this;return new Promise(function(_0x53dba5,_0x4fe1d7){var _0x178fc6={'body':_0x4330e5[_0x7431('0x2a')],'channel':_0x7431('0x2b')};var _0x12feb1=_['difference'](_[_0x7431('0x2c')](db['CmContact'][_0x7431('0x2d')]),[_0x7431('0x2e'),'updatedAt',_0x7431('0x2f'),_0x7431('0x30')]);var _0x32589c=![];if(_0x4330e5['body']['id']){delete _0x4330e5[_0x7431('0x2a')]['id'];}if(_[_0x7431('0x31')](_0x4330e5['body'][_0x7431('0x32')])){return _0x4fe1d7(_0x13d903[_0x7431('0x17')](0x1f4,_0x7431('0x33')));}if(_[_0x7431('0x31')](_0x4330e5['body'][_0x7431('0x2a')])||_0x4330e5['body']['body']===''){return _0x4fe1d7(_0x13d903[_0x7431('0x17')](0x1f4,_0x7431('0x34')));}if(_[_0x7431('0x31')](_0x4330e5['body']['mapKey'])){return _0x4fe1d7(_0x13d903[_0x7431('0x17')](0x1f4,_0x7431('0x35')+_0x12feb1));}if(!_['includes'](_0x12feb1,_0x4330e5[_0x7431('0x2a')][_0x7431('0x36')])){return _0x4fe1d7(_0x13d903['error'](0x1f4,'mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x12feb1));}var _0x119608={};_0x119608[_0x4330e5['body'][_0x7431('0x36')]]=_0x4330e5[_0x7431('0x2a')][_0x7431('0x32')];return db['MailAccount'][_0x7431('0x24')]({'where':{'id':_0x4330e5['account']['id']},'include':[{'model':db[_0x7431('0x37')],'as':_0x7431('0x38'),'include':[{'model':db[_0x7431('0x39')],'as':_0x7431('0x3a'),'where':_0x119608,'limit':0x1,'order':[[_0x7431('0x3b'),'DESC']]}]},{'model':db['MailApplication'],'as':_0x7431('0x3c')},{'model':db[_0x7431('0x3d')],'as':_0x7431('0x3e'),'include':[{'model':db[_0x7431('0x3f')],'as':_0x7431('0x3f')}]}]})[_0x7431('0x10')](function(_0x6f7b4){if(_0x6f7b4&&_0x6f7b4['List']){var _0x43ebe4=_0x6f7b4[_0x7431('0x40')]({'plain':!![]});_0x178fc6[_0x7431('0x41')]=_[_0x7431('0x42')](_0x43ebe4,[_0x7431('0x3c'),'List']);_0x178fc6[_0x7431('0x41')]['smtpOptions']=_0x6f7b4['getSmtpOptions']();_0x178fc6[_0x7431('0x43')]=_0x43ebe4[_0x7431('0x3c')];_0x178fc6[_0x7431('0x44')]=_0x43ebe4[_0x7431('0x38')];_0x178fc6[_0x7431('0x43')]=_[_0x7431('0x45')](_0x178fc6['applications'],[_0x7431('0x46')],[_0x7431('0x47')]);if(_0x178fc6['account']&&_0x178fc6[_0x7431('0x41')][_0x7431('0x48')]){_0x178fc6[_0x7431('0x43')][_0x7431('0x49')]({'id':0x0,'priority':_0x178fc6['applications'][_0x7431('0x4a')]+0x1,'app':'close','appdata':'autoclose','interval':_0x7431('0x4b')});}if(_0x178fc6[_0x7431('0x41')][_0x7431('0x3c')]){delete _0x178fc6[_0x7431('0x41')][_0x7431('0x3c')];}if(_0x178fc6[_0x7431('0x41')][_0x7431('0x38')]){delete _0x178fc6['account'][_0x7431('0x38')];}if(_0x43ebe4[_0x7431('0x38')]['Contacts'][_0x7431('0x4a')]){return _0x43ebe4[_0x7431('0x38')]['Contacts'][0x0];}var _0x840001=_[_0x7431('0x9')](_0x4330e5[_0x7431('0x2a')],{'firstName':_0x4330e5['body']['from'],'ListId':_0x43ebe4[_0x7431('0x30')]});_0x840001[_0x4330e5[_0x7431('0x2a')][_0x7431('0x36')]]=_0x4330e5['body'][_0x7431('0x32')];return db[_0x7431('0x39')][_0x7431('0x4c')](_0x840001);}else{throw new Error(_0x7431('0x4d'));}})[_0x7431('0x10')](function(_0x39e2aa){_0x178fc6['contact']=_0x39e2aa;if(_0x39e2aa){if(!_0x4330e5['body'][_0x7431('0x18')]['messageId']){_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')]['messageId']=rs[_0x7431('0x4e')]()+'\x20'+_0x4330e5[_0x7431('0x2a')]['message']['from'];}var _0x12cb28={'ContactId':_0x39e2aa['id'],'MailAccountId':_0x4330e5[_0x7431('0x41')]['id'],'inReplyTo':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x4f')],'to':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x32')],'cc':_0x4330e5['body']['message']['cc'],'subject':_0x4330e5['body']['message'][_0x7431('0x50')],'attach':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')]['attach'],'lastMsgAt':moment()[_0x7431('0x51')](_0x7431('0x52')),'lastMsgDirection':'in'};return db[_0x7431('0x53')][_0x7431('0x24')]({'where':{'closed':![],'MailAccountId':_0x4330e5[_0x7431('0x41')]['id']},'include':[{'model':db[_0x7431('0x54')],'as':_0x7431('0x55'),'attributes':[_0x7431('0x4f')],'where':{'messageId':_0x4330e5['body'][_0x7431('0x18')][_0x7431('0x56')]}}]})['then'](function(_0x5db955){if(_0x5db955){return[_0x5db955,![]];}return db['MailInteraction'][_0x7431('0x4c')](_0x12cb28)[_0x7431('0x10')](function(_0x52b2e8){return[_0x52b2e8,!![]];});});}else{throw new Error(_0x7431('0x57'));}})[_0x7431('0x58')](function(_0x3d26be,_0x5109ee){if(_0x3d26be){_0x32589c=_0x5109ee;if(!_0x5109ee){return _0x3d26be[_0x7431('0x59')]({'inReplyTo':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x4f')],'to':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')]['from'],'cc':_0x4330e5[_0x7431('0x2a')]['message']['cc'],'subject':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x50')],'attach':_0x3d26be[_0x7431('0x5a')]?_0x3d26be['attach']:_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x5a')],'substatus':null,'substatusAt':moment()[_0x7431('0x51')](_0x7431('0x52')),'lastMsgAt':moment()[_0x7431('0x51')](_0x7431('0x52')),'lastMsgDirection':'in'});}else{return _0x3d26be;}}})[_0x7431('0x10')](function(_0x2f24f1){_0x178fc6[_0x7431('0x5b')]=_0x2f24f1[_0x7431('0x40')]({'plain':!![]});_0x178fc6[_0x7431('0x5b')][_0x7431('0x5c')]=_0x32589c;return db[_0x7431('0x54')][_0x7431('0x4c')]({'body':_0x4330e5[_0x7431('0x2a')][_0x7431('0x2a')],'MailAccountId':_0x4330e5[_0x7431('0x41')]['id'],'MailInteractionId':_0x2f24f1['id'],'direction':'in','ContactId':_0x178fc6[_0x7431('0x5d')]['id'],'messageId':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x4f')],'from':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x32')],'to':_0x4330e5['body'][_0x7431('0x18')]['to'],'cc':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')]['cc'],'subject':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x50')],'attach':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x5a')],'Attachments':_0x4330e5[_0x7431('0x2a')]['message'][_0x7431('0x5e')],'originTo':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x5f')],'originCc':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x60')],'bcc':_0x4330e5['body'][_0x7431('0x18')][_0x7431('0x61')],'ChatWebsiteId':_0x4330e5[_0x7431('0x2a')][_0x7431('0x18')][_0x7431('0x62')]||null,'originChannel':_0x4330e5['body'][_0x7431('0x18')][_0x7431('0x63')]||_0x7431('0x64'),'ChatOfflineMessageId':_0x4330e5[_0x7431('0x2a')]['message'][_0x7431('0x65')]||null},{'include':[{'model':db[_0x7431('0x66')],'as':_0x7431('0x67')}]});})['then'](function(_0x25695b){_0x178fc6[_0x7431('0x18')]=_0x25695b;if(!_0x178fc6[_0x7431('0x5b')][_0x7431('0x68')]){return;}return db[_0x7431('0x69')][_0x7431('0x24')]({'attributes':['id',_0x7431('0x6a')],'where':{'id':_0x178fc6['interaction'][_0x7431('0x68')]}});})[_0x7431('0x10')](function(_0x3071bb){if(_0x3071bb){_0x178fc6[_0x7431('0x43')]['unshift']({'id':0x0,'priority':0x0,'app':_0x7431('0x6b'),'appdata':_0x3071bb[_0x7431('0x6a')]+','+(_0x178fc6['account'][_0x7431('0x6c')]||0xa),'interval':_0x7431('0x4b')});}if(!_0x178fc6[_0x7431('0x5b')][_0x7431('0x6d')]){return;}return db[_0x7431('0x6e')][_0x7431('0x24')]({'attributes':['id',_0x7431('0x6a')],'where':{'id':_0x178fc6[_0x7431('0x5b')][_0x7431('0x6d')]}});})[_0x7431('0x10')](function(_0x4f9001){if(_0x4f9001){_0x178fc6[_0x7431('0x43')][_0x7431('0x6f')]({'id':0x0,'priority':0x1,'app':_0x7431('0x70'),'appdata':_0x4f9001['name']+','+(_0x178fc6[_0x7431('0x41')][_0x7431('0x71')]||0x12c),'interval':_0x7431('0x4b')});}return respondWithRpcPromise('Start',_0x7431('0x72'),_0x178fc6);})[_0x7431('0x10')](function(_0x39ce1b){_0x53dba5(_0x39ce1b);})[_0x7431('0x23')](function(_0x10d474){_0x4fe1d7(_0x13d903[_0x7431('0x17')](0x1f4,_0x10d474[_0x7431('0x18')]));});});};