Built motion from commit c1aa34a0.|2.6.28
[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 _0x10f3=['SmsMessage','addMessage','ids','omit','getMessages','findOne','hasOwnProperty','nolimit','order','sort','filter','$gte','format','YYYY-MM-DD\x20HH:mm:ss','where','count','rows','addTags','setTags','spread','Tag','color','removeTags','download','unix','toString','root','files','tmp','join','User','fullname','firstName','lastName','Messages','direction','secret','read','SmsAccount','Account','push','Owner','get','Sms','N.A.','createdAt','closedAt','disposition','System','Contact','out','value','renderFile','server','views','transcript-','.pdf','err','unlinkSync','rimraf','zip-dir','moment','bluebird','util','lodash','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','../../config/environment','../../mysqldb','defaults','redis','localhost','socket.io-emitter','./smsInteraction.socket','register','sendStatus','status','end','json','undefined','limit','offset','Content-Range','then','error','name','index','map','SmsInteraction','rawAttributes','fieldName','type','getOptions','findAll','sms','user','catch','show','params','keys','query','filters','model','attributes','intersection','fields','length','includeAll','merge','options','find','create','body','update','destroy','describe'];(function(_0x3b86df,_0x4111f1){var _0x21a1c5=function(_0x592dc5){while(--_0x592dc5){_0x3b86df['push'](_0x3b86df['shift']());}};_0x21a1c5(++_0x4111f1);}(_0x10f3,0xb1));var _0x310f=function(_0x433fed,_0x33e872){_0x433fed=_0x433fed-0x0;var _0x435cab=_0x10f3[_0x433fed];return _0x435cab;};'use strict';var rimraf=require(_0x310f('0x0'));var zipdir=require(_0x310f('0x1'));var moment=require(_0x310f('0x2'));var BPromise=require(_0x310f('0x3'));var util=require(_0x310f('0x4'));var path=require('path');var ejs=require('ejs');var fs=require('fs');var _=require(_0x310f('0x5'));var Redis=require('ioredis');var interaction=require(_0x310f('0x6'));var qs=require(_0x310f('0x7'));var pdf=require(_0x310f('0x8'));var logger=require(_0x310f('0x9'))('api');var config=require(_0x310f('0xa'));var db=require(_0x310f('0xb'))['db'];config['redis']=_[_0x310f('0xc')](config[_0x310f('0xd')],{'host':_0x310f('0xe'),'port':0x18eb});var socket=require(_0x310f('0xf'))(new Redis(config['redis']));require(_0x310f('0x10'))[_0x310f('0x11')](socket);function respondWithStatusCode(_0x4f3f14,_0x1235f1){_0x1235f1=_0x1235f1||0xcc;return function(_0x20b162){if(_0x20b162){return _0x4f3f14[_0x310f('0x12')](_0x1235f1);}return _0x4f3f14[_0x310f('0x13')](_0x1235f1)[_0x310f('0x14')]();};}function respondWithResult(_0x47fd2d,_0x1582d9){_0x1582d9=_0x1582d9||0xc8;return function(_0x2c328c){if(_0x2c328c){return _0x47fd2d[_0x310f('0x13')](_0x1582d9)[_0x310f('0x15')](_0x2c328c);}};}function respondWithFilteredResult(_0x4701e5,_0x554d42){return function(_0x1b4d2f){if(_0x1b4d2f){var _0x15291e=typeof _0x554d42['offset']===_0x310f('0x16')&&typeof _0x554d42[_0x310f('0x17')]==='undefined';var _0x2ba381=_0x1b4d2f['count'];var _0x51a4a2=_0x15291e?0x0:_0x554d42['offset'];var _0x145011=_0x15291e?_0x1b4d2f['count']:_0x554d42[_0x310f('0x18')]+_0x554d42['limit'];var _0x2927cf;if(_0x145011>=_0x2ba381){_0x145011=_0x2ba381;_0x2927cf=0xc8;}else{_0x2927cf=0xce;}_0x4701e5[_0x310f('0x13')](_0x2927cf);return _0x4701e5['set'](_0x310f('0x19'),_0x51a4a2+'-'+_0x145011+'/'+_0x2ba381)[_0x310f('0x15')](_0x1b4d2f);}return null;};}function saveUpdates(_0xdeabb5){return function(_0x44f395){if(_0x44f395){return _0x44f395['update'](_0xdeabb5)['then'](function(_0x4dbb9b){return _0x4dbb9b;});}return null;};}function removeEntity(_0x24f655){return function(_0x4491db){if(_0x4491db){return _0x4491db['destroy']()[_0x310f('0x1a')](function(){_0x24f655[_0x310f('0x13')](0xcc)[_0x310f('0x14')]();});}};}function handleEntityNotFound(_0xe7cf4a){return function(_0x52db69){if(!_0x52db69){_0xe7cf4a[_0x310f('0x12')](0x194);}return _0x52db69;};}function handleError(_0x240149,_0x10fadd){_0x10fadd=_0x10fadd||0x1f4;return function(_0x1170e5){logger[_0x310f('0x1b')](_0x1170e5['stack']);if(_0x1170e5[_0x310f('0x1c')]){delete _0x1170e5['name'];}_0x240149['status'](_0x10fadd)['send'](_0x1170e5);};}exports[_0x310f('0x1d')]=function(_0x224131,_0x6b5168){var _0x4ee1e7={};var _0x21c232=_[_0x310f('0x1e')](db[_0x310f('0x1f')][_0x310f('0x20')],function(_0xd721a4){return{'name':_0xd721a4[_0x310f('0x21')],'type':_0xd721a4[_0x310f('0x22')]['key']};});_0x4ee1e7=qs[_0x310f('0x23')](_0x21c232,_0x224131);return interaction[_0x310f('0x24')](_0x310f('0x25'),_0x224131['query'],_0x224131[_0x310f('0x26')],_0x4ee1e7,_0x21c232)['then'](respondWithFilteredResult(_0x6b5168,_0x4ee1e7))[_0x310f('0x27')](handleError(_0x6b5168,null));};exports[_0x310f('0x28')]=function(_0x864b1d,_0x1e2dc1){var _0x2a6d50={'raw':![],'where':{'id':_0x864b1d[_0x310f('0x29')]['id']}},_0x1fc920={};_0x1fc920['model']=_[_0x310f('0x2a')](db[_0x310f('0x1f')][_0x310f('0x20')]);_0x1fc920[_0x310f('0x2b')]=_['keys'](_0x864b1d['query']);_0x1fc920[_0x310f('0x2c')]=_['intersection'](_0x1fc920[_0x310f('0x2d')],_0x1fc920['query']);_0x2a6d50[_0x310f('0x2e')]=_[_0x310f('0x2f')](_0x1fc920[_0x310f('0x2d')],qs['fields'](_0x864b1d[_0x310f('0x2b')][_0x310f('0x30')]));_0x2a6d50[_0x310f('0x2e')]=_0x2a6d50[_0x310f('0x2e')][_0x310f('0x31')]?_0x2a6d50[_0x310f('0x2e')]:_0x1fc920[_0x310f('0x2d')];if(_0x864b1d['query'][_0x310f('0x32')]){_0x2a6d50['include']=[{'all':!![]}];}_0x2a6d50=_[_0x310f('0x33')]({},_0x2a6d50,_0x864b1d[_0x310f('0x34')]);return db[_0x310f('0x1f')][_0x310f('0x35')](_0x2a6d50)[_0x310f('0x1a')](handleEntityNotFound(_0x1e2dc1,null))['then'](respondWithResult(_0x1e2dc1,null))[_0x310f('0x27')](handleError(_0x1e2dc1,null));};exports[_0x310f('0x36')]=function(_0x48b9c7,_0x1d5509){return db[_0x310f('0x1f')][_0x310f('0x36')](_0x48b9c7[_0x310f('0x37')],{})[_0x310f('0x1a')](respondWithResult(_0x1d5509,0xc9))[_0x310f('0x27')](handleError(_0x1d5509,null));};exports[_0x310f('0x38')]=function(_0x7beb80,_0x40f3d7){if(_0x7beb80[_0x310f('0x37')]['id']){delete _0x7beb80[_0x310f('0x37')]['id'];}return db[_0x310f('0x1f')][_0x310f('0x35')]({'where':{'id':_0x7beb80['params']['id']}})[_0x310f('0x1a')](handleEntityNotFound(_0x40f3d7,null))[_0x310f('0x1a')](saveUpdates(_0x7beb80[_0x310f('0x37')],null))[_0x310f('0x1a')](respondWithResult(_0x40f3d7,null))[_0x310f('0x27')](handleError(_0x40f3d7,null));};exports[_0x310f('0x39')]=function(_0x45688c,_0x45b1c1){return db[_0x310f('0x1f')]['find']({'where':{'id':_0x45688c['params']['id']}})['then'](handleEntityNotFound(_0x45b1c1,null))[_0x310f('0x1a')](removeEntity(_0x45b1c1,null))['catch'](handleError(_0x45b1c1,null));};exports[_0x310f('0x3a')]=function(_0x506c0d,_0x4d11a0){return db[_0x310f('0x1f')][_0x310f('0x3a')]()['then'](respondWithResult(_0x4d11a0,null))[_0x310f('0x27')](handleError(_0x4d11a0,null));};exports['addMessage']=function(_0x2ed3af,_0x2d772d){return db[_0x310f('0x3b')][_0x310f('0x35')]({'where':{'id':_0x2ed3af[_0x310f('0x29')]['id']}})['then'](handleEntityNotFound(_0x2d772d,null))['then'](function(_0x43867c){if(_0x43867c){return _0x43867c[_0x310f('0x3c')](_0x2ed3af[_0x310f('0x37')][_0x310f('0x3d')],_[_0x310f('0x3e')](_0x2ed3af['body'],['ids','id'])||{});}})[_0x310f('0x1a')](respondWithResult(_0x2d772d,null))['catch'](handleError(_0x2d772d,null));};exports[_0x310f('0x3f')]=function(_0x184b10,_0x5979f5){var _0x53200b={'raw':![],'where':{}};var _0x2ac2ff={};var _0x599fe3={'count':0x0,'rows':[]};return db[_0x310f('0x1f')][_0x310f('0x40')]({'where':{'id':_0x184b10[_0x310f('0x29')]['id']}})[_0x310f('0x1a')](handleEntityNotFound(_0x5979f5,null))[_0x310f('0x1a')](function(_0x548c5d){if(_0x548c5d){_0x2ac2ff['model']=_['keys'](db['SmsMessage'][_0x310f('0x20')]);_0x2ac2ff[_0x310f('0x2b')]=_[_0x310f('0x2a')](_0x184b10[_0x310f('0x2b')]);_0x2ac2ff[_0x310f('0x2c')]=_[_0x310f('0x2f')](_0x2ac2ff['model'],_0x2ac2ff[_0x310f('0x2b')]);_0x53200b[_0x310f('0x2e')]=_[_0x310f('0x2f')](_0x2ac2ff['model'],qs[_0x310f('0x30')](_0x184b10[_0x310f('0x2b')][_0x310f('0x30')]));_0x53200b[_0x310f('0x2e')]=_0x53200b[_0x310f('0x2e')]['length']?_0x53200b[_0x310f('0x2e')]:_0x2ac2ff[_0x310f('0x2d')];if(!_0x184b10[_0x310f('0x2b')][_0x310f('0x41')](_0x310f('0x42'))){_0x53200b['limit']=qs[_0x310f('0x17')](_0x184b10[_0x310f('0x2b')][_0x310f('0x17')]);_0x53200b[_0x310f('0x18')]=qs[_0x310f('0x18')](_0x184b10[_0x310f('0x2b')][_0x310f('0x18')]);}_0x53200b[_0x310f('0x43')]=qs['sort'](_0x184b10[_0x310f('0x2b')][_0x310f('0x44')]);_0x53200b['where']=qs[_0x310f('0x2c')](_['pick'](_0x184b10['query'],_0x2ac2ff['filters']));_0x53200b['where']['SmsInteractionId']=_0x548c5d['id'];if(_0x184b10[_0x310f('0x2b')][_0x310f('0x45')]){_0x53200b['where']=_[_0x310f('0x33')](_0x53200b['where'],{'$or':_[_0x310f('0x1e')](_0x53200b[_0x310f('0x2e')],function(_0x8daaef){var _0x3502e9={};_0x3502e9[_0x8daaef]={'$like':'%'+_0x184b10[_0x310f('0x2b')][_0x310f('0x45')]+'%'};return _0x3502e9;})});}if(_0x184b10[_0x310f('0x2b')]['$gte']){var _0x5d1ac7=_0x184b10['query'][_0x310f('0x46')]['split'](',');var _0x1f996e={};_0x1f996e[_0x5d1ac7[0x0]]={'$gte':moment(_0x5d1ac7[0x1])[_0x310f('0x47')](_0x310f('0x48'))};_0x53200b['where']=_[_0x310f('0x33')](_0x53200b[_0x310f('0x49')],_0x1f996e);}_0x53200b=_[_0x310f('0x33')]({},_0x53200b,_0x184b10['options']);return db[_0x310f('0x3b')][_0x310f('0x4a')]({'where':_0x53200b['where']})[_0x310f('0x1a')](function(_0x53fc0b){_0x599fe3[_0x310f('0x4a')]=_0x53fc0b;if(_0x184b10['query']['includeAll']){_0x53200b['include']=[{'all':!![]}];}return db[_0x310f('0x3b')]['findAll'](_0x53200b);})[_0x310f('0x1a')](function(_0x2d50ab){_0x599fe3[_0x310f('0x4b')]=_0x2d50ab;return _0x599fe3;});}})[_0x310f('0x1a')](respondWithFilteredResult(_0x5979f5,_0x53200b))[_0x310f('0x27')](handleError(_0x5979f5,null));};exports[_0x310f('0x4c')]=function(_0x5a1280,_0x3226b9){return db['SmsInteraction'][_0x310f('0x35')]({'where':{'id':_0x5a1280['params']['id']}})[_0x310f('0x1a')](handleEntityNotFound(_0x3226b9,null))['then'](function(_0x578512){if(_0x578512){return _0x578512[_0x310f('0x4d')](_0x5a1280[_0x310f('0x37')][_0x310f('0x3d')],_['omit'](_0x5a1280[_0x310f('0x37')],[_0x310f('0x3d'),'id'])||{})[_0x310f('0x4e')](function(){return db[_0x310f('0x4f')][_0x310f('0x24')]({'attributes':['id',_0x310f('0x1c'),_0x310f('0x50')],'where':{'id':_0x5a1280[_0x310f('0x37')][_0x310f('0x3d')]}});})[_0x310f('0x1a')](function(_0x3b55db){socket['emit']('smsInteractionTags:save',{'id':Number(_0x5a1280[_0x310f('0x29')]['id']),'tags':_0x3b55db||[]});return{'id':Number(_0x5a1280[_0x310f('0x29')]['id']),'tags':_0x3b55db||[]};});}})[_0x310f('0x1a')](respondWithResult(_0x3226b9,null))[_0x310f('0x27')](handleError(_0x3226b9,null));};exports['removeTags']=function(_0x57d6e6,_0x52490b){return db['SmsInteraction'][_0x310f('0x35')]({'where':{'id':_0x57d6e6[_0x310f('0x29')]['id']}})['then'](handleEntityNotFound(_0x52490b,null))[_0x310f('0x1a')](function(_0x4f21da){if(_0x4f21da){return _0x4f21da[_0x310f('0x51')](_0x57d6e6[_0x310f('0x2b')][_0x310f('0x3d')]);}})[_0x310f('0x1a')](respondWithStatusCode(_0x52490b,null))[_0x310f('0x27')](handleError(_0x52490b,null));};exports[_0x310f('0x52')]=function(_0x376cf3,_0x1c2dc9){var _0x1ea025=moment()[_0x310f('0x53')]()[_0x310f('0x54')]();var _0x1c01a0=path['join'](config[_0x310f('0x55')],'server',_0x310f('0x56'),_0x310f('0x57'));var _0x4feccc=path['join'](_0x1c01a0,_0x1ea025);var _0x5d5c40=util[_0x310f('0x47')]('transcript-%d-%s.zip',_0x376cf3[_0x310f('0x29')]['id'],_0x1ea025);var _0x265599=path[_0x310f('0x58')](_0x1c01a0,_0x5d5c40);var _0x1db4b8=[];_0x1db4b8['push']({'model':db['User'],'as':_0x310f('0x59'),'attributes':['id',_0x310f('0x5a')],'raw':!![]});_0x1db4b8['push']({'model':db['CmContact'],'as':'Contact','attributes':['id',_0x310f('0x5b'),_0x310f('0x5c')],'raw':!![]});var _0xa4eba=[{'model':db[_0x310f('0x3b')],'as':_0x310f('0x5d'),'attributes':['id',_0x310f('0x37'),'createdAt',_0x310f('0x5e'),_0x310f('0x5f'),_0x310f('0x60')],'include':_0x1db4b8}];_0xa4eba['push']({'model':db[_0x310f('0x61')],'as':_0x310f('0x62'),'attributes':['id',_0x310f('0x1c')]});_0xa4eba[_0x310f('0x63')]({'model':db[_0x310f('0x59')],'as':_0x310f('0x64'),'attributes':['id','fullname']});return db[_0x310f('0x1f')][_0x310f('0x35')]({'where':{'id':_0x376cf3['params']['id']},'include':_0xa4eba})[_0x310f('0x1a')](handleEntityNotFound(_0x1c2dc9,null))[_0x310f('0x1a')](function(_0x4ad0c9){if(_0x4ad0c9){var _0x54725b=_0x4ad0c9[_0x310f('0x65')]({'plain':!![]});fs['mkdirSync'](_0x4feccc);var _0x5ddd3f={'channel':_0x310f('0x66'),'account':_0x54725b[_0x310f('0x62')][_0x310f('0x1c')],'agent':_0x54725b['Owner']?_0x54725b[_0x310f('0x64')]['fullname']:_0x310f('0x67'),'createdAt':moment(_0x54725b[_0x310f('0x68')])['format'](_0x310f('0x48'))[_0x310f('0x54')](),'closedAt':_0x54725b['closed']?moment(_0x54725b[_0x310f('0x69')])['format'](_0x310f('0x48'))[_0x310f('0x54')]():'','disposition':_0x54725b[_0x310f('0x6a')],'messages':_(_0x54725b[_0x310f('0x5d')])['filter']({'secret':![]})['map'](function(_0x311d1d){return{'date':moment(_0x311d1d['createdAt'])[_0x310f('0x47')]('YYYY-MM-DD\x20HH:mm:ss')[_0x310f('0x54')](),'sender':_0x311d1d['direction']==='out'?_0x311d1d[_0x310f('0x59')]?_0x311d1d[_0x310f('0x59')][_0x310f('0x5a')]:_0x310f('0x6b'):_0x311d1d['Contact'][_0x310f('0x5b')]+(_0x311d1d[_0x310f('0x6c')][_0x310f('0x5c')]?'\x20'+_0x311d1d['Contact'][_0x310f('0x5c')]:''),'body':_0x311d1d[_0x310f('0x37')],'direction':_0x311d1d[_0x310f('0x5e')]===_0x310f('0x6d')?'A':'C','secret':_0x311d1d[_0x310f('0x5f')],'read':_0x311d1d[_0x310f('0x60')]};})[_0x310f('0x6e')]()};return ejs[_0x310f('0x6f')](path[_0x310f('0x58')](config[_0x310f('0x55')],_0x310f('0x70'),_0x310f('0x71'),'downloadInteraction.ejs'),{'interaction':_0x5ddd3f})[_0x310f('0x1a')](function(_0x29cece){var _0x557cb7=path['join'](_0x4feccc,_0x310f('0x72')+_0x54725b['id']+'-'+_0x1ea025+_0x310f('0x73'));var _0x544c7e={'path':_0x557cb7,'channel':_0x310f('0x66'),'interactionId':_0x54725b['id']};return pdf['createPdfFromHTML'](_0x29cece,_0x544c7e);})['then'](function(){return new BPromise(function(_0x215d83,_0x419d84){zipdir(_0x4feccc,{'saveTo':_0x265599},function(_0x4b989e,_0x204243){if(_0x4b989e)return _0x419d84(_0x4b989e);return _0x215d83(_0x204243);});})['then'](function(){return new BPromise(function(_0x487f1b,_0x43c4fe){rimraf(_0x4feccc,function(_0x4903b0){if(_0x4903b0)_0x43c4fe(_0x4903b0);return _0x487f1b();});});})[_0x310f('0x1a')](function(){return _0x1c2dc9[_0x310f('0x52')](_0x265599,_0x5d5c40,function(_0x24b79b){if(_0x24b79b){console['log'](_0x310f('0x74'),_0x24b79b);}else{fs[_0x310f('0x75')](_0x265599);}});});});}else{return _0x1c2dc9[_0x310f('0x12')](0xc8);}})['catch'](handleError(_0x1c2dc9,null));};