Built motion from commit 21186556.|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 _0xeafb=['disposition','System','Contact','out','value','renderFile','server','views','transcript-','.pdf','createPdfFromHTML','log','err','rimraf','bluebird','util','path','ejs','ioredis','../../components/interaction/service','../../components/export/pdf','../../config/logger','defaults','redis','localhost','register','sendStatus','status','offset','limit','undefined','count','set','Content-Range','then','destroy','name','send','index','map','SmsInteraction','rawAttributes','fieldName','type','key','findAll','sms','user','catch','show','params','model','keys','query','intersection','fields','attributes','length','includeAll','merge','find','create','body','update','describe','SmsMessage','addMessage','ids','omit','filters','hasOwnProperty','order','sort','where','SmsInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','include','rows','addTags','setTags','spread','Tag','emit','removeTags','unix','toString','join','root','files','transcript-%d-%s.zip','push','User','fullname','firstName','lastName','Messages','createdAt','direction','secret','read','SmsAccount','Account','Owner','get','Sms','N.A.'];(function(_0x4148fc,_0x1a51c7){var _0x1fff84=function(_0x4f7a39){while(--_0x4f7a39){_0x4148fc['push'](_0x4148fc['shift']());}};_0x1fff84(++_0x1a51c7);}(_0xeafb,0x157));var _0xbeaf=function(_0x4f8d4b,_0x1b739d){_0x4f8d4b=_0x4f8d4b-0x0;var _0x19c786=_0xeafb[_0x4f8d4b];return _0x19c786;};'use strict';var rimraf=require(_0xbeaf('0x0'));var zipdir=require('zip-dir');var moment=require('moment');var BPromise=require(_0xbeaf('0x1'));var util=require(_0xbeaf('0x2'));var path=require(_0xbeaf('0x3'));var ejs=require(_0xbeaf('0x4'));var fs=require('fs');var _=require('lodash');var Redis=require(_0xbeaf('0x5'));var interaction=require(_0xbeaf('0x6'));var qs=require('../../components/parsers/qs');var pdf=require(_0xbeaf('0x7'));var logger=require(_0xbeaf('0x8'))('api');var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config['redis']=_[_0xbeaf('0x9')](config[_0xbeaf('0xa')],{'host':_0xbeaf('0xb'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require('./smsInteraction.socket')[_0xbeaf('0xc')](socket);function respondWithStatusCode(_0x398e24,_0x102eb9){_0x102eb9=_0x102eb9||0xcc;return function(_0x145daa){if(_0x145daa){return _0x398e24[_0xbeaf('0xd')](_0x102eb9);}return _0x398e24[_0xbeaf('0xe')](_0x102eb9)['end']();};}function respondWithResult(_0x3291b3,_0x30a27d){_0x30a27d=_0x30a27d||0xc8;return function(_0x42b6ee){if(_0x42b6ee){return _0x3291b3[_0xbeaf('0xe')](_0x30a27d)['json'](_0x42b6ee);}};}function respondWithFilteredResult(_0x3642f6,_0x3e4be1){return function(_0x1327a7){if(_0x1327a7){var _0x4bf905=typeof _0x3e4be1[_0xbeaf('0xf')]==='undefined'&&typeof _0x3e4be1[_0xbeaf('0x10')]===_0xbeaf('0x11');var _0x2410d4=_0x1327a7[_0xbeaf('0x12')];var _0x1f3ced=_0x4bf905?0x0:_0x3e4be1['offset'];var _0xae13bf=_0x4bf905?_0x1327a7[_0xbeaf('0x12')]:_0x3e4be1[_0xbeaf('0xf')]+_0x3e4be1[_0xbeaf('0x10')];var _0x2d1268;if(_0xae13bf>=_0x2410d4){_0xae13bf=_0x2410d4;_0x2d1268=0xc8;}else{_0x2d1268=0xce;}_0x3642f6[_0xbeaf('0xe')](_0x2d1268);return _0x3642f6[_0xbeaf('0x13')](_0xbeaf('0x14'),_0x1f3ced+'-'+_0xae13bf+'/'+_0x2410d4)['json'](_0x1327a7);}return null;};}function saveUpdates(_0xee740){return function(_0x1f7e17){if(_0x1f7e17){return _0x1f7e17['update'](_0xee740)[_0xbeaf('0x15')](function(_0x483c1e){return _0x483c1e;});}return null;};}function removeEntity(_0x5bfa66){return function(_0x41b56b){if(_0x41b56b){return _0x41b56b[_0xbeaf('0x16')]()[_0xbeaf('0x15')](function(){_0x5bfa66['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0xed6e59){return function(_0x3bbff1){if(!_0x3bbff1){_0xed6e59[_0xbeaf('0xd')](0x194);}return _0x3bbff1;};}function handleError(_0x14a59c,_0x241f4a){_0x241f4a=_0x241f4a||0x1f4;return function(_0x534034){logger['error'](_0x534034['stack']);if(_0x534034[_0xbeaf('0x17')]){delete _0x534034[_0xbeaf('0x17')];}_0x14a59c[_0xbeaf('0xe')](_0x241f4a)[_0xbeaf('0x18')](_0x534034);};}exports[_0xbeaf('0x19')]=function(_0x6f157c,_0x4ab762){var _0x2899fb={};var _0x501254=_[_0xbeaf('0x1a')](db[_0xbeaf('0x1b')][_0xbeaf('0x1c')],function(_0x5337cc){return{'name':_0x5337cc[_0xbeaf('0x1d')],'type':_0x5337cc[_0xbeaf('0x1e')][_0xbeaf('0x1f')]};});_0x2899fb=qs['getOptions'](_0x501254,_0x6f157c);return interaction[_0xbeaf('0x20')](_0xbeaf('0x21'),_0x6f157c['query'],_0x6f157c[_0xbeaf('0x22')],_0x2899fb,_0x501254)[_0xbeaf('0x15')](respondWithFilteredResult(_0x4ab762,_0x2899fb))[_0xbeaf('0x23')](handleError(_0x4ab762,null));};exports[_0xbeaf('0x24')]=function(_0x173f2e,_0x461e20){var _0x50799d={'raw':![],'where':{'id':_0x173f2e[_0xbeaf('0x25')]['id']}},_0x571ee3={};_0x571ee3[_0xbeaf('0x26')]=_[_0xbeaf('0x27')](db[_0xbeaf('0x1b')]['rawAttributes']);_0x571ee3[_0xbeaf('0x28')]=_['keys'](_0x173f2e['query']);_0x571ee3['filters']=_[_0xbeaf('0x29')](_0x571ee3[_0xbeaf('0x26')],_0x571ee3['query']);_0x50799d['attributes']=_['intersection'](_0x571ee3[_0xbeaf('0x26')],qs[_0xbeaf('0x2a')](_0x173f2e['query'][_0xbeaf('0x2a')]));_0x50799d['attributes']=_0x50799d[_0xbeaf('0x2b')][_0xbeaf('0x2c')]?_0x50799d[_0xbeaf('0x2b')]:_0x571ee3[_0xbeaf('0x26')];if(_0x173f2e[_0xbeaf('0x28')][_0xbeaf('0x2d')]){_0x50799d['include']=[{'all':!![]}];}_0x50799d=_[_0xbeaf('0x2e')]({},_0x50799d,_0x173f2e['options']);return db[_0xbeaf('0x1b')][_0xbeaf('0x2f')](_0x50799d)[_0xbeaf('0x15')](handleEntityNotFound(_0x461e20,null))[_0xbeaf('0x15')](respondWithResult(_0x461e20,null))[_0xbeaf('0x23')](handleError(_0x461e20,null));};exports[_0xbeaf('0x30')]=function(_0x32d4b6,_0x1606cf){return db[_0xbeaf('0x1b')]['create'](_0x32d4b6[_0xbeaf('0x31')],{})[_0xbeaf('0x15')](respondWithResult(_0x1606cf,0xc9))['catch'](handleError(_0x1606cf,null));};exports[_0xbeaf('0x32')]=function(_0x38945d,_0x1efdfd){if(_0x38945d['body']['id']){delete _0x38945d['body']['id'];}return db[_0xbeaf('0x1b')][_0xbeaf('0x2f')]({'where':{'id':_0x38945d['params']['id']}})[_0xbeaf('0x15')](handleEntityNotFound(_0x1efdfd,null))['then'](saveUpdates(_0x38945d['body'],null))[_0xbeaf('0x15')](respondWithResult(_0x1efdfd,null))['catch'](handleError(_0x1efdfd,null));};exports[_0xbeaf('0x16')]=function(_0x1613d3,_0x3c9ae8){return db[_0xbeaf('0x1b')]['find']({'where':{'id':_0x1613d3[_0xbeaf('0x25')]['id']}})[_0xbeaf('0x15')](handleEntityNotFound(_0x3c9ae8,null))[_0xbeaf('0x15')](removeEntity(_0x3c9ae8,null))[_0xbeaf('0x23')](handleError(_0x3c9ae8,null));};exports[_0xbeaf('0x33')]=function(_0x5797c2,_0xfb20d2){return db[_0xbeaf('0x1b')][_0xbeaf('0x33')]()['then'](respondWithResult(_0xfb20d2,null))['catch'](handleError(_0xfb20d2,null));};exports['addMessage']=function(_0x44a423,_0x298bc4){return db[_0xbeaf('0x34')]['find']({'where':{'id':_0x44a423[_0xbeaf('0x25')]['id']}})[_0xbeaf('0x15')](handleEntityNotFound(_0x298bc4,null))[_0xbeaf('0x15')](function(_0x5e4878){if(_0x5e4878){return _0x5e4878[_0xbeaf('0x35')](_0x44a423[_0xbeaf('0x31')][_0xbeaf('0x36')],_[_0xbeaf('0x37')](_0x44a423['body'],[_0xbeaf('0x36'),'id'])||{});}})[_0xbeaf('0x15')](respondWithResult(_0x298bc4,null))[_0xbeaf('0x23')](handleError(_0x298bc4,null));};exports['getMessages']=function(_0x4bcd0e,_0x1a185b){var _0xfd3e04={'raw':![],'where':{}};var _0x58761c={};var _0x1201d1={'count':0x0,'rows':[]};return db['SmsInteraction']['findOne']({'where':{'id':_0x4bcd0e[_0xbeaf('0x25')]['id']}})[_0xbeaf('0x15')](handleEntityNotFound(_0x1a185b,null))[_0xbeaf('0x15')](function(_0x4df18f){if(_0x4df18f){_0x58761c[_0xbeaf('0x26')]=_[_0xbeaf('0x27')](db[_0xbeaf('0x34')][_0xbeaf('0x1c')]);_0x58761c[_0xbeaf('0x28')]=_[_0xbeaf('0x27')](_0x4bcd0e[_0xbeaf('0x28')]);_0x58761c[_0xbeaf('0x38')]=_[_0xbeaf('0x29')](_0x58761c['model'],_0x58761c[_0xbeaf('0x28')]);_0xfd3e04[_0xbeaf('0x2b')]=_['intersection'](_0x58761c[_0xbeaf('0x26')],qs[_0xbeaf('0x2a')](_0x4bcd0e[_0xbeaf('0x28')]['fields']));_0xfd3e04[_0xbeaf('0x2b')]=_0xfd3e04[_0xbeaf('0x2b')]['length']?_0xfd3e04[_0xbeaf('0x2b')]:_0x58761c['model'];if(!_0x4bcd0e[_0xbeaf('0x28')][_0xbeaf('0x39')]('nolimit')){_0xfd3e04['limit']=qs[_0xbeaf('0x10')](_0x4bcd0e[_0xbeaf('0x28')]['limit']);_0xfd3e04[_0xbeaf('0xf')]=qs[_0xbeaf('0xf')](_0x4bcd0e[_0xbeaf('0x28')]['offset']);}_0xfd3e04[_0xbeaf('0x3a')]=qs[_0xbeaf('0x3b')](_0x4bcd0e[_0xbeaf('0x28')][_0xbeaf('0x3b')]);_0xfd3e04[_0xbeaf('0x3c')]=qs[_0xbeaf('0x38')](_['pick'](_0x4bcd0e[_0xbeaf('0x28')],_0x58761c[_0xbeaf('0x38')]));_0xfd3e04[_0xbeaf('0x3c')][_0xbeaf('0x3d')]=_0x4df18f['id'];if(_0x4bcd0e[_0xbeaf('0x28')]['filter']){_0xfd3e04[_0xbeaf('0x3c')]=_[_0xbeaf('0x2e')](_0xfd3e04['where'],{'$or':_['map'](_0xfd3e04['attributes'],function(_0x3e3aae){var _0x121bb2={};_0x121bb2[_0x3e3aae]={'$like':'%'+_0x4bcd0e[_0xbeaf('0x28')][_0xbeaf('0x3e')]+'%'};return _0x121bb2;})});}if(_0x4bcd0e[_0xbeaf('0x28')][_0xbeaf('0x3f')]){var _0x49b205=_0x4bcd0e[_0xbeaf('0x28')]['$gte'][_0xbeaf('0x40')](',');var _0x314dd1={};_0x314dd1[_0x49b205[0x0]]={'$gte':moment(_0x49b205[0x1])[_0xbeaf('0x41')](_0xbeaf('0x42'))};_0xfd3e04[_0xbeaf('0x3c')]=_[_0xbeaf('0x2e')](_0xfd3e04['where'],_0x314dd1);}_0xfd3e04=_[_0xbeaf('0x2e')]({},_0xfd3e04,_0x4bcd0e['options']);return db[_0xbeaf('0x34')][_0xbeaf('0x12')]({'where':_0xfd3e04[_0xbeaf('0x3c')]})[_0xbeaf('0x15')](function(_0x22f884){_0x1201d1[_0xbeaf('0x12')]=_0x22f884;if(_0x4bcd0e[_0xbeaf('0x28')][_0xbeaf('0x2d')]){_0xfd3e04[_0xbeaf('0x43')]=[{'all':!![]}];}return db['SmsMessage'][_0xbeaf('0x20')](_0xfd3e04);})[_0xbeaf('0x15')](function(_0x3fd84c){_0x1201d1[_0xbeaf('0x44')]=_0x3fd84c;return _0x1201d1;});}})[_0xbeaf('0x15')](respondWithFilteredResult(_0x1a185b,_0xfd3e04))['catch'](handleError(_0x1a185b,null));};exports[_0xbeaf('0x45')]=function(_0xe7a229,_0x592d2b){return db[_0xbeaf('0x1b')][_0xbeaf('0x2f')]({'where':{'id':_0xe7a229[_0xbeaf('0x25')]['id']}})[_0xbeaf('0x15')](handleEntityNotFound(_0x592d2b,null))['then'](function(_0x583ede){if(_0x583ede){return _0x583ede[_0xbeaf('0x46')](_0xe7a229[_0xbeaf('0x31')]['ids'],_['omit'](_0xe7a229['body'],[_0xbeaf('0x36'),'id'])||{})[_0xbeaf('0x47')](function(){return db[_0xbeaf('0x48')][_0xbeaf('0x20')]({'attributes':['id','name','color'],'where':{'id':_0xe7a229[_0xbeaf('0x31')][_0xbeaf('0x36')]}});})[_0xbeaf('0x15')](function(_0x5c5d1c){socket[_0xbeaf('0x49')]('smsInteractionTags:save',{'id':Number(_0xe7a229[_0xbeaf('0x25')]['id']),'tags':_0x5c5d1c||[]});return{'id':Number(_0xe7a229[_0xbeaf('0x25')]['id']),'tags':_0x5c5d1c||[]};});}})[_0xbeaf('0x15')](respondWithResult(_0x592d2b,null))[_0xbeaf('0x23')](handleError(_0x592d2b,null));};exports[_0xbeaf('0x4a')]=function(_0x34516a,_0xf07a77){return db[_0xbeaf('0x1b')]['find']({'where':{'id':_0x34516a['params']['id']}})['then'](handleEntityNotFound(_0xf07a77,null))[_0xbeaf('0x15')](function(_0x1e7454){if(_0x1e7454){return _0x1e7454['removeTags'](_0x34516a['query'][_0xbeaf('0x36')]);}})[_0xbeaf('0x15')](respondWithStatusCode(_0xf07a77,null))['catch'](handleError(_0xf07a77,null));};exports['download']=function(_0x13ccc,_0x287b55){var _0x42f250=moment()[_0xbeaf('0x4b')]()[_0xbeaf('0x4c')]();var _0x39069a=path[_0xbeaf('0x4d')](config[_0xbeaf('0x4e')],'server',_0xbeaf('0x4f'),'tmp');var _0x411210=path[_0xbeaf('0x4d')](_0x39069a,_0x42f250);var _0x53067b=util[_0xbeaf('0x41')](_0xbeaf('0x50'),_0x13ccc[_0xbeaf('0x25')]['id'],_0x42f250);var _0x51d560=path['join'](_0x39069a,_0x53067b);var _0x417f03=[];_0x417f03[_0xbeaf('0x51')]({'model':db[_0xbeaf('0x52')],'as':'User','attributes':['id',_0xbeaf('0x53')],'raw':!![]});_0x417f03[_0xbeaf('0x51')]({'model':db['CmContact'],'as':'Contact','attributes':['id',_0xbeaf('0x54'),_0xbeaf('0x55')],'raw':!![]});var _0x7679fe=[{'model':db[_0xbeaf('0x34')],'as':_0xbeaf('0x56'),'attributes':['id',_0xbeaf('0x31'),_0xbeaf('0x57'),_0xbeaf('0x58'),_0xbeaf('0x59'),_0xbeaf('0x5a')],'include':_0x417f03}];_0x7679fe[_0xbeaf('0x51')]({'model':db[_0xbeaf('0x5b')],'as':_0xbeaf('0x5c'),'attributes':['id',_0xbeaf('0x17')]});_0x7679fe[_0xbeaf('0x51')]({'model':db[_0xbeaf('0x52')],'as':_0xbeaf('0x5d'),'attributes':['id',_0xbeaf('0x53')]});return db[_0xbeaf('0x1b')][_0xbeaf('0x2f')]({'where':{'id':_0x13ccc[_0xbeaf('0x25')]['id']},'include':_0x7679fe})[_0xbeaf('0x15')](handleEntityNotFound(_0x287b55,null))[_0xbeaf('0x15')](function(_0x5ea326){if(_0x5ea326){var _0x476ad7=_0x5ea326[_0xbeaf('0x5e')]({'plain':!![]});fs['mkdirSync'](_0x411210);var _0xdadbf2={'channel':_0xbeaf('0x5f'),'account':_0x476ad7[_0xbeaf('0x5c')]['name'],'agent':_0x476ad7[_0xbeaf('0x5d')]?_0x476ad7[_0xbeaf('0x5d')][_0xbeaf('0x53')]:_0xbeaf('0x60'),'createdAt':moment(_0x476ad7[_0xbeaf('0x57')])[_0xbeaf('0x41')](_0xbeaf('0x42'))[_0xbeaf('0x4c')](),'closedAt':_0x476ad7['closed']?moment(_0x476ad7['closedAt'])[_0xbeaf('0x41')](_0xbeaf('0x42'))[_0xbeaf('0x4c')]():'','disposition':_0x476ad7[_0xbeaf('0x61')],'messages':_(_0x476ad7['Messages'])[_0xbeaf('0x3e')]({'secret':![]})[_0xbeaf('0x1a')](function(_0x28f65d){return{'date':moment(_0x28f65d[_0xbeaf('0x57')])['format'](_0xbeaf('0x42'))['toString'](),'sender':_0x28f65d[_0xbeaf('0x58')]==='out'?_0x28f65d[_0xbeaf('0x52')]?_0x28f65d[_0xbeaf('0x52')][_0xbeaf('0x53')]:_0xbeaf('0x62'):_0x28f65d[_0xbeaf('0x63')]['firstName']+(_0x28f65d[_0xbeaf('0x63')]['lastName']?'\x20'+_0x28f65d['Contact'][_0xbeaf('0x55')]:''),'body':_0x28f65d[_0xbeaf('0x31')],'direction':_0x28f65d[_0xbeaf('0x58')]===_0xbeaf('0x64')?'A':'C','secret':_0x28f65d[_0xbeaf('0x59')],'read':_0x28f65d[_0xbeaf('0x5a')]};})[_0xbeaf('0x65')]()};return ejs[_0xbeaf('0x66')](path[_0xbeaf('0x4d')](config['root'],_0xbeaf('0x67'),_0xbeaf('0x68'),'downloadInteraction.ejs'),{'interaction':_0xdadbf2})[_0xbeaf('0x15')](function(_0x579c7a){var _0x1dae3a=path[_0xbeaf('0x4d')](_0x411210,_0xbeaf('0x69')+_0x476ad7['id']+'-'+_0x42f250+_0xbeaf('0x6a'));var _0x2f492f={'path':_0x1dae3a,'channel':_0xbeaf('0x5f'),'interactionId':_0x476ad7['id']};return pdf[_0xbeaf('0x6b')](_0x579c7a,_0x2f492f);})[_0xbeaf('0x15')](function(){return new BPromise(function(_0x39a921,_0x38d6ba){zipdir(_0x411210,{'saveTo':_0x51d560},function(_0xc64b5a,_0xbc30b3){if(_0xc64b5a)return _0x38d6ba(_0xc64b5a);return _0x39a921(_0xbc30b3);});})[_0xbeaf('0x15')](function(){return new BPromise(function(_0x2598c9,_0x337298){rimraf(_0x411210,function(_0x1b5ee8){if(_0x1b5ee8)_0x337298(_0x1b5ee8);return _0x2598c9();});});})[_0xbeaf('0x15')](function(){return _0x287b55['download'](_0x51d560,_0x53067b,function(_0x441a56){if(_0x441a56){console[_0xbeaf('0x6c')](_0xbeaf('0x6d'),_0x441a56);}else{fs['unlinkSync'](_0x51d560);}});});});}else{return _0x287b55['sendStatus'](0xc8);}})[_0xbeaf('0x23')](handleError(_0x287b55,null));};