44c3221a19f6ca3f64be816cee7e993803cbc2b0
[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 _0xf416=['rimraf','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/utils','../../config/environment','../../config/license/util','redis','defaults','localhost','socket.io-emitter','./chatGroup.socket','register','sendStatus','status','json','undefined','limit','count','offset','set','Content-Range','apply','reject','save','update','then','destroy','error','name','send','map','rawAttributes','fieldName','key','model','query','keys','filters','intersection','attributes','fields','order','sort','where','pick','merge','type','VIRTUAL','filter','options','ChatGroup','includeAll','include','findAll','catch','show','params','length','find','create','body','describe','user','toInteger','addMembers','ids','omit','getMembers','findOne','User','nolimit','removeMembers','addMessage','ChatGroupId','ChatInternalMessage','$gte','split','YYYY-MM-DD\x20HH:mm:ss','getMessages','eml-format'];(function(_0x2a6572,_0x5c1f97){var _0x5482f9=function(_0x4f23cc){while(--_0x4f23cc){_0x2a6572['push'](_0x2a6572['shift']());}};_0x5482f9(++_0x5c1f97);}(_0xf416,0x183));var _0x6f41=function(_0x425fa5,_0x79182e){_0x425fa5=_0x425fa5-0x0;var _0x3d4402=_0xf416[_0x425fa5];return _0x3d4402;};'use strict';var emlformat=require(_0x6f41('0x0'));var rimraf=require(_0x6f41('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x6f41('0x2'));var rp=require(_0x6f41('0x3'));var moment=require('moment');var BPromise=require(_0x6f41('0x4'));var Mustache=require(_0x6f41('0x5'));var util=require(_0x6f41('0x6'));var path=require(_0x6f41('0x7'));var sox=require(_0x6f41('0x8'));var csv=require(_0x6f41('0x9'));var ejs=require(_0x6f41('0xa'));var fs=require('fs');var fs_extra=require(_0x6f41('0xb'));var _=require(_0x6f41('0xc'));var squel=require(_0x6f41('0xd'));var crypto=require(_0x6f41('0xe'));var jsforce=require(_0x6f41('0xf'));var deskjs=require(_0x6f41('0x10'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x6f41('0x11'));var Redis=require(_0x6f41('0x12'));var authService=require(_0x6f41('0x13'));var qs=require(_0x6f41('0x14'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')('api');var utils=require(_0x6f41('0x15'));var config=require(_0x6f41('0x16'));var licenseUtil=require(_0x6f41('0x17'));var db=require('../../mysqldb')['db'];config[_0x6f41('0x18')]=_[_0x6f41('0x19')](config[_0x6f41('0x18')],{'host':_0x6f41('0x1a'),'port':0x18eb});var socket=require(_0x6f41('0x1b'))(new Redis(config[_0x6f41('0x18')]));require(_0x6f41('0x1c'))[_0x6f41('0x1d')](socket);function respondWithStatusCode(_0x44e31a,_0x4eb09b){_0x4eb09b=_0x4eb09b||0xcc;return function(_0x30bd04){if(_0x30bd04){return _0x44e31a[_0x6f41('0x1e')](_0x4eb09b);}return _0x44e31a[_0x6f41('0x1f')](_0x4eb09b)['end']();};}function respondWithResult(_0x782a47,_0x30fdf1){_0x30fdf1=_0x30fdf1||0xc8;return function(_0x272437){if(_0x272437){return _0x782a47[_0x6f41('0x1f')](_0x30fdf1)[_0x6f41('0x20')](_0x272437);}};}function respondWithFilteredResult(_0x4c0102,_0x19db03){return function(_0x3b6af5){if(_0x3b6af5){var _0x28fef3=typeof _0x19db03['offset']===_0x6f41('0x21')&&typeof _0x19db03[_0x6f41('0x22')]===_0x6f41('0x21');var _0x2df3d4=_0x3b6af5[_0x6f41('0x23')];var _0x5a7408=_0x28fef3?0x0:_0x19db03['offset'];var _0x1497fb=_0x28fef3?_0x3b6af5[_0x6f41('0x23')]:_0x19db03[_0x6f41('0x24')]+_0x19db03[_0x6f41('0x22')];var _0x180714;if(_0x1497fb>=_0x2df3d4){_0x1497fb=_0x2df3d4;_0x180714=0xc8;}else{_0x180714=0xce;}_0x4c0102[_0x6f41('0x1f')](_0x180714);return _0x4c0102[_0x6f41('0x25')](_0x6f41('0x26'),_0x5a7408+'-'+_0x1497fb+'/'+_0x2df3d4)[_0x6f41('0x20')](_0x3b6af5);}return null;};}function patchUpdates(_0x1cff21){return function(_0x177a20){try{jsonpatch[_0x6f41('0x27')](_0x177a20,_0x1cff21,!![]);}catch(_0x2e2623){return BPromise[_0x6f41('0x28')](_0x2e2623);}return _0x177a20[_0x6f41('0x29')]();};}function saveUpdates(_0x135730,_0x42ad2c){return function(_0x801480){if(_0x801480){return _0x801480[_0x6f41('0x2a')](_0x135730)[_0x6f41('0x2b')](function(_0x5e29e2){return _0x5e29e2;});}return null;};}function removeEntity(_0x4f22c9,_0x32c730){return function(_0x36dd3c){if(_0x36dd3c){return _0x36dd3c[_0x6f41('0x2c')]()[_0x6f41('0x2b')](function(){_0x4f22c9[_0x6f41('0x1f')](0xcc)['end']();});}};}function handleEntityNotFound(_0x5bd0c9,_0x136993){return function(_0x25a620){if(!_0x25a620){_0x5bd0c9[_0x6f41('0x1e')](0x194);}return _0x25a620;};}function handleError(_0x492fd0,_0x1a5acc){_0x1a5acc=_0x1a5acc||0x1f4;return function(_0x55063f){logger[_0x6f41('0x2d')](_0x55063f['stack']);if(_0x55063f[_0x6f41('0x2e')]){delete _0x55063f[_0x6f41('0x2e')];}_0x492fd0['status'](_0x1a5acc)[_0x6f41('0x2f')](_0x55063f);};}exports['index']=function(_0x19be0a,_0x549a54){var _0x57b27a={},_0x3368f9={},_0x3023cb={'count':0x0,'rows':[]};var _0x5e15c6=_[_0x6f41('0x30')](db['ChatGroup'][_0x6f41('0x31')],function(_0x36ad4d){return{'name':_0x36ad4d[_0x6f41('0x32')],'type':_0x36ad4d['type'][_0x6f41('0x33')]};});_0x3368f9[_0x6f41('0x34')]=_['map'](_0x5e15c6,_0x6f41('0x2e'));_0x3368f9[_0x6f41('0x35')]=_[_0x6f41('0x36')](_0x19be0a[_0x6f41('0x35')]);_0x3368f9[_0x6f41('0x37')]=_[_0x6f41('0x38')](_0x3368f9[_0x6f41('0x34')],_0x3368f9['query']);_0x57b27a[_0x6f41('0x39')]=_['intersection'](_0x3368f9[_0x6f41('0x34')],qs[_0x6f41('0x3a')](_0x19be0a[_0x6f41('0x35')][_0x6f41('0x3a')]));_0x57b27a[_0x6f41('0x39')]=_0x57b27a[_0x6f41('0x39')]['length']?_0x57b27a[_0x6f41('0x39')]:_0x3368f9[_0x6f41('0x34')];if(!_0x19be0a[_0x6f41('0x35')]['hasOwnProperty']('nolimit')){_0x57b27a[_0x6f41('0x22')]=qs[_0x6f41('0x22')](_0x19be0a[_0x6f41('0x35')][_0x6f41('0x22')]);_0x57b27a['offset']=qs[_0x6f41('0x24')](_0x19be0a[_0x6f41('0x35')][_0x6f41('0x24')]);}_0x57b27a[_0x6f41('0x3b')]=qs[_0x6f41('0x3c')](_0x19be0a['query'][_0x6f41('0x3c')]);_0x57b27a[_0x6f41('0x3d')]=qs[_0x6f41('0x37')](_[_0x6f41('0x3e')](_0x19be0a[_0x6f41('0x35')],_0x3368f9[_0x6f41('0x37')]),_0x5e15c6);if(_0x19be0a['query']['filter']){_0x57b27a[_0x6f41('0x3d')]=_[_0x6f41('0x3f')](_0x57b27a['where'],{'$or':_[_0x6f41('0x30')](_0x5e15c6,function(_0x175fbf){if(_0x175fbf[_0x6f41('0x40')]!==_0x6f41('0x41')){var _0x4bb49b={};_0x4bb49b[_0x175fbf[_0x6f41('0x2e')]]={'$like':'%'+_0x19be0a['query'][_0x6f41('0x42')]+'%'};return _0x4bb49b;}})});}_0x57b27a=_[_0x6f41('0x3f')]({},_0x57b27a,_0x19be0a[_0x6f41('0x43')]);var _0x42b667={'where':_0x57b27a[_0x6f41('0x3d')]};return db[_0x6f41('0x44')]['count'](_0x42b667)[_0x6f41('0x2b')](function(_0x466528){_0x3023cb['count']=_0x466528;if(_0x19be0a[_0x6f41('0x35')][_0x6f41('0x45')]){_0x57b27a[_0x6f41('0x46')]=[{'all':!![]}];}return db['ChatGroup'][_0x6f41('0x47')](_0x57b27a);})[_0x6f41('0x2b')](function(_0x42e16f){_0x3023cb['rows']=_0x42e16f;return _0x3023cb;})[_0x6f41('0x2b')](respondWithFilteredResult(_0x549a54,_0x57b27a))[_0x6f41('0x48')](handleError(_0x549a54,null));};exports[_0x6f41('0x49')]=function(_0x38ce52,_0x2f46ff){var _0x1e66e9={'raw':!![],'where':{'id':_0x38ce52[_0x6f41('0x4a')]['id']}},_0x80aae0={};_0x80aae0[_0x6f41('0x34')]=_[_0x6f41('0x36')](db['ChatGroup'][_0x6f41('0x31')]);_0x80aae0['query']=_[_0x6f41('0x36')](_0x38ce52[_0x6f41('0x35')]);_0x80aae0[_0x6f41('0x37')]=_[_0x6f41('0x38')](_0x80aae0[_0x6f41('0x34')],_0x80aae0['query']);_0x1e66e9['attributes']=_['intersection'](_0x80aae0[_0x6f41('0x34')],qs[_0x6f41('0x3a')](_0x38ce52['query'][_0x6f41('0x3a')]));_0x1e66e9[_0x6f41('0x39')]=_0x1e66e9['attributes'][_0x6f41('0x4b')]?_0x1e66e9[_0x6f41('0x39')]:_0x80aae0[_0x6f41('0x34')];if(_0x38ce52[_0x6f41('0x35')][_0x6f41('0x45')]){_0x1e66e9['include']=[{'all':!![]}];}_0x1e66e9=_[_0x6f41('0x3f')]({},_0x1e66e9,_0x38ce52[_0x6f41('0x43')]);return db[_0x6f41('0x44')][_0x6f41('0x4c')](_0x1e66e9)['then'](handleEntityNotFound(_0x2f46ff,null))[_0x6f41('0x2b')](respondWithResult(_0x2f46ff,null))['catch'](handleError(_0x2f46ff,null));};exports[_0x6f41('0x4d')]=function(_0x15675a,_0x442bea){return db[_0x6f41('0x44')]['create'](_0x15675a[_0x6f41('0x4e')],{})[_0x6f41('0x2b')](respondWithResult(_0x442bea,0xc9))[_0x6f41('0x48')](handleError(_0x442bea,null));};exports['update']=function(_0xa76c41,_0x155c8e){if(_0xa76c41[_0x6f41('0x4e')]['id']){delete _0xa76c41[_0x6f41('0x4e')]['id'];}return db['ChatGroup']['find']({'where':{'id':_0xa76c41[_0x6f41('0x4a')]['id']}})[_0x6f41('0x2b')](handleEntityNotFound(_0x155c8e,null))[_0x6f41('0x2b')](saveUpdates(_0xa76c41[_0x6f41('0x4e')],null))[_0x6f41('0x2b')](respondWithResult(_0x155c8e,null))['catch'](handleError(_0x155c8e,null));};exports['destroy']=function(_0x11767d,_0x27d3cb){return db[_0x6f41('0x44')][_0x6f41('0x4c')]({'where':{'id':_0x11767d[_0x6f41('0x4a')]['id']}})[_0x6f41('0x2b')](handleEntityNotFound(_0x27d3cb,null))[_0x6f41('0x2b')](removeEntity(_0x27d3cb,null))[_0x6f41('0x48')](handleError(_0x27d3cb,null));};exports[_0x6f41('0x4f')]=function(_0x41a501,_0x44e96a){return db[_0x6f41('0x44')]['describe']()['then'](respondWithResult(_0x44e96a,null))[_0x6f41('0x48')](handleError(_0x44e96a,null));};exports['getUnread']=function(_0xbc86aa,_0x116010,_0x23f587){return db['ChatInternalMessage'][_0x6f41('0x23')]({'where':{'ChatGroupId':_0xbc86aa['params']['id'],'ToId':_0xbc86aa[_0x6f41('0x50')]['id'],'read':![]}})[_0x6f41('0x2b')](function(_0xa13500){return{'id':_[_0x6f41('0x51')](_0xbc86aa[_0x6f41('0x4a')]['id']),'count':_0xa13500};})[_0x6f41('0x2b')](respondWithResult(_0x116010,null))[_0x6f41('0x48')](handleError(_0x116010,null));};exports[_0x6f41('0x52')]=function(_0x35e999,_0x1313d5,_0x24e747){return db['ChatGroup'][_0x6f41('0x4c')]({'where':{'id':_0x35e999[_0x6f41('0x4a')]['id']}})['then'](handleEntityNotFound(_0x1313d5,null))[_0x6f41('0x2b')](function(_0x543ff9){if(_0x543ff9){return _0x543ff9[_0x6f41('0x52')](_0x35e999[_0x6f41('0x4e')][_0x6f41('0x53')],_[_0x6f41('0x54')](_0x35e999[_0x6f41('0x4e')],['ids','id'])||{});}})[_0x6f41('0x2b')](respondWithResult(_0x1313d5,null))[_0x6f41('0x48')](handleError(_0x1313d5,null));};exports[_0x6f41('0x55')]=function(_0x52e7b7,_0x399e56,_0x5362c4){var _0x65de2={};var _0x2afa43={};var _0x18a338;var _0x1c8dc4;return db['ChatGroup'][_0x6f41('0x56')]({'where':{'id':_0x52e7b7[_0x6f41('0x4a')]['id']}})[_0x6f41('0x2b')](handleEntityNotFound(_0x399e56,null))[_0x6f41('0x2b')](function(_0x2b06a7){if(_0x2b06a7){_0x18a338=_0x2b06a7;_0x2afa43[_0x6f41('0x34')]=_[_0x6f41('0x36')](db[_0x6f41('0x57')][_0x6f41('0x31')]);_0x2afa43['query']=_[_0x6f41('0x36')](_0x52e7b7[_0x6f41('0x35')]);_0x2afa43[_0x6f41('0x37')]=_['intersection'](_0x2afa43['model'],_0x2afa43['query']);_0x65de2['attributes']=_['intersection'](_0x2afa43['model'],qs[_0x6f41('0x3a')](_0x52e7b7[_0x6f41('0x35')][_0x6f41('0x3a')]));_0x65de2[_0x6f41('0x39')]=_0x65de2[_0x6f41('0x39')][_0x6f41('0x4b')]?_0x65de2[_0x6f41('0x39')]:_0x2afa43[_0x6f41('0x34')];_0x65de2[_0x6f41('0x3b')]=qs['sort'](_0x52e7b7[_0x6f41('0x35')][_0x6f41('0x3c')]);_0x65de2[_0x6f41('0x3d')]=qs['filters'](_[_0x6f41('0x3e')](_0x52e7b7[_0x6f41('0x35')],_0x2afa43['filters']));if(_0x52e7b7[_0x6f41('0x35')][_0x6f41('0x42')]){_0x65de2[_0x6f41('0x3d')]=_[_0x6f41('0x3f')](_0x65de2[_0x6f41('0x3d')],{'$or':_[_0x6f41('0x30')](_0x65de2['attributes'],function(_0x4fa1dd){var _0x4ef087={};_0x4ef087[_0x4fa1dd]={'$like':'%'+_0x52e7b7[_0x6f41('0x35')][_0x6f41('0x42')]+'%'};return _0x4ef087;})});}_0x65de2=_[_0x6f41('0x3f')]({},_0x65de2,_0x52e7b7['options']);return _0x18a338[_0x6f41('0x55')](_0x65de2);}})[_0x6f41('0x2b')](function(_0x5214e){if(_0x5214e){_0x1c8dc4=_0x5214e['length'];if(!_0x52e7b7[_0x6f41('0x35')]['hasOwnProperty'](_0x6f41('0x58'))){_0x65de2[_0x6f41('0x22')]=qs[_0x6f41('0x22')](_0x52e7b7['query'][_0x6f41('0x22')]);_0x65de2[_0x6f41('0x24')]=qs[_0x6f41('0x24')](_0x52e7b7[_0x6f41('0x35')][_0x6f41('0x24')]);}return _0x18a338[_0x6f41('0x55')](_0x65de2);}})[_0x6f41('0x2b')](function(_0x458685){if(_0x458685){return _0x458685?{'count':_0x1c8dc4,'rows':_0x458685}:null;}})[_0x6f41('0x2b')](respondWithResult(_0x399e56,null))['catch'](handleError(_0x399e56,null));};exports['removeMembers']=function(_0x427e2f,_0x139b53,_0x21c8ea){return db[_0x6f41('0x44')][_0x6f41('0x4c')]({'where':{'id':_0x427e2f[_0x6f41('0x4a')]['id']}})[_0x6f41('0x2b')](handleEntityNotFound(_0x139b53,null))[_0x6f41('0x2b')](function(_0x53e486){if(_0x53e486){return _0x53e486[_0x6f41('0x59')](_0x427e2f['query'][_0x6f41('0x53')]);}})['then'](respondWithStatusCode(_0x139b53,null))['catch'](handleError(_0x139b53,null));};exports[_0x6f41('0x5a')]=function(_0xa65d9e,_0x349dd8,_0x433c12){if(_0xa65d9e[_0x6f41('0x4e')]['id']){delete _0xa65d9e[_0x6f41('0x4e')]['id'];}return db[_0x6f41('0x44')][_0x6f41('0x4c')]({'where':{'id':_0xa65d9e[_0x6f41('0x4a')]['id']}})['then'](handleEntityNotFound(_0x349dd8,null))[_0x6f41('0x2b')](function(_0x3a8059){if(_0x3a8059){_0xa65d9e['body'][_0x6f41('0x5b')]=_0x3a8059['id'];return db['ChatInternalMessage'][_0x6f41('0x4d')](_0xa65d9e['body']);}})['then'](respondWithResult(_0x349dd8,null))[_0x6f41('0x48')](handleError(_0x349dd8,null));};exports['getMessages']=function(_0x4dbfea,_0xb3f9ec,_0x1f7926){var _0x578bf4={};var _0x3de8e8={};var _0x39add3;var _0x530734;return db[_0x6f41('0x44')][_0x6f41('0x56')]({'where':{'id':_0x4dbfea['params']['id']}})[_0x6f41('0x2b')](handleEntityNotFound(_0xb3f9ec,null))['then'](function(_0x4ff6cb){if(_0x4ff6cb){_0x39add3=_0x4ff6cb;_0x3de8e8['model']=_[_0x6f41('0x36')](db[_0x6f41('0x5c')][_0x6f41('0x31')]);_0x3de8e8[_0x6f41('0x35')]=_[_0x6f41('0x36')](_0x4dbfea['query']);_0x3de8e8['filters']=_[_0x6f41('0x38')](_0x3de8e8[_0x6f41('0x34')],_0x3de8e8['query']);_0x578bf4['attributes']=_[_0x6f41('0x38')](_0x3de8e8[_0x6f41('0x34')],qs[_0x6f41('0x3a')](_0x4dbfea[_0x6f41('0x35')][_0x6f41('0x3a')]));_0x578bf4[_0x6f41('0x39')]=_0x578bf4['attributes']['length']?_0x578bf4[_0x6f41('0x39')]:_0x3de8e8[_0x6f41('0x34')];_0x578bf4['order']=qs['sort'](_0x4dbfea[_0x6f41('0x35')][_0x6f41('0x3c')]);_0x578bf4[_0x6f41('0x3d')]=qs['filters'](_[_0x6f41('0x3e')](_0x4dbfea[_0x6f41('0x35')],_0x3de8e8[_0x6f41('0x37')]));if(_0x4dbfea[_0x6f41('0x35')][_0x6f41('0x42')]){_0x578bf4[_0x6f41('0x3d')]=_[_0x6f41('0x3f')](_0x578bf4['where'],{'$or':_['map'](_0x578bf4[_0x6f41('0x39')],function(_0x4eae2d){var _0x1297f5={};_0x1297f5[_0x4eae2d]={'$like':'%'+_0x4dbfea['query'][_0x6f41('0x42')]+'%'};return _0x1297f5;})});}if(_0x4dbfea['query']['$gte']){var _0x193d82=_0x4dbfea[_0x6f41('0x35')][_0x6f41('0x5d')][_0x6f41('0x5e')](',');var _0x4ff0db={};_0x4ff0db[_0x193d82[0x0]]={'$gte':moment(_0x193d82[0x1])['format'](_0x6f41('0x5f'))};_0x578bf4[_0x6f41('0x3d')]=_[_0x6f41('0x3f')](_0x578bf4['where'],_0x4ff0db);}_0x578bf4=_[_0x6f41('0x3f')]({},_0x578bf4,_0x4dbfea['options']);return _0x39add3[_0x6f41('0x60')](_0x578bf4);}})[_0x6f41('0x2b')](function(_0xce5b6){if(_0xce5b6){_0x530734=_0xce5b6[_0x6f41('0x4b')];if(!_0x4dbfea[_0x6f41('0x35')]['hasOwnProperty'](_0x6f41('0x58'))){_0x578bf4['limit']=qs[_0x6f41('0x22')](_0x4dbfea[_0x6f41('0x35')][_0x6f41('0x22')]);_0x578bf4[_0x6f41('0x24')]=qs[_0x6f41('0x24')](_0x4dbfea[_0x6f41('0x35')]['offset']);}return _0x39add3['getMessages'](_0x578bf4);}})[_0x6f41('0x2b')](function(_0x55a283){if(_0x55a283){return _0x55a283?{'count':_0x530734,'rows':_0x55a283}:null;}})['then'](respondWithResult(_0xb3f9ec,null))[_0x6f41('0x48')](handleError(_0xb3f9ec,null));};