Built motion from commit d9028345.|2.5.43
[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 _0x845f=['getMessages','$gte','split','format','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../config/license/hardware','../../config/utils','../../config/license/util','../../mysqldb','defaults','redis','./chatGroup.socket','register','sendStatus','status','end','offset','undefined','limit','count','set','Content-Range','json','apply','reject','save','then','destroy','stack','name','send','index','map','ChatGroup','rawAttributes','fieldName','type','model','query','keys','filters','intersection','attributes','fields','length','nolimit','sort','where','pick','filter','merge','options','includeAll','findAll','rows','catch','show','params','include','find','create','update','body','describe','getUnread','user','toInteger','addMembers','ids','getMembers','findOne','User','order','hasOwnProperty','removeMembers','addMessage','ChatInternalMessage'];(function(_0x1ac595,_0x5d2081){var _0x455fff=function(_0x3043ee){while(--_0x3043ee){_0x1ac595['push'](_0x1ac595['shift']());}};_0x455fff(++_0x5d2081);}(_0x845f,0x124));var _0xf845=function(_0x5d5bcb,_0x25e2ea){_0x5d5bcb=_0x5d5bcb-0x0;var _0x4ea5eb=_0x845f[_0x5d5bcb];return _0x4ea5eb;};'use strict';var emlformat=require(_0xf845('0x0'));var rimraf=require(_0xf845('0x1'));var zipdir=require(_0xf845('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf845('0x3'));var moment=require(_0xf845('0x4'));var BPromise=require(_0xf845('0x5'));var Mustache=require(_0xf845('0x6'));var util=require(_0xf845('0x7'));var path=require('path');var sox=require(_0xf845('0x8'));var csv=require(_0xf845('0x9'));var ejs=require(_0xf845('0xa'));var fs=require('fs');var fs_extra=require(_0xf845('0xb'));var _=require(_0xf845('0xc'));var squel=require(_0xf845('0xd'));var crypto=require(_0xf845('0xe'));var jsforce=require(_0xf845('0xf'));var deskjs=require(_0xf845('0x10'));var toCsv=require('to-csv');var querystring=require(_0xf845('0x11'));var Papa=require(_0xf845('0x12'));var Redis=require('ioredis');var authService=require(_0xf845('0x13'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf845('0x14'));var logger=require('../../config/logger')('api');var utils=require(_0xf845('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0xf845('0x16'));var db=require(_0xf845('0x17'))['db'];config['redis']=_[_0xf845('0x18')](config[_0xf845('0x19')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xf845('0x19')]));require(_0xf845('0x1a'))[_0xf845('0x1b')](socket);function respondWithStatusCode(_0x3e4dee,_0x57fd43){_0x57fd43=_0x57fd43||0xcc;return function(_0x3d0137){if(_0x3d0137){return _0x3e4dee[_0xf845('0x1c')](_0x57fd43);}return _0x3e4dee[_0xf845('0x1d')](_0x57fd43)[_0xf845('0x1e')]();};}function respondWithResult(_0x39cdd7,_0x559b39){_0x559b39=_0x559b39||0xc8;return function(_0x662638){if(_0x662638){return _0x39cdd7[_0xf845('0x1d')](_0x559b39)['json'](_0x662638);}};}function respondWithFilteredResult(_0x50fe4b,_0x3a79f8){return function(_0x1d0a8a){if(_0x1d0a8a){var _0x269a45=typeof _0x3a79f8[_0xf845('0x1f')]===_0xf845('0x20')&&typeof _0x3a79f8[_0xf845('0x21')]===_0xf845('0x20');var _0x8b8994=_0x1d0a8a[_0xf845('0x22')];var _0x50e54b=_0x269a45?0x0:_0x3a79f8[_0xf845('0x1f')];var _0x215297=_0x269a45?_0x1d0a8a[_0xf845('0x22')]:_0x3a79f8[_0xf845('0x1f')]+_0x3a79f8[_0xf845('0x21')];var _0x90f094;if(_0x215297>=_0x8b8994){_0x215297=_0x8b8994;_0x90f094=0xc8;}else{_0x90f094=0xce;}_0x50fe4b[_0xf845('0x1d')](_0x90f094);return _0x50fe4b[_0xf845('0x23')](_0xf845('0x24'),_0x50e54b+'-'+_0x215297+'/'+_0x8b8994)[_0xf845('0x25')](_0x1d0a8a);}return null;};}function patchUpdates(_0x4fd2bf){return function(_0x5538e2){try{jsonpatch[_0xf845('0x26')](_0x5538e2,_0x4fd2bf,!![]);}catch(_0x227dc5){return BPromise[_0xf845('0x27')](_0x227dc5);}return _0x5538e2[_0xf845('0x28')]();};}function saveUpdates(_0x41cf7e,_0x487636){return function(_0x5dc4cf){if(_0x5dc4cf){return _0x5dc4cf['update'](_0x41cf7e)[_0xf845('0x29')](function(_0x5ac6af){return _0x5ac6af;});}return null;};}function removeEntity(_0x1346b3,_0xa5b728){return function(_0xc3c06f){if(_0xc3c06f){return _0xc3c06f[_0xf845('0x2a')]()[_0xf845('0x29')](function(){_0x1346b3[_0xf845('0x1d')](0xcc)[_0xf845('0x1e')]();});}};}function handleEntityNotFound(_0x1a82d7,_0x310041){return function(_0x19e997){if(!_0x19e997){_0x1a82d7['sendStatus'](0x194);}return _0x19e997;};}function handleError(_0x7936b1,_0x4458df){_0x4458df=_0x4458df||0x1f4;return function(_0x1b43bf){logger['error'](_0x1b43bf[_0xf845('0x2b')]);if(_0x1b43bf[_0xf845('0x2c')]){delete _0x1b43bf[_0xf845('0x2c')];}_0x7936b1[_0xf845('0x1d')](_0x4458df)[_0xf845('0x2d')](_0x1b43bf);};}exports[_0xf845('0x2e')]=function(_0x361720,_0x3b1a64){var _0x1e8dfa={},_0xc804f8={},_0xb51467={'count':0x0,'rows':[]};var _0x50f7a3=_[_0xf845('0x2f')](db[_0xf845('0x30')][_0xf845('0x31')],function(_0x3f449c){return{'name':_0x3f449c[_0xf845('0x32')],'type':_0x3f449c[_0xf845('0x33')]['key']};});_0xc804f8[_0xf845('0x34')]=_['map'](_0x50f7a3,_0xf845('0x2c'));_0xc804f8[_0xf845('0x35')]=_[_0xf845('0x36')](_0x361720['query']);_0xc804f8[_0xf845('0x37')]=_[_0xf845('0x38')](_0xc804f8[_0xf845('0x34')],_0xc804f8[_0xf845('0x35')]);_0x1e8dfa[_0xf845('0x39')]=_[_0xf845('0x38')](_0xc804f8[_0xf845('0x34')],qs[_0xf845('0x3a')](_0x361720[_0xf845('0x35')][_0xf845('0x3a')]));_0x1e8dfa[_0xf845('0x39')]=_0x1e8dfa[_0xf845('0x39')][_0xf845('0x3b')]?_0x1e8dfa[_0xf845('0x39')]:_0xc804f8[_0xf845('0x34')];if(!_0x361720[_0xf845('0x35')]['hasOwnProperty'](_0xf845('0x3c'))){_0x1e8dfa[_0xf845('0x21')]=qs[_0xf845('0x21')](_0x361720[_0xf845('0x35')][_0xf845('0x21')]);_0x1e8dfa[_0xf845('0x1f')]=qs[_0xf845('0x1f')](_0x361720[_0xf845('0x35')][_0xf845('0x1f')]);}_0x1e8dfa['order']=qs[_0xf845('0x3d')](_0x361720[_0xf845('0x35')][_0xf845('0x3d')]);_0x1e8dfa[_0xf845('0x3e')]=qs['filters'](_[_0xf845('0x3f')](_0x361720[_0xf845('0x35')],_0xc804f8['filters']),_0x50f7a3);if(_0x361720[_0xf845('0x35')][_0xf845('0x40')]){_0x1e8dfa[_0xf845('0x3e')]=_[_0xf845('0x41')](_0x1e8dfa[_0xf845('0x3e')],{'$or':_[_0xf845('0x2f')](_0x50f7a3,function(_0x1332bb){if(_0x1332bb[_0xf845('0x33')]!=='VIRTUAL'){var _0x484069={};_0x484069[_0x1332bb[_0xf845('0x2c')]]={'$like':'%'+_0x361720[_0xf845('0x35')][_0xf845('0x40')]+'%'};return _0x484069;}})});}_0x1e8dfa=_['merge']({},_0x1e8dfa,_0x361720[_0xf845('0x42')]);var _0x301469={'where':_0x1e8dfa['where']};return db[_0xf845('0x30')][_0xf845('0x22')](_0x301469)[_0xf845('0x29')](function(_0x595c89){_0xb51467[_0xf845('0x22')]=_0x595c89;if(_0x361720[_0xf845('0x35')][_0xf845('0x43')]){_0x1e8dfa['include']=[{'all':!![]}];}return db[_0xf845('0x30')][_0xf845('0x44')](_0x1e8dfa);})[_0xf845('0x29')](function(_0x310e91){_0xb51467[_0xf845('0x45')]=_0x310e91;return _0xb51467;})[_0xf845('0x29')](respondWithFilteredResult(_0x3b1a64,_0x1e8dfa))[_0xf845('0x46')](handleError(_0x3b1a64,null));};exports[_0xf845('0x47')]=function(_0x7f2c34,_0x563a5d){var _0x3cf36c={'raw':!![],'where':{'id':_0x7f2c34[_0xf845('0x48')]['id']}},_0x46d1d1={};_0x46d1d1[_0xf845('0x34')]=_[_0xf845('0x36')](db[_0xf845('0x30')][_0xf845('0x31')]);_0x46d1d1['query']=_['keys'](_0x7f2c34[_0xf845('0x35')]);_0x46d1d1[_0xf845('0x37')]=_[_0xf845('0x38')](_0x46d1d1['model'],_0x46d1d1[_0xf845('0x35')]);_0x3cf36c[_0xf845('0x39')]=_[_0xf845('0x38')](_0x46d1d1[_0xf845('0x34')],qs['fields'](_0x7f2c34[_0xf845('0x35')][_0xf845('0x3a')]));_0x3cf36c[_0xf845('0x39')]=_0x3cf36c[_0xf845('0x39')]['length']?_0x3cf36c[_0xf845('0x39')]:_0x46d1d1[_0xf845('0x34')];if(_0x7f2c34['query'][_0xf845('0x43')]){_0x3cf36c[_0xf845('0x49')]=[{'all':!![]}];}_0x3cf36c=_[_0xf845('0x41')]({},_0x3cf36c,_0x7f2c34[_0xf845('0x42')]);return db['ChatGroup'][_0xf845('0x4a')](_0x3cf36c)[_0xf845('0x29')](handleEntityNotFound(_0x563a5d,null))[_0xf845('0x29')](respondWithResult(_0x563a5d,null))[_0xf845('0x46')](handleError(_0x563a5d,null));};exports['create']=function(_0x274a8a,_0x5fe4cf){return db[_0xf845('0x30')][_0xf845('0x4b')](_0x274a8a['body'],{})[_0xf845('0x29')](respondWithResult(_0x5fe4cf,0xc9))[_0xf845('0x46')](handleError(_0x5fe4cf,null));};exports[_0xf845('0x4c')]=function(_0x585821,_0x12ea74){if(_0x585821['body']['id']){delete _0x585821['body']['id'];}return db[_0xf845('0x30')][_0xf845('0x4a')]({'where':{'id':_0x585821['params']['id']}})['then'](handleEntityNotFound(_0x12ea74,null))['then'](saveUpdates(_0x585821[_0xf845('0x4d')],null))[_0xf845('0x29')](respondWithResult(_0x12ea74,null))['catch'](handleError(_0x12ea74,null));};exports[_0xf845('0x2a')]=function(_0x3ab6a7,_0xa2f870){return db['ChatGroup'][_0xf845('0x4a')]({'where':{'id':_0x3ab6a7[_0xf845('0x48')]['id']}})['then'](handleEntityNotFound(_0xa2f870,null))[_0xf845('0x29')](removeEntity(_0xa2f870,null))[_0xf845('0x46')](handleError(_0xa2f870,null));};exports['describe']=function(_0x75613a,_0xce9b1e){return db['ChatGroup'][_0xf845('0x4e')]()[_0xf845('0x29')](respondWithResult(_0xce9b1e,null))[_0xf845('0x46')](handleError(_0xce9b1e,null));};exports[_0xf845('0x4f')]=function(_0x352ea0,_0x4724ba,_0x369a6b){return db['ChatInternalMessage'][_0xf845('0x22')]({'where':{'ChatGroupId':_0x352ea0[_0xf845('0x48')]['id'],'ToId':_0x352ea0[_0xf845('0x50')]['id'],'read':![]}})['then'](function(_0xd52f7e){return{'id':_[_0xf845('0x51')](_0x352ea0[_0xf845('0x48')]['id']),'count':_0xd52f7e};})[_0xf845('0x29')](respondWithResult(_0x4724ba,null))[_0xf845('0x46')](handleError(_0x4724ba,null));};exports[_0xf845('0x52')]=function(_0xd09f03,_0xe75c7f,_0x5f5613){return db[_0xf845('0x30')]['find']({'where':{'id':_0xd09f03[_0xf845('0x48')]['id']}})['then'](handleEntityNotFound(_0xe75c7f,null))[_0xf845('0x29')](function(_0x413640){if(_0x413640){return _0x413640[_0xf845('0x52')](_0xd09f03[_0xf845('0x4d')]['ids'],_['omit'](_0xd09f03[_0xf845('0x4d')],[_0xf845('0x53'),'id'])||{});}})['then'](respondWithResult(_0xe75c7f,null))[_0xf845('0x46')](handleError(_0xe75c7f,null));};exports[_0xf845('0x54')]=function(_0xf0340e,_0xa73cfb,_0x3d1d49){var _0x136e5c={};var _0x1420b5={};var _0x358285;var _0x2e2d66;return db['ChatGroup'][_0xf845('0x55')]({'where':{'id':_0xf0340e[_0xf845('0x48')]['id']}})['then'](handleEntityNotFound(_0xa73cfb,null))[_0xf845('0x29')](function(_0x2a4aed){if(_0x2a4aed){_0x358285=_0x2a4aed;_0x1420b5['model']=_[_0xf845('0x36')](db[_0xf845('0x56')]['rawAttributes']);_0x1420b5[_0xf845('0x35')]=_['keys'](_0xf0340e['query']);_0x1420b5[_0xf845('0x37')]=_[_0xf845('0x38')](_0x1420b5[_0xf845('0x34')],_0x1420b5['query']);_0x136e5c['attributes']=_[_0xf845('0x38')](_0x1420b5[_0xf845('0x34')],qs[_0xf845('0x3a')](_0xf0340e[_0xf845('0x35')]['fields']));_0x136e5c[_0xf845('0x39')]=_0x136e5c[_0xf845('0x39')][_0xf845('0x3b')]?_0x136e5c[_0xf845('0x39')]:_0x1420b5[_0xf845('0x34')];_0x136e5c[_0xf845('0x57')]=qs[_0xf845('0x3d')](_0xf0340e[_0xf845('0x35')][_0xf845('0x3d')]);_0x136e5c[_0xf845('0x3e')]=qs[_0xf845('0x37')](_[_0xf845('0x3f')](_0xf0340e['query'],_0x1420b5[_0xf845('0x37')]));if(_0xf0340e[_0xf845('0x35')][_0xf845('0x40')]){_0x136e5c[_0xf845('0x3e')]=_[_0xf845('0x41')](_0x136e5c['where'],{'$or':_[_0xf845('0x2f')](_0x136e5c['attributes'],function(_0x8f5069){var _0x257c8a={};_0x257c8a[_0x8f5069]={'$like':'%'+_0xf0340e['query'][_0xf845('0x40')]+'%'};return _0x257c8a;})});}_0x136e5c=_[_0xf845('0x41')]({},_0x136e5c,_0xf0340e[_0xf845('0x42')]);return _0x358285['getMembers'](_0x136e5c);}})[_0xf845('0x29')](function(_0xc9336a){if(_0xc9336a){_0x2e2d66=_0xc9336a[_0xf845('0x3b')];if(!_0xf0340e['query'][_0xf845('0x58')]('nolimit')){_0x136e5c[_0xf845('0x21')]=qs[_0xf845('0x21')](_0xf0340e[_0xf845('0x35')][_0xf845('0x21')]);_0x136e5c[_0xf845('0x1f')]=qs[_0xf845('0x1f')](_0xf0340e[_0xf845('0x35')][_0xf845('0x1f')]);}return _0x358285[_0xf845('0x54')](_0x136e5c);}})[_0xf845('0x29')](function(_0x34f5b9){if(_0x34f5b9){return _0x34f5b9?{'count':_0x2e2d66,'rows':_0x34f5b9}:null;}})[_0xf845('0x29')](respondWithResult(_0xa73cfb,null))[_0xf845('0x46')](handleError(_0xa73cfb,null));};exports[_0xf845('0x59')]=function(_0x5a5195,_0x39cdac,_0x184321){return db[_0xf845('0x30')]['find']({'where':{'id':_0x5a5195[_0xf845('0x48')]['id']}})[_0xf845('0x29')](handleEntityNotFound(_0x39cdac,null))[_0xf845('0x29')](function(_0x432b0f){if(_0x432b0f){return _0x432b0f[_0xf845('0x59')](_0x5a5195[_0xf845('0x35')][_0xf845('0x53')]);}})['then'](respondWithStatusCode(_0x39cdac,null))['catch'](handleError(_0x39cdac,null));};exports[_0xf845('0x5a')]=function(_0x27aa41,_0x5683a9,_0x563a7f){if(_0x27aa41[_0xf845('0x4d')]['id']){delete _0x27aa41['body']['id'];}return db[_0xf845('0x30')]['find']({'where':{'id':_0x27aa41[_0xf845('0x48')]['id']}})[_0xf845('0x29')](handleEntityNotFound(_0x5683a9,null))['then'](function(_0x139842){if(_0x139842){_0x27aa41['body']['ChatGroupId']=_0x139842['id'];return db[_0xf845('0x5b')]['create'](_0x27aa41['body']);}})[_0xf845('0x29')](respondWithResult(_0x5683a9,null))[_0xf845('0x46')](handleError(_0x5683a9,null));};exports[_0xf845('0x5c')]=function(_0x46e179,_0x555132,_0x4271fe){var _0x1f9c37={};var _0x35b146={};var _0x585720;var _0x3d7d61;return db[_0xf845('0x30')]['findOne']({'where':{'id':_0x46e179[_0xf845('0x48')]['id']}})[_0xf845('0x29')](handleEntityNotFound(_0x555132,null))['then'](function(_0x1bcdd0){if(_0x1bcdd0){_0x585720=_0x1bcdd0;_0x35b146[_0xf845('0x34')]=_['keys'](db['ChatInternalMessage'][_0xf845('0x31')]);_0x35b146[_0xf845('0x35')]=_[_0xf845('0x36')](_0x46e179[_0xf845('0x35')]);_0x35b146[_0xf845('0x37')]=_[_0xf845('0x38')](_0x35b146[_0xf845('0x34')],_0x35b146['query']);_0x1f9c37[_0xf845('0x39')]=_[_0xf845('0x38')](_0x35b146[_0xf845('0x34')],qs[_0xf845('0x3a')](_0x46e179['query'][_0xf845('0x3a')]));_0x1f9c37[_0xf845('0x39')]=_0x1f9c37[_0xf845('0x39')][_0xf845('0x3b')]?_0x1f9c37[_0xf845('0x39')]:_0x35b146[_0xf845('0x34')];_0x1f9c37[_0xf845('0x57')]=qs['sort'](_0x46e179[_0xf845('0x35')][_0xf845('0x3d')]);_0x1f9c37[_0xf845('0x3e')]=qs[_0xf845('0x37')](_[_0xf845('0x3f')](_0x46e179['query'],_0x35b146['filters']));if(_0x46e179[_0xf845('0x35')][_0xf845('0x40')]){_0x1f9c37[_0xf845('0x3e')]=_[_0xf845('0x41')](_0x1f9c37[_0xf845('0x3e')],{'$or':_['map'](_0x1f9c37[_0xf845('0x39')],function(_0x1cd838){var _0x31a9bc={};_0x31a9bc[_0x1cd838]={'$like':'%'+_0x46e179[_0xf845('0x35')][_0xf845('0x40')]+'%'};return _0x31a9bc;})});}if(_0x46e179['query'][_0xf845('0x5d')]){var _0x34d12f=_0x46e179[_0xf845('0x35')]['$gte'][_0xf845('0x5e')](',');var _0x31efac={};_0x31efac[_0x34d12f[0x0]]={'$gte':moment(_0x34d12f[0x1])[_0xf845('0x5f')]('YYYY-MM-DD\x20HH:mm:ss')};_0x1f9c37[_0xf845('0x3e')]=_[_0xf845('0x41')](_0x1f9c37[_0xf845('0x3e')],_0x31efac);}_0x1f9c37=_[_0xf845('0x41')]({},_0x1f9c37,_0x46e179[_0xf845('0x42')]);return _0x585720[_0xf845('0x5c')](_0x1f9c37);}})['then'](function(_0x5cdbd3){if(_0x5cdbd3){_0x3d7d61=_0x5cdbd3['length'];if(!_0x46e179[_0xf845('0x35')][_0xf845('0x58')](_0xf845('0x3c'))){_0x1f9c37[_0xf845('0x21')]=qs[_0xf845('0x21')](_0x46e179[_0xf845('0x35')][_0xf845('0x21')]);_0x1f9c37['offset']=qs['offset'](_0x46e179['query'][_0xf845('0x1f')]);}return _0x585720[_0xf845('0x5c')](_0x1f9c37);}})[_0xf845('0x29')](function(_0xacfcb3){if(_0xacfcb3){return _0xacfcb3?{'count':_0x3d7d61,'rows':_0xacfcb3}:null;}})[_0xf845('0x29')](respondWithResult(_0x555132,null))[_0xf845('0x46')](handleError(_0x555132,null));};