c48cc6ff935ee26528bc6fa79b070d62ad0ad574
[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 _0x8876=['applications','list','orderBy','priority','asc','autoclose','close','*,*,*,*','length','create','List\x20not\x20found','contact','messageId','subject','attach','format','Messages','inReplyTo','MailInteraction','update','YYYY-MM-DD\x20HH:mm:ss','interaction','created','MailMessage','attachments','bcc','UserId','User','name','agent','startRouting','util','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','jayson/promise','client','http','redis','defaults','localhost','socket.io-emitter','./mailAccount.socket','register','request','then','info','debug','MailAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','MailAccount,\x20%s,\x20%s','message','result','catch','GetMailAccount','MailAccount','findAll','options','where','attributes','include','map','model','find','raw','ShowMailAccount','NotifyMailAccount','body','mail','difference','keys','CmContact','rawAttributes','createdAt','isNil','from\x20is\x20mandatory','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','account','CmList','List','Contacts','updatedAt','DESC','MailApplication','MailServerOut','Smtp','get','omit','Applications'];(function(_0x4634ce,_0x419f76){var _0x636fc0=function(_0x351d00){while(--_0x351d00){_0x4634ce['push'](_0x4634ce['shift']());}};_0x636fc0(++_0x419f76);}(_0x8876,0x83));var _0x6887=function(_0x3be16a,_0x2d008c){_0x3be16a=_0x3be16a-0x0;var _0x2845ef=_0x8876[_0x3be16a];return _0x2845ef;};'use strict';var _=require('lodash');var util=require(_0x6887('0x0'));var moment=require('moment');var BPromise=require(_0x6887('0x1'));var rs=require(_0x6887('0x2'));var fs=require('fs');var Redis=require(_0x6887('0x3'));var db=require(_0x6887('0x4'))['db'];var utils=require(_0x6887('0x5'));var logger=require(_0x6887('0x6'))(_0x6887('0x7'));var config=require('../../config/environment');var jayson=require(_0x6887('0x8'));var client=jayson[_0x6887('0x9')][_0x6887('0xa')]({'port':0x232c});config[_0x6887('0xb')]=_[_0x6887('0xc')](config[_0x6887('0xb')],{'host':_0x6887('0xd'),'port':0x18eb});var socket=require(_0x6887('0xe'))(new Redis(config[_0x6887('0xb')]));require(_0x6887('0xf'))[_0x6887('0x10')](socket);function respondWithRpcPromise(_0x407a34,_0x34a3c7,_0x1c7c1a){return new BPromise(function(_0x59c8ba,_0xbd8b1c){return client[_0x6887('0x11')](_0x407a34,_0x1c7c1a)[_0x6887('0x12')](function(_0x4fde9a){logger[_0x6887('0x13')]('MailAccount,\x20%s,\x20%s',_0x34a3c7,'request\x20sent');logger[_0x6887('0x14')](_0x6887('0x15'),_0x34a3c7,_0x6887('0x16'),JSON[_0x6887('0x17')](_0x4fde9a));if(_0x4fde9a[_0x6887('0x18')]){if(_0x4fde9a[_0x6887('0x18')]['code']===0x1f4){logger[_0x6887('0x18')](_0x6887('0x19'),_0x34a3c7,_0x4fde9a['error'][_0x6887('0x1a')]);return _0xbd8b1c(_0x4fde9a[_0x6887('0x18')]['message']);}logger[_0x6887('0x18')](_0x6887('0x19'),_0x34a3c7,_0x4fde9a[_0x6887('0x18')]['message']);return _0x59c8ba(_0x4fde9a[_0x6887('0x18')][_0x6887('0x1a')]);}else{logger['info'](_0x6887('0x19'),_0x34a3c7,_0x6887('0x16'));_0x59c8ba(_0x4fde9a[_0x6887('0x1b')][_0x6887('0x1a')]);}})[_0x6887('0x1c')](function(_0x34305d){logger[_0x6887('0x18')](_0x6887('0x19'),_0x34a3c7,_0x34305d);_0xbd8b1c(_0x34305d);});});}exports[_0x6887('0x1d')]=function(_0x2ab8cf){var _0x321f13=this;return new Promise(function(_0x5b4d51,_0x31a662){return db[_0x6887('0x1e')][_0x6887('0x1f')]({'raw':_0x2ab8cf[_0x6887('0x20')]?_0x2ab8cf['options']['raw']===undefined?!![]:![]:!![],'where':_0x2ab8cf[_0x6887('0x20')]?_0x2ab8cf['options'][_0x6887('0x21')]||null:null,'attributes':_0x2ab8cf[_0x6887('0x20')]?_0x2ab8cf[_0x6887('0x20')][_0x6887('0x22')]||null:null,'limit':_0x2ab8cf[_0x6887('0x20')]?_0x2ab8cf['options']['limit']||null:null,'include':_0x2ab8cf[_0x6887('0x20')]?_0x2ab8cf['options'][_0x6887('0x23')]?_[_0x6887('0x24')](_0x2ab8cf['options'][_0x6887('0x23')],function(_0x17211f){return{'model':db[_0x17211f['model']],'as':_0x17211f['as'],'attributes':_0x17211f[_0x6887('0x22')],'include':_0x17211f[_0x6887('0x23')]?_['map'](_0x17211f[_0x6887('0x23')],function(_0xc6ba7c){return{'model':db[_0xc6ba7c['model']],'as':_0xc6ba7c['as'],'attributes':_0xc6ba7c[_0x6887('0x22')],'include':_0xc6ba7c['include']?_['map'](_0xc6ba7c[_0x6887('0x23')],function(_0x397e62){return{'model':db[_0x397e62[_0x6887('0x25')]],'as':_0x397e62['as'],'attributes':_0x397e62[_0x6887('0x22')]};}):[]};}):[]};}):[]:[]})[_0x6887('0x12')](function(_0x21f9fd){logger[_0x6887('0x13')]('GetMailAccount',_0x2ab8cf);logger[_0x6887('0x14')](_0x6887('0x1d'),_0x2ab8cf,JSON[_0x6887('0x17')](_0x21f9fd));_0x5b4d51(_0x21f9fd);})['catch'](function(_0x1cdbb6){logger[_0x6887('0x18')]('GetMailAccount',_0x1cdbb6[_0x6887('0x1a')],_0x2ab8cf);_0x31a662(_0x321f13[_0x6887('0x18')](0x1f4,_0x1cdbb6[_0x6887('0x1a')]));});});};exports['ShowMailAccount']=function(_0x438778){var _0xb7964f=this;return new Promise(function(_0x5e577d,_0x541965){return db[_0x6887('0x1e')][_0x6887('0x26')]({'raw':_0x438778[_0x6887('0x20')]?_0x438778['options'][_0x6887('0x27')]===undefined?!![]:![]:!![],'where':_0x438778[_0x6887('0x20')]?_0x438778[_0x6887('0x20')][_0x6887('0x21')]||null:null,'attributes':_0x438778['options']?_0x438778['options'][_0x6887('0x22')]||null:null,'include':_0x438778[_0x6887('0x20')]?_0x438778[_0x6887('0x20')][_0x6887('0x23')]?_[_0x6887('0x24')](_0x438778[_0x6887('0x20')][_0x6887('0x23')],function(_0x2a0e08){return{'model':db[_0x2a0e08['model']],'as':_0x2a0e08['as'],'attributes':_0x2a0e08[_0x6887('0x22')],'include':_0x2a0e08[_0x6887('0x23')]?_[_0x6887('0x24')](_0x2a0e08[_0x6887('0x23')],function(_0x22f16c){return{'model':db[_0x22f16c[_0x6887('0x25')]],'as':_0x22f16c['as'],'attributes':_0x22f16c['attributes'],'include':_0x22f16c['include']?_[_0x6887('0x24')](_0x22f16c[_0x6887('0x23')],function(_0x4fd61b){return{'model':db[_0x4fd61b[_0x6887('0x25')]],'as':_0x4fd61b['as'],'attributes':_0x4fd61b['attributes']};}):[]};}):[]};}):[]:[]})[_0x6887('0x12')](function(_0x138d76){logger[_0x6887('0x13')]('ShowMailAccount',_0x438778);logger[_0x6887('0x14')](_0x6887('0x28'),_0x438778,JSON[_0x6887('0x17')](_0x138d76));_0x5e577d(_0x138d76);})[_0x6887('0x1c')](function(_0x5dd7f3){logger[_0x6887('0x18')]('ShowMailAccount',_0x5dd7f3['message'],_0x438778);_0x541965(_0xb7964f[_0x6887('0x18')](0x1f4,_0x5dd7f3[_0x6887('0x1a')]));});});};exports[_0x6887('0x29')]=function(_0xf54a85){var _0x40bf1a=this;return new Promise(function(_0x2df935,_0xfc4e4f){var _0x12eb76={'body':_0xf54a85[_0x6887('0x2a')],'channel':_0x6887('0x2b')};var _0x5bbf9d=_[_0x6887('0x2c')](_[_0x6887('0x2d')](db[_0x6887('0x2e')][_0x6887('0x2f')]),[_0x6887('0x30'),'updatedAt','CompanyId','ListId']);var _0xadaeb2=![];if(_0xf54a85[_0x6887('0x2a')]['id']){delete _0xf54a85['body']['id'];}if(_[_0x6887('0x31')](_0xf54a85[_0x6887('0x2a')]['from'])){return _0xfc4e4f(_0x40bf1a['error'](0x1f4,_0x6887('0x32')));}if(_[_0x6887('0x31')](_0xf54a85[_0x6887('0x2a')]['body'])||_0xf54a85[_0x6887('0x2a')][_0x6887('0x2a')]===''){return _0xfc4e4f(_0x40bf1a[_0x6887('0x18')](0x1f4,_0x6887('0x33')));}if(_[_0x6887('0x31')](_0xf54a85[_0x6887('0x2a')]['mapKey'])){return _0xfc4e4f(_0x40bf1a['error'](0x1f4,_0x6887('0x34')+_0x5bbf9d));}if(!_[_0x6887('0x35')](_0x5bbf9d,_0xf54a85[_0x6887('0x2a')]['mapKey'])){return _0xfc4e4f(_0x40bf1a['error'](0x1f4,_0x6887('0x36')+_0x5bbf9d));}var _0x8ec202={};_0x8ec202[_0xf54a85[_0x6887('0x2a')][_0x6887('0x37')]]=_0xf54a85['body'][_0x6887('0x38')];return db[_0x6887('0x1e')]['find']({'where':{'id':_0xf54a85[_0x6887('0x39')]['id']},'include':[{'model':db[_0x6887('0x3a')],'as':_0x6887('0x3b'),'include':[{'model':db['CmContact'],'as':_0x6887('0x3c'),'where':_0x8ec202,'limit':0x1,'order':[[_0x6887('0x3d'),_0x6887('0x3e')]]}]},{'model':db[_0x6887('0x3f')],'as':'Applications'},{'model':db[_0x6887('0x40')],'as':_0x6887('0x41')}]})[_0x6887('0x12')](function(_0x4acad6){if(_0x4acad6&&_0x4acad6[_0x6887('0x3b')]){var _0x53a3c0=_0x4acad6[_0x6887('0x42')]({'plain':!![]});_0x12eb76[_0x6887('0x39')]=_[_0x6887('0x43')](_0x53a3c0,[_0x6887('0x44'),_0x6887('0x3b')]);_0x12eb76[_0x6887('0x45')]=_0x53a3c0['Applications'];_0x12eb76[_0x6887('0x46')]=_0x53a3c0['List'];_0x12eb76[_0x6887('0x45')]=_[_0x6887('0x47')](_0x12eb76['applications'],[_0x6887('0x48')],[_0x6887('0x49')]);if(_0x12eb76[_0x6887('0x39')]&&_0x12eb76[_0x6887('0x39')][_0x6887('0x4a')]){_0x12eb76[_0x6887('0x45')]['push']({'id':0x0,'priority':_0x12eb76[_0x6887('0x45')]['length']+0x1,'app':_0x6887('0x4b'),'appdata':_0x6887('0x4a'),'interval':_0x6887('0x4c')});}if(_0x12eb76[_0x6887('0x39')][_0x6887('0x44')]){delete _0x12eb76[_0x6887('0x39')][_0x6887('0x44')];}if(_0x12eb76[_0x6887('0x39')]['List']){delete _0x12eb76[_0x6887('0x39')]['List'];}if(_0x53a3c0[_0x6887('0x3b')]['Contacts'][_0x6887('0x4d')]){return _0x53a3c0[_0x6887('0x3b')][_0x6887('0x3c')][0x0];}var _0xc13e05=_[_0x6887('0xc')](_0xf54a85[_0x6887('0x2a')],{'firstName':_0xf54a85[_0x6887('0x2a')][_0x6887('0x38')],'ListId':_0x53a3c0['ListId']});_0xc13e05[_0xf54a85[_0x6887('0x2a')][_0x6887('0x37')]]=_0xf54a85[_0x6887('0x2a')][_0x6887('0x38')];return db['CmContact'][_0x6887('0x4e')](_0xc13e05);}else{throw new Error(_0x6887('0x4f'));}})['then'](function(_0xf05fd6){_0x12eb76[_0x6887('0x50')]=_0xf05fd6;if(_0xf05fd6){if(!_0xf54a85[_0x6887('0x2a')]['message']['messageId']){_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x51')]=rs['generate']()+'\x20'+_0xf54a85['body'][_0x6887('0x1a')][_0x6887('0x38')];}var _0x2c7fed={'ContactId':_0xf05fd6['id'],'MailAccountId':_0xf54a85['account']['id'],'inReplyTo':_0xf54a85['body'][_0x6887('0x1a')][_0x6887('0x51')],'to':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x38')],'cc':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')]['cc'],'subject':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x52')],'attach':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x53')],'lastMsgAt':moment()[_0x6887('0x54')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in','lastMsgBody':_0xf54a85[_0x6887('0x2a')][_0x6887('0x2a')]};return db['MailInteraction'][_0x6887('0x26')]({'where':{'closed':![],'MailAccountId':_0xf54a85[_0x6887('0x39')]['id']},'include':[{'model':db['MailMessage'],'as':_0x6887('0x55'),'attributes':[_0x6887('0x51')],'where':{'messageId':_0xf54a85[_0x6887('0x2a')]['message'][_0x6887('0x56')]}}]})['then'](function(_0x133e86){if(_0x133e86){return[_0x133e86,![]];}return db[_0x6887('0x57')][_0x6887('0x4e')](_0x2c7fed)['then'](function(_0x473d32){return[_0x473d32,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})['spread'](function(_0x146120,_0x19d261){if(_0x146120){_0xadaeb2=_0x19d261;if(!_0x19d261){return _0x146120[_0x6887('0x58')]({'inReplyTo':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x51')],'to':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x38')],'cc':_0xf54a85['body'][_0x6887('0x1a')]['cc'],'subject':_0xf54a85[_0x6887('0x2a')]['message'][_0x6887('0x52')],'attach':_0x146120[_0x6887('0x53')]?_0x146120[_0x6887('0x53')]:_0xf54a85[_0x6887('0x2a')]['message'][_0x6887('0x53')],'substatus':null,'substatusAt':moment()['format'](_0x6887('0x59')),'lastMsgAt':moment()['format'](_0x6887('0x59')),'lastMsgDirection':'in','lastMsgBody':_0xf54a85[_0x6887('0x2a')][_0x6887('0x2a')]});}else{return _0x146120;}}})[_0x6887('0x12')](function(_0x49a92d){_0x12eb76[_0x6887('0x5a')]=_0x49a92d[_0x6887('0x42')]({'plain':!![]});_0x12eb76[_0x6887('0x5a')][_0x6887('0x5b')]=_0xadaeb2;return db[_0x6887('0x5c')][_0x6887('0x4e')]({'body':_0xf54a85[_0x6887('0x2a')][_0x6887('0x2a')],'MailAccountId':_0xf54a85[_0x6887('0x39')]['id'],'MailInteractionId':_0x49a92d['id'],'direction':'in','ContactId':_0x12eb76[_0x6887('0x50')]['id'],'messageId':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x51')],'from':_0xf54a85['body']['message'][_0x6887('0x38')],'to':_0xf54a85[_0x6887('0x2a')]['message']['to'],'cc':_0xf54a85['body'][_0x6887('0x1a')]['cc'],'subject':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x52')],'attach':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x53')],'Attachments':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x5d')],'originTo':_0xf54a85['body'][_0x6887('0x1a')]['originTo'],'originCc':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')]['originCc'],'bcc':_0xf54a85[_0x6887('0x2a')][_0x6887('0x1a')][_0x6887('0x5e')]},{'include':[{'model':db['Attachment'],'as':'Attachments'}]});})[_0x6887('0x12')](function(_0x19e978){_0x12eb76[_0x6887('0x1a')]=_0x19e978;if(_0x12eb76[_0x6887('0x5a')][_0x6887('0x5f')]){return db[_0x6887('0x60')][_0x6887('0x26')]({'attributes':['id',_0x6887('0x61')],'where':{'id':_0x12eb76[_0x6887('0x5a')]['UserId']}})[_0x6887('0x12')](function(_0x56a94f){if(_0x56a94f){_0x12eb76[_0x6887('0x45')]['unshift']({'id':0x0,'priority':0x0,'app':_0x6887('0x62'),'appdata':_0x56a94f[_0x6887('0x61')]+','+(_0x12eb76[_0x6887('0x39')]['waitForTheAssignedAgent']||0xa),'interval':_0x6887('0x4c')});}return respondWithRpcPromise('Start',_0x6887('0x63'),_0x12eb76);});}return respondWithRpcPromise('Start',_0x6887('0x63'),_0x12eb76);})[_0x6887('0x12')](function(_0x50a03f){_0x2df935(_0x50a03f);})[_0x6887('0x1c')](function(_0x4a6c98){_0xfc4e4f(_0x40bf1a[_0x6887('0x18')](0x1f4,_0x4a6c98[_0x6887('0x1a')]));});});};