Built motion from commit (unavailable).|2.5.13
[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 _0x56b9=['socket.io-emitter','./chatGroup.socket','register','jayson/promise','http','request','then','ChatGroup,\x20%s,\x20%s','request\x20sent','debug','stringify','code','error','message','info','result','catch','sendStatus','status','end','json','count','offset','limit','set','Content-Range','apply','reject','update','stack','name','send','index','map','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','hasOwnProperty','order','sort','where','pick','filter','merge','options','ChatGroup','include','findAll','show','length','find','create','body','params','destroy','describe','toInteger','addMembers','ids','getMembers','findOne','User','nolimit','removeMembers','ChatGroupId','getMessages','ChatInternalMessage','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','util','path','sox','ejs','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','redis','defaults','localhost'];(function(_0x28f09c,_0x5d4927){var _0x14df90=function(_0x1ce8ae){while(--_0x1ce8ae){_0x28f09c['push'](_0x28f09c['shift']());}};_0x14df90(++_0x5d4927);}(_0x56b9,0x196));var _0x956b=function(_0x211897,_0x12b0ac){_0x211897=_0x211897-0x0;var _0x35952f=_0x56b9[_0x211897];return _0x35952f;};'use strict';var pdf=require(_0x956b('0x0'));var emlformat=require(_0x956b('0x1'));var rimraf=require(_0x956b('0x2'));var zipdir=require(_0x956b('0x3'));var jsonpatch=require(_0x956b('0x4'));var rp=require(_0x956b('0x5'));var moment=require('moment');var BPromise=require(_0x956b('0x6'));var Mustache=require('mustache');var util=require(_0x956b('0x7'));var path=require(_0x956b('0x8'));var sox=require(_0x956b('0x9'));var csv=require('to-csv');var ejs=require(_0x956b('0xa'));var fs=require('fs');var _=require(_0x956b('0xb'));var squel=require(_0x956b('0xc'));var crypto=require(_0x956b('0xd'));var jsforce=require(_0x956b('0xe'));var deskjs=require(_0x956b('0xf'));var toCsv=require(_0x956b('0x10'));var querystring=require(_0x956b('0x11'));var Papa=require('papaparse');var Redis=require(_0x956b('0x12'));var authService=require(_0x956b('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0x956b('0x14'));var hardwareService=require(_0x956b('0x15'));var logger=require(_0x956b('0x16'))(_0x956b('0x17'));var utils=require(_0x956b('0x18'));var config=require('../../config/environment');var licenseUtil=require(_0x956b('0x19'));var db=require(_0x956b('0x1a'))['db'];config[_0x956b('0x1b')]=_[_0x956b('0x1c')](config[_0x956b('0x1b')],{'host':_0x956b('0x1d'),'port':0x18eb});var socket=require(_0x956b('0x1e'))(new Redis(config[_0x956b('0x1b')]));require(_0x956b('0x1f'))[_0x956b('0x20')](socket);var jayson=require(_0x956b('0x21'));var client=jayson['client'][_0x956b('0x22')]({'port':0x232a});function respondWithRpcPromise(_0x32a806,_0xe68d03,_0x4e4029,_0x96ea9d){return new BPromise(function(_0x3426a2,_0x3e37f1){var _0x3cc1b7=_0x96ea9d||client;return _0x3cc1b7[_0x956b('0x23')](_0x32a806,_0x4e4029)[_0x956b('0x24')](function(_0x978146){logger['info'](_0x956b('0x25'),_0xe68d03,_0x956b('0x26'));logger[_0x956b('0x27')]('ChatGroup,\x20%s,\x20%s,\x20%s',_0xe68d03,_0x956b('0x26'),JSON[_0x956b('0x28')](_0x978146));if(_0x978146['error']){if(_0x978146['error'][_0x956b('0x29')]===0x1f4){logger[_0x956b('0x2a')](_0x956b('0x25'),_0xe68d03,_0x978146['error'][_0x956b('0x2b')]);return _0x3e37f1(_0x978146[_0x956b('0x2a')][_0x956b('0x2b')]);}logger[_0x956b('0x2a')](_0x956b('0x25'),_0xe68d03,_0x978146[_0x956b('0x2a')]['message']);return _0x3426a2(_0x978146[_0x956b('0x2a')][_0x956b('0x2b')]);}else{logger[_0x956b('0x2c')](_0x956b('0x25'),_0xe68d03,_0x956b('0x26'));_0x3426a2(_0x978146[_0x956b('0x2d')][_0x956b('0x2b')]);}})[_0x956b('0x2e')](function(_0x634a65){logger[_0x956b('0x2a')](_0x956b('0x25'),_0xe68d03,_0x634a65);_0x3e37f1(_0x634a65);});});}function respondWithStatusCode(_0x512a68,_0x502875){_0x502875=_0x502875||0xcc;return function(_0x3940bb){if(_0x3940bb){return _0x512a68[_0x956b('0x2f')](_0x502875);}return _0x512a68[_0x956b('0x30')](_0x502875)[_0x956b('0x31')]();};}function respondWithResult(_0x91a931,_0xa0105e){_0xa0105e=_0xa0105e||0xc8;return function(_0x331c58){if(_0x331c58){return _0x91a931['status'](_0xa0105e)[_0x956b('0x32')](_0x331c58);}};}function respondWithFilteredResult(_0x30786d,_0x6268cb){return function(_0x3d33ad){if(_0x3d33ad){var _0x3d17a0=_0x3d33ad[_0x956b('0x33')],_0x33fb5e=_0x6268cb[_0x956b('0x34')],_0x1f692e=_0x6268cb[_0x956b('0x34')]+_0x6268cb[_0x956b('0x35')],_0x9c0e2e;if(_0x1f692e>=_0x3d17a0){_0x1f692e=_0x3d17a0;_0x9c0e2e=0xc8;}else{_0x9c0e2e=0xce;}_0x30786d[_0x956b('0x30')](_0x9c0e2e);return _0x30786d[_0x956b('0x36')](_0x956b('0x37'),_0x33fb5e+'-'+_0x1f692e+'/'+_0x3d17a0)[_0x956b('0x32')](_0x3d33ad);}return null;};}function patchUpdates(_0x46a67c){return function(_0x9a31fa){try{jsonpatch[_0x956b('0x38')](_0x9a31fa,_0x46a67c,!![]);}catch(_0x1c793f){return BPromise[_0x956b('0x39')](_0x1c793f);}return _0x9a31fa['save']();};}function saveUpdates(_0xb637d9,_0x128493){return function(_0x19a9a8){if(_0x19a9a8){return _0x19a9a8[_0x956b('0x3a')](_0xb637d9)[_0x956b('0x24')](function(_0x446870){return _0x446870;});}return null;};}function removeEntity(_0x3481eb,_0x2f930e){return function(_0x530e77){if(_0x530e77){return _0x530e77['destroy']()[_0x956b('0x24')](function(){_0x3481eb[_0x956b('0x30')](0xcc)[_0x956b('0x31')]();});}};}function handleEntityNotFound(_0x255cbb,_0x3be048){return function(_0x1b0a65){if(!_0x1b0a65){_0x255cbb[_0x956b('0x2f')](0x194);}return _0x1b0a65;};}function handleError(_0x5e1545,_0x2e00f8){_0x2e00f8=_0x2e00f8||0x1f4;return function(_0x4e6d7c){logger['error'](_0x4e6d7c[_0x956b('0x3b')]);if(_0x4e6d7c[_0x956b('0x3c')]){delete _0x4e6d7c[_0x956b('0x3c')];}_0x5e1545[_0x956b('0x30')](_0x2e00f8)[_0x956b('0x3d')](_0x4e6d7c);};}exports[_0x956b('0x3e')]=function(_0x468b38,_0x5865bb){var _0xafba20={},_0x465187={},_0x4cac1a={'count':0x0,'rows':[]};var _0x32acba=_[_0x956b('0x3f')](db['ChatGroup'][_0x956b('0x40')],function(_0x49e297){return{'name':_0x49e297[_0x956b('0x41')],'type':_0x49e297[_0x956b('0x42')][_0x956b('0x43')]};});_0x465187[_0x956b('0x44')]=_['map'](_0x32acba,'name');_0x465187[_0x956b('0x45')]=_[_0x956b('0x46')](_0x468b38[_0x956b('0x45')]);_0x465187[_0x956b('0x47')]=_[_0x956b('0x48')](_0x465187[_0x956b('0x44')],_0x465187['query']);_0xafba20[_0x956b('0x49')]=_[_0x956b('0x48')](_0x465187['model'],qs[_0x956b('0x4a')](_0x468b38['query']['fields']));_0xafba20[_0x956b('0x49')]=_0xafba20[_0x956b('0x49')]['length']?_0xafba20[_0x956b('0x49')]:_0x465187[_0x956b('0x44')];if(!_0x468b38['query'][_0x956b('0x4b')]('nolimit')){_0xafba20['limit']=qs[_0x956b('0x35')](_0x468b38[_0x956b('0x45')][_0x956b('0x35')]);_0xafba20['offset']=qs[_0x956b('0x34')](_0x468b38[_0x956b('0x45')][_0x956b('0x34')]);}_0xafba20[_0x956b('0x4c')]=qs[_0x956b('0x4d')](_0x468b38[_0x956b('0x45')][_0x956b('0x4d')]);_0xafba20[_0x956b('0x4e')]=qs[_0x956b('0x47')](_[_0x956b('0x4f')](_0x468b38[_0x956b('0x45')],_0x465187['filters']),_0x32acba);if(_0x468b38[_0x956b('0x45')][_0x956b('0x50')]){_0xafba20[_0x956b('0x4e')]=_['merge'](_0xafba20[_0x956b('0x4e')],{'$or':_[_0x956b('0x3f')](_0x32acba,function(_0x56d3b1){if(_0x56d3b1['type']!=='VIRTUAL'){var _0xcc2be2={};_0xcc2be2[_0x56d3b1[_0x956b('0x3c')]]={'$like':'%'+_0x468b38[_0x956b('0x45')][_0x956b('0x50')]+'%'};return _0xcc2be2;}})});}_0xafba20=_[_0x956b('0x51')]({},_0xafba20,_0x468b38[_0x956b('0x52')]);var _0x1ed099={'where':_0xafba20[_0x956b('0x4e')]};return db[_0x956b('0x53')][_0x956b('0x33')](_0x1ed099)['then'](function(_0x5efa71){_0x4cac1a['count']=_0x5efa71;if(_0x468b38['query']['includeAll']){_0xafba20[_0x956b('0x54')]=[{'all':!![]}];}return db[_0x956b('0x53')][_0x956b('0x55')](_0xafba20);})[_0x956b('0x24')](function(_0x1472db){_0x4cac1a['rows']=_0x1472db;return _0x4cac1a;})[_0x956b('0x24')](respondWithFilteredResult(_0x5865bb,_0xafba20))[_0x956b('0x2e')](handleError(_0x5865bb,null));};exports[_0x956b('0x56')]=function(_0x4f1cfd,_0x49f331){var _0x2f6fe2={'raw':!![],'where':{'id':_0x4f1cfd['params']['id']}},_0x387df0={};_0x387df0[_0x956b('0x44')]=_[_0x956b('0x46')](db[_0x956b('0x53')]['rawAttributes']);_0x387df0[_0x956b('0x45')]=_['keys'](_0x4f1cfd[_0x956b('0x45')]);_0x387df0[_0x956b('0x47')]=_[_0x956b('0x48')](_0x387df0[_0x956b('0x44')],_0x387df0[_0x956b('0x45')]);_0x2f6fe2[_0x956b('0x49')]=_[_0x956b('0x48')](_0x387df0[_0x956b('0x44')],qs[_0x956b('0x4a')](_0x4f1cfd[_0x956b('0x45')][_0x956b('0x4a')]));_0x2f6fe2[_0x956b('0x49')]=_0x2f6fe2[_0x956b('0x49')][_0x956b('0x57')]?_0x2f6fe2[_0x956b('0x49')]:_0x387df0['model'];if(_0x4f1cfd[_0x956b('0x45')]['includeAll']){_0x2f6fe2[_0x956b('0x54')]=[{'all':!![]}];}_0x2f6fe2=_[_0x956b('0x51')]({},_0x2f6fe2,_0x4f1cfd[_0x956b('0x52')]);return db[_0x956b('0x53')][_0x956b('0x58')](_0x2f6fe2)[_0x956b('0x24')](handleEntityNotFound(_0x49f331,null))['then'](respondWithResult(_0x49f331,null))['catch'](handleError(_0x49f331,null));};exports[_0x956b('0x59')]=function(_0x2807f9,_0x41c908){return db[_0x956b('0x53')]['create'](_0x2807f9[_0x956b('0x5a')],{})[_0x956b('0x24')](respondWithResult(_0x41c908,0xc9))[_0x956b('0x2e')](handleError(_0x41c908,null));};exports[_0x956b('0x3a')]=function(_0x26e7f9,_0x234b3f){if(_0x26e7f9[_0x956b('0x5a')]['id']){delete _0x26e7f9[_0x956b('0x5a')]['id'];}return db[_0x956b('0x53')][_0x956b('0x58')]({'where':{'id':_0x26e7f9[_0x956b('0x5b')]['id']}})[_0x956b('0x24')](handleEntityNotFound(_0x234b3f,null))[_0x956b('0x24')](saveUpdates(_0x26e7f9[_0x956b('0x5a')],null))[_0x956b('0x24')](respondWithResult(_0x234b3f,null))[_0x956b('0x2e')](handleError(_0x234b3f,null));};exports[_0x956b('0x5c')]=function(_0xe12e4c,_0x3e235c){return db['ChatGroup'][_0x956b('0x58')]({'where':{'id':_0xe12e4c[_0x956b('0x5b')]['id']}})[_0x956b('0x24')](handleEntityNotFound(_0x3e235c,null))['then'](removeEntity(_0x3e235c,null))['catch'](handleError(_0x3e235c,null));};exports['describe']=function(_0x2fba6a,_0x31bd22){return db[_0x956b('0x53')][_0x956b('0x5d')]()[_0x956b('0x24')](respondWithResult(_0x31bd22,null))['catch'](handleError(_0x31bd22,null));};exports['getUnread']=function(_0x28bbd3,_0x5a2bf3,_0x13e991){return db['ChatInternalMessage'][_0x956b('0x33')]({'where':{'ChatGroupId':_0x28bbd3['params']['id'],'ToId':_0x28bbd3['user']['id'],'read':![]}})['then'](function(_0x9fe6fc){return{'id':_[_0x956b('0x5e')](_0x28bbd3[_0x956b('0x5b')]['id']),'count':_0x9fe6fc};})['then'](respondWithResult(_0x5a2bf3,null))[_0x956b('0x2e')](handleError(_0x5a2bf3,null));};exports[_0x956b('0x5f')]=function(_0x1aa76f,_0x4365f4,_0x4a6beb){return db['ChatGroup']['find']({'where':{'id':_0x1aa76f[_0x956b('0x5b')]['id']}})[_0x956b('0x24')](handleEntityNotFound(_0x4365f4,null))['then'](function(_0x2e8261){if(_0x2e8261){return _0x2e8261[_0x956b('0x5f')](_0x1aa76f['body'][_0x956b('0x60')],_['omit'](_0x1aa76f[_0x956b('0x5a')],['ids','id'])||{});}})[_0x956b('0x24')](respondWithResult(_0x4365f4,null))[_0x956b('0x2e')](handleError(_0x4365f4,null));};exports[_0x956b('0x61')]=function(_0x34284d,_0xc809ff,_0x5ed404){var _0x431932={};var _0x3924a9={};var _0x75a57e;var _0x1476fa;return db[_0x956b('0x53')][_0x956b('0x62')]({'where':{'id':_0x34284d[_0x956b('0x5b')]['id']}})[_0x956b('0x24')](handleEntityNotFound(_0xc809ff,null))[_0x956b('0x24')](function(_0x913ce9){if(_0x913ce9){_0x75a57e=_0x913ce9;_0x3924a9[_0x956b('0x44')]=_[_0x956b('0x46')](db[_0x956b('0x63')][_0x956b('0x40')]);_0x3924a9[_0x956b('0x45')]=_[_0x956b('0x46')](_0x34284d['query']);_0x3924a9[_0x956b('0x47')]=_[_0x956b('0x48')](_0x3924a9[_0x956b('0x44')],_0x3924a9[_0x956b('0x45')]);_0x431932['attributes']=_[_0x956b('0x48')](_0x3924a9['model'],qs[_0x956b('0x4a')](_0x34284d[_0x956b('0x45')][_0x956b('0x4a')]));_0x431932['attributes']=_0x431932['attributes'][_0x956b('0x57')]?_0x431932[_0x956b('0x49')]:_0x3924a9[_0x956b('0x44')];_0x431932['order']=qs[_0x956b('0x4d')](_0x34284d[_0x956b('0x45')]['sort']);_0x431932[_0x956b('0x4e')]=qs[_0x956b('0x47')](_[_0x956b('0x4f')](_0x34284d[_0x956b('0x45')],_0x3924a9[_0x956b('0x47')]));if(_0x34284d[_0x956b('0x45')][_0x956b('0x50')]){_0x431932[_0x956b('0x4e')]=_[_0x956b('0x51')](_0x431932['where'],{'$or':_[_0x956b('0x3f')](_0x431932[_0x956b('0x49')],function(_0x57b3fe){var _0x4aa86a={};_0x4aa86a[_0x57b3fe]={'$like':'%'+_0x34284d[_0x956b('0x45')][_0x956b('0x50')]+'%'};return _0x4aa86a;})});}_0x431932=_[_0x956b('0x51')]({},_0x431932,_0x34284d['options']);return _0x75a57e[_0x956b('0x61')](_0x431932);}})['then'](function(_0x23a2c3){if(_0x23a2c3){_0x1476fa=_0x23a2c3[_0x956b('0x57')];if(!_0x34284d[_0x956b('0x45')][_0x956b('0x4b')](_0x956b('0x64'))){_0x431932[_0x956b('0x35')]=qs[_0x956b('0x35')](_0x34284d[_0x956b('0x45')][_0x956b('0x35')]);_0x431932['offset']=qs['offset'](_0x34284d[_0x956b('0x45')][_0x956b('0x34')]);}return _0x75a57e[_0x956b('0x61')](_0x431932);}})['then'](function(_0x1ad830){if(_0x1ad830){return _0x1ad830?{'count':_0x1476fa,'rows':_0x1ad830}:null;}})['then'](respondWithResult(_0xc809ff,null))[_0x956b('0x2e')](handleError(_0xc809ff,null));};exports[_0x956b('0x65')]=function(_0x50b5b,_0x39c45b,_0xaa06f7){return db[_0x956b('0x53')][_0x956b('0x58')]({'where':{'id':_0x50b5b[_0x956b('0x5b')]['id']}})[_0x956b('0x24')](handleEntityNotFound(_0x39c45b,null))[_0x956b('0x24')](function(_0x1a1009){if(_0x1a1009){return _0x1a1009[_0x956b('0x65')](_0x50b5b['query'][_0x956b('0x60')]);}})[_0x956b('0x24')](respondWithStatusCode(_0x39c45b,null))['catch'](handleError(_0x39c45b,null));};exports['addMessage']=function(_0x2f4973,_0x3ed02f,_0x32dea5){if(_0x2f4973[_0x956b('0x5a')]['id']){delete _0x2f4973[_0x956b('0x5a')]['id'];}return db[_0x956b('0x53')]['find']({'where':{'id':_0x2f4973[_0x956b('0x5b')]['id']}})[_0x956b('0x24')](handleEntityNotFound(_0x3ed02f,null))[_0x956b('0x24')](function(_0x1264a6){if(_0x1264a6){_0x2f4973[_0x956b('0x5a')][_0x956b('0x66')]=_0x1264a6['id'];return db['ChatInternalMessage'][_0x956b('0x59')](_0x2f4973['body']);}})[_0x956b('0x24')](respondWithResult(_0x3ed02f,null))[_0x956b('0x2e')](handleError(_0x3ed02f,null));};exports[_0x956b('0x67')]=function(_0x4de0b2,_0x3e0473,_0x430b9b){var _0x461383={};var _0x1e8953={};var _0x1f8405;var _0x531cba;return db['ChatGroup'][_0x956b('0x62')]({'where':{'id':_0x4de0b2[_0x956b('0x5b')]['id']}})['then'](handleEntityNotFound(_0x3e0473,null))['then'](function(_0x38e366){if(_0x38e366){_0x1f8405=_0x38e366;_0x1e8953['model']=_['keys'](db[_0x956b('0x68')][_0x956b('0x40')]);_0x1e8953[_0x956b('0x45')]=_['keys'](_0x4de0b2[_0x956b('0x45')]);_0x1e8953['filters']=_['intersection'](_0x1e8953['model'],_0x1e8953[_0x956b('0x45')]);_0x461383['attributes']=_[_0x956b('0x48')](_0x1e8953[_0x956b('0x44')],qs['fields'](_0x4de0b2[_0x956b('0x45')][_0x956b('0x4a')]));_0x461383['attributes']=_0x461383[_0x956b('0x49')][_0x956b('0x57')]?_0x461383[_0x956b('0x49')]:_0x1e8953['model'];_0x461383['order']=qs[_0x956b('0x4d')](_0x4de0b2[_0x956b('0x45')][_0x956b('0x4d')]);_0x461383[_0x956b('0x4e')]=qs[_0x956b('0x47')](_[_0x956b('0x4f')](_0x4de0b2['query'],_0x1e8953[_0x956b('0x47')]));if(_0x4de0b2['query'][_0x956b('0x50')]){_0x461383['where']=_[_0x956b('0x51')](_0x461383[_0x956b('0x4e')],{'$or':_[_0x956b('0x3f')](_0x461383['attributes'],function(_0xc7e389){var _0x5cbf1c={};_0x5cbf1c[_0xc7e389]={'$like':'%'+_0x4de0b2[_0x956b('0x45')][_0x956b('0x50')]+'%'};return _0x5cbf1c;})});}if(_0x4de0b2['query'][_0x956b('0x69')]){var _0x3052e0=_0x4de0b2[_0x956b('0x45')][_0x956b('0x69')][_0x956b('0x6a')](',');var _0x34bfc9={};_0x34bfc9[_0x3052e0[0x0]]={'$gte':moment(_0x3052e0[0x1])[_0x956b('0x6b')](_0x956b('0x6c'))};_0x461383[_0x956b('0x4e')]=_['merge'](_0x461383[_0x956b('0x4e')],_0x34bfc9);}_0x461383=_[_0x956b('0x51')]({},_0x461383,_0x4de0b2['options']);return _0x1f8405['getMessages'](_0x461383);}})[_0x956b('0x24')](function(_0x5cee44){if(_0x5cee44){_0x531cba=_0x5cee44[_0x956b('0x57')];if(!_0x4de0b2[_0x956b('0x45')]['hasOwnProperty'](_0x956b('0x64'))){_0x461383[_0x956b('0x35')]=qs[_0x956b('0x35')](_0x4de0b2[_0x956b('0x45')][_0x956b('0x35')]);_0x461383[_0x956b('0x34')]=qs[_0x956b('0x34')](_0x4de0b2[_0x956b('0x45')][_0x956b('0x34')]);}return _0x1f8405['getMessages'](_0x461383);}})[_0x956b('0x24')](function(_0x5c8ef5){if(_0x5c8ef5){return _0x5c8ef5?{'count':_0x531cba,'rows':_0x5c8ef5}:null;}})[_0x956b('0x24')](respondWithResult(_0x3e0473,null))['catch'](handleError(_0x3e0473,null));};