Built motion from commit e6806ed6.|2.6.0
[motion2.git] / server / api / chatGroup / chatGroup.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 _0x6f34=['User','hasOwnProperty','removeMembers','addMessage','ChatGroupId','ChatInternalMessage','create','getMessages','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','eml-format','rimraf','fast-json-patch','moment','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','./chatGroup.socket','register','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','name','index','map','ChatGroup','rawAttributes','fieldName','key','query','keys','filters','intersection','model','attributes','fields','length','nolimit','order','sort','where','pick','merge','type','VIRTUAL','filter','options','includeAll','include','findAll','rows','catch','show','body','find','params','getUnread','user','toInteger','addMembers','ids','omit','getMembers','findOne'];(function(_0xe79658,_0x250cad){var _0x752f54=function(_0x34d343){while(--_0x34d343){_0xe79658['push'](_0xe79658['shift']());}};_0x752f54(++_0x250cad);}(_0x6f34,0x144));var _0x46f3=function(_0x585bfa,_0x57378d){_0x585bfa=_0x585bfa-0x0;var _0x48275a=_0x6f34[_0x585bfa];return _0x48275a;};'use strict';var emlformat=require(_0x46f3('0x0'));var rimraf=require(_0x46f3('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x46f3('0x2'));var rp=require('request-promise');var moment=require(_0x46f3('0x3'));var BPromise=require(_0x46f3('0x4'));var Mustache=require(_0x46f3('0x5'));var util=require('util');var path=require(_0x46f3('0x6'));var sox=require(_0x46f3('0x7'));var csv=require(_0x46f3('0x8'));var ejs=require(_0x46f3('0x9'));var fs=require('fs');var fs_extra=require(_0x46f3('0xa'));var _=require(_0x46f3('0xb'));var squel=require(_0x46f3('0xc'));var crypto=require(_0x46f3('0xd'));var jsforce=require(_0x46f3('0xe'));var deskjs=require('desk.js');var toCsv=require(_0x46f3('0x8'));var querystring=require(_0x46f3('0xf'));var Papa=require(_0x46f3('0x10'));var Redis=require(_0x46f3('0x11'));var authService=require(_0x46f3('0x12'));var qs=require(_0x46f3('0x13'));var as=require(_0x46f3('0x14'));var hardwareService=require(_0x46f3('0x15'));var logger=require(_0x46f3('0x16'))(_0x46f3('0x17'));var utils=require('../../config/utils');var config=require(_0x46f3('0x18'));var licenseUtil=require(_0x46f3('0x19'));var db=require(_0x46f3('0x1a'))['db'];config[_0x46f3('0x1b')]=_[_0x46f3('0x1c')](config['redis'],{'host':_0x46f3('0x1d'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x46f3('0x1b')]));require(_0x46f3('0x1e'))[_0x46f3('0x1f')](socket);function respondWithStatusCode(_0x2a2fd7,_0x59ae1f){_0x59ae1f=_0x59ae1f||0xcc;return function(_0x17dc67){if(_0x17dc67){return _0x2a2fd7[_0x46f3('0x20')](_0x59ae1f);}return _0x2a2fd7[_0x46f3('0x21')](_0x59ae1f)[_0x46f3('0x22')]();};}function respondWithResult(_0x8cbf46,_0x219a40){_0x219a40=_0x219a40||0xc8;return function(_0x385e97){if(_0x385e97){return _0x8cbf46[_0x46f3('0x21')](_0x219a40)[_0x46f3('0x23')](_0x385e97);}};}function respondWithFilteredResult(_0x566870,_0x464287){return function(_0x1ccd77){if(_0x1ccd77){var _0x4f57e6=typeof _0x464287[_0x46f3('0x24')]===_0x46f3('0x25')&&typeof _0x464287[_0x46f3('0x26')]===_0x46f3('0x25');var _0x377deb=_0x1ccd77[_0x46f3('0x27')];var _0x1ca49e=_0x4f57e6?0x0:_0x464287[_0x46f3('0x24')];var _0x445b17=_0x4f57e6?_0x1ccd77[_0x46f3('0x27')]:_0x464287[_0x46f3('0x24')]+_0x464287[_0x46f3('0x26')];var _0x4c3dd1;if(_0x445b17>=_0x377deb){_0x445b17=_0x377deb;_0x4c3dd1=0xc8;}else{_0x4c3dd1=0xce;}_0x566870[_0x46f3('0x21')](_0x4c3dd1);return _0x566870[_0x46f3('0x28')](_0x46f3('0x29'),_0x1ca49e+'-'+_0x445b17+'/'+_0x377deb)[_0x46f3('0x23')](_0x1ccd77);}return null;};}function patchUpdates(_0x44190b){return function(_0x4edd62){try{jsonpatch[_0x46f3('0x2a')](_0x4edd62,_0x44190b,!![]);}catch(_0x3cde04){return BPromise[_0x46f3('0x2b')](_0x3cde04);}return _0x4edd62[_0x46f3('0x2c')]();};}function saveUpdates(_0x3e4610,_0x2bebeb){return function(_0x2f36f7){if(_0x2f36f7){return _0x2f36f7[_0x46f3('0x2d')](_0x3e4610)[_0x46f3('0x2e')](function(_0x5dacd9){return _0x5dacd9;});}return null;};}function removeEntity(_0x53fe67,_0x3dc10d){return function(_0x3ad93f){if(_0x3ad93f){return _0x3ad93f[_0x46f3('0x2f')]()[_0x46f3('0x2e')](function(){_0x53fe67[_0x46f3('0x21')](0xcc)['end']();});}};}function handleEntityNotFound(_0x1f4682,_0x13dd2b){return function(_0x857ae0){if(!_0x857ae0){_0x1f4682[_0x46f3('0x20')](0x194);}return _0x857ae0;};}function handleError(_0x4760ef,_0x5ee021){_0x5ee021=_0x5ee021||0x1f4;return function(_0x396864){logger[_0x46f3('0x30')](_0x396864[_0x46f3('0x31')]);if(_0x396864[_0x46f3('0x32')]){delete _0x396864['name'];}_0x4760ef[_0x46f3('0x21')](_0x5ee021)['send'](_0x396864);};}exports[_0x46f3('0x33')]=function(_0x158c1c,_0x4e2768){var _0x30d203={},_0xba89ef={},_0x47d770={'count':0x0,'rows':[]};var _0x3a3c22=_[_0x46f3('0x34')](db[_0x46f3('0x35')][_0x46f3('0x36')],function(_0x2220d3){return{'name':_0x2220d3[_0x46f3('0x37')],'type':_0x2220d3['type'][_0x46f3('0x38')]};});_0xba89ef['model']=_[_0x46f3('0x34')](_0x3a3c22,'name');_0xba89ef[_0x46f3('0x39')]=_[_0x46f3('0x3a')](_0x158c1c[_0x46f3('0x39')]);_0xba89ef[_0x46f3('0x3b')]=_[_0x46f3('0x3c')](_0xba89ef[_0x46f3('0x3d')],_0xba89ef[_0x46f3('0x39')]);_0x30d203[_0x46f3('0x3e')]=_[_0x46f3('0x3c')](_0xba89ef['model'],qs[_0x46f3('0x3f')](_0x158c1c[_0x46f3('0x39')][_0x46f3('0x3f')]));_0x30d203[_0x46f3('0x3e')]=_0x30d203['attributes'][_0x46f3('0x40')]?_0x30d203['attributes']:_0xba89ef[_0x46f3('0x3d')];if(!_0x158c1c[_0x46f3('0x39')]['hasOwnProperty'](_0x46f3('0x41'))){_0x30d203['limit']=qs[_0x46f3('0x26')](_0x158c1c['query'][_0x46f3('0x26')]);_0x30d203[_0x46f3('0x24')]=qs[_0x46f3('0x24')](_0x158c1c['query'][_0x46f3('0x24')]);}_0x30d203[_0x46f3('0x42')]=qs['sort'](_0x158c1c[_0x46f3('0x39')][_0x46f3('0x43')]);_0x30d203[_0x46f3('0x44')]=qs[_0x46f3('0x3b')](_[_0x46f3('0x45')](_0x158c1c[_0x46f3('0x39')],_0xba89ef['filters']),_0x3a3c22);if(_0x158c1c[_0x46f3('0x39')]['filter']){_0x30d203[_0x46f3('0x44')]=_[_0x46f3('0x46')](_0x30d203[_0x46f3('0x44')],{'$or':_[_0x46f3('0x34')](_0x3a3c22,function(_0x16315b){if(_0x16315b[_0x46f3('0x47')]!==_0x46f3('0x48')){var _0x39a414={};_0x39a414[_0x16315b[_0x46f3('0x32')]]={'$like':'%'+_0x158c1c['query'][_0x46f3('0x49')]+'%'};return _0x39a414;}})});}_0x30d203=_[_0x46f3('0x46')]({},_0x30d203,_0x158c1c[_0x46f3('0x4a')]);var _0x50822a={'where':_0x30d203['where']};return db[_0x46f3('0x35')]['count'](_0x50822a)[_0x46f3('0x2e')](function(_0x24bbdf){_0x47d770[_0x46f3('0x27')]=_0x24bbdf;if(_0x158c1c['query'][_0x46f3('0x4b')]){_0x30d203[_0x46f3('0x4c')]=[{'all':!![]}];}return db[_0x46f3('0x35')][_0x46f3('0x4d')](_0x30d203);})[_0x46f3('0x2e')](function(_0x29482a){_0x47d770[_0x46f3('0x4e')]=_0x29482a;return _0x47d770;})[_0x46f3('0x2e')](respondWithFilteredResult(_0x4e2768,_0x30d203))[_0x46f3('0x4f')](handleError(_0x4e2768,null));};exports[_0x46f3('0x50')]=function(_0x37f822,_0x3610ef){var _0x41f1dc={'raw':!![],'where':{'id':_0x37f822['params']['id']}},_0xe12f99={};_0xe12f99[_0x46f3('0x3d')]=_[_0x46f3('0x3a')](db[_0x46f3('0x35')][_0x46f3('0x36')]);_0xe12f99['query']=_['keys'](_0x37f822[_0x46f3('0x39')]);_0xe12f99[_0x46f3('0x3b')]=_[_0x46f3('0x3c')](_0xe12f99[_0x46f3('0x3d')],_0xe12f99[_0x46f3('0x39')]);_0x41f1dc[_0x46f3('0x3e')]=_[_0x46f3('0x3c')](_0xe12f99[_0x46f3('0x3d')],qs[_0x46f3('0x3f')](_0x37f822[_0x46f3('0x39')][_0x46f3('0x3f')]));_0x41f1dc[_0x46f3('0x3e')]=_0x41f1dc[_0x46f3('0x3e')][_0x46f3('0x40')]?_0x41f1dc['attributes']:_0xe12f99[_0x46f3('0x3d')];if(_0x37f822[_0x46f3('0x39')][_0x46f3('0x4b')]){_0x41f1dc[_0x46f3('0x4c')]=[{'all':!![]}];}_0x41f1dc=_[_0x46f3('0x46')]({},_0x41f1dc,_0x37f822[_0x46f3('0x4a')]);return db[_0x46f3('0x35')]['find'](_0x41f1dc)[_0x46f3('0x2e')](handleEntityNotFound(_0x3610ef,null))[_0x46f3('0x2e')](respondWithResult(_0x3610ef,null))[_0x46f3('0x4f')](handleError(_0x3610ef,null));};exports['create']=function(_0x39ac56,_0xcc8f80){return db[_0x46f3('0x35')]['create'](_0x39ac56[_0x46f3('0x51')],{})[_0x46f3('0x2e')](respondWithResult(_0xcc8f80,0xc9))['catch'](handleError(_0xcc8f80,null));};exports['update']=function(_0x516d05,_0x16c86d){if(_0x516d05['body']['id']){delete _0x516d05[_0x46f3('0x51')]['id'];}return db['ChatGroup'][_0x46f3('0x52')]({'where':{'id':_0x516d05[_0x46f3('0x53')]['id']}})[_0x46f3('0x2e')](handleEntityNotFound(_0x16c86d,null))[_0x46f3('0x2e')](saveUpdates(_0x516d05['body'],null))['then'](respondWithResult(_0x16c86d,null))[_0x46f3('0x4f')](handleError(_0x16c86d,null));};exports[_0x46f3('0x2f')]=function(_0x175cac,_0x18bde5){return db['ChatGroup'][_0x46f3('0x52')]({'where':{'id':_0x175cac['params']['id']}})[_0x46f3('0x2e')](handleEntityNotFound(_0x18bde5,null))[_0x46f3('0x2e')](removeEntity(_0x18bde5,null))[_0x46f3('0x4f')](handleError(_0x18bde5,null));};exports['describe']=function(_0x537b13,_0x5e5e30){return db['ChatGroup']['describe']()[_0x46f3('0x2e')](respondWithResult(_0x5e5e30,null))['catch'](handleError(_0x5e5e30,null));};exports[_0x46f3('0x54')]=function(_0x576741,_0x4a5d50,_0xd2bb62){return db['ChatInternalMessage'][_0x46f3('0x27')]({'where':{'ChatGroupId':_0x576741[_0x46f3('0x53')]['id'],'ToId':_0x576741[_0x46f3('0x55')]['id'],'read':![]}})[_0x46f3('0x2e')](function(_0x3cb282){return{'id':_[_0x46f3('0x56')](_0x576741[_0x46f3('0x53')]['id']),'count':_0x3cb282};})[_0x46f3('0x2e')](respondWithResult(_0x4a5d50,null))[_0x46f3('0x4f')](handleError(_0x4a5d50,null));};exports['addMembers']=function(_0x56ea9f,_0x3b3a0f,_0x5411be){return db['ChatGroup']['find']({'where':{'id':_0x56ea9f[_0x46f3('0x53')]['id']}})[_0x46f3('0x2e')](handleEntityNotFound(_0x3b3a0f,null))[_0x46f3('0x2e')](function(_0x47635d){if(_0x47635d){return _0x47635d[_0x46f3('0x57')](_0x56ea9f[_0x46f3('0x51')][_0x46f3('0x58')],_[_0x46f3('0x59')](_0x56ea9f[_0x46f3('0x51')],[_0x46f3('0x58'),'id'])||{});}})[_0x46f3('0x2e')](respondWithResult(_0x3b3a0f,null))[_0x46f3('0x4f')](handleError(_0x3b3a0f,null));};exports[_0x46f3('0x5a')]=function(_0x104334,_0x1ed0f9,_0x3a9937){var _0x45b743={};var _0x111eaf={};var _0x40973b;var _0x150a8d;return db[_0x46f3('0x35')][_0x46f3('0x5b')]({'where':{'id':_0x104334['params']['id']}})[_0x46f3('0x2e')](handleEntityNotFound(_0x1ed0f9,null))[_0x46f3('0x2e')](function(_0x154a3e){if(_0x154a3e){_0x40973b=_0x154a3e;_0x111eaf[_0x46f3('0x3d')]=_[_0x46f3('0x3a')](db[_0x46f3('0x5c')][_0x46f3('0x36')]);_0x111eaf['query']=_[_0x46f3('0x3a')](_0x104334['query']);_0x111eaf[_0x46f3('0x3b')]=_[_0x46f3('0x3c')](_0x111eaf[_0x46f3('0x3d')],_0x111eaf[_0x46f3('0x39')]);_0x45b743[_0x46f3('0x3e')]=_[_0x46f3('0x3c')](_0x111eaf[_0x46f3('0x3d')],qs[_0x46f3('0x3f')](_0x104334[_0x46f3('0x39')][_0x46f3('0x3f')]));_0x45b743[_0x46f3('0x3e')]=_0x45b743[_0x46f3('0x3e')][_0x46f3('0x40')]?_0x45b743['attributes']:_0x111eaf[_0x46f3('0x3d')];_0x45b743[_0x46f3('0x42')]=qs[_0x46f3('0x43')](_0x104334[_0x46f3('0x39')][_0x46f3('0x43')]);_0x45b743[_0x46f3('0x44')]=qs[_0x46f3('0x3b')](_[_0x46f3('0x45')](_0x104334[_0x46f3('0x39')],_0x111eaf[_0x46f3('0x3b')]));if(_0x104334['query'][_0x46f3('0x49')]){_0x45b743['where']=_[_0x46f3('0x46')](_0x45b743[_0x46f3('0x44')],{'$or':_['map'](_0x45b743[_0x46f3('0x3e')],function(_0x1d5a85){var _0x23f88e={};_0x23f88e[_0x1d5a85]={'$like':'%'+_0x104334[_0x46f3('0x39')][_0x46f3('0x49')]+'%'};return _0x23f88e;})});}_0x45b743=_[_0x46f3('0x46')]({},_0x45b743,_0x104334[_0x46f3('0x4a')]);return _0x40973b[_0x46f3('0x5a')](_0x45b743);}})[_0x46f3('0x2e')](function(_0x4380ef){if(_0x4380ef){_0x150a8d=_0x4380ef[_0x46f3('0x40')];if(!_0x104334['query'][_0x46f3('0x5d')]('nolimit')){_0x45b743[_0x46f3('0x26')]=qs['limit'](_0x104334[_0x46f3('0x39')][_0x46f3('0x26')]);_0x45b743['offset']=qs[_0x46f3('0x24')](_0x104334['query'][_0x46f3('0x24')]);}return _0x40973b['getMembers'](_0x45b743);}})[_0x46f3('0x2e')](function(_0x1d2fee){if(_0x1d2fee){return _0x1d2fee?{'count':_0x150a8d,'rows':_0x1d2fee}:null;}})[_0x46f3('0x2e')](respondWithResult(_0x1ed0f9,null))[_0x46f3('0x4f')](handleError(_0x1ed0f9,null));};exports[_0x46f3('0x5e')]=function(_0x5510ef,_0x2029a3,_0x109a3c){return db[_0x46f3('0x35')][_0x46f3('0x52')]({'where':{'id':_0x5510ef['params']['id']}})[_0x46f3('0x2e')](handleEntityNotFound(_0x2029a3,null))['then'](function(_0x71c1fb){if(_0x71c1fb){return _0x71c1fb[_0x46f3('0x5e')](_0x5510ef[_0x46f3('0x39')][_0x46f3('0x58')]);}})[_0x46f3('0x2e')](respondWithStatusCode(_0x2029a3,null))[_0x46f3('0x4f')](handleError(_0x2029a3,null));};exports[_0x46f3('0x5f')]=function(_0x1bd08f,_0x447b8a,_0x5d8c9d){if(_0x1bd08f[_0x46f3('0x51')]['id']){delete _0x1bd08f[_0x46f3('0x51')]['id'];}return db['ChatGroup']['find']({'where':{'id':_0x1bd08f[_0x46f3('0x53')]['id']}})['then'](handleEntityNotFound(_0x447b8a,null))[_0x46f3('0x2e')](function(_0x22e24c){if(_0x22e24c){_0x1bd08f[_0x46f3('0x51')][_0x46f3('0x60')]=_0x22e24c['id'];return db[_0x46f3('0x61')][_0x46f3('0x62')](_0x1bd08f['body']);}})[_0x46f3('0x2e')](respondWithResult(_0x447b8a,null))['catch'](handleError(_0x447b8a,null));};exports[_0x46f3('0x63')]=function(_0x38694b,_0x1e5cd2,_0x4b1720){var _0x2719dc={};var _0x262d28={};var _0x2e412d;var _0x3aa102;return db[_0x46f3('0x35')][_0x46f3('0x5b')]({'where':{'id':_0x38694b[_0x46f3('0x53')]['id']}})['then'](handleEntityNotFound(_0x1e5cd2,null))[_0x46f3('0x2e')](function(_0x5a3e90){if(_0x5a3e90){_0x2e412d=_0x5a3e90;_0x262d28[_0x46f3('0x3d')]=_['keys'](db[_0x46f3('0x61')]['rawAttributes']);_0x262d28[_0x46f3('0x39')]=_[_0x46f3('0x3a')](_0x38694b[_0x46f3('0x39')]);_0x262d28[_0x46f3('0x3b')]=_[_0x46f3('0x3c')](_0x262d28[_0x46f3('0x3d')],_0x262d28['query']);_0x2719dc['attributes']=_['intersection'](_0x262d28[_0x46f3('0x3d')],qs[_0x46f3('0x3f')](_0x38694b['query']['fields']));_0x2719dc[_0x46f3('0x3e')]=_0x2719dc[_0x46f3('0x3e')][_0x46f3('0x40')]?_0x2719dc[_0x46f3('0x3e')]:_0x262d28[_0x46f3('0x3d')];_0x2719dc[_0x46f3('0x42')]=qs[_0x46f3('0x43')](_0x38694b[_0x46f3('0x39')]['sort']);_0x2719dc[_0x46f3('0x44')]=qs['filters'](_[_0x46f3('0x45')](_0x38694b[_0x46f3('0x39')],_0x262d28[_0x46f3('0x3b')]));if(_0x38694b[_0x46f3('0x39')]['filter']){_0x2719dc[_0x46f3('0x44')]=_[_0x46f3('0x46')](_0x2719dc['where'],{'$or':_['map'](_0x2719dc[_0x46f3('0x3e')],function(_0x4340eb){var _0x3a237b={};_0x3a237b[_0x4340eb]={'$like':'%'+_0x38694b[_0x46f3('0x39')]['filter']+'%'};return _0x3a237b;})});}if(_0x38694b[_0x46f3('0x39')][_0x46f3('0x64')]){var _0x3dbcef=_0x38694b[_0x46f3('0x39')]['$gte'][_0x46f3('0x65')](',');var _0x15a640={};_0x15a640[_0x3dbcef[0x0]]={'$gte':moment(_0x3dbcef[0x1])[_0x46f3('0x66')](_0x46f3('0x67'))};_0x2719dc[_0x46f3('0x44')]=_[_0x46f3('0x46')](_0x2719dc['where'],_0x15a640);}_0x2719dc=_[_0x46f3('0x46')]({},_0x2719dc,_0x38694b[_0x46f3('0x4a')]);return _0x2e412d[_0x46f3('0x63')](_0x2719dc);}})[_0x46f3('0x2e')](function(_0x4012dd){if(_0x4012dd){_0x3aa102=_0x4012dd[_0x46f3('0x40')];if(!_0x38694b[_0x46f3('0x39')][_0x46f3('0x5d')](_0x46f3('0x41'))){_0x2719dc[_0x46f3('0x26')]=qs[_0x46f3('0x26')](_0x38694b['query'][_0x46f3('0x26')]);_0x2719dc[_0x46f3('0x24')]=qs[_0x46f3('0x24')](_0x38694b['query']['offset']);}return _0x2e412d[_0x46f3('0x63')](_0x2719dc);}})['then'](function(_0x1be2a9){if(_0x1be2a9){return _0x1be2a9?{'count':_0x3aa102,'rows':_0x1be2a9}:null;}})['then'](respondWithResult(_0x1e5cd2,null))['catch'](handleError(_0x1e5cd2,null));};