5a0f2aca08e518bcbc33cfc74f818a5e6e3ac769
[motion2.git] / server / api / smsInteraction / smsInteraction.controller.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 _0x061b=['ioredis','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../mysqldb','redis','defaults','socket.io-emitter','./smsInteraction.socket','sendStatus','status','offset','undefined','limit','count','json','update','then','error','name','send','index','map','SmsInteraction','fieldName','type','getOptions','findAll','query','user','show','model','keys','filters','intersection','fields','attributes','length','include','merge','options','find','body','params','catch','destroy','describe','addMessage','SmsMessage','ids','getMessages','findOne','rawAttributes','hasOwnProperty','nolimit','order','sort','where','SmsInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','includeAll','rows','addTags','omit','spread','color','emit','smsInteractionTags:save','removeTags','download','toString','join','server','transcript-%d-%s.zip','push','User','CmContact','Contact','lastName','Messages','createdAt','secret','read','SmsAccount','Owner','fullname','get','mkdirSync','Sms','N.A.','closed','disposition','direction','out','System','firstName','value','root','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','log','err','unlinkSync','rimraf','zip-dir','moment','util','path','ejs','lodash'];(function(_0x3bee0c,_0x1d412f){var _0x531ce2=function(_0x4c6e19){while(--_0x4c6e19){_0x3bee0c['push'](_0x3bee0c['shift']());}};_0x531ce2(++_0x1d412f);}(_0x061b,0x6f));var _0xb061=function(_0x51020b,_0x38c2c7){_0x51020b=_0x51020b-0x0;var _0x371e47=_0x061b[_0x51020b];return _0x371e47;};'use strict';var rimraf=require(_0xb061('0x0'));var zipdir=require(_0xb061('0x1'));var moment=require(_0xb061('0x2'));var BPromise=require('bluebird');var util=require(_0xb061('0x3'));var path=require(_0xb061('0x4'));var ejs=require(_0xb061('0x5'));var fs=require('fs');var _=require(_0xb061('0x6'));var Redis=require(_0xb061('0x7'));var interaction=require('../../components/interaction/service');var qs=require(_0xb061('0x8'));var pdf=require(_0xb061('0x9'));var logger=require(_0xb061('0xa'))(_0xb061('0xb'));var config=require('../../config/environment');var db=require(_0xb061('0xc'))['db'];config[_0xb061('0xd')]=_[_0xb061('0xe')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0xb061('0xf'))(new Redis(config[_0xb061('0xd')]));require(_0xb061('0x10'))['register'](socket);function respondWithStatusCode(_0x428528,_0x273ad5){_0x273ad5=_0x273ad5||0xcc;return function(_0x4622da){if(_0x4622da){return _0x428528[_0xb061('0x11')](_0x273ad5);}return _0x428528[_0xb061('0x12')](_0x273ad5)['end']();};}function respondWithResult(_0x5c7285,_0x1ddad7){_0x1ddad7=_0x1ddad7||0xc8;return function(_0x2b49dd){if(_0x2b49dd){return _0x5c7285[_0xb061('0x12')](_0x1ddad7)['json'](_0x2b49dd);}};}function respondWithFilteredResult(_0x108657,_0x3d452f){return function(_0x3ca9b5){if(_0x3ca9b5){var _0x33fdf1=typeof _0x3d452f[_0xb061('0x13')]===_0xb061('0x14')&&typeof _0x3d452f[_0xb061('0x15')]==='undefined';var _0x333de6=_0x3ca9b5[_0xb061('0x16')];var _0x35b5ad=_0x33fdf1?0x0:_0x3d452f[_0xb061('0x13')];var _0x464842=_0x33fdf1?_0x3ca9b5[_0xb061('0x16')]:_0x3d452f[_0xb061('0x13')]+_0x3d452f[_0xb061('0x15')];var _0xa5a89e;if(_0x464842>=_0x333de6){_0x464842=_0x333de6;_0xa5a89e=0xc8;}else{_0xa5a89e=0xce;}_0x108657[_0xb061('0x12')](_0xa5a89e);return _0x108657['set']('Content-Range',_0x35b5ad+'-'+_0x464842+'/'+_0x333de6)[_0xb061('0x17')](_0x3ca9b5);}return null;};}function saveUpdates(_0x255d38){return function(_0x20e518){if(_0x20e518){return _0x20e518[_0xb061('0x18')](_0x255d38)[_0xb061('0x19')](function(_0x190157){return _0x190157;});}return null;};}function removeEntity(_0x4df96e){return function(_0x266c99){if(_0x266c99){return _0x266c99['destroy']()['then'](function(){_0x4df96e['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x2f3401){return function(_0x37cc19){if(!_0x37cc19){_0x2f3401[_0xb061('0x11')](0x194);}return _0x37cc19;};}function handleError(_0x11ee00,_0x161257){_0x161257=_0x161257||0x1f4;return function(_0x1cf4ae){logger[_0xb061('0x1a')](_0x1cf4ae['stack']);if(_0x1cf4ae[_0xb061('0x1b')]){delete _0x1cf4ae['name'];}_0x11ee00[_0xb061('0x12')](_0x161257)[_0xb061('0x1c')](_0x1cf4ae);};}exports[_0xb061('0x1d')]=function(_0x422cc7,_0x2da824){var _0x390371={};var _0x25bae2=_[_0xb061('0x1e')](db[_0xb061('0x1f')]['rawAttributes'],function(_0x37da52){return{'name':_0x37da52[_0xb061('0x20')],'type':_0x37da52[_0xb061('0x21')]['key']};});_0x390371=qs[_0xb061('0x22')](_0x25bae2,_0x422cc7);return interaction[_0xb061('0x23')]('sms',_0x422cc7[_0xb061('0x24')],_0x422cc7[_0xb061('0x25')],_0x390371,_0x25bae2)[_0xb061('0x19')](respondWithFilteredResult(_0x2da824,_0x390371))['catch'](handleError(_0x2da824,null));};exports[_0xb061('0x26')]=function(_0x1b393e,_0x5d1937){var _0x1fec75={'raw':![],'where':{'id':_0x1b393e['params']['id']}},_0x5f2dbb={};_0x5f2dbb[_0xb061('0x27')]=_[_0xb061('0x28')](db['SmsInteraction']['rawAttributes']);_0x5f2dbb[_0xb061('0x24')]=_[_0xb061('0x28')](_0x1b393e[_0xb061('0x24')]);_0x5f2dbb[_0xb061('0x29')]=_[_0xb061('0x2a')](_0x5f2dbb[_0xb061('0x27')],_0x5f2dbb[_0xb061('0x24')]);_0x1fec75['attributes']=_[_0xb061('0x2a')](_0x5f2dbb[_0xb061('0x27')],qs[_0xb061('0x2b')](_0x1b393e[_0xb061('0x24')][_0xb061('0x2b')]));_0x1fec75[_0xb061('0x2c')]=_0x1fec75['attributes'][_0xb061('0x2d')]?_0x1fec75[_0xb061('0x2c')]:_0x5f2dbb[_0xb061('0x27')];if(_0x1b393e['query']['includeAll']){_0x1fec75[_0xb061('0x2e')]=[{'all':!![]}];}_0x1fec75=_[_0xb061('0x2f')]({},_0x1fec75,_0x1b393e[_0xb061('0x30')]);return db[_0xb061('0x1f')][_0xb061('0x31')](_0x1fec75)[_0xb061('0x19')](handleEntityNotFound(_0x5d1937,null))[_0xb061('0x19')](respondWithResult(_0x5d1937,null))['catch'](handleError(_0x5d1937,null));};exports['create']=function(_0x7cac4f,_0x4ae7a0){return db[_0xb061('0x1f')]['create'](_0x7cac4f[_0xb061('0x32')],{})[_0xb061('0x19')](respondWithResult(_0x4ae7a0,0xc9))['catch'](handleError(_0x4ae7a0,null));};exports['update']=function(_0x501c61,_0x1db69f){if(_0x501c61[_0xb061('0x32')]['id']){delete _0x501c61[_0xb061('0x32')]['id'];}return db[_0xb061('0x1f')][_0xb061('0x31')]({'where':{'id':_0x501c61[_0xb061('0x33')]['id']}})[_0xb061('0x19')](handleEntityNotFound(_0x1db69f,null))['then'](saveUpdates(_0x501c61[_0xb061('0x32')],null))[_0xb061('0x19')](respondWithResult(_0x1db69f,null))[_0xb061('0x34')](handleError(_0x1db69f,null));};exports[_0xb061('0x35')]=function(_0x1034ad,_0x5e4aa8){return db[_0xb061('0x1f')][_0xb061('0x31')]({'where':{'id':_0x1034ad[_0xb061('0x33')]['id']}})[_0xb061('0x19')](handleEntityNotFound(_0x5e4aa8,null))[_0xb061('0x19')](removeEntity(_0x5e4aa8,null))[_0xb061('0x34')](handleError(_0x5e4aa8,null));};exports[_0xb061('0x36')]=function(_0x2a240a,_0x483d5a){return db[_0xb061('0x1f')][_0xb061('0x36')]()[_0xb061('0x19')](respondWithResult(_0x483d5a,null))[_0xb061('0x34')](handleError(_0x483d5a,null));};exports[_0xb061('0x37')]=function(_0x1705e3,_0x269350){return db[_0xb061('0x38')]['find']({'where':{'id':_0x1705e3[_0xb061('0x33')]['id']}})[_0xb061('0x19')](handleEntityNotFound(_0x269350,null))['then'](function(_0xe46d98){if(_0xe46d98){return _0xe46d98['addMessage'](_0x1705e3['body'][_0xb061('0x39')],_['omit'](_0x1705e3['body'],[_0xb061('0x39'),'id'])||{});}})[_0xb061('0x19')](respondWithResult(_0x269350,null))['catch'](handleError(_0x269350,null));};exports[_0xb061('0x3a')]=function(_0xc89300,_0x551ff8){var _0x3989a6={'raw':![],'where':{}};var _0x538097={};var _0x100726={'count':0x0,'rows':[]};return db[_0xb061('0x1f')][_0xb061('0x3b')]({'where':{'id':_0xc89300[_0xb061('0x33')]['id']}})[_0xb061('0x19')](handleEntityNotFound(_0x551ff8,null))['then'](function(_0x43887a){if(_0x43887a){_0x538097[_0xb061('0x27')]=_[_0xb061('0x28')](db['SmsMessage'][_0xb061('0x3c')]);_0x538097[_0xb061('0x24')]=_[_0xb061('0x28')](_0xc89300[_0xb061('0x24')]);_0x538097['filters']=_[_0xb061('0x2a')](_0x538097[_0xb061('0x27')],_0x538097[_0xb061('0x24')]);_0x3989a6[_0xb061('0x2c')]=_['intersection'](_0x538097[_0xb061('0x27')],qs[_0xb061('0x2b')](_0xc89300[_0xb061('0x24')]['fields']));_0x3989a6[_0xb061('0x2c')]=_0x3989a6[_0xb061('0x2c')][_0xb061('0x2d')]?_0x3989a6[_0xb061('0x2c')]:_0x538097[_0xb061('0x27')];if(!_0xc89300[_0xb061('0x24')][_0xb061('0x3d')](_0xb061('0x3e'))){_0x3989a6[_0xb061('0x15')]=qs[_0xb061('0x15')](_0xc89300[_0xb061('0x24')][_0xb061('0x15')]);_0x3989a6['offset']=qs[_0xb061('0x13')](_0xc89300[_0xb061('0x24')][_0xb061('0x13')]);}_0x3989a6[_0xb061('0x3f')]=qs[_0xb061('0x40')](_0xc89300[_0xb061('0x24')][_0xb061('0x40')]);_0x3989a6[_0xb061('0x41')]=qs[_0xb061('0x29')](_['pick'](_0xc89300[_0xb061('0x24')],_0x538097[_0xb061('0x29')]));_0x3989a6[_0xb061('0x41')][_0xb061('0x42')]=_0x43887a['id'];if(_0xc89300['query'][_0xb061('0x43')]){_0x3989a6[_0xb061('0x41')]=_[_0xb061('0x2f')](_0x3989a6['where'],{'$or':_[_0xb061('0x1e')](_0x3989a6[_0xb061('0x2c')],function(_0x3aff5e){var _0x105d16={};_0x105d16[_0x3aff5e]={'$like':'%'+_0xc89300[_0xb061('0x24')][_0xb061('0x43')]+'%'};return _0x105d16;})});}if(_0xc89300[_0xb061('0x24')][_0xb061('0x44')]){var _0x1bf08e=_0xc89300[_0xb061('0x24')][_0xb061('0x44')][_0xb061('0x45')](',');var _0x516f3b={};_0x516f3b[_0x1bf08e[0x0]]={'$gte':moment(_0x1bf08e[0x1])[_0xb061('0x46')](_0xb061('0x47'))};_0x3989a6['where']=_['merge'](_0x3989a6[_0xb061('0x41')],_0x516f3b);}_0x3989a6=_[_0xb061('0x2f')]({},_0x3989a6,_0xc89300[_0xb061('0x30')]);return db[_0xb061('0x38')]['count']({'where':_0x3989a6[_0xb061('0x41')]})[_0xb061('0x19')](function(_0x1b3804){_0x100726['count']=_0x1b3804;if(_0xc89300[_0xb061('0x24')][_0xb061('0x48')]){_0x3989a6[_0xb061('0x2e')]=[{'all':!![]}];}return db['SmsMessage'][_0xb061('0x23')](_0x3989a6);})[_0xb061('0x19')](function(_0x568cb5){_0x100726[_0xb061('0x49')]=_0x568cb5;return _0x100726;});}})[_0xb061('0x19')](respondWithFilteredResult(_0x551ff8,_0x3989a6))[_0xb061('0x34')](handleError(_0x551ff8,null));};exports[_0xb061('0x4a')]=function(_0xea8bd5,_0x1143c5){return db[_0xb061('0x1f')][_0xb061('0x31')]({'where':{'id':_0xea8bd5[_0xb061('0x33')]['id']}})[_0xb061('0x19')](handleEntityNotFound(_0x1143c5,null))[_0xb061('0x19')](function(_0x431a5a){if(_0x431a5a){return _0x431a5a['setTags'](_0xea8bd5[_0xb061('0x32')]['ids'],_[_0xb061('0x4b')](_0xea8bd5[_0xb061('0x32')],[_0xb061('0x39'),'id'])||{})[_0xb061('0x4c')](function(){return db['Tag'][_0xb061('0x23')]({'attributes':['id',_0xb061('0x1b'),_0xb061('0x4d')],'where':{'id':_0xea8bd5[_0xb061('0x32')][_0xb061('0x39')]}});})[_0xb061('0x19')](function(_0x22f3fc){socket[_0xb061('0x4e')](_0xb061('0x4f'),{'id':Number(_0xea8bd5[_0xb061('0x33')]['id']),'tags':_0x22f3fc||[]});return{'id':Number(_0xea8bd5[_0xb061('0x33')]['id']),'tags':_0x22f3fc||[]};});}})['then'](respondWithResult(_0x1143c5,null))[_0xb061('0x34')](handleError(_0x1143c5,null));};exports[_0xb061('0x50')]=function(_0x26b959,_0x54e63f){return db[_0xb061('0x1f')]['find']({'where':{'id':_0x26b959[_0xb061('0x33')]['id']}})[_0xb061('0x19')](handleEntityNotFound(_0x54e63f,null))['then'](function(_0x559922){if(_0x559922){return _0x559922[_0xb061('0x50')](_0x26b959[_0xb061('0x24')]['ids']);}})['then'](respondWithStatusCode(_0x54e63f,null))['catch'](handleError(_0x54e63f,null));};exports[_0xb061('0x51')]=function(_0x276e5c,_0xe918a1){var _0x31be08=moment()['unix']()[_0xb061('0x52')]();var _0x43db33=path[_0xb061('0x53')](config['root'],_0xb061('0x54'),'files','tmp');var _0x1f53f9=path[_0xb061('0x53')](_0x43db33,_0x31be08);var _0x247739=util[_0xb061('0x46')](_0xb061('0x55'),_0x276e5c[_0xb061('0x33')]['id'],_0x31be08);var _0x20d5d4=path['join'](_0x43db33,_0x247739);var _0x39e7bf=[];_0x39e7bf[_0xb061('0x56')]({'model':db['User'],'as':_0xb061('0x57'),'attributes':['id','fullname'],'raw':!![]});_0x39e7bf[_0xb061('0x56')]({'model':db[_0xb061('0x58')],'as':_0xb061('0x59'),'attributes':['id','firstName',_0xb061('0x5a')],'raw':!![]});var _0x4363a8=[{'model':db[_0xb061('0x38')],'as':_0xb061('0x5b'),'attributes':['id',_0xb061('0x32'),_0xb061('0x5c'),'direction',_0xb061('0x5d'),_0xb061('0x5e')],'include':_0x39e7bf}];_0x4363a8[_0xb061('0x56')]({'model':db[_0xb061('0x5f')],'as':'Account','attributes':['id',_0xb061('0x1b')]});_0x4363a8[_0xb061('0x56')]({'model':db[_0xb061('0x57')],'as':_0xb061('0x60'),'attributes':['id',_0xb061('0x61')]});return db[_0xb061('0x1f')][_0xb061('0x31')]({'where':{'id':_0x276e5c[_0xb061('0x33')]['id']},'include':_0x4363a8})['then'](handleEntityNotFound(_0xe918a1,null))[_0xb061('0x19')](function(_0x2f0041){if(_0x2f0041){var _0x2b1be4=_0x2f0041[_0xb061('0x62')]({'plain':!![]});fs[_0xb061('0x63')](_0x1f53f9);var _0x387212={'channel':_0xb061('0x64'),'account':_0x2b1be4['Account'][_0xb061('0x1b')],'agent':_0x2b1be4[_0xb061('0x60')]?_0x2b1be4[_0xb061('0x60')]['fullname']:_0xb061('0x65'),'createdAt':moment(_0x2b1be4['createdAt'])[_0xb061('0x46')](_0xb061('0x47'))[_0xb061('0x52')](),'closedAt':_0x2b1be4[_0xb061('0x66')]?moment(_0x2b1be4['closedAt'])[_0xb061('0x46')](_0xb061('0x47'))[_0xb061('0x52')]():'','disposition':_0x2b1be4[_0xb061('0x67')],'messages':_(_0x2b1be4[_0xb061('0x5b')])[_0xb061('0x43')]({'secret':![]})[_0xb061('0x1e')](function(_0x47ec7f){return{'date':moment(_0x47ec7f[_0xb061('0x5c')])[_0xb061('0x46')](_0xb061('0x47'))[_0xb061('0x52')](),'sender':_0x47ec7f[_0xb061('0x68')]===_0xb061('0x69')?_0x47ec7f[_0xb061('0x57')]?_0x47ec7f[_0xb061('0x57')][_0xb061('0x61')]:_0xb061('0x6a'):_0x47ec7f['Contact'][_0xb061('0x6b')]+(_0x47ec7f['Contact'][_0xb061('0x5a')]?'\x20'+_0x47ec7f[_0xb061('0x59')]['lastName']:''),'body':_0x47ec7f['body'],'direction':_0x47ec7f[_0xb061('0x68')]===_0xb061('0x69')?'A':'C','secret':_0x47ec7f[_0xb061('0x5d')],'read':_0x47ec7f[_0xb061('0x5e')]};})[_0xb061('0x6c')]()};return ejs['renderFile'](path[_0xb061('0x53')](config[_0xb061('0x6d')],_0xb061('0x54'),_0xb061('0x6e'),_0xb061('0x6f')),{'interaction':_0x387212})[_0xb061('0x19')](function(_0x5b9162){var _0x3aae43=path[_0xb061('0x53')](_0x1f53f9,_0xb061('0x70')+_0x2b1be4['id']+'-'+_0x31be08+_0xb061('0x71'));var _0x428f46={'path':_0x3aae43,'channel':_0xb061('0x64'),'interactionId':_0x2b1be4['id']};return pdf[_0xb061('0x72')](_0x5b9162,_0x428f46);})[_0xb061('0x19')](function(){return new BPromise(function(_0x4612fe,_0x5c9e63){zipdir(_0x1f53f9,{'saveTo':_0x20d5d4},function(_0x2b3ddd,_0x5d4341){if(_0x2b3ddd)return _0x5c9e63(_0x2b3ddd);return _0x4612fe(_0x5d4341);});})[_0xb061('0x19')](function(){return new BPromise(function(_0x597dc5,_0x2c3df5){rimraf(_0x1f53f9,function(_0x5bc387){if(_0x5bc387)_0x2c3df5(_0x5bc387);return _0x597dc5();});});})[_0xb061('0x19')](function(){return _0xe918a1[_0xb061('0x51')](_0x20d5d4,_0x247739,function(_0x4f8601){if(_0x4f8601){console[_0xb061('0x73')](_0xb061('0x74'),_0x4f8601);}else{fs[_0xb061('0x75')](_0x20d5d4);}});});});}else{return _0xe918a1['sendStatus'](0xc8);}})[_0xb061('0x34')](handleError(_0xe918a1,null));};