Built motion from commit fe909640.|2.6.8
[motion2.git] / server / api / chatInternalMessage / chatInternalMessage.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 _0x328b=['internal','online','lastLoginAt','allowmessenger','VoiceQueue','get','role','UserProfileSection','Agents','userProfileId','autoAssociation','UserProfileResource','isEmpty','agent','resourceId','push','FromId','ToId','read','-createdAt','all','rimraf','zip-dir','moment','bluebird','to-csv','lodash','squel','crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','api','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','socket.io-emitter','./chatInternalMessage.socket','status','json','limit','offset','count','set','Content-Range','apply','reject','save','update','then','destroy','end','stack','name','send','index','map','ChatInternalMessage','fieldName','type','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','filter','options','includeAll','findAll','rows','catch','show','params','rawAttributes','include','find','create','body','describe','messages','lastMessageAt','createdAt','lastMessage','format','getUsers','User','admin','user','fullname','userpic'];(function(_0x43f5c7,_0x118763){var _0x46be5f=function(_0x1a43f2){while(--_0x1a43f2){_0x43f5c7['push'](_0x43f5c7['shift']());}};_0x46be5f(++_0x118763);}(_0x328b,0xe9));var _0xb328=function(_0x1ac77b,_0x10ee7a){_0x1ac77b=_0x1ac77b-0x0;var _0x161584=_0x328b[_0x1ac77b];return _0x161584;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xb328('0x0'));var zipdir=require(_0xb328('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xb328('0x2'));var BPromise=require(_0xb328('0x3'));var Mustache=require('mustache');var util=require('util');var path=require('path');var sox=require('sox');var csv=require(_0xb328('0x4'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xb328('0x5'));var squel=require(_0xb328('0x6'));var crypto=require(_0xb328('0x7'));var jsforce=require(_0xb328('0x8'));var deskjs=require(_0xb328('0x9'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xb328('0xa'));var authService=require(_0xb328('0xb'));var qs=require(_0xb328('0xc'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xb328('0xd'));var logger=require('../../config/logger')(_0xb328('0xe'));var utils=require('../../config/utils');var config=require(_0xb328('0xf'));var licenseUtil=require(_0xb328('0x10'));var db=require(_0xb328('0x11'))['db'];config[_0xb328('0x12')]=_[_0xb328('0x13')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0xb328('0x14'))(new Redis(config[_0xb328('0x12')]));require(_0xb328('0x15'))['register'](socket);function respondWithStatusCode(_0x2d802c,_0x5826cc){_0x5826cc=_0x5826cc||0xcc;return function(_0x2a7af2){if(_0x2a7af2){return _0x2d802c['sendStatus'](_0x5826cc);}return _0x2d802c['status'](_0x5826cc)['end']();};}function respondWithResult(_0x41d473,_0x3d7057){_0x3d7057=_0x3d7057||0xc8;return function(_0x4bb019){if(_0x4bb019){return _0x41d473[_0xb328('0x16')](_0x3d7057)[_0xb328('0x17')](_0x4bb019);}};}function respondWithFilteredResult(_0x48798d,_0x585de0){return function(_0x2db8fc){if(_0x2db8fc){var _0x54dc43=typeof _0x585de0['offset']==='undefined'&&typeof _0x585de0[_0xb328('0x18')]==='undefined';var _0x265830=_0x2db8fc['count'];var _0x1853e2=_0x54dc43?0x0:_0x585de0[_0xb328('0x19')];var _0x1864a1=_0x54dc43?_0x2db8fc[_0xb328('0x1a')]:_0x585de0[_0xb328('0x19')]+_0x585de0[_0xb328('0x18')];var _0x2e989a;if(_0x1864a1>=_0x265830){_0x1864a1=_0x265830;_0x2e989a=0xc8;}else{_0x2e989a=0xce;}_0x48798d[_0xb328('0x16')](_0x2e989a);return _0x48798d[_0xb328('0x1b')](_0xb328('0x1c'),_0x1853e2+'-'+_0x1864a1+'/'+_0x265830)[_0xb328('0x17')](_0x2db8fc);}return null;};}function patchUpdates(_0x45b212){return function(_0x37170f){try{jsonpatch[_0xb328('0x1d')](_0x37170f,_0x45b212,!![]);}catch(_0x2a0744){return BPromise[_0xb328('0x1e')](_0x2a0744);}return _0x37170f[_0xb328('0x1f')]();};}function saveUpdates(_0x55afc0,_0x293676){return function(_0x54c431){if(_0x54c431){return _0x54c431[_0xb328('0x20')](_0x55afc0)[_0xb328('0x21')](function(_0x5c4438){return _0x5c4438;});}return null;};}function removeEntity(_0x49c393,_0x2479a3){return function(_0x16fe2f){if(_0x16fe2f){return _0x16fe2f[_0xb328('0x22')]()[_0xb328('0x21')](function(){_0x49c393[_0xb328('0x16')](0xcc)[_0xb328('0x23')]();});}};}function handleEntityNotFound(_0x3bcbbf,_0x4eabf3){return function(_0x39db39){if(!_0x39db39){_0x3bcbbf['sendStatus'](0x194);}return _0x39db39;};}function handleError(_0x2e1ef7,_0x9f1316){_0x9f1316=_0x9f1316||0x1f4;return function(_0x37900e){logger['error'](_0x37900e[_0xb328('0x24')]);if(_0x37900e[_0xb328('0x25')]){delete _0x37900e[_0xb328('0x25')];}_0x2e1ef7['status'](_0x9f1316)[_0xb328('0x26')](_0x37900e);};}exports[_0xb328('0x27')]=function(_0x5a94d1,_0xa4ac1f){var _0x1586f2={},_0x20414d={},_0x227e70={'count':0x0,'rows':[]};var _0x334718=_[_0xb328('0x28')](db[_0xb328('0x29')]['rawAttributes'],function(_0x2e0c63){return{'name':_0x2e0c63[_0xb328('0x2a')],'type':_0x2e0c63[_0xb328('0x2b')]['key']};});_0x20414d[_0xb328('0x2c')]=_[_0xb328('0x28')](_0x334718,_0xb328('0x25'));_0x20414d[_0xb328('0x2d')]=_[_0xb328('0x2e')](_0x5a94d1['query']);_0x20414d[_0xb328('0x2f')]=_['intersection'](_0x20414d['model'],_0x20414d[_0xb328('0x2d')]);_0x1586f2[_0xb328('0x30')]=_[_0xb328('0x31')](_0x20414d['model'],qs['fields'](_0x5a94d1[_0xb328('0x2d')][_0xb328('0x32')]));_0x1586f2[_0xb328('0x30')]=_0x1586f2[_0xb328('0x30')][_0xb328('0x33')]?_0x1586f2[_0xb328('0x30')]:_0x20414d[_0xb328('0x2c')];if(!_0x5a94d1[_0xb328('0x2d')][_0xb328('0x34')](_0xb328('0x35'))){_0x1586f2[_0xb328('0x18')]=qs[_0xb328('0x18')](_0x5a94d1[_0xb328('0x2d')][_0xb328('0x18')]);_0x1586f2[_0xb328('0x19')]=qs[_0xb328('0x19')](_0x5a94d1[_0xb328('0x2d')][_0xb328('0x19')]);}_0x1586f2[_0xb328('0x36')]=qs['sort'](_0x5a94d1['query'][_0xb328('0x37')]);_0x1586f2[_0xb328('0x38')]=qs[_0xb328('0x2f')](_[_0xb328('0x39')](_0x5a94d1[_0xb328('0x2d')],_0x20414d['filters']),_0x334718);if(_0x5a94d1[_0xb328('0x2d')]['filter']){_0x1586f2[_0xb328('0x38')]=_[_0xb328('0x3a')](_0x1586f2['where'],{'$or':_[_0xb328('0x28')](_0x334718,function(_0x137d79){if(_0x137d79[_0xb328('0x2b')]!==_0xb328('0x3b')){var _0x3dd42d={};_0x3dd42d[_0x137d79['name']]={'$like':'%'+_0x5a94d1['query'][_0xb328('0x3c')]+'%'};return _0x3dd42d;}})});}_0x1586f2=_[_0xb328('0x3a')]({},_0x1586f2,_0x5a94d1[_0xb328('0x3d')]);var _0x42939b={'where':_0x1586f2[_0xb328('0x38')]};return db[_0xb328('0x29')][_0xb328('0x1a')](_0x42939b)[_0xb328('0x21')](function(_0x1f9744){_0x227e70[_0xb328('0x1a')]=_0x1f9744;if(_0x5a94d1[_0xb328('0x2d')][_0xb328('0x3e')]){_0x1586f2['include']=[{'all':!![]}];}return db[_0xb328('0x29')][_0xb328('0x3f')](_0x1586f2);})['then'](function(_0x1c7aec){_0x227e70[_0xb328('0x40')]=_0x1c7aec;return _0x227e70;})[_0xb328('0x21')](respondWithFilteredResult(_0xa4ac1f,_0x1586f2))[_0xb328('0x41')](handleError(_0xa4ac1f,null));};exports[_0xb328('0x42')]=function(_0x45e5fb,_0xaa7d70){var _0x4dea1f={'raw':!![],'where':{'id':_0x45e5fb[_0xb328('0x43')]['id']}},_0x410e8e={};_0x410e8e[_0xb328('0x2c')]=_[_0xb328('0x2e')](db[_0xb328('0x29')][_0xb328('0x44')]);_0x410e8e[_0xb328('0x2d')]=_[_0xb328('0x2e')](_0x45e5fb[_0xb328('0x2d')]);_0x410e8e['filters']=_[_0xb328('0x31')](_0x410e8e[_0xb328('0x2c')],_0x410e8e[_0xb328('0x2d')]);_0x4dea1f[_0xb328('0x30')]=_[_0xb328('0x31')](_0x410e8e[_0xb328('0x2c')],qs[_0xb328('0x32')](_0x45e5fb[_0xb328('0x2d')][_0xb328('0x32')]));_0x4dea1f['attributes']=_0x4dea1f[_0xb328('0x30')][_0xb328('0x33')]?_0x4dea1f['attributes']:_0x410e8e['model'];if(_0x45e5fb['query']['includeAll']){_0x4dea1f[_0xb328('0x45')]=[{'all':!![]}];}_0x4dea1f=_['merge']({},_0x4dea1f,_0x45e5fb[_0xb328('0x3d')]);return db[_0xb328('0x29')][_0xb328('0x46')](_0x4dea1f)['then'](handleEntityNotFound(_0xaa7d70,null))['then'](respondWithResult(_0xaa7d70,null))[_0xb328('0x41')](handleError(_0xaa7d70,null));};exports[_0xb328('0x47')]=function(_0x14c9bf,_0x536d7a){return db[_0xb328('0x29')][_0xb328('0x47')](_0x14c9bf[_0xb328('0x48')],{})['then'](respondWithResult(_0x536d7a,0xc9))['catch'](handleError(_0x536d7a,null));};exports[_0xb328('0x20')]=function(_0x114a24,_0x101c1a){if(_0x114a24[_0xb328('0x48')]['id']){delete _0x114a24[_0xb328('0x48')]['id'];}return db['ChatInternalMessage']['find']({'where':{'id':_0x114a24['params']['id']}})[_0xb328('0x21')](handleEntityNotFound(_0x101c1a,null))[_0xb328('0x21')](saveUpdates(_0x114a24[_0xb328('0x48')],null))[_0xb328('0x21')](respondWithResult(_0x101c1a,null))['catch'](handleError(_0x101c1a,null));};exports[_0xb328('0x22')]=function(_0x155dc6,_0x20a172){return db[_0xb328('0x29')][_0xb328('0x46')]({'where':{'id':_0x155dc6[_0xb328('0x43')]['id']}})[_0xb328('0x21')](handleEntityNotFound(_0x20a172,null))['then'](removeEntity(_0x20a172,null))[_0xb328('0x41')](handleError(_0x20a172,null));};exports[_0xb328('0x49')]=function(_0x3a3cdd,_0x30e642){return db[_0xb328('0x29')][_0xb328('0x49')]()['then'](respondWithResult(_0x30e642,null))['catch'](handleError(_0x30e642,null));};function wrapUser(_0x5d0e6b){return function(_0x53cab8){var _0x20ce21=_0x5d0e6b['get']({'plain':!![]});_0x20ce21[_0xb328('0x4a')]=[];if(_0x53cab8){_0x20ce21[_0xb328('0x4b')]=_0x53cab8[_0xb328('0x4c')];_0x20ce21[_0xb328('0x4d')]=_0x53cab8;}else{_0x20ce21['lastMessageAt']=moment(0x0)[_0xb328('0x4e')]('YYYY-MM-DD\x20HH:mm:ss');_0x20ce21[_0xb328('0x4d')]={};}return _0x20ce21;};}exports[_0xb328('0x4f')]=function(_0x95d553,_0x174322,_0x150ad7){var _0x21b323=[];return db[_0xb328('0x50')][_0xb328('0x3f')]({'where':{'role':['agent',_0xb328('0x51'),_0xb328('0x52')]},'attributes':['id',_0xb328('0x53'),_0xb328('0x25'),'role',_0xb328('0x54'),_0xb328('0x55'),_0xb328('0x56'),_0xb328('0x57'),_0xb328('0x58'),'showWebBar'],'order':qs['sort']('-online'),'include':[{'model':db[_0xb328('0x59')],'as':'VoiceQueues','attributes':['id',_0xb328('0x25'),_0xb328('0x2b')],'through':{'attributes':[]}}]})[_0xb328('0x21')](function(_0x33886b){if(_0x33886b){var _0x19db19=_0x95d553[_0xb328('0x52')][_0xb328('0x5a')]({'plain':!![]});if(!_0x19db19)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x19db19[_0xb328('0x5b')]===_0xb328('0x52')){return db[_0xb328('0x5c')][_0xb328('0x46')]({'where':{'name':_0xb328('0x5d'),'userProfileId':_0x19db19[_0xb328('0x5e')]},'raw':!![]})['then'](function(_0x569700){if(_0x569700){if(_0x569700[_0xb328('0x5f')]===0x0){return db[_0xb328('0x60')][_0xb328('0x3f')]({'where':{'sectionId':_0x569700['id']},'raw':!![]})['then'](function(_0x3d9497){var _0x2785f9=[];if(_[_0xb328('0x61')](_0x3d9497)){_0x2785f9=_[_0xb328('0x3c')](_0x33886b,function(_0x5c0001){if(_0x5c0001[_0xb328('0x5b')]!==_0xb328('0x62')){return _0x5c0001;}});return _0x2785f9;}else{for(var _0x54be8d=0x0;_0x54be8d<_0x33886b[_0xb328('0x33')];_0x54be8d++){var _0x3f26b3=_0x33886b[_0x54be8d];if(_0x3f26b3[_0xb328('0x5b')]===_0xb328('0x62')){var _0x205503=_[_0xb328('0x46')](_0x3d9497,function(_0x58087c){return _0x58087c[_0xb328('0x63')]===_0x3f26b3['id'];});if(_0x205503)_0x2785f9['push'](_0x3f26b3);}else{_0x2785f9[_0xb328('0x64')](_0x3f26b3);}}return _0x2785f9;}});}else{return _0x33886b;}}else{var _0x4c795f=_[_0xb328('0x3c')](_0x33886b,function(_0x27a4dd){if(_0x27a4dd[_0xb328('0x5b')]!=='agent'){return _0x27a4dd;}});return _0x4c795f;}});}else{return _0x33886b;}}})[_0xb328('0x21')](function(_0x1d3686){var _0x2f5c1b=[];if(_0x1d3686){_0x21b323=_0x1d3686;for(var _0x52116e=0x0;_0x52116e<_0x1d3686[_0xb328('0x33')];_0x52116e+=0x1){_0x21b323[_0x52116e][_0xb328('0x4a')]=[];_0x2f5c1b[_0xb328('0x64')](db['ChatInternalMessage'][_0xb328('0x46')]({'where':{'FromId':[_0x95d553[_0xb328('0x52')]['id'],_0x1d3686[_0x52116e]['id']],'ToId':[_0x95d553[_0xb328('0x52')]['id'],_0x1d3686[_0x52116e]['id']],'ChatGroupId':null},'attributes':['id',_0xb328('0x4c'),_0xb328('0x65'),_0xb328('0x66'),_0xb328('0x67'),_0xb328('0x48')],'order':qs[_0xb328('0x37')](_0xb328('0x68')),'limit':0x1,'offset':0x0,'raw':!![]})['then'](wrapUser(_0x1d3686[_0x52116e])));}return BPromise[_0xb328('0x69')](_0x2f5c1b);}return[];})[_0xb328('0x21')](function(_0x3b0017){return{'rows':_0x3b0017,'count':_0x3b0017[_0xb328('0x33')]};})['then'](respondWithResult(_0x174322,null))[_0xb328('0x41')](handleError(_0x174322,null));};