Built motion from commit 639a1a7f.|2.6.19
[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 _0x234d=['../../config/logger','../../config/environment','../../mysqldb','defaults','redis','socket.io-emitter','./smsInteraction.socket','status','json','offset','limit','undefined','count','set','Content-Range','update','then','end','sendStatus','stack','name','send','index','SmsInteraction','rawAttributes','fieldName','type','getOptions','findAll','query','user','catch','show','params','model','keys','intersection','fields','attributes','length','includeAll','merge','find','create','body','destroy','describe','addMessage','SmsMessage','ids','omit','getMessages','filters','hasOwnProperty','nolimit','sort','where','SmsInteractionId','filter','map','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','options','include','addTags','setTags','spread','Tag','color','emit','smsInteractionTags:save','removeTags','unix','toString','join','files','tmp','transcript-%d-%s.zip','User','fullname','push','CmContact','Contact','lastName','Messages','createdAt','direction','secret','SmsAccount','Owner','get','mkdirSync','Sms','closed','closedAt','out','System','firstName','read','value','renderFile','root','views','downloadInteraction.ejs','createPdfFromHTML','log','err','unlinkSync','rimraf','zip-dir','moment','util','path','ejs','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs'];(function(_0x2c67f9,_0x358108){var _0x4aaae7=function(_0x5f40cb){while(--_0x5f40cb){_0x2c67f9['push'](_0x2c67f9['shift']());}};_0x4aaae7(++_0x358108);}(_0x234d,0x1d6));var _0xd234=function(_0x47aa3f,_0x9a5a94){_0x47aa3f=_0x47aa3f-0x0;var _0x1a7a7b=_0x234d[_0x47aa3f];return _0x1a7a7b;};'use strict';var rimraf=require(_0xd234('0x0'));var zipdir=require(_0xd234('0x1'));var moment=require(_0xd234('0x2'));var BPromise=require('bluebird');var util=require(_0xd234('0x3'));var path=require(_0xd234('0x4'));var ejs=require(_0xd234('0x5'));var fs=require('fs');var _=require(_0xd234('0x6'));var Redis=require(_0xd234('0x7'));var interaction=require(_0xd234('0x8'));var qs=require(_0xd234('0x9'));var pdf=require('../../components/export/pdf');var logger=require(_0xd234('0xa'))('api');var config=require(_0xd234('0xb'));var db=require(_0xd234('0xc'))['db'];config['redis']=_[_0xd234('0xd')](config[_0xd234('0xe')],{'host':'localhost','port':0x18eb});var socket=require(_0xd234('0xf'))(new Redis(config[_0xd234('0xe')]));require(_0xd234('0x10'))['register'](socket);function respondWithStatusCode(_0x13342c,_0x1a03ae){_0x1a03ae=_0x1a03ae||0xcc;return function(_0x2814a3){if(_0x2814a3){return _0x13342c['sendStatus'](_0x1a03ae);}return _0x13342c[_0xd234('0x11')](_0x1a03ae)['end']();};}function respondWithResult(_0x73d60b,_0x449fc8){_0x449fc8=_0x449fc8||0xc8;return function(_0x1b5d1c){if(_0x1b5d1c){return _0x73d60b[_0xd234('0x11')](_0x449fc8)[_0xd234('0x12')](_0x1b5d1c);}};}function respondWithFilteredResult(_0x1534e2,_0x1c9e3c){return function(_0x1af440){if(_0x1af440){var _0x57bc51=typeof _0x1c9e3c[_0xd234('0x13')]==='undefined'&&typeof _0x1c9e3c[_0xd234('0x14')]===_0xd234('0x15');var _0x425bd7=_0x1af440[_0xd234('0x16')];var _0x2456f2=_0x57bc51?0x0:_0x1c9e3c[_0xd234('0x13')];var _0x5067b0=_0x57bc51?_0x1af440['count']:_0x1c9e3c[_0xd234('0x13')]+_0x1c9e3c[_0xd234('0x14')];var _0x496e66;if(_0x5067b0>=_0x425bd7){_0x5067b0=_0x425bd7;_0x496e66=0xc8;}else{_0x496e66=0xce;}_0x1534e2[_0xd234('0x11')](_0x496e66);return _0x1534e2[_0xd234('0x17')](_0xd234('0x18'),_0x2456f2+'-'+_0x5067b0+'/'+_0x425bd7)['json'](_0x1af440);}return null;};}function saveUpdates(_0x1c0622){return function(_0x3a78fb){if(_0x3a78fb){return _0x3a78fb[_0xd234('0x19')](_0x1c0622)[_0xd234('0x1a')](function(_0x55f016){return _0x55f016;});}return null;};}function removeEntity(_0x28e55f){return function(_0x49e45f){if(_0x49e45f){return _0x49e45f['destroy']()[_0xd234('0x1a')](function(){_0x28e55f[_0xd234('0x11')](0xcc)[_0xd234('0x1b')]();});}};}function handleEntityNotFound(_0x3a847b){return function(_0x3e4a0b){if(!_0x3e4a0b){_0x3a847b[_0xd234('0x1c')](0x194);}return _0x3e4a0b;};}function handleError(_0x4bb32a,_0x5acfee){_0x5acfee=_0x5acfee||0x1f4;return function(_0x2de093){logger['error'](_0x2de093[_0xd234('0x1d')]);if(_0x2de093[_0xd234('0x1e')]){delete _0x2de093[_0xd234('0x1e')];}_0x4bb32a[_0xd234('0x11')](_0x5acfee)[_0xd234('0x1f')](_0x2de093);};}exports[_0xd234('0x20')]=function(_0x389773,_0x249051){var _0x5459de={};var _0x527def=_['map'](db[_0xd234('0x21')][_0xd234('0x22')],function(_0x2cd6aa){return{'name':_0x2cd6aa[_0xd234('0x23')],'type':_0x2cd6aa[_0xd234('0x24')]['key']};});_0x5459de=qs[_0xd234('0x25')](_0x527def,_0x389773);return interaction[_0xd234('0x26')]('sms',_0x389773[_0xd234('0x27')],_0x389773[_0xd234('0x28')],_0x5459de,_0x527def)[_0xd234('0x1a')](respondWithFilteredResult(_0x249051,_0x5459de))[_0xd234('0x29')](handleError(_0x249051,null));};exports[_0xd234('0x2a')]=function(_0x1bee90,_0x233f74){var _0x13b49a={'raw':![],'where':{'id':_0x1bee90[_0xd234('0x2b')]['id']}},_0x3d284f={};_0x3d284f[_0xd234('0x2c')]=_[_0xd234('0x2d')](db[_0xd234('0x21')]['rawAttributes']);_0x3d284f[_0xd234('0x27')]=_[_0xd234('0x2d')](_0x1bee90['query']);_0x3d284f['filters']=_[_0xd234('0x2e')](_0x3d284f['model'],_0x3d284f[_0xd234('0x27')]);_0x13b49a['attributes']=_['intersection'](_0x3d284f[_0xd234('0x2c')],qs['fields'](_0x1bee90[_0xd234('0x27')][_0xd234('0x2f')]));_0x13b49a[_0xd234('0x30')]=_0x13b49a[_0xd234('0x30')][_0xd234('0x31')]?_0x13b49a['attributes']:_0x3d284f['model'];if(_0x1bee90[_0xd234('0x27')][_0xd234('0x32')]){_0x13b49a['include']=[{'all':!![]}];}_0x13b49a=_[_0xd234('0x33')]({},_0x13b49a,_0x1bee90['options']);return db[_0xd234('0x21')][_0xd234('0x34')](_0x13b49a)[_0xd234('0x1a')](handleEntityNotFound(_0x233f74,null))[_0xd234('0x1a')](respondWithResult(_0x233f74,null))[_0xd234('0x29')](handleError(_0x233f74,null));};exports[_0xd234('0x35')]=function(_0x15fbbd,_0x4ee847){return db['SmsInteraction']['create'](_0x15fbbd[_0xd234('0x36')],{})[_0xd234('0x1a')](respondWithResult(_0x4ee847,0xc9))[_0xd234('0x29')](handleError(_0x4ee847,null));};exports[_0xd234('0x19')]=function(_0x356a37,_0xead640){if(_0x356a37[_0xd234('0x36')]['id']){delete _0x356a37[_0xd234('0x36')]['id'];}return db[_0xd234('0x21')][_0xd234('0x34')]({'where':{'id':_0x356a37[_0xd234('0x2b')]['id']}})[_0xd234('0x1a')](handleEntityNotFound(_0xead640,null))['then'](saveUpdates(_0x356a37[_0xd234('0x36')],null))['then'](respondWithResult(_0xead640,null))[_0xd234('0x29')](handleError(_0xead640,null));};exports[_0xd234('0x37')]=function(_0x2c7ecd,_0x253fd9){return db[_0xd234('0x21')]['find']({'where':{'id':_0x2c7ecd[_0xd234('0x2b')]['id']}})[_0xd234('0x1a')](handleEntityNotFound(_0x253fd9,null))['then'](removeEntity(_0x253fd9,null))[_0xd234('0x29')](handleError(_0x253fd9,null));};exports[_0xd234('0x38')]=function(_0x3b046d,_0x306277){return db['SmsInteraction']['describe']()[_0xd234('0x1a')](respondWithResult(_0x306277,null))[_0xd234('0x29')](handleError(_0x306277,null));};exports[_0xd234('0x39')]=function(_0x45328a,_0x1c3a6d){return db[_0xd234('0x3a')][_0xd234('0x34')]({'where':{'id':_0x45328a['params']['id']}})[_0xd234('0x1a')](handleEntityNotFound(_0x1c3a6d,null))[_0xd234('0x1a')](function(_0x2438ca){if(_0x2438ca){return _0x2438ca[_0xd234('0x39')](_0x45328a[_0xd234('0x36')][_0xd234('0x3b')],_[_0xd234('0x3c')](_0x45328a[_0xd234('0x36')],['ids','id'])||{});}})[_0xd234('0x1a')](respondWithResult(_0x1c3a6d,null))[_0xd234('0x29')](handleError(_0x1c3a6d,null));};exports[_0xd234('0x3d')]=function(_0x56d013,_0x2f230e){var _0x153ea6={'raw':![],'where':{}};var _0x1e6723={};var _0x46bf06={'count':0x0,'rows':[]};return db[_0xd234('0x21')]['findOne']({'where':{'id':_0x56d013['params']['id']}})[_0xd234('0x1a')](handleEntityNotFound(_0x2f230e,null))[_0xd234('0x1a')](function(_0x3bebc9){if(_0x3bebc9){_0x1e6723['model']=_[_0xd234('0x2d')](db[_0xd234('0x3a')][_0xd234('0x22')]);_0x1e6723[_0xd234('0x27')]=_[_0xd234('0x2d')](_0x56d013[_0xd234('0x27')]);_0x1e6723[_0xd234('0x3e')]=_['intersection'](_0x1e6723[_0xd234('0x2c')],_0x1e6723[_0xd234('0x27')]);_0x153ea6[_0xd234('0x30')]=_[_0xd234('0x2e')](_0x1e6723[_0xd234('0x2c')],qs['fields'](_0x56d013[_0xd234('0x27')][_0xd234('0x2f')]));_0x153ea6[_0xd234('0x30')]=_0x153ea6[_0xd234('0x30')][_0xd234('0x31')]?_0x153ea6[_0xd234('0x30')]:_0x1e6723[_0xd234('0x2c')];if(!_0x56d013[_0xd234('0x27')][_0xd234('0x3f')](_0xd234('0x40'))){_0x153ea6[_0xd234('0x14')]=qs['limit'](_0x56d013[_0xd234('0x27')]['limit']);_0x153ea6[_0xd234('0x13')]=qs[_0xd234('0x13')](_0x56d013[_0xd234('0x27')]['offset']);}_0x153ea6['order']=qs[_0xd234('0x41')](_0x56d013['query'][_0xd234('0x41')]);_0x153ea6[_0xd234('0x42')]=qs['filters'](_['pick'](_0x56d013[_0xd234('0x27')],_0x1e6723[_0xd234('0x3e')]));_0x153ea6[_0xd234('0x42')][_0xd234('0x43')]=_0x3bebc9['id'];if(_0x56d013['query'][_0xd234('0x44')]){_0x153ea6['where']=_[_0xd234('0x33')](_0x153ea6[_0xd234('0x42')],{'$or':_[_0xd234('0x45')](_0x153ea6[_0xd234('0x30')],function(_0x533358){var _0x21431d={};_0x21431d[_0x533358]={'$like':'%'+_0x56d013[_0xd234('0x27')][_0xd234('0x44')]+'%'};return _0x21431d;})});}if(_0x56d013[_0xd234('0x27')][_0xd234('0x46')]){var _0x494451=_0x56d013[_0xd234('0x27')]['$gte'][_0xd234('0x47')](',');var _0x466cac={};_0x466cac[_0x494451[0x0]]={'$gte':moment(_0x494451[0x1])[_0xd234('0x48')](_0xd234('0x49'))};_0x153ea6['where']=_[_0xd234('0x33')](_0x153ea6[_0xd234('0x42')],_0x466cac);}_0x153ea6=_[_0xd234('0x33')]({},_0x153ea6,_0x56d013[_0xd234('0x4a')]);return db['SmsMessage'][_0xd234('0x16')]({'where':_0x153ea6[_0xd234('0x42')]})[_0xd234('0x1a')](function(_0x100a9b){_0x46bf06['count']=_0x100a9b;if(_0x56d013[_0xd234('0x27')][_0xd234('0x32')]){_0x153ea6[_0xd234('0x4b')]=[{'all':!![]}];}return db[_0xd234('0x3a')][_0xd234('0x26')](_0x153ea6);})[_0xd234('0x1a')](function(_0x48ca77){_0x46bf06['rows']=_0x48ca77;return _0x46bf06;});}})[_0xd234('0x1a')](respondWithFilteredResult(_0x2f230e,_0x153ea6))[_0xd234('0x29')](handleError(_0x2f230e,null));};exports[_0xd234('0x4c')]=function(_0x40d5da,_0x27410a){return db[_0xd234('0x21')][_0xd234('0x34')]({'where':{'id':_0x40d5da['params']['id']}})[_0xd234('0x1a')](handleEntityNotFound(_0x27410a,null))['then'](function(_0x537275){if(_0x537275){return _0x537275[_0xd234('0x4d')](_0x40d5da[_0xd234('0x36')][_0xd234('0x3b')],_[_0xd234('0x3c')](_0x40d5da[_0xd234('0x36')],[_0xd234('0x3b'),'id'])||{})[_0xd234('0x4e')](function(){return db[_0xd234('0x4f')][_0xd234('0x26')]({'attributes':['id','name',_0xd234('0x50')],'where':{'id':_0x40d5da[_0xd234('0x36')][_0xd234('0x3b')]}});})[_0xd234('0x1a')](function(_0x41ba6e){socket[_0xd234('0x51')](_0xd234('0x52'),{'id':Number(_0x40d5da[_0xd234('0x2b')]['id']),'tags':_0x41ba6e||[]});return{'id':Number(_0x40d5da['params']['id']),'tags':_0x41ba6e||[]};});}})[_0xd234('0x1a')](respondWithResult(_0x27410a,null))[_0xd234('0x29')](handleError(_0x27410a,null));};exports[_0xd234('0x53')]=function(_0x226199,_0x3a9bd8){return db[_0xd234('0x21')][_0xd234('0x34')]({'where':{'id':_0x226199[_0xd234('0x2b')]['id']}})['then'](handleEntityNotFound(_0x3a9bd8,null))[_0xd234('0x1a')](function(_0x199f80){if(_0x199f80){return _0x199f80[_0xd234('0x53')](_0x226199['query'][_0xd234('0x3b')]);}})[_0xd234('0x1a')](respondWithStatusCode(_0x3a9bd8,null))['catch'](handleError(_0x3a9bd8,null));};exports['download']=function(_0x4ad837,_0x239eb8){var _0x51388d=moment()[_0xd234('0x54')]()[_0xd234('0x55')]();var _0x33d6e3=path[_0xd234('0x56')](config['root'],'server',_0xd234('0x57'),_0xd234('0x58'));var _0x391b36=path[_0xd234('0x56')](_0x33d6e3,_0x51388d);var _0x38a835=util[_0xd234('0x48')](_0xd234('0x59'),_0x4ad837['params']['id'],_0x51388d);var _0x5b02ea=path[_0xd234('0x56')](_0x33d6e3,_0x38a835);var _0x51e35a=[];_0x51e35a['push']({'model':db[_0xd234('0x5a')],'as':_0xd234('0x5a'),'attributes':['id',_0xd234('0x5b')],'raw':!![]});_0x51e35a[_0xd234('0x5c')]({'model':db[_0xd234('0x5d')],'as':_0xd234('0x5e'),'attributes':['id','firstName',_0xd234('0x5f')],'raw':!![]});var _0x3cb664=[{'model':db['SmsMessage'],'as':_0xd234('0x60'),'attributes':['id','body',_0xd234('0x61'),_0xd234('0x62'),_0xd234('0x63'),'read'],'include':_0x51e35a}];_0x3cb664['push']({'model':db[_0xd234('0x64')],'as':'Account','attributes':['id',_0xd234('0x1e')]});_0x3cb664[_0xd234('0x5c')]({'model':db[_0xd234('0x5a')],'as':_0xd234('0x65'),'attributes':['id',_0xd234('0x5b')]});return db[_0xd234('0x21')][_0xd234('0x34')]({'where':{'id':_0x4ad837[_0xd234('0x2b')]['id']},'include':_0x3cb664})['then'](handleEntityNotFound(_0x239eb8,null))['then'](function(_0x2f0ceb){if(_0x2f0ceb){var _0x55794b=_0x2f0ceb[_0xd234('0x66')]({'plain':!![]});fs[_0xd234('0x67')](_0x391b36);var _0x4093ea={'channel':_0xd234('0x68'),'account':_0x55794b['Account'][_0xd234('0x1e')],'agent':_0x55794b[_0xd234('0x65')]?_0x55794b['Owner'][_0xd234('0x5b')]:'N.A.','createdAt':moment(_0x55794b[_0xd234('0x61')])[_0xd234('0x48')](_0xd234('0x49'))[_0xd234('0x55')](),'closedAt':_0x55794b[_0xd234('0x69')]?moment(_0x55794b[_0xd234('0x6a')])[_0xd234('0x48')](_0xd234('0x49'))['toString']():'','disposition':_0x55794b['disposition'],'messages':_(_0x55794b[_0xd234('0x60')])[_0xd234('0x44')]({'secret':![]})['map'](function(_0x577bcf){return{'date':moment(_0x577bcf[_0xd234('0x61')])['format']('YYYY-MM-DD\x20HH:mm:ss')['toString'](),'sender':_0x577bcf[_0xd234('0x62')]===_0xd234('0x6b')?_0x577bcf['User']?_0x577bcf[_0xd234('0x5a')][_0xd234('0x5b')]:_0xd234('0x6c'):_0x577bcf[_0xd234('0x5e')][_0xd234('0x6d')]+(_0x577bcf[_0xd234('0x5e')][_0xd234('0x5f')]?'\x20'+_0x577bcf['Contact']['lastName']:''),'body':_0x577bcf[_0xd234('0x36')],'direction':_0x577bcf[_0xd234('0x62')]==='out'?'A':'C','secret':_0x577bcf[_0xd234('0x63')],'read':_0x577bcf[_0xd234('0x6e')]};})[_0xd234('0x6f')]()};return ejs[_0xd234('0x70')](path[_0xd234('0x56')](config[_0xd234('0x71')],'server',_0xd234('0x72'),_0xd234('0x73')),{'interaction':_0x4093ea})[_0xd234('0x1a')](function(_0x5dfcd4){var _0x115a1d=path[_0xd234('0x56')](_0x391b36,'transcript-'+_0x55794b['id']+'-'+_0x51388d+'.pdf');var _0x2b99b4={'path':_0x115a1d,'channel':_0xd234('0x68'),'interactionId':_0x55794b['id']};return pdf[_0xd234('0x74')](_0x5dfcd4,_0x2b99b4);})[_0xd234('0x1a')](function(){return new BPromise(function(_0x19fa7b,_0x38f0de){zipdir(_0x391b36,{'saveTo':_0x5b02ea},function(_0x377b82,_0x5f12a4){if(_0x377b82)return _0x38f0de(_0x377b82);return _0x19fa7b(_0x5f12a4);});})[_0xd234('0x1a')](function(){return new BPromise(function(_0x1eb017,_0x243621){rimraf(_0x391b36,function(_0x3e58f0){if(_0x3e58f0)_0x243621(_0x3e58f0);return _0x1eb017();});});})[_0xd234('0x1a')](function(){return _0x239eb8['download'](_0x5b02ea,_0x38a835,function(_0x579e55){if(_0x579e55){console[_0xd234('0x75')](_0xd234('0x76'),_0x579e55);}else{fs[_0xd234('0x77')](_0x5b02ea);}});});});}else{return _0x239eb8[_0xd234('0x1c')](0xc8);}})[_0xd234('0x29')](handleError(_0x239eb8,null));};