cf9256316102cdff08df1f9ea3af835a48075248
[motion2.git] / server / api / squareMessage / squareMessage.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 _0xd2e0=['path','sox','to-csv','fs-extra','lodash','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','end','json','offset','undefined','count','limit','status','Content-Range','apply','reject','save','update','destroy','stack','name','send','map','SquareMessage','rawAttributes','fieldName','type','key','model','query','keys','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','filters','pick','filter','VIRTUAL','merge','options','then','includeAll','include','findAll','rows','catch','show','create','body','find','params','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util'];(function(_0x19a21c,_0x4e03f8){var _0x3e9b9f=function(_0x40efad){while(--_0x40efad){_0x19a21c['push'](_0x19a21c['shift']());}};_0x3e9b9f(++_0x4e03f8);}(_0xd2e0,0x174));var _0x0d2e=function(_0x22b0cf,_0x4d038b){_0x22b0cf=_0x22b0cf-0x0;var _0x8b75fc=_0xd2e0[_0x22b0cf];return _0x8b75fc;};'use strict';var emlformat=require(_0x0d2e('0x0'));var rimraf=require(_0x0d2e('0x1'));var zipdir=require(_0x0d2e('0x2'));var jsonpatch=require(_0x0d2e('0x3'));var rp=require('request-promise');var moment=require(_0x0d2e('0x4'));var BPromise=require(_0x0d2e('0x5'));var Mustache=require(_0x0d2e('0x6'));var util=require(_0x0d2e('0x7'));var path=require(_0x0d2e('0x8'));var sox=require(_0x0d2e('0x9'));var csv=require(_0x0d2e('0xa'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x0d2e('0xb'));var _=require(_0x0d2e('0xc'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x0d2e('0xd'));var deskjs=require('desk.js');var toCsv=require(_0x0d2e('0xa'));var querystring=require(_0x0d2e('0xe'));var Papa=require(_0x0d2e('0xf'));var Redis=require(_0x0d2e('0x10'));var authService=require(_0x0d2e('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0x0d2e('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x0d2e('0x13'))(_0x0d2e('0x14'));var utils=require(_0x0d2e('0x15'));var config=require(_0x0d2e('0x16'));var licenseUtil=require(_0x0d2e('0x17'));var db=require(_0x0d2e('0x18'))['db'];function respondWithStatusCode(_0x416264,_0x179d6f){_0x179d6f=_0x179d6f||0xcc;return function(_0x4535d9){if(_0x4535d9){return _0x416264['sendStatus'](_0x179d6f);}return _0x416264['status'](_0x179d6f)[_0x0d2e('0x19')]();};}function respondWithResult(_0x168fde,_0x5bc375){_0x5bc375=_0x5bc375||0xc8;return function(_0x49a210){if(_0x49a210){return _0x168fde['status'](_0x5bc375)[_0x0d2e('0x1a')](_0x49a210);}};}function respondWithFilteredResult(_0x15f230,_0x16a83a){return function(_0x27d384){if(_0x27d384){var _0x5578e4=typeof _0x16a83a[_0x0d2e('0x1b')]===_0x0d2e('0x1c')&&typeof _0x16a83a['limit']===_0x0d2e('0x1c');var _0x324cfe=_0x27d384[_0x0d2e('0x1d')];var _0x3aa44d=_0x5578e4?0x0:_0x16a83a[_0x0d2e('0x1b')];var _0x15c04a=_0x5578e4?_0x27d384[_0x0d2e('0x1d')]:_0x16a83a[_0x0d2e('0x1b')]+_0x16a83a[_0x0d2e('0x1e')];var _0x190630;if(_0x15c04a>=_0x324cfe){_0x15c04a=_0x324cfe;_0x190630=0xc8;}else{_0x190630=0xce;}_0x15f230[_0x0d2e('0x1f')](_0x190630);return _0x15f230['set'](_0x0d2e('0x20'),_0x3aa44d+'-'+_0x15c04a+'/'+_0x324cfe)[_0x0d2e('0x1a')](_0x27d384);}return null;};}function patchUpdates(_0x4961a3){return function(_0x200d77){try{jsonpatch[_0x0d2e('0x21')](_0x200d77,_0x4961a3,!![]);}catch(_0x15c21d){return BPromise[_0x0d2e('0x22')](_0x15c21d);}return _0x200d77[_0x0d2e('0x23')]();};}function saveUpdates(_0x1f40df,_0x2c1b44){return function(_0x3e9e9d){if(_0x3e9e9d){return _0x3e9e9d[_0x0d2e('0x24')](_0x1f40df)['then'](function(_0x442c61){return _0x442c61;});}return null;};}function removeEntity(_0x21cdce,_0x4147b6){return function(_0x392ba2){if(_0x392ba2){return _0x392ba2[_0x0d2e('0x25')]()['then'](function(){_0x21cdce[_0x0d2e('0x1f')](0xcc)[_0x0d2e('0x19')]();});}};}function handleEntityNotFound(_0x48bced,_0x4d0a89){return function(_0x341673){if(!_0x341673){_0x48bced['sendStatus'](0x194);}return _0x341673;};}function handleError(_0x16426c,_0x545e4f){_0x545e4f=_0x545e4f||0x1f4;return function(_0xadb8be){logger['error'](_0xadb8be[_0x0d2e('0x26')]);if(_0xadb8be[_0x0d2e('0x27')]){delete _0xadb8be[_0x0d2e('0x27')];}_0x16426c[_0x0d2e('0x1f')](_0x545e4f)[_0x0d2e('0x28')](_0xadb8be);};}exports['index']=function(_0x48d827,_0x4479f7){var _0x3611ce={},_0x447a3c={},_0x4d9071={'count':0x0,'rows':[]};var _0x683c60=_[_0x0d2e('0x29')](db[_0x0d2e('0x2a')][_0x0d2e('0x2b')],function(_0x2efbcb){return{'name':_0x2efbcb[_0x0d2e('0x2c')],'type':_0x2efbcb[_0x0d2e('0x2d')][_0x0d2e('0x2e')]};});_0x447a3c[_0x0d2e('0x2f')]=_[_0x0d2e('0x29')](_0x683c60,_0x0d2e('0x27'));_0x447a3c[_0x0d2e('0x30')]=_[_0x0d2e('0x31')](_0x48d827['query']);_0x447a3c['filters']=_[_0x0d2e('0x32')](_0x447a3c[_0x0d2e('0x2f')],_0x447a3c[_0x0d2e('0x30')]);_0x3611ce['attributes']=_[_0x0d2e('0x32')](_0x447a3c['model'],qs[_0x0d2e('0x33')](_0x48d827[_0x0d2e('0x30')][_0x0d2e('0x33')]));_0x3611ce['attributes']=_0x3611ce[_0x0d2e('0x34')][_0x0d2e('0x35')]?_0x3611ce[_0x0d2e('0x34')]:_0x447a3c[_0x0d2e('0x2f')];if(!_0x48d827[_0x0d2e('0x30')][_0x0d2e('0x36')](_0x0d2e('0x37'))){_0x3611ce[_0x0d2e('0x1e')]=qs[_0x0d2e('0x1e')](_0x48d827[_0x0d2e('0x30')][_0x0d2e('0x1e')]);_0x3611ce['offset']=qs[_0x0d2e('0x1b')](_0x48d827[_0x0d2e('0x30')]['offset']);}_0x3611ce[_0x0d2e('0x38')]=qs[_0x0d2e('0x39')](_0x48d827['query'][_0x0d2e('0x39')]);_0x3611ce[_0x0d2e('0x3a')]=qs[_0x0d2e('0x3b')](_[_0x0d2e('0x3c')](_0x48d827[_0x0d2e('0x30')],_0x447a3c['filters']),_0x683c60);if(_0x48d827[_0x0d2e('0x30')][_0x0d2e('0x3d')]){_0x3611ce[_0x0d2e('0x3a')]=_['merge'](_0x3611ce[_0x0d2e('0x3a')],{'$or':_['map'](_0x683c60,function(_0x5af910){if(_0x5af910['type']!==_0x0d2e('0x3e')){var _0x21b79b={};_0x21b79b[_0x5af910[_0x0d2e('0x27')]]={'$like':'%'+_0x48d827[_0x0d2e('0x30')][_0x0d2e('0x3d')]+'%'};return _0x21b79b;}})});}_0x3611ce=_[_0x0d2e('0x3f')]({},_0x3611ce,_0x48d827[_0x0d2e('0x40')]);var _0x265b8d={'where':_0x3611ce[_0x0d2e('0x3a')]};return db[_0x0d2e('0x2a')][_0x0d2e('0x1d')](_0x265b8d)[_0x0d2e('0x41')](function(_0x21eb38){_0x4d9071['count']=_0x21eb38;if(_0x48d827[_0x0d2e('0x30')][_0x0d2e('0x42')]){_0x3611ce[_0x0d2e('0x43')]=[{'all':!![]}];}return db[_0x0d2e('0x2a')][_0x0d2e('0x44')](_0x3611ce);})[_0x0d2e('0x41')](function(_0x2ed5ac){_0x4d9071[_0x0d2e('0x45')]=_0x2ed5ac;return _0x4d9071;})['then'](respondWithFilteredResult(_0x4479f7,_0x3611ce))[_0x0d2e('0x46')](handleError(_0x4479f7,null));};exports[_0x0d2e('0x47')]=function(_0x4a5f78,_0x23f0ac){var _0x1e359f={'raw':!![],'where':{'id':_0x4a5f78['params']['id']}},_0x5ddab4={};_0x5ddab4[_0x0d2e('0x2f')]=_[_0x0d2e('0x31')](db[_0x0d2e('0x2a')]['rawAttributes']);_0x5ddab4[_0x0d2e('0x30')]=_[_0x0d2e('0x31')](_0x4a5f78[_0x0d2e('0x30')]);_0x5ddab4[_0x0d2e('0x3b')]=_[_0x0d2e('0x32')](_0x5ddab4[_0x0d2e('0x2f')],_0x5ddab4['query']);_0x1e359f[_0x0d2e('0x34')]=_[_0x0d2e('0x32')](_0x5ddab4['model'],qs[_0x0d2e('0x33')](_0x4a5f78[_0x0d2e('0x30')]['fields']));_0x1e359f['attributes']=_0x1e359f[_0x0d2e('0x34')][_0x0d2e('0x35')]?_0x1e359f[_0x0d2e('0x34')]:_0x5ddab4['model'];if(_0x4a5f78[_0x0d2e('0x30')][_0x0d2e('0x42')]){_0x1e359f[_0x0d2e('0x43')]=[{'all':!![]}];}_0x1e359f=_[_0x0d2e('0x3f')]({},_0x1e359f,_0x4a5f78[_0x0d2e('0x40')]);return db['SquareMessage']['find'](_0x1e359f)[_0x0d2e('0x41')](handleEntityNotFound(_0x23f0ac,null))[_0x0d2e('0x41')](respondWithResult(_0x23f0ac,null))[_0x0d2e('0x46')](handleError(_0x23f0ac,null));};exports[_0x0d2e('0x48')]=function(_0x563615,_0x27e1bc){return db[_0x0d2e('0x2a')][_0x0d2e('0x48')](_0x563615['body'],{})[_0x0d2e('0x41')](respondWithResult(_0x27e1bc,0xc9))[_0x0d2e('0x46')](handleError(_0x27e1bc,null));};exports[_0x0d2e('0x24')]=function(_0x32b94c,_0x136d5b){if(_0x32b94c[_0x0d2e('0x49')]['id']){delete _0x32b94c[_0x0d2e('0x49')]['id'];}return db['SquareMessage'][_0x0d2e('0x4a')]({'where':{'id':_0x32b94c[_0x0d2e('0x4b')]['id']}})['then'](handleEntityNotFound(_0x136d5b,null))[_0x0d2e('0x41')](saveUpdates(_0x32b94c[_0x0d2e('0x49')],null))[_0x0d2e('0x41')](respondWithResult(_0x136d5b,null))[_0x0d2e('0x46')](handleError(_0x136d5b,null));};exports[_0x0d2e('0x25')]=function(_0x530f85,_0x3cdacf){return db[_0x0d2e('0x2a')]['find']({'where':{'id':_0x530f85[_0x0d2e('0x4b')]['id']}})[_0x0d2e('0x41')](handleEntityNotFound(_0x3cdacf,null))[_0x0d2e('0x41')](removeEntity(_0x3cdacf,null))['catch'](handleError(_0x3cdacf,null));};