Built motion from commit (unavailable).|2.0.74
[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 _0x19be=['day','merge','filter','map','options','ChatInternalMessage','includeAll','findAll','rows','catch','show','params','include','find','create','body','describe','messages','lastMessageAt','createdAt','YYYY-MM-DD\x20HH:mm:ss','getUsers','User','agent','user','fullname','userpic','internal','lastLoginAt','allowmessenger','-online','VoiceQueue','push','FromId','ToId','read','-createdAt','all','fast-json-patch','request-promise','moment','bluebird','util','sox','to-csv','lodash','desk.js','querystring','papaparse','../../components/parsers/qs','../../config/logger','../../config/environment','../../mysqldb','socket.io-emitter','localhost','./chatInternalMessage.socket','register','sendStatus','status','end','json','count','offset','limit','set','apply','reject','save','update','then','destroy','stack','name','index','rawAttributes','model','keys','query','dateStart','dateEnd','filters','intersection','fields','attributes','length','nolimit','sort','where'];(function(_0x3f54f4,_0x988092){var _0x2008de=function(_0x399831){while(--_0x399831){_0x3f54f4['push'](_0x3f54f4['shift']());}};_0x2008de(++_0x988092);}(_0x19be,0x1de));var _0xe19b=function(_0x49f876,_0x232e17){_0x49f876=_0x49f876-0x0;var _0x2d5f8c=_0x19be[_0x49f876];return _0x2d5f8c;};'use strict';var jsonpatch=require(_0xe19b('0x0'));var rp=require(_0xe19b('0x1'));var moment=require(_0xe19b('0x2'));var BPromise=require(_0xe19b('0x3'));var Mustache=require('mustache');var util=require(_0xe19b('0x4'));var path=require('path');var sox=require(_0xe19b('0x5'));var csv=require(_0xe19b('0x6'));var ejs=require('ejs');var fs=require('fs');var _=require(_0xe19b('0x7'));var squel=require('squel');var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xe19b('0x8'));var toCsv=require('to-csv');var querystring=require(_0xe19b('0x9'));var Papa=require(_0xe19b('0xa'));var qs=require(_0xe19b('0xb'));var logger=require(_0xe19b('0xc'))('api');var utils=require('../../config/utils');var config=require(_0xe19b('0xd'));var db=require(_0xe19b('0xe'))['db'];var socket=require(_0xe19b('0xf'))({'host':_0xe19b('0x10'),'port':0x18eb});require(_0xe19b('0x11'))[_0xe19b('0x12')](socket);function respondWithStatusCode(_0x3fbcd3,_0x376cf4){_0x376cf4=_0x376cf4||0xcc;return function(_0x32da06){if(_0x32da06){return _0x3fbcd3[_0xe19b('0x13')](_0x376cf4);}return _0x3fbcd3[_0xe19b('0x14')](_0x376cf4)[_0xe19b('0x15')]();};}function respondWithResult(_0xfe565f,_0x29d7f5){_0x29d7f5=_0x29d7f5||0xc8;return function(_0x403835){if(_0x403835){return _0xfe565f[_0xe19b('0x14')](_0x29d7f5)[_0xe19b('0x16')](_0x403835);}};}function respondWithFilteredResult(_0x1bd3dd,_0x530f7d){return function(_0x5451dd){if(_0x5451dd){var _0xf5b854=_0x5451dd[_0xe19b('0x17')],_0x6895d9=_0x530f7d[_0xe19b('0x18')],_0x244a43=_0x530f7d[_0xe19b('0x18')]+_0x530f7d[_0xe19b('0x19')],_0x3e94f3;if(_0x244a43>=_0xf5b854){_0x244a43=_0xf5b854;_0x3e94f3=0xc8;}else{_0x3e94f3=0xce;}_0x1bd3dd[_0xe19b('0x14')](_0x3e94f3);return _0x1bd3dd[_0xe19b('0x1a')]('Content-Range',_0x6895d9+'-'+_0x244a43+'/'+_0xf5b854)['json'](_0x5451dd);}return null;};}function patchUpdates(_0x32adb4){return function(_0x3e6fa1){try{jsonpatch[_0xe19b('0x1b')](_0x3e6fa1,_0x32adb4,!![]);}catch(_0x4a011d){return BPromise[_0xe19b('0x1c')](_0x4a011d);}return _0x3e6fa1[_0xe19b('0x1d')]();};}function saveUpdates(_0x54dcd7,_0x56413e){return function(_0x412d32){if(_0x412d32){return _0x412d32[_0xe19b('0x1e')](_0x54dcd7)[_0xe19b('0x1f')](function(_0xe3dc1b){return _0xe3dc1b;});}return null;};}function removeEntity(_0x54c32d,_0x412a6a){return function(_0x2f7f3b){if(_0x2f7f3b){return _0x2f7f3b[_0xe19b('0x20')]()[_0xe19b('0x1f')](function(){_0x54c32d[_0xe19b('0x14')](0xcc)[_0xe19b('0x15')]();});}};}function handleEntityNotFound(_0x1e7e45,_0x4e4fee){return function(_0x1e2b41){if(!_0x1e2b41){_0x1e7e45[_0xe19b('0x13')](0x194);}return _0x1e2b41;};}function handleError(_0x4dfe6f,_0xef87){_0xef87=_0xef87||0x1f4;return function(_0x7d35ab){logger['error'](_0x7d35ab[_0xe19b('0x21')]);if(_0x7d35ab[_0xe19b('0x22')]){delete _0x7d35ab['name'];}_0x4dfe6f[_0xe19b('0x14')](_0xef87)['send'](_0x7d35ab);};}exports[_0xe19b('0x23')]=function(_0x24a959,_0x34d390){var _0x273bbb={},_0x284338={},_0x55f54c={'count':0x0,'rows':[]};var _0x45b053=db['ChatInternalMessage'][_0xe19b('0x24')];_0x284338[_0xe19b('0x25')]=_[_0xe19b('0x26')](_0x45b053);var _0x4cbb40={'dateStart':_0x24a959[_0xe19b('0x27')][_0xe19b('0x28')],'dateEnd':_0x24a959['query'][_0xe19b('0x29')]};delete _0x24a959[_0xe19b('0x27')]['dateStart'];delete _0x24a959['query']['dateEnd'];_0x284338[_0xe19b('0x27')]=_[_0xe19b('0x26')](_0x24a959[_0xe19b('0x27')]);_0x284338[_0xe19b('0x2a')]=_[_0xe19b('0x2b')](_0x284338[_0xe19b('0x25')],_0x284338[_0xe19b('0x27')]);_0x273bbb['attributes']=_[_0xe19b('0x2b')](_0x284338[_0xe19b('0x25')],qs[_0xe19b('0x2c')](_0x24a959[_0xe19b('0x27')][_0xe19b('0x2c')]));_0x273bbb[_0xe19b('0x2d')]=_0x273bbb[_0xe19b('0x2d')][_0xe19b('0x2e')]?_0x273bbb['attributes']:_0x284338[_0xe19b('0x25')];if(!_0x24a959[_0xe19b('0x27')]['hasOwnProperty'](_0xe19b('0x2f'))){_0x273bbb[_0xe19b('0x19')]=qs[_0xe19b('0x19')](_0x24a959[_0xe19b('0x27')][_0xe19b('0x19')]);_0x273bbb[_0xe19b('0x18')]=qs[_0xe19b('0x18')](_0x24a959['query']['offset']);}_0x273bbb['order']=qs[_0xe19b('0x30')](_0x24a959[_0xe19b('0x27')][_0xe19b('0x30')]);_0x273bbb[_0xe19b('0x31')]=qs[_0xe19b('0x2a')](_['pick'](_0x24a959['query'],_0x284338['filters']));if(_0x4cbb40[_0xe19b('0x28')]){if(_0x4cbb40[_0xe19b('0x29')]){_0x4cbb40[_0xe19b('0x29')]=moment(_0x4cbb40[_0xe19b('0x29')])['add'](0x1,_0xe19b('0x32'));}else{_0x4cbb40['dateEnd']=moment(_0x4cbb40[_0xe19b('0x28')])['add'](0x1,_0xe19b('0x32'));}_[_0xe19b('0x33')](_0x273bbb[_0xe19b('0x31')],{'createdAt':{'$gte':_0x4cbb40[_0xe19b('0x28')],'$lte':_0x4cbb40[_0xe19b('0x29')]}});}if(_0x24a959['query'][_0xe19b('0x34')]){_0x273bbb[_0xe19b('0x31')]=_[_0xe19b('0x33')](_0x273bbb[_0xe19b('0x31')],{'$or':_[_0xe19b('0x35')](_0x273bbb[_0xe19b('0x2d')],function(_0x224725){var _0x57485d={};_0x57485d[_0x224725]={'$like':'%'+_0x24a959[_0xe19b('0x27')]['filter']+'%'};return _0x57485d;})});}_0x273bbb=_[_0xe19b('0x33')]({},_0x273bbb,_0x24a959[_0xe19b('0x36')]);var _0x232026={'where':_0x273bbb['where']};return db[_0xe19b('0x37')]['count'](_0x232026)['then'](function(_0x59c697){_0x55f54c['count']=_0x59c697;if(_0x24a959['query'][_0xe19b('0x38')]){_0x273bbb['include']=[{'all':!![]}];}return db[_0xe19b('0x37')][_0xe19b('0x39')](_0x273bbb);})[_0xe19b('0x1f')](function(_0x7acb21){_0x55f54c[_0xe19b('0x3a')]=_0x7acb21;return _0x55f54c;})[_0xe19b('0x1f')](respondWithFilteredResult(_0x34d390,_0x273bbb))[_0xe19b('0x3b')](handleError(_0x34d390,null));};exports[_0xe19b('0x3c')]=function(_0x56f0ac,_0x1059ac){var _0x4b5b01={'raw':!![],'where':{'id':_0x56f0ac[_0xe19b('0x3d')]['id']}},_0x4ad549={};_0x4ad549[_0xe19b('0x25')]=_[_0xe19b('0x26')](db[_0xe19b('0x37')][_0xe19b('0x24')]);_0x4ad549['query']=_[_0xe19b('0x26')](_0x56f0ac[_0xe19b('0x27')]);_0x4ad549[_0xe19b('0x2a')]=_[_0xe19b('0x2b')](_0x4ad549[_0xe19b('0x25')],_0x4ad549['query']);_0x4b5b01[_0xe19b('0x2d')]=_[_0xe19b('0x2b')](_0x4ad549[_0xe19b('0x25')],qs[_0xe19b('0x2c')](_0x56f0ac[_0xe19b('0x27')]['fields']));_0x4b5b01['attributes']=_0x4b5b01[_0xe19b('0x2d')][_0xe19b('0x2e')]?_0x4b5b01['attributes']:_0x4ad549[_0xe19b('0x25')];if(_0x56f0ac[_0xe19b('0x27')]['includeAll']){_0x4b5b01[_0xe19b('0x3e')]=[{'all':!![]}];}_0x4b5b01=_[_0xe19b('0x33')]({},_0x4b5b01,_0x56f0ac[_0xe19b('0x36')]);return db[_0xe19b('0x37')][_0xe19b('0x3f')](_0x4b5b01)[_0xe19b('0x1f')](handleEntityNotFound(_0x1059ac,null))[_0xe19b('0x1f')](respondWithResult(_0x1059ac,null))[_0xe19b('0x3b')](handleError(_0x1059ac,null));};exports[_0xe19b('0x40')]=function(_0x22dccd,_0x77c44a){return db[_0xe19b('0x37')][_0xe19b('0x40')](_0x22dccd[_0xe19b('0x41')],{})[_0xe19b('0x1f')](respondWithResult(_0x77c44a,0xc9))['catch'](handleError(_0x77c44a,null));};exports[_0xe19b('0x1e')]=function(_0x137c81,_0x2d56a0){if(_0x137c81['body']['id']){delete _0x137c81[_0xe19b('0x41')]['id'];}return db[_0xe19b('0x37')][_0xe19b('0x3f')]({'where':{'id':_0x137c81[_0xe19b('0x3d')]['id']}})['then'](handleEntityNotFound(_0x2d56a0,null))[_0xe19b('0x1f')](saveUpdates(_0x137c81[_0xe19b('0x41')],null))[_0xe19b('0x1f')](respondWithResult(_0x2d56a0,null))['catch'](handleError(_0x2d56a0,null));};exports[_0xe19b('0x20')]=function(_0x539509,_0x4852ec){return db[_0xe19b('0x37')]['find']({'where':{'id':_0x539509[_0xe19b('0x3d')]['id']}})[_0xe19b('0x1f')](handleEntityNotFound(_0x4852ec,null))[_0xe19b('0x1f')](removeEntity(_0x4852ec,null))['catch'](handleError(_0x4852ec,null));};exports[_0xe19b('0x42')]=function(_0x2f34bd,_0x1f45ca){return db[_0xe19b('0x37')][_0xe19b('0x42')]()[_0xe19b('0x1f')](respondWithResult(_0x1f45ca,null))[_0xe19b('0x3b')](handleError(_0x1f45ca,null));};function wrapUser(_0x285916){return function(_0x2c93cc){var _0x25221b=_0x285916['get']({'plain':!![]});_0x25221b[_0xe19b('0x43')]=[];if(_0x2c93cc){_0x25221b[_0xe19b('0x44')]=_0x2c93cc[_0xe19b('0x45')];_0x25221b['lastMessage']=_0x2c93cc;}else{_0x25221b[_0xe19b('0x44')]=moment(0x0)['format'](_0xe19b('0x46'));_0x25221b['lastMessage']={};}return _0x25221b;};}exports[_0xe19b('0x47')]=function(_0x403a80,_0x349396,_0xda84b0){var _0x31ee9e=[];return db[_0xe19b('0x48')][_0xe19b('0x39')]({'where':{'role':[_0xe19b('0x49'),'admin',_0xe19b('0x4a')]},'attributes':['id',_0xe19b('0x4b'),_0xe19b('0x22'),'role',_0xe19b('0x4c'),_0xe19b('0x4d'),'online',_0xe19b('0x4e'),_0xe19b('0x4f'),'showWebBar'],'order':qs[_0xe19b('0x30')](_0xe19b('0x50')),'include':[{'model':db[_0xe19b('0x51')],'as':'VoiceQueues','attributes':['id',_0xe19b('0x22'),'type'],'through':{'attributes':[]}}]})['then'](function(_0xf35aa){var _0x2c8987=[];if(_0xf35aa){_0x31ee9e=_0xf35aa;for(var _0x576b05=0x0;_0x576b05<_0xf35aa[_0xe19b('0x2e')];_0x576b05+=0x1){_0x31ee9e[_0x576b05][_0xe19b('0x43')]=[];_0x2c8987[_0xe19b('0x52')](db['ChatInternalMessage'][_0xe19b('0x3f')]({'where':{'FromId':[_0x403a80['user']['id'],_0xf35aa[_0x576b05]['id']],'ToId':[_0x403a80[_0xe19b('0x4a')]['id'],_0xf35aa[_0x576b05]['id']],'ChatGroupId':null},'attributes':['id',_0xe19b('0x45'),_0xe19b('0x53'),_0xe19b('0x54'),_0xe19b('0x55'),_0xe19b('0x41')],'order':qs['sort'](_0xe19b('0x56')),'limit':0x1,'offset':0x0,'raw':!![]})[_0xe19b('0x1f')](wrapUser(_0xf35aa[_0x576b05])));}return BPromise[_0xe19b('0x57')](_0x2c8987);}return[];})[_0xe19b('0x1f')](function(_0x34a91d){return{'rows':_0x34a91d,'count':_0x34a91d[_0xe19b('0x2e')]};})[_0xe19b('0x1f')](respondWithResult(_0x349396,null))[_0xe19b('0x3b')](handleError(_0x349396,null));};