ffb0dd882b4d05ad0e7299ab7c89fcbaf3fd1400
[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 _0xf03b=['YYYY-MM-DD\x20HH:mm:ss','findAll','rows','addTags','spread','Tag','name','color','emit','removeTags','download','toString','join','root','server','files','User','push','CmContact','Contact','firstName','lastName','Messages','direction','secret','read','SmsAccount','Owner','fullname','get','mkdirSync','Sms','Account','createdAt','closed','closedAt','out','value','renderFile','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','log','unlinkSync','rimraf','zip-dir','moment','bluebird','path','ejs','lodash','ioredis','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','socket.io-emitter','./smsInteraction.socket','register','sendStatus','status','end','undefined','count','offset','limit','set','Content-Range','update','then','destroy','stack','send','index','map','rawAttributes','fieldName','type','key','getOptions','query','user','catch','show','model','keys','SmsInteraction','filters','intersection','fields','length','includeAll','include','merge','options','create','body','find','describe','addMessage','SmsMessage','params','ids','omit','getMessages','findOne','attributes','hasOwnProperty','nolimit','order','sort','where','pick','SmsInteractionId','filter','$gte','format'];(function(_0x36b037,_0x2b234e){var _0x409194=function(_0xddc9dc){while(--_0xddc9dc){_0x36b037['push'](_0x36b037['shift']());}};_0x409194(++_0x2b234e);}(_0xf03b,0x19f));var _0xbf03=function(_0xf36a0d,_0x3b603f){_0xf36a0d=_0xf36a0d-0x0;var _0x2da0e7=_0xf03b[_0xf36a0d];return _0x2da0e7;};'use strict';var rimraf=require(_0xbf03('0x0'));var zipdir=require(_0xbf03('0x1'));var moment=require(_0xbf03('0x2'));var BPromise=require(_0xbf03('0x3'));var util=require('util');var path=require(_0xbf03('0x4'));var ejs=require(_0xbf03('0x5'));var fs=require('fs');var _=require(_0xbf03('0x6'));var Redis=require(_0xbf03('0x7'));var interaction=require('../../components/interaction/service');var qs=require(_0xbf03('0x8'));var pdf=require(_0xbf03('0x9'));var logger=require(_0xbf03('0xa'))(_0xbf03('0xb'));var config=require(_0xbf03('0xc'));var db=require(_0xbf03('0xd'))['db'];config[_0xbf03('0xe')]=_[_0xbf03('0xf')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0xbf03('0x10'))(new Redis(config[_0xbf03('0xe')]));require(_0xbf03('0x11'))[_0xbf03('0x12')](socket);function respondWithStatusCode(_0x5136b6,_0x384ed0){_0x384ed0=_0x384ed0||0xcc;return function(_0x5461d0){if(_0x5461d0){return _0x5136b6[_0xbf03('0x13')](_0x384ed0);}return _0x5136b6[_0xbf03('0x14')](_0x384ed0)[_0xbf03('0x15')]();};}function respondWithResult(_0xe5a032,_0xdf276e){_0xdf276e=_0xdf276e||0xc8;return function(_0xb01e9e){if(_0xb01e9e){return _0xe5a032[_0xbf03('0x14')](_0xdf276e)['json'](_0xb01e9e);}};}function respondWithFilteredResult(_0x1f7879,_0x3e36a9){return function(_0x4af423){if(_0x4af423){var _0x47479b=typeof _0x3e36a9['offset']==='undefined'&&typeof _0x3e36a9['limit']===_0xbf03('0x16');var _0x995ed=_0x4af423[_0xbf03('0x17')];var _0x29e486=_0x47479b?0x0:_0x3e36a9[_0xbf03('0x18')];var _0x3c072d=_0x47479b?_0x4af423[_0xbf03('0x17')]:_0x3e36a9[_0xbf03('0x18')]+_0x3e36a9[_0xbf03('0x19')];var _0x1a5a6d;if(_0x3c072d>=_0x995ed){_0x3c072d=_0x995ed;_0x1a5a6d=0xc8;}else{_0x1a5a6d=0xce;}_0x1f7879['status'](_0x1a5a6d);return _0x1f7879[_0xbf03('0x1a')](_0xbf03('0x1b'),_0x29e486+'-'+_0x3c072d+'/'+_0x995ed)['json'](_0x4af423);}return null;};}function saveUpdates(_0x4a96bb){return function(_0x585e31){if(_0x585e31){return _0x585e31[_0xbf03('0x1c')](_0x4a96bb)[_0xbf03('0x1d')](function(_0xc4260a){return _0xc4260a;});}return null;};}function removeEntity(_0x186b5a){return function(_0x238653){if(_0x238653){return _0x238653[_0xbf03('0x1e')]()['then'](function(){_0x186b5a[_0xbf03('0x14')](0xcc)[_0xbf03('0x15')]();});}};}function handleEntityNotFound(_0x473808){return function(_0x4eb2ed){if(!_0x4eb2ed){_0x473808[_0xbf03('0x13')](0x194);}return _0x4eb2ed;};}function handleError(_0x12bc3b,_0x3f3f41){_0x3f3f41=_0x3f3f41||0x1f4;return function(_0x22f0ec){logger['error'](_0x22f0ec[_0xbf03('0x1f')]);if(_0x22f0ec['name']){delete _0x22f0ec['name'];}_0x12bc3b[_0xbf03('0x14')](_0x3f3f41)[_0xbf03('0x20')](_0x22f0ec);};}exports[_0xbf03('0x21')]=function(_0xc68bdf,_0x49b60d){var _0x5975fc={};var _0x50b236=_[_0xbf03('0x22')](db['SmsInteraction'][_0xbf03('0x23')],function(_0x265bb4){return{'name':_0x265bb4[_0xbf03('0x24')],'type':_0x265bb4[_0xbf03('0x25')][_0xbf03('0x26')]};});_0x5975fc=qs[_0xbf03('0x27')](_0x50b236,_0xc68bdf);return interaction['findAll']('sms',_0xc68bdf[_0xbf03('0x28')],_0xc68bdf[_0xbf03('0x29')],_0x5975fc,_0x50b236)[_0xbf03('0x1d')](respondWithFilteredResult(_0x49b60d,_0x5975fc))[_0xbf03('0x2a')](handleError(_0x49b60d,null));};exports[_0xbf03('0x2b')]=function(_0x2a0942,_0x39c70f){var _0x2159a1={'raw':![],'where':{'id':_0x2a0942['params']['id']}},_0x549bf3={};_0x549bf3[_0xbf03('0x2c')]=_[_0xbf03('0x2d')](db[_0xbf03('0x2e')][_0xbf03('0x23')]);_0x549bf3['query']=_[_0xbf03('0x2d')](_0x2a0942['query']);_0x549bf3[_0xbf03('0x2f')]=_[_0xbf03('0x30')](_0x549bf3[_0xbf03('0x2c')],_0x549bf3[_0xbf03('0x28')]);_0x2159a1['attributes']=_[_0xbf03('0x30')](_0x549bf3[_0xbf03('0x2c')],qs['fields'](_0x2a0942[_0xbf03('0x28')][_0xbf03('0x31')]));_0x2159a1['attributes']=_0x2159a1['attributes'][_0xbf03('0x32')]?_0x2159a1['attributes']:_0x549bf3['model'];if(_0x2a0942[_0xbf03('0x28')][_0xbf03('0x33')]){_0x2159a1[_0xbf03('0x34')]=[{'all':!![]}];}_0x2159a1=_[_0xbf03('0x35')]({},_0x2159a1,_0x2a0942[_0xbf03('0x36')]);return db[_0xbf03('0x2e')]['find'](_0x2159a1)[_0xbf03('0x1d')](handleEntityNotFound(_0x39c70f,null))[_0xbf03('0x1d')](respondWithResult(_0x39c70f,null))[_0xbf03('0x2a')](handleError(_0x39c70f,null));};exports[_0xbf03('0x37')]=function(_0x2ce051,_0x2a2d15){return db[_0xbf03('0x2e')][_0xbf03('0x37')](_0x2ce051[_0xbf03('0x38')],{})['then'](respondWithResult(_0x2a2d15,0xc9))['catch'](handleError(_0x2a2d15,null));};exports['update']=function(_0x135f94,_0x4ae930){if(_0x135f94[_0xbf03('0x38')]['id']){delete _0x135f94['body']['id'];}return db['SmsInteraction'][_0xbf03('0x39')]({'where':{'id':_0x135f94['params']['id']}})['then'](handleEntityNotFound(_0x4ae930,null))['then'](saveUpdates(_0x135f94[_0xbf03('0x38')],null))[_0xbf03('0x1d')](respondWithResult(_0x4ae930,null))[_0xbf03('0x2a')](handleError(_0x4ae930,null));};exports['destroy']=function(_0x47cb3d,_0x4b601a){return db[_0xbf03('0x2e')]['find']({'where':{'id':_0x47cb3d['params']['id']}})[_0xbf03('0x1d')](handleEntityNotFound(_0x4b601a,null))['then'](removeEntity(_0x4b601a,null))[_0xbf03('0x2a')](handleError(_0x4b601a,null));};exports['describe']=function(_0x9c0dd3,_0x2fd76d){return db[_0xbf03('0x2e')][_0xbf03('0x3a')]()[_0xbf03('0x1d')](respondWithResult(_0x2fd76d,null))[_0xbf03('0x2a')](handleError(_0x2fd76d,null));};exports[_0xbf03('0x3b')]=function(_0xda416,_0x227c33){return db[_0xbf03('0x3c')]['find']({'where':{'id':_0xda416[_0xbf03('0x3d')]['id']}})[_0xbf03('0x1d')](handleEntityNotFound(_0x227c33,null))[_0xbf03('0x1d')](function(_0x37ca5c){if(_0x37ca5c){return _0x37ca5c[_0xbf03('0x3b')](_0xda416[_0xbf03('0x38')][_0xbf03('0x3e')],_[_0xbf03('0x3f')](_0xda416[_0xbf03('0x38')],[_0xbf03('0x3e'),'id'])||{});}})[_0xbf03('0x1d')](respondWithResult(_0x227c33,null))[_0xbf03('0x2a')](handleError(_0x227c33,null));};exports[_0xbf03('0x40')]=function(_0x186c3e,_0xe96ef7){var _0x27f98f={'raw':![],'where':{}};var _0x373755={};var _0x5ba356={'count':0x0,'rows':[]};return db[_0xbf03('0x2e')][_0xbf03('0x41')]({'where':{'id':_0x186c3e['params']['id']}})[_0xbf03('0x1d')](handleEntityNotFound(_0xe96ef7,null))[_0xbf03('0x1d')](function(_0x45ef92){if(_0x45ef92){_0x373755[_0xbf03('0x2c')]=_[_0xbf03('0x2d')](db[_0xbf03('0x3c')][_0xbf03('0x23')]);_0x373755[_0xbf03('0x28')]=_[_0xbf03('0x2d')](_0x186c3e[_0xbf03('0x28')]);_0x373755[_0xbf03('0x2f')]=_[_0xbf03('0x30')](_0x373755[_0xbf03('0x2c')],_0x373755[_0xbf03('0x28')]);_0x27f98f['attributes']=_[_0xbf03('0x30')](_0x373755[_0xbf03('0x2c')],qs['fields'](_0x186c3e['query'][_0xbf03('0x31')]));_0x27f98f[_0xbf03('0x42')]=_0x27f98f[_0xbf03('0x42')]['length']?_0x27f98f[_0xbf03('0x42')]:_0x373755[_0xbf03('0x2c')];if(!_0x186c3e[_0xbf03('0x28')][_0xbf03('0x43')](_0xbf03('0x44'))){_0x27f98f[_0xbf03('0x19')]=qs[_0xbf03('0x19')](_0x186c3e[_0xbf03('0x28')][_0xbf03('0x19')]);_0x27f98f[_0xbf03('0x18')]=qs[_0xbf03('0x18')](_0x186c3e['query']['offset']);}_0x27f98f[_0xbf03('0x45')]=qs[_0xbf03('0x46')](_0x186c3e[_0xbf03('0x28')][_0xbf03('0x46')]);_0x27f98f[_0xbf03('0x47')]=qs[_0xbf03('0x2f')](_[_0xbf03('0x48')](_0x186c3e[_0xbf03('0x28')],_0x373755[_0xbf03('0x2f')]));_0x27f98f[_0xbf03('0x47')][_0xbf03('0x49')]=_0x45ef92['id'];if(_0x186c3e[_0xbf03('0x28')][_0xbf03('0x4a')]){_0x27f98f[_0xbf03('0x47')]=_[_0xbf03('0x35')](_0x27f98f[_0xbf03('0x47')],{'$or':_['map'](_0x27f98f['attributes'],function(_0x514186){var _0x198870={};_0x198870[_0x514186]={'$like':'%'+_0x186c3e[_0xbf03('0x28')][_0xbf03('0x4a')]+'%'};return _0x198870;})});}if(_0x186c3e[_0xbf03('0x28')][_0xbf03('0x4b')]){var _0x24b21b=_0x186c3e['query'][_0xbf03('0x4b')]['split'](',');var _0x512aac={};_0x512aac[_0x24b21b[0x0]]={'$gte':moment(_0x24b21b[0x1])[_0xbf03('0x4c')](_0xbf03('0x4d'))};_0x27f98f[_0xbf03('0x47')]=_[_0xbf03('0x35')](_0x27f98f[_0xbf03('0x47')],_0x512aac);}_0x27f98f=_['merge']({},_0x27f98f,_0x186c3e[_0xbf03('0x36')]);return db[_0xbf03('0x3c')][_0xbf03('0x17')]({'where':_0x27f98f[_0xbf03('0x47')]})[_0xbf03('0x1d')](function(_0x5c43ec){_0x5ba356[_0xbf03('0x17')]=_0x5c43ec;if(_0x186c3e['query']['includeAll']){_0x27f98f['include']=[{'all':!![]}];}return db['SmsMessage'][_0xbf03('0x4e')](_0x27f98f);})[_0xbf03('0x1d')](function(_0x1ac8a7){_0x5ba356[_0xbf03('0x4f')]=_0x1ac8a7;return _0x5ba356;});}})[_0xbf03('0x1d')](respondWithFilteredResult(_0xe96ef7,_0x27f98f))['catch'](handleError(_0xe96ef7,null));};exports[_0xbf03('0x50')]=function(_0xbe004e,_0x3874d7){return db[_0xbf03('0x2e')][_0xbf03('0x39')]({'where':{'id':_0xbe004e[_0xbf03('0x3d')]['id']}})[_0xbf03('0x1d')](handleEntityNotFound(_0x3874d7,null))['then'](function(_0x2ed964){if(_0x2ed964){return _0x2ed964['setTags'](_0xbe004e[_0xbf03('0x38')][_0xbf03('0x3e')],_[_0xbf03('0x3f')](_0xbe004e[_0xbf03('0x38')],[_0xbf03('0x3e'),'id'])||{})[_0xbf03('0x51')](function(){return db[_0xbf03('0x52')][_0xbf03('0x4e')]({'attributes':['id',_0xbf03('0x53'),_0xbf03('0x54')],'where':{'id':_0xbe004e[_0xbf03('0x38')][_0xbf03('0x3e')]}});})['then'](function(_0x1db0b9){socket[_0xbf03('0x55')]('smsInteractionTags:save',{'id':Number(_0xbe004e[_0xbf03('0x3d')]['id']),'tags':_0x1db0b9||[]});return{'id':Number(_0xbe004e[_0xbf03('0x3d')]['id']),'tags':_0x1db0b9||[]};});}})[_0xbf03('0x1d')](respondWithResult(_0x3874d7,null))[_0xbf03('0x2a')](handleError(_0x3874d7,null));};exports[_0xbf03('0x56')]=function(_0x2bc4fd,_0x825825){return db[_0xbf03('0x2e')]['find']({'where':{'id':_0x2bc4fd['params']['id']}})[_0xbf03('0x1d')](handleEntityNotFound(_0x825825,null))[_0xbf03('0x1d')](function(_0x1dbcd9){if(_0x1dbcd9){return _0x1dbcd9[_0xbf03('0x56')](_0x2bc4fd['query'][_0xbf03('0x3e')]);}})[_0xbf03('0x1d')](respondWithStatusCode(_0x825825,null))[_0xbf03('0x2a')](handleError(_0x825825,null));};exports[_0xbf03('0x57')]=function(_0x428bea,_0x23566d){var _0x5b353f=moment()['unix']()[_0xbf03('0x58')]();var _0x173f9e=path[_0xbf03('0x59')](config[_0xbf03('0x5a')],_0xbf03('0x5b'),_0xbf03('0x5c'),'tmp');var _0x2212f6=path['join'](_0x173f9e,_0x5b353f);var _0x13ab05=util['format']('transcript-%d-%s.zip',_0x428bea[_0xbf03('0x3d')]['id'],_0x5b353f);var _0x3e602e=path[_0xbf03('0x59')](_0x173f9e,_0x13ab05);var _0x3a6a53=[];_0x3a6a53['push']({'model':db[_0xbf03('0x5d')],'as':'User','attributes':['id','fullname'],'raw':!![]});_0x3a6a53[_0xbf03('0x5e')]({'model':db[_0xbf03('0x5f')],'as':_0xbf03('0x60'),'attributes':['id',_0xbf03('0x61'),_0xbf03('0x62')],'raw':!![]});var _0xb35ae7=[{'model':db[_0xbf03('0x3c')],'as':_0xbf03('0x63'),'attributes':['id',_0xbf03('0x38'),'createdAt',_0xbf03('0x64'),_0xbf03('0x65'),_0xbf03('0x66')],'include':_0x3a6a53}];_0xb35ae7['push']({'model':db[_0xbf03('0x67')],'as':'Account','attributes':['id',_0xbf03('0x53')]});_0xb35ae7[_0xbf03('0x5e')]({'model':db[_0xbf03('0x5d')],'as':_0xbf03('0x68'),'attributes':['id',_0xbf03('0x69')]});return db[_0xbf03('0x2e')][_0xbf03('0x39')]({'where':{'id':_0x428bea[_0xbf03('0x3d')]['id']},'include':_0xb35ae7})[_0xbf03('0x1d')](handleEntityNotFound(_0x23566d,null))[_0xbf03('0x1d')](function(_0x2cd1ec){if(_0x2cd1ec){var _0x311d81=_0x2cd1ec[_0xbf03('0x6a')]({'plain':!![]});fs[_0xbf03('0x6b')](_0x2212f6);var _0x2e4f7f={'channel':_0xbf03('0x6c'),'account':_0x311d81[_0xbf03('0x6d')][_0xbf03('0x53')],'agent':_0x311d81[_0xbf03('0x68')]?_0x311d81['Owner']['fullname']:'N.A.','createdAt':moment(_0x311d81[_0xbf03('0x6e')])['format'](_0xbf03('0x4d'))[_0xbf03('0x58')](),'closedAt':_0x311d81[_0xbf03('0x6f')]?moment(_0x311d81[_0xbf03('0x70')])['format'](_0xbf03('0x4d'))[_0xbf03('0x58')]():'','disposition':_0x311d81['disposition'],'messages':_(_0x311d81[_0xbf03('0x63')])[_0xbf03('0x4a')]({'secret':![]})[_0xbf03('0x22')](function(_0xcd9708){return{'date':moment(_0xcd9708[_0xbf03('0x6e')])[_0xbf03('0x4c')]('YYYY-MM-DD\x20HH:mm:ss')[_0xbf03('0x58')](),'sender':_0xcd9708['direction']==='out'?_0xcd9708[_0xbf03('0x5d')]?_0xcd9708[_0xbf03('0x5d')][_0xbf03('0x69')]:'System':_0xcd9708[_0xbf03('0x60')]['firstName']+(_0xcd9708[_0xbf03('0x60')][_0xbf03('0x62')]?'\x20'+_0xcd9708[_0xbf03('0x60')][_0xbf03('0x62')]:''),'body':_0xcd9708['body'],'direction':_0xcd9708[_0xbf03('0x64')]===_0xbf03('0x71')?'A':'C','secret':_0xcd9708[_0xbf03('0x65')],'read':_0xcd9708[_0xbf03('0x66')]};})[_0xbf03('0x72')]()};return ejs[_0xbf03('0x73')](path[_0xbf03('0x59')](config[_0xbf03('0x5a')],_0xbf03('0x5b'),_0xbf03('0x74'),_0xbf03('0x75')),{'interaction':_0x2e4f7f})[_0xbf03('0x1d')](function(_0x528dbf){var _0x1b7e9b=path[_0xbf03('0x59')](_0x2212f6,_0xbf03('0x76')+_0x311d81['id']+'-'+_0x5b353f+_0xbf03('0x77'));var _0xa19833={'path':_0x1b7e9b,'channel':_0xbf03('0x6c'),'interactionId':_0x311d81['id']};return pdf[_0xbf03('0x78')](_0x528dbf,_0xa19833);})['then'](function(){return new BPromise(function(_0x403775,_0x4f65ed){zipdir(_0x2212f6,{'saveTo':_0x3e602e},function(_0x5ace83,_0x5ad59e){if(_0x5ace83)return _0x4f65ed(_0x5ace83);return _0x403775(_0x5ad59e);});})[_0xbf03('0x1d')](function(){return new BPromise(function(_0x1fd9a0,_0x45669d){rimraf(_0x2212f6,function(_0x373cec){if(_0x373cec)_0x45669d(_0x373cec);return _0x1fd9a0();});});})['then'](function(){return _0x23566d['download'](_0x3e602e,_0x13ab05,function(_0x12ab67){if(_0x12ab67){console[_0xbf03('0x79')]('err',_0x12ab67);}else{fs[_0xbf03('0x7a')](_0x3e602e);}});});});}else{return _0x23566d[_0xbf03('0x13')](0xc8);}})[_0xbf03('0x2a')](handleError(_0x23566d,null));};