259e9562868e3d61eb3ec17d9b6ff72b5694288a
[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 _0x644f=['fields','length','includeAll','merge','find','body','describe','SmsMessage','addMessage','omit','ids','getMessages','findOne','hasOwnProperty','nolimit','order','sort','where','SmsInteractionId','filter','$gte','split','format','include','findAll','rows','addTags','setTags','Tag','emit','removeTags','download','unix','toString','join','server','tmp','transcript-%d-%s.zip','push','User','CmContact','Contact','firstName','lastName','Messages','createdAt','direction','read','SmsAccount','Account','Owner','fullname','get','mkdirSync','Sms','N.A.','closed','closedAt','YYYY-MM-DD\x20HH:mm:ss','out','System','secret','renderFile','root','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','log','err','unlinkSync','sendStatus','rimraf','zip-dir','util','path','ejs','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','defaults','redis','socket.io-emitter','register','status','end','offset','undefined','limit','count','Content-Range','json','update','destroy','then','error','name','send','index','map','SmsInteraction','rawAttributes','fieldName','type','key','getOptions','sms','query','catch','show','params','keys','filters','intersection','model','attributes'];(function(_0x17141f,_0x4d7c4b){var _0x3ee900=function(_0x5ec0f4){while(--_0x5ec0f4){_0x17141f['push'](_0x17141f['shift']());}};_0x3ee900(++_0x4d7c4b);}(_0x644f,0xc2));var _0xf644=function(_0x260f28,_0x4e9b30){_0x260f28=_0x260f28-0x0;var _0x3aefcc=_0x644f[_0x260f28];return _0x3aefcc;};'use strict';var rimraf=require(_0xf644('0x0'));var zipdir=require(_0xf644('0x1'));var moment=require('moment');var BPromise=require('bluebird');var util=require(_0xf644('0x2'));var path=require(_0xf644('0x3'));var ejs=require(_0xf644('0x4'));var fs=require('fs');var _=require(_0xf644('0x5'));var Redis=require(_0xf644('0x6'));var interaction=require(_0xf644('0x7'));var qs=require(_0xf644('0x8'));var pdf=require(_0xf644('0x9'));var logger=require(_0xf644('0xa'))(_0xf644('0xb'));var config=require(_0xf644('0xc'));var db=require(_0xf644('0xd'))['db'];config['redis']=_[_0xf644('0xe')](config[_0xf644('0xf')],{'host':'localhost','port':0x18eb});var socket=require(_0xf644('0x10'))(new Redis(config['redis']));require('./smsInteraction.socket')[_0xf644('0x11')](socket);function respondWithStatusCode(_0x4a490d,_0x2c7bd0){_0x2c7bd0=_0x2c7bd0||0xcc;return function(_0x865312){if(_0x865312){return _0x4a490d['sendStatus'](_0x2c7bd0);}return _0x4a490d[_0xf644('0x12')](_0x2c7bd0)[_0xf644('0x13')]();};}function respondWithResult(_0x1ef872,_0x5911d3){_0x5911d3=_0x5911d3||0xc8;return function(_0x296647){if(_0x296647){return _0x1ef872[_0xf644('0x12')](_0x5911d3)['json'](_0x296647);}};}function respondWithFilteredResult(_0x3678c9,_0xaa5d1c){return function(_0x4ed4d6){if(_0x4ed4d6){var _0x400906=typeof _0xaa5d1c[_0xf644('0x14')]===_0xf644('0x15')&&typeof _0xaa5d1c[_0xf644('0x16')]==='undefined';var _0x155fd9=_0x4ed4d6[_0xf644('0x17')];var _0x52f74a=_0x400906?0x0:_0xaa5d1c['offset'];var _0x4c197b=_0x400906?_0x4ed4d6['count']:_0xaa5d1c[_0xf644('0x14')]+_0xaa5d1c[_0xf644('0x16')];var _0x34dec2;if(_0x4c197b>=_0x155fd9){_0x4c197b=_0x155fd9;_0x34dec2=0xc8;}else{_0x34dec2=0xce;}_0x3678c9[_0xf644('0x12')](_0x34dec2);return _0x3678c9['set'](_0xf644('0x18'),_0x52f74a+'-'+_0x4c197b+'/'+_0x155fd9)[_0xf644('0x19')](_0x4ed4d6);}return null;};}function saveUpdates(_0x4650b5){return function(_0x44fc39){if(_0x44fc39){return _0x44fc39[_0xf644('0x1a')](_0x4650b5)['then'](function(_0x4beeb0){return _0x4beeb0;});}return null;};}function removeEntity(_0x37c32d){return function(_0x305515){if(_0x305515){return _0x305515[_0xf644('0x1b')]()[_0xf644('0x1c')](function(){_0x37c32d[_0xf644('0x12')](0xcc)[_0xf644('0x13')]();});}};}function handleEntityNotFound(_0x67e595){return function(_0x20c0fb){if(!_0x20c0fb){_0x67e595['sendStatus'](0x194);}return _0x20c0fb;};}function handleError(_0x14af1b,_0x1c36b4){_0x1c36b4=_0x1c36b4||0x1f4;return function(_0x2180c6){logger[_0xf644('0x1d')](_0x2180c6['stack']);if(_0x2180c6[_0xf644('0x1e')]){delete _0x2180c6[_0xf644('0x1e')];}_0x14af1b[_0xf644('0x12')](_0x1c36b4)[_0xf644('0x1f')](_0x2180c6);};}exports[_0xf644('0x20')]=function(_0x3173c1,_0xae30c0){var _0x4cb86c={};var _0x4b6369=_[_0xf644('0x21')](db[_0xf644('0x22')][_0xf644('0x23')],function(_0x383987){return{'name':_0x383987[_0xf644('0x24')],'type':_0x383987[_0xf644('0x25')][_0xf644('0x26')]};});_0x4cb86c=qs[_0xf644('0x27')](_0x4b6369,_0x3173c1);return interaction['findAll'](_0xf644('0x28'),_0x3173c1[_0xf644('0x29')],_0x3173c1['user'],_0x4cb86c,_0x4b6369)['then'](respondWithFilteredResult(_0xae30c0,_0x4cb86c))[_0xf644('0x2a')](handleError(_0xae30c0,null));};exports[_0xf644('0x2b')]=function(_0x16c745,_0x3881e3){var _0x2bcb20={'raw':![],'where':{'id':_0x16c745[_0xf644('0x2c')]['id']}},_0x1ef501={};_0x1ef501['model']=_[_0xf644('0x2d')](db[_0xf644('0x22')]['rawAttributes']);_0x1ef501['query']=_[_0xf644('0x2d')](_0x16c745[_0xf644('0x29')]);_0x1ef501[_0xf644('0x2e')]=_[_0xf644('0x2f')](_0x1ef501[_0xf644('0x30')],_0x1ef501[_0xf644('0x29')]);_0x2bcb20[_0xf644('0x31')]=_[_0xf644('0x2f')](_0x1ef501[_0xf644('0x30')],qs[_0xf644('0x32')](_0x16c745['query']['fields']));_0x2bcb20[_0xf644('0x31')]=_0x2bcb20[_0xf644('0x31')][_0xf644('0x33')]?_0x2bcb20[_0xf644('0x31')]:_0x1ef501[_0xf644('0x30')];if(_0x16c745[_0xf644('0x29')][_0xf644('0x34')]){_0x2bcb20['include']=[{'all':!![]}];}_0x2bcb20=_[_0xf644('0x35')]({},_0x2bcb20,_0x16c745['options']);return db['SmsInteraction'][_0xf644('0x36')](_0x2bcb20)[_0xf644('0x1c')](handleEntityNotFound(_0x3881e3,null))[_0xf644('0x1c')](respondWithResult(_0x3881e3,null))['catch'](handleError(_0x3881e3,null));};exports['create']=function(_0x3aa198,_0xd5790a){return db[_0xf644('0x22')]['create'](_0x3aa198[_0xf644('0x37')],{})[_0xf644('0x1c')](respondWithResult(_0xd5790a,0xc9))['catch'](handleError(_0xd5790a,null));};exports[_0xf644('0x1a')]=function(_0x67cbfc,_0x27fbc7){if(_0x67cbfc[_0xf644('0x37')]['id']){delete _0x67cbfc[_0xf644('0x37')]['id'];}return db[_0xf644('0x22')]['find']({'where':{'id':_0x67cbfc[_0xf644('0x2c')]['id']}})['then'](handleEntityNotFound(_0x27fbc7,null))[_0xf644('0x1c')](saveUpdates(_0x67cbfc[_0xf644('0x37')],null))[_0xf644('0x1c')](respondWithResult(_0x27fbc7,null))[_0xf644('0x2a')](handleError(_0x27fbc7,null));};exports[_0xf644('0x1b')]=function(_0x60bec,_0x52e738){return db[_0xf644('0x22')][_0xf644('0x36')]({'where':{'id':_0x60bec[_0xf644('0x2c')]['id']}})['then'](handleEntityNotFound(_0x52e738,null))['then'](removeEntity(_0x52e738,null))[_0xf644('0x2a')](handleError(_0x52e738,null));};exports[_0xf644('0x38')]=function(_0x11d879,_0x14efb3){return db['SmsInteraction'][_0xf644('0x38')]()['then'](respondWithResult(_0x14efb3,null))[_0xf644('0x2a')](handleError(_0x14efb3,null));};exports['addMessage']=function(_0x2d9f52,_0x3475cf){return db[_0xf644('0x39')][_0xf644('0x36')]({'where':{'id':_0x2d9f52['params']['id']}})[_0xf644('0x1c')](handleEntityNotFound(_0x3475cf,null))[_0xf644('0x1c')](function(_0x565e83){if(_0x565e83){return _0x565e83[_0xf644('0x3a')](_0x2d9f52[_0xf644('0x37')]['ids'],_[_0xf644('0x3b')](_0x2d9f52['body'],[_0xf644('0x3c'),'id'])||{});}})[_0xf644('0x1c')](respondWithResult(_0x3475cf,null))[_0xf644('0x2a')](handleError(_0x3475cf,null));};exports[_0xf644('0x3d')]=function(_0x29724d,_0x594615){var _0x1062e3={'raw':![],'where':{}};var _0x395340={};var _0x1b9e4f={'count':0x0,'rows':[]};return db[_0xf644('0x22')][_0xf644('0x3e')]({'where':{'id':_0x29724d['params']['id']}})[_0xf644('0x1c')](handleEntityNotFound(_0x594615,null))[_0xf644('0x1c')](function(_0x2db98c){if(_0x2db98c){_0x395340['model']=_[_0xf644('0x2d')](db[_0xf644('0x39')][_0xf644('0x23')]);_0x395340[_0xf644('0x29')]=_['keys'](_0x29724d['query']);_0x395340[_0xf644('0x2e')]=_[_0xf644('0x2f')](_0x395340[_0xf644('0x30')],_0x395340[_0xf644('0x29')]);_0x1062e3['attributes']=_['intersection'](_0x395340[_0xf644('0x30')],qs[_0xf644('0x32')](_0x29724d[_0xf644('0x29')]['fields']));_0x1062e3[_0xf644('0x31')]=_0x1062e3[_0xf644('0x31')][_0xf644('0x33')]?_0x1062e3[_0xf644('0x31')]:_0x395340[_0xf644('0x30')];if(!_0x29724d[_0xf644('0x29')][_0xf644('0x3f')](_0xf644('0x40'))){_0x1062e3[_0xf644('0x16')]=qs['limit'](_0x29724d[_0xf644('0x29')][_0xf644('0x16')]);_0x1062e3[_0xf644('0x14')]=qs[_0xf644('0x14')](_0x29724d[_0xf644('0x29')][_0xf644('0x14')]);}_0x1062e3[_0xf644('0x41')]=qs['sort'](_0x29724d[_0xf644('0x29')][_0xf644('0x42')]);_0x1062e3['where']=qs['filters'](_['pick'](_0x29724d[_0xf644('0x29')],_0x395340['filters']));_0x1062e3[_0xf644('0x43')][_0xf644('0x44')]=_0x2db98c['id'];if(_0x29724d['query'][_0xf644('0x45')]){_0x1062e3[_0xf644('0x43')]=_[_0xf644('0x35')](_0x1062e3[_0xf644('0x43')],{'$or':_[_0xf644('0x21')](_0x1062e3[_0xf644('0x31')],function(_0x40910c){var _0x3d0915={};_0x3d0915[_0x40910c]={'$like':'%'+_0x29724d['query'][_0xf644('0x45')]+'%'};return _0x3d0915;})});}if(_0x29724d[_0xf644('0x29')][_0xf644('0x46')]){var _0x53701d=_0x29724d[_0xf644('0x29')][_0xf644('0x46')][_0xf644('0x47')](',');var _0x58a3b8={};_0x58a3b8[_0x53701d[0x0]]={'$gte':moment(_0x53701d[0x1])[_0xf644('0x48')]('YYYY-MM-DD\x20HH:mm:ss')};_0x1062e3[_0xf644('0x43')]=_['merge'](_0x1062e3[_0xf644('0x43')],_0x58a3b8);}_0x1062e3=_[_0xf644('0x35')]({},_0x1062e3,_0x29724d['options']);return db[_0xf644('0x39')][_0xf644('0x17')]({'where':_0x1062e3[_0xf644('0x43')]})[_0xf644('0x1c')](function(_0x1d68fd){_0x1b9e4f['count']=_0x1d68fd;if(_0x29724d['query'][_0xf644('0x34')]){_0x1062e3[_0xf644('0x49')]=[{'all':!![]}];}return db[_0xf644('0x39')][_0xf644('0x4a')](_0x1062e3);})[_0xf644('0x1c')](function(_0x4763c7){_0x1b9e4f[_0xf644('0x4b')]=_0x4763c7;return _0x1b9e4f;});}})[_0xf644('0x1c')](respondWithFilteredResult(_0x594615,_0x1062e3))[_0xf644('0x2a')](handleError(_0x594615,null));};exports[_0xf644('0x4c')]=function(_0x389c40,_0xf74e1){return db[_0xf644('0x22')][_0xf644('0x36')]({'where':{'id':_0x389c40['params']['id']}})[_0xf644('0x1c')](handleEntityNotFound(_0xf74e1,null))[_0xf644('0x1c')](function(_0x2ad9a3){if(_0x2ad9a3){return _0x2ad9a3[_0xf644('0x4d')](_0x389c40[_0xf644('0x37')][_0xf644('0x3c')],_[_0xf644('0x3b')](_0x389c40[_0xf644('0x37')],[_0xf644('0x3c'),'id'])||{})['spread'](function(){return db[_0xf644('0x4e')][_0xf644('0x4a')]({'attributes':['id',_0xf644('0x1e'),'color'],'where':{'id':_0x389c40[_0xf644('0x37')][_0xf644('0x3c')]}});})['then'](function(_0x4efff5){socket[_0xf644('0x4f')]('smsInteractionTags:save',{'id':Number(_0x389c40[_0xf644('0x2c')]['id']),'tags':_0x4efff5||[]});return{'id':Number(_0x389c40[_0xf644('0x2c')]['id']),'tags':_0x4efff5||[]};});}})[_0xf644('0x1c')](respondWithResult(_0xf74e1,null))['catch'](handleError(_0xf74e1,null));};exports[_0xf644('0x50')]=function(_0xc225ae,_0x41e3d8){return db['SmsInteraction'][_0xf644('0x36')]({'where':{'id':_0xc225ae[_0xf644('0x2c')]['id']}})['then'](handleEntityNotFound(_0x41e3d8,null))[_0xf644('0x1c')](function(_0x479d21){if(_0x479d21){return _0x479d21[_0xf644('0x50')](_0xc225ae['query'][_0xf644('0x3c')]);}})['then'](respondWithStatusCode(_0x41e3d8,null))[_0xf644('0x2a')](handleError(_0x41e3d8,null));};exports[_0xf644('0x51')]=function(_0x27c13d,_0x36c5db){var _0x31e76a=moment()[_0xf644('0x52')]()[_0xf644('0x53')]();var _0xf258d8=path[_0xf644('0x54')](config['root'],_0xf644('0x55'),'files',_0xf644('0x56'));var _0x18b52b=path[_0xf644('0x54')](_0xf258d8,_0x31e76a);var _0xbd0649=util['format'](_0xf644('0x57'),_0x27c13d[_0xf644('0x2c')]['id'],_0x31e76a);var _0x2eac5e=path[_0xf644('0x54')](_0xf258d8,_0xbd0649);var _0x25ccee=[];_0x25ccee[_0xf644('0x58')]({'model':db[_0xf644('0x59')],'as':_0xf644('0x59'),'attributes':['id','fullname'],'raw':!![]});_0x25ccee[_0xf644('0x58')]({'model':db[_0xf644('0x5a')],'as':_0xf644('0x5b'),'attributes':['id',_0xf644('0x5c'),_0xf644('0x5d')],'raw':!![]});var _0x275a9e=[{'model':db[_0xf644('0x39')],'as':_0xf644('0x5e'),'attributes':['id','body',_0xf644('0x5f'),_0xf644('0x60'),'secret',_0xf644('0x61')],'include':_0x25ccee}];_0x275a9e['push']({'model':db[_0xf644('0x62')],'as':_0xf644('0x63'),'attributes':['id',_0xf644('0x1e')]});_0x275a9e['push']({'model':db['User'],'as':_0xf644('0x64'),'attributes':['id',_0xf644('0x65')]});return db[_0xf644('0x22')][_0xf644('0x36')]({'where':{'id':_0x27c13d[_0xf644('0x2c')]['id']},'include':_0x275a9e})['then'](handleEntityNotFound(_0x36c5db,null))[_0xf644('0x1c')](function(_0x4ec2b1){if(_0x4ec2b1){var _0x580871=_0x4ec2b1[_0xf644('0x66')]({'plain':!![]});fs[_0xf644('0x67')](_0x18b52b);var _0x174bfd={'channel':_0xf644('0x68'),'account':_0x580871[_0xf644('0x63')][_0xf644('0x1e')],'agent':_0x580871[_0xf644('0x64')]?_0x580871['Owner'][_0xf644('0x65')]:_0xf644('0x69'),'createdAt':moment(_0x580871[_0xf644('0x5f')])[_0xf644('0x48')]('YYYY-MM-DD\x20HH:mm:ss')['toString'](),'closedAt':_0x580871[_0xf644('0x6a')]?moment(_0x580871[_0xf644('0x6b')])[_0xf644('0x48')](_0xf644('0x6c'))[_0xf644('0x53')]():'','disposition':_0x580871['disposition'],'messages':_(_0x580871['Messages'])['filter']({'secret':![]})[_0xf644('0x21')](function(_0xfd5aad){return{'date':moment(_0xfd5aad[_0xf644('0x5f')])[_0xf644('0x48')](_0xf644('0x6c'))['toString'](),'sender':_0xfd5aad[_0xf644('0x60')]===_0xf644('0x6d')?_0xfd5aad[_0xf644('0x59')]?_0xfd5aad[_0xf644('0x59')][_0xf644('0x65')]:_0xf644('0x6e'):_0xfd5aad[_0xf644('0x5b')]['firstName']+(_0xfd5aad['Contact'][_0xf644('0x5d')]?'\x20'+_0xfd5aad['Contact'][_0xf644('0x5d')]:''),'body':_0xfd5aad[_0xf644('0x37')],'direction':_0xfd5aad[_0xf644('0x60')]===_0xf644('0x6d')?'A':'C','secret':_0xfd5aad[_0xf644('0x6f')],'read':_0xfd5aad[_0xf644('0x61')]};})['value']()};return ejs[_0xf644('0x70')](path['join'](config[_0xf644('0x71')],'server','views',_0xf644('0x72')),{'interaction':_0x174bfd})['then'](function(_0x29f9f9){var _0x2310e8=path['join'](_0x18b52b,_0xf644('0x73')+_0x580871['id']+'-'+_0x31e76a+_0xf644('0x74'));var _0x173a93={'path':_0x2310e8,'channel':_0xf644('0x68'),'interactionId':_0x580871['id']};return pdf[_0xf644('0x75')](_0x29f9f9,_0x173a93);})[_0xf644('0x1c')](function(){return new BPromise(function(_0x1a3ba2,_0x152f1e){zipdir(_0x18b52b,{'saveTo':_0x2eac5e},function(_0x50bdd7,_0x4f485f){if(_0x50bdd7)return _0x152f1e(_0x50bdd7);return _0x1a3ba2(_0x4f485f);});})['then'](function(){return new BPromise(function(_0x49f208,_0x4b3a85){rimraf(_0x18b52b,function(_0x59d2e1){if(_0x59d2e1)_0x4b3a85(_0x59d2e1);return _0x49f208();});});})[_0xf644('0x1c')](function(){return _0x36c5db['download'](_0x2eac5e,_0xbd0649,function(_0x17586f){if(_0x17586f){console[_0xf644('0x76')](_0xf644('0x77'),_0x17586f);}else{fs[_0xf644('0x78')](_0x2eac5e);}});});});}else{return _0x36c5db[_0xf644('0x79')](0xc8);}})['catch'](handleError(_0x36c5db,null));};