Built motion from commit 9efc8423.|2.6.31
[motion2.git] / server / api / openchannelInteraction / openchannelInteraction.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 _0xdf63=['threadId','Public','Private','map','basename','createReadStream','pipe','createWriteStream','out','lastName','views','transcript-','download','error','Unable\x20to\x20delete\x20zip\x20file','unlinkSync','moment','util','path','ejs','lodash','ioredis','../../components/interaction/service','../../config/logger','api','../../config/environment','../../mysqldb','defaults','redis','localhost','socket.io-emitter','register','sendStatus','status','offset','undefined','count','limit','set','Content-Range','update','destroy','then','end','stack','name','send','index','OpenchannelInteraction','fieldName','key','findAll','openchannel','query','user','catch','show','type','from','params','model','keys','rawAttributes','filters','attributes','intersection','fields','length','includeAll','merge','options','find','create','body','describe','addMessage','OpenchannelMessage','ids','getMessages','findOne','hasOwnProperty','nolimit','order','sort','where','pick','OpenchannelInteractionId','filter','$gte','format','include','addTags','setTags','omit','spread','color','openchannelInteractionTags:save','removeTags','unix','join','root','tmp','server','files','attachments','transcript-%d-%s.zip','User','push','CmContact','Contact','firstName','Attachment','Messages','createdAt','direction','secret','read','OpenchannelAccount','Openchannel','Account','Owner','fullname','N.A.','toString','closed','closedAt','YYYY-MM-DD\x20HH:mm:ss','disposition'];(function(_0x1b1b64,_0x482097){var _0x1f546a=function(_0x55f1d4){while(--_0x55f1d4){_0x1b1b64['push'](_0x1b1b64['shift']());}};_0x1f546a(++_0x482097);}(_0xdf63,0x90));var _0x3df6=function(_0x3a1972,_0x194511){_0x3a1972=_0x3a1972-0x0;var _0xc7e0b5=_0xdf63[_0x3a1972];return _0xc7e0b5;};'use strict';var rimraf=require('rimraf');var zipdir=require('zip-dir');var moment=require(_0x3df6('0x0'));var util=require(_0x3df6('0x1'));var path=require(_0x3df6('0x2'));var ejs=require(_0x3df6('0x3'));var fs=require('fs');var _=require(_0x3df6('0x4'));var Redis=require(_0x3df6('0x5'));var interaction=require(_0x3df6('0x6'));var qs=require('../../components/parsers/qs');var pdf=require('../../components/export/pdf');var logger=require(_0x3df6('0x7'))(_0x3df6('0x8'));var config=require(_0x3df6('0x9'));var db=require(_0x3df6('0xa'))['db'];config['redis']=_[_0x3df6('0xb')](config[_0x3df6('0xc')],{'host':_0x3df6('0xd'),'port':0x18eb});var socket=require(_0x3df6('0xe'))(new Redis(config[_0x3df6('0xc')]));require('./openchannelInteraction.socket')[_0x3df6('0xf')](socket);function respondWithStatusCode(_0x377c68,_0xd5b325){_0xd5b325=_0xd5b325||0xcc;return function(_0x116c8f){if(_0x116c8f){return _0x377c68[_0x3df6('0x10')](_0xd5b325);}return _0x377c68[_0x3df6('0x11')](_0xd5b325)['end']();};}function respondWithResult(_0x4491df,_0x105396){_0x105396=_0x105396||0xc8;return function(_0x368719){if(_0x368719){return _0x4491df['status'](_0x105396)['json'](_0x368719);}};}function respondWithFilteredResult(_0x5696cd,_0x575f17){return function(_0x40e7cf){if(_0x40e7cf){var _0x6c349b=typeof _0x575f17[_0x3df6('0x12')]===_0x3df6('0x13')&&typeof _0x575f17['limit']===_0x3df6('0x13');var _0x1d8678=_0x40e7cf[_0x3df6('0x14')];var _0x2e550d=_0x6c349b?0x0:_0x575f17[_0x3df6('0x12')];var _0x39aa4f=_0x6c349b?_0x40e7cf[_0x3df6('0x14')]:_0x575f17[_0x3df6('0x12')]+_0x575f17[_0x3df6('0x15')];var _0x3f2d15;if(_0x39aa4f>=_0x1d8678){_0x39aa4f=_0x1d8678;_0x3f2d15=0xc8;}else{_0x3f2d15=0xce;}_0x5696cd[_0x3df6('0x11')](_0x3f2d15);return _0x5696cd[_0x3df6('0x16')](_0x3df6('0x17'),_0x2e550d+'-'+_0x39aa4f+'/'+_0x1d8678)['json'](_0x40e7cf);}return null;};}function saveUpdates(_0x45070b){return function(_0x29aca6){if(_0x29aca6){return _0x29aca6[_0x3df6('0x18')](_0x45070b)['then'](function(_0x255ce8){return _0x255ce8;});}return null;};}function removeEntity(_0x39c416){return function(_0x4ed2b7){if(_0x4ed2b7){return _0x4ed2b7[_0x3df6('0x19')]()[_0x3df6('0x1a')](function(){_0x39c416[_0x3df6('0x11')](0xcc)[_0x3df6('0x1b')]();});}};}function handleEntityNotFound(_0x27a5f1){return function(_0x47a50d){if(!_0x47a50d){_0x27a5f1[_0x3df6('0x10')](0x194);}return _0x47a50d;};}function handleError(_0x52ccae,_0x5ed711){_0x5ed711=_0x5ed711||0x1f4;return function(_0x465c13){logger['error'](_0x465c13[_0x3df6('0x1c')]);if(_0x465c13[_0x3df6('0x1d')]){delete _0x465c13[_0x3df6('0x1d')];}_0x52ccae[_0x3df6('0x11')](_0x5ed711)[_0x3df6('0x1e')](_0x465c13);};}exports[_0x3df6('0x1f')]=function(_0x3904cd,_0x3a6a13){var _0x2415d2={};var _0x3ddd6c=_['map'](db[_0x3df6('0x20')]['rawAttributes'],function(_0xc2a7e0){return{'name':_0xc2a7e0[_0x3df6('0x21')],'type':_0xc2a7e0['type'][_0x3df6('0x22')]};});_0x2415d2=qs['getOptions'](_0x3ddd6c,_0x3904cd);return interaction[_0x3df6('0x23')](_0x3df6('0x24'),_0x3904cd[_0x3df6('0x25')],_0x3904cd[_0x3df6('0x26')],_0x2415d2,_0x3ddd6c)[_0x3df6('0x1a')](respondWithFilteredResult(_0x3a6a13,_0x2415d2))[_0x3df6('0x27')](handleError(_0x3a6a13,null));};exports[_0x3df6('0x28')]=function(_0x16fe12,_0x41652a){var _0x152737={};if(_0x16fe12[_0x3df6('0x25')][_0x3df6('0x29')]&&_0x16fe12[_0x3df6('0x25')][_0x3df6('0x29')]==='from'){_0x152737[_0x3df6('0x2a')]=_0x16fe12[_0x3df6('0x2b')]['id'];}else{_0x152737['id']=_0x16fe12[_0x3df6('0x2b')]['id'];}var _0x35b859={'raw':![],'where':_0x152737},_0x47ddeb={};_0x47ddeb[_0x3df6('0x2c')]=_[_0x3df6('0x2d')](db['OpenchannelInteraction'][_0x3df6('0x2e')]);_0x47ddeb[_0x3df6('0x25')]=_[_0x3df6('0x2d')](_0x16fe12[_0x3df6('0x25')]);_0x47ddeb[_0x3df6('0x2f')]=_['intersection'](_0x47ddeb[_0x3df6('0x2c')],_0x47ddeb[_0x3df6('0x25')]);_0x35b859[_0x3df6('0x30')]=_[_0x3df6('0x31')](_0x47ddeb[_0x3df6('0x2c')],qs[_0x3df6('0x32')](_0x16fe12[_0x3df6('0x25')][_0x3df6('0x32')]));_0x35b859[_0x3df6('0x30')]=_0x35b859['attributes'][_0x3df6('0x33')]?_0x35b859[_0x3df6('0x30')]:_0x47ddeb['model'];if(_0x16fe12[_0x3df6('0x25')][_0x3df6('0x34')]){_0x35b859['include']=[{'all':!![]}];}_0x35b859=_[_0x3df6('0x35')]({},_0x35b859,_0x16fe12[_0x3df6('0x36')]);return db[_0x3df6('0x20')][_0x3df6('0x37')](_0x35b859)['then'](handleEntityNotFound(_0x41652a,null))[_0x3df6('0x1a')](respondWithResult(_0x41652a,null))[_0x3df6('0x27')](handleError(_0x41652a,null));};exports['create']=function(_0x9bde7e,_0x19b9e1){return db[_0x3df6('0x20')][_0x3df6('0x38')](_0x9bde7e['body'],{})[_0x3df6('0x1a')](respondWithResult(_0x19b9e1,0xc9))['catch'](handleError(_0x19b9e1,null));};exports[_0x3df6('0x18')]=function(_0x20adb9,_0x225490){if(_0x20adb9['body']['id']){delete _0x20adb9[_0x3df6('0x39')]['id'];}return db[_0x3df6('0x20')][_0x3df6('0x37')]({'where':{'id':_0x20adb9[_0x3df6('0x2b')]['id']}})[_0x3df6('0x1a')](handleEntityNotFound(_0x225490,null))[_0x3df6('0x1a')](saveUpdates(_0x20adb9[_0x3df6('0x39')],null))[_0x3df6('0x1a')](respondWithResult(_0x225490,null))['catch'](handleError(_0x225490,null));};exports['destroy']=function(_0x2a6eae,_0x3ea8af){return db[_0x3df6('0x20')][_0x3df6('0x37')]({'where':{'id':_0x2a6eae[_0x3df6('0x2b')]['id']}})[_0x3df6('0x1a')](handleEntityNotFound(_0x3ea8af,null))[_0x3df6('0x1a')](removeEntity(_0x3ea8af,null))['catch'](handleError(_0x3ea8af,null));};exports['describe']=function(_0x249a16,_0x2005f9){return db[_0x3df6('0x20')][_0x3df6('0x3a')]()['then'](respondWithResult(_0x2005f9,null))[_0x3df6('0x27')](handleError(_0x2005f9,null));};exports[_0x3df6('0x3b')]=function(_0x3928ac,_0x1710af){return db[_0x3df6('0x3c')][_0x3df6('0x37')]({'where':{'id':_0x3928ac[_0x3df6('0x2b')]['id']}})['then'](handleEntityNotFound(_0x1710af,null))[_0x3df6('0x1a')](function(_0xaaa937){if(_0xaaa937){return _0xaaa937[_0x3df6('0x3b')](_0x3928ac['body'][_0x3df6('0x3d')],_['omit'](_0x3928ac['body'],[_0x3df6('0x3d'),'id'])||{});}})['then'](respondWithResult(_0x1710af,null))['catch'](handleError(_0x1710af,null));};exports[_0x3df6('0x3e')]=function(_0x29a470,_0x219a14){var _0xeac8b={'raw':![],'where':{}};var _0x5a7d6e={};var _0x4c02c0={'count':0x0,'rows':[]};return db['OpenchannelInteraction'][_0x3df6('0x3f')]({'where':{'id':_0x29a470[_0x3df6('0x2b')]['id']}})[_0x3df6('0x1a')](handleEntityNotFound(_0x219a14,null))['then'](function(_0x24dd61){if(_0x24dd61){_0x5a7d6e[_0x3df6('0x2c')]=_[_0x3df6('0x2d')](db['OpenchannelMessage'][_0x3df6('0x2e')]);_0x5a7d6e[_0x3df6('0x25')]=_[_0x3df6('0x2d')](_0x29a470['query']);_0x5a7d6e[_0x3df6('0x2f')]=_[_0x3df6('0x31')](_0x5a7d6e[_0x3df6('0x2c')],_0x5a7d6e[_0x3df6('0x25')]);_0xeac8b[_0x3df6('0x30')]=_[_0x3df6('0x31')](_0x5a7d6e['model'],qs['fields'](_0x29a470[_0x3df6('0x25')]['fields']));_0xeac8b[_0x3df6('0x30')]=_0xeac8b[_0x3df6('0x30')][_0x3df6('0x33')]?_0xeac8b[_0x3df6('0x30')]:_0x5a7d6e[_0x3df6('0x2c')];if(!_0x29a470[_0x3df6('0x25')][_0x3df6('0x40')](_0x3df6('0x41'))){_0xeac8b[_0x3df6('0x15')]=qs[_0x3df6('0x15')](_0x29a470[_0x3df6('0x25')][_0x3df6('0x15')]);_0xeac8b[_0x3df6('0x12')]=qs[_0x3df6('0x12')](_0x29a470['query'][_0x3df6('0x12')]);}_0xeac8b[_0x3df6('0x42')]=qs['sort'](_0x29a470[_0x3df6('0x25')][_0x3df6('0x43')]);_0xeac8b[_0x3df6('0x44')]=qs[_0x3df6('0x2f')](_[_0x3df6('0x45')](_0x29a470['query'],_0x5a7d6e['filters']));_0xeac8b[_0x3df6('0x44')][_0x3df6('0x46')]=_0x24dd61['id'];if(_0x29a470[_0x3df6('0x25')][_0x3df6('0x47')]){_0xeac8b[_0x3df6('0x44')]=_[_0x3df6('0x35')](_0xeac8b[_0x3df6('0x44')],{'$or':_['map'](_0xeac8b[_0x3df6('0x30')],function(_0x2e8ae2){var _0x38b9d5={};_0x38b9d5[_0x2e8ae2]={'$like':'%'+_0x29a470[_0x3df6('0x25')][_0x3df6('0x47')]+'%'};return _0x38b9d5;})});}if(_0x29a470['query'][_0x3df6('0x48')]){var _0x276d8b=_0x29a470[_0x3df6('0x25')][_0x3df6('0x48')]['split'](',');var _0x49e638={};_0x49e638[_0x276d8b[0x0]]={'$gte':moment(_0x276d8b[0x1])[_0x3df6('0x49')]('YYYY-MM-DD\x20HH:mm:ss')};_0xeac8b[_0x3df6('0x44')]=_[_0x3df6('0x35')](_0xeac8b[_0x3df6('0x44')],_0x49e638);}_0xeac8b=_['merge']({},_0xeac8b,_0x29a470[_0x3df6('0x36')]);return db[_0x3df6('0x3c')][_0x3df6('0x14')]({'where':_0xeac8b[_0x3df6('0x44')]})[_0x3df6('0x1a')](function(_0x41d324){_0x4c02c0[_0x3df6('0x14')]=_0x41d324;if(_0x29a470[_0x3df6('0x25')]['includeAll']){_0xeac8b[_0x3df6('0x4a')]=[{'all':!![]}];}return db[_0x3df6('0x3c')][_0x3df6('0x23')](_0xeac8b);})[_0x3df6('0x1a')](function(_0x18126a){_0x4c02c0['rows']=_0x18126a;return _0x4c02c0;});}})['then'](respondWithFilteredResult(_0x219a14,_0xeac8b))[_0x3df6('0x27')](handleError(_0x219a14,null));};exports[_0x3df6('0x4b')]=function(_0x30aa21,_0x1d2e98){return db[_0x3df6('0x20')][_0x3df6('0x37')]({'where':{'id':_0x30aa21[_0x3df6('0x2b')]['id']}})['then'](handleEntityNotFound(_0x1d2e98,null))['then'](function(_0x489055){if(_0x489055){return _0x489055[_0x3df6('0x4c')](_0x30aa21[_0x3df6('0x39')][_0x3df6('0x3d')],_[_0x3df6('0x4d')](_0x30aa21['body'],[_0x3df6('0x3d'),'id'])||{})[_0x3df6('0x4e')](function(){return db['Tag'][_0x3df6('0x23')]({'attributes':['id',_0x3df6('0x1d'),_0x3df6('0x4f')],'where':{'id':_0x30aa21[_0x3df6('0x39')][_0x3df6('0x3d')]}});})[_0x3df6('0x1a')](function(_0x1ebe0e){socket['emit'](_0x3df6('0x50'),{'id':Number(_0x30aa21['params']['id']),'tags':_0x1ebe0e||[]});return{'id':Number(_0x30aa21[_0x3df6('0x2b')]['id']),'tags':_0x1ebe0e||[]};});}})[_0x3df6('0x1a')](respondWithResult(_0x1d2e98,null))[_0x3df6('0x27')](handleError(_0x1d2e98,null));};exports[_0x3df6('0x51')]=function(_0x30dcea,_0x38d0ee){return db['OpenchannelInteraction']['find']({'where':{'id':_0x30dcea[_0x3df6('0x2b')]['id']}})[_0x3df6('0x1a')](handleEntityNotFound(_0x38d0ee,null))[_0x3df6('0x1a')](function(_0x12b007){if(_0x12b007){return _0x12b007[_0x3df6('0x51')](_0x30dcea[_0x3df6('0x25')][_0x3df6('0x3d')]);}})[_0x3df6('0x1a')](respondWithStatusCode(_0x38d0ee,null))[_0x3df6('0x27')](handleError(_0x38d0ee,null));};exports['download']=function(_0x151514,_0x485d58){var _0x54cbc9=moment()[_0x3df6('0x52')]()['toString']();var _0x258113=path[_0x3df6('0x53')](config[_0x3df6('0x54')],'server','files',_0x3df6('0x55'));var _0x4c8ad3=path[_0x3df6('0x53')](config[_0x3df6('0x54')],_0x3df6('0x56'),_0x3df6('0x57'),_0x3df6('0x58'));var _0x63532b=path[_0x3df6('0x53')](_0x258113,_0x54cbc9);var _0x3cbd3e=util[_0x3df6('0x49')](_0x3df6('0x59'),_0x151514[_0x3df6('0x2b')]['id'],_0x54cbc9);var _0x171cad=path[_0x3df6('0x53')](_0x258113,_0x3cbd3e);var _0x3926b9=[];_0x3926b9['push']({'model':db[_0x3df6('0x5a')],'as':_0x3df6('0x5a'),'attributes':['id','fullname'],'raw':!![]});_0x3926b9[_0x3df6('0x5b')]({'model':db[_0x3df6('0x5c')],'as':_0x3df6('0x5d'),'attributes':['id',_0x3df6('0x5e'),'lastName'],'raw':!![]});if(_0x151514[_0x3df6('0x25')]['attachments']){_0x3926b9[_0x3df6('0x5b')]({'model':db[_0x3df6('0x5f')],'as':_0x3df6('0x5f'),'raw':!![]});}var _0x48c974=[{'model':db[_0x3df6('0x3c')],'as':_0x3df6('0x60'),'attributes':['id',_0x3df6('0x39'),_0x3df6('0x61'),_0x3df6('0x62'),_0x3df6('0x63'),_0x3df6('0x64')],'include':_0x3926b9}];_0x48c974[_0x3df6('0x5b')]({'model':db[_0x3df6('0x65')],'as':'Account','attributes':['id',_0x3df6('0x1d')]});_0x48c974[_0x3df6('0x5b')]({'model':db['User'],'as':'Owner','attributes':['id','fullname']});return db[_0x3df6('0x20')][_0x3df6('0x37')]({'where':{'id':_0x151514[_0x3df6('0x2b')]['id']},'include':_0x48c974})['then'](handleEntityNotFound(_0x485d58,null))[_0x3df6('0x1a')](function(_0x213604){if(_0x213604){var _0x3fff27=_0x213604['get']({'plain':!![]});fs['mkdirSync'](_0x63532b);var _0x27f81d={'channel':_0x3df6('0x66'),'account':_0x3fff27[_0x3df6('0x67')]['name'],'agent':_0x3fff27['Owner']?_0x3fff27[_0x3df6('0x68')][_0x3df6('0x69')]:_0x3df6('0x6a'),'createdAt':moment(_0x3fff27[_0x3df6('0x61')])['format']('YYYY-MM-DD\x20HH:mm:ss')[_0x3df6('0x6b')](),'closedAt':_0x3fff27[_0x3df6('0x6c')]?moment(_0x3fff27[_0x3df6('0x6d')])[_0x3df6('0x49')](_0x3df6('0x6e'))[_0x3df6('0x6b')]():'','disposition':_0x3fff27[_0x3df6('0x6f')],'visibility':_0x3fff27[_0x3df6('0x70')]?_0x3df6('0x71'):_0x3df6('0x72'),'messages':_(_0x3fff27[_0x3df6('0x60')])[_0x3df6('0x47')]({'secret':![]})[_0x3df6('0x73')](function(_0x44c2d6){if(_0x44c2d6[_0x3df6('0x5f')]){var _0x3e76ff=path[_0x3df6('0x53')](_0x4c8ad3,_0x44c2d6['Attachment'][_0x3df6('0x74')]);if(fs['existsSync'](_0x3e76ff)){fs[_0x3df6('0x75')](_0x3e76ff)[_0x3df6('0x76')](fs[_0x3df6('0x77')](path[_0x3df6('0x53')](_0x63532b,_0x44c2d6[_0x3df6('0x5f')][_0x3df6('0x74')])));}}return{'date':moment(_0x44c2d6[_0x3df6('0x61')])['format'](_0x3df6('0x6e'))['toString'](),'sender':_0x44c2d6['direction']===_0x3df6('0x78')?_0x44c2d6['User']?_0x44c2d6[_0x3df6('0x5a')]['fullname']:'System':_0x44c2d6[_0x3df6('0x5d')][_0x3df6('0x5e')]+(_0x44c2d6['Contact'][_0x3df6('0x79')]?'\x20'+_0x44c2d6[_0x3df6('0x5d')][_0x3df6('0x79')]:''),'body':_0x44c2d6[_0x3df6('0x5f')]?_0x44c2d6['Attachment'][_0x3df6('0x74')]:_0x44c2d6[_0x3df6('0x39')],'direction':_0x44c2d6[_0x3df6('0x62')]===_0x3df6('0x78')?'A':'C','secret':_0x44c2d6['secret'],'read':_0x44c2d6[_0x3df6('0x64')],'attachment':_0x44c2d6[_0x3df6('0x5f')]};})['value']()};return ejs['renderFile'](path[_0x3df6('0x53')](config[_0x3df6('0x54')],_0x3df6('0x56'),_0x3df6('0x7a'),'downloadInteraction.ejs'),{'interaction':_0x27f81d})['then'](function(_0x2ad09f){var _0x3e5e54=path[_0x3df6('0x53')](_0x63532b,_0x3df6('0x7b')+_0x3fff27['id']+'-'+_0x54cbc9+'.pdf');var _0x11c5af={'path':_0x3e5e54,'channel':_0x3df6('0x66'),'interactionId':_0x3fff27['id']};return pdf['createPdfFromHTML'](_0x2ad09f,_0x11c5af);})[_0x3df6('0x1a')](function(){return new Promise(function(_0xdbd77a,_0x3ba9c4){zipdir(_0x63532b,{'saveTo':_0x171cad},function(_0x5de9d1,_0x3d30ac){if(_0x5de9d1)return _0x3ba9c4(_0x5de9d1);return _0xdbd77a(_0x3d30ac);});})[_0x3df6('0x1a')](function(){return new Promise(function(_0x4de01c,_0x433224){rimraf(_0x63532b,function(_0x233f6e){if(_0x233f6e)_0x433224(_0x233f6e);return _0x4de01c();});});})[_0x3df6('0x1a')](function(){return _0x485d58[_0x3df6('0x7c')](_0x171cad,_0x3cbd3e,function(_0x29cfbd){if(_0x29cfbd){logger[_0x3df6('0x7d')](_0x3df6('0x7e'),_0x29cfbd);}else{fs[_0x3df6('0x7f')](_0x171cad);}});});});}else{return _0x485d58['sendStatus'](0xc8);}})[_0x3df6('0x27')](handleError(_0x485d58,null));};