d184c5f2fb6d860c375d4e28f5d793953fbdd993
[motion2.git] / server / api / intDynamics365Field / intDynamics365Field.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 _0x88f0=['moment','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/environment','../../config/license/util','sendStatus','status','end','json','undefined','limit','offset','set','apply','save','update','then','error','name','index','map','Dynamics365Field','fieldName','type','key','model','keys','query','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','filters','pick','filter','where','merge','VIRTUAL','options','count','includeAll','include','catch','show','params','rawAttributes','create','body','find','destroy','eml-format','rimraf','zip-dir','request-promise'];(function(_0x355a88,_0xfe8822){var _0x3bf74f=function(_0x36c5ef){while(--_0x36c5ef){_0x355a88['push'](_0x355a88['shift']());}};_0x3bf74f(++_0xfe8822);}(_0x88f0,0x1ac));var _0x088f=function(_0x2de4e6,_0x2dbab4){_0x2de4e6=_0x2de4e6-0x0;var _0x156984=_0x88f0[_0x2de4e6];return _0x156984;};'use strict';var emlformat=require(_0x088f('0x0'));var rimraf=require(_0x088f('0x1'));var zipdir=require(_0x088f('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x088f('0x3'));var moment=require(_0x088f('0x4'));var BPromise=require(_0x088f('0x5'));var Mustache=require(_0x088f('0x6'));var util=require('util');var path=require(_0x088f('0x7'));var sox=require(_0x088f('0x8'));var csv=require(_0x088f('0x9'));var ejs=require(_0x088f('0xa'));var fs=require('fs');var fs_extra=require(_0x088f('0xb'));var _=require(_0x088f('0xc'));var squel=require('squel');var crypto=require(_0x088f('0xd'));var jsforce=require(_0x088f('0xe'));var deskjs=require(_0x088f('0xf'));var toCsv=require(_0x088f('0x9'));var querystring=require('querystring');var Papa=require(_0x088f('0x10'));var Redis=require(_0x088f('0x11'));var authService=require(_0x088f('0x12'));var qs=require(_0x088f('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x088f('0x14'));var logger=require('../../config/logger')('api');var utils=require('../../config/utils');var config=require(_0x088f('0x15'));var licenseUtil=require(_0x088f('0x16'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x192d83,_0x90157){_0x90157=_0x90157||0xcc;return function(_0x3454d1){if(_0x3454d1){return _0x192d83[_0x088f('0x17')](_0x90157);}return _0x192d83[_0x088f('0x18')](_0x90157)[_0x088f('0x19')]();};}function respondWithResult(_0x18afaa,_0x14d6b1){_0x14d6b1=_0x14d6b1||0xc8;return function(_0x1d0079){if(_0x1d0079){return _0x18afaa[_0x088f('0x18')](_0x14d6b1)[_0x088f('0x1a')](_0x1d0079);}};}function respondWithFilteredResult(_0x21c87f,_0x982054){return function(_0x492f41){if(_0x492f41){var _0x4f2e04=typeof _0x982054['offset']===_0x088f('0x1b')&&typeof _0x982054[_0x088f('0x1c')]==='undefined';var _0x5cf9cb=_0x492f41['count'];var _0x1c4b07=_0x4f2e04?0x0:_0x982054['offset'];var _0x2bf8f2=_0x4f2e04?_0x492f41['count']:_0x982054[_0x088f('0x1d')]+_0x982054[_0x088f('0x1c')];var _0x1f7c75;if(_0x2bf8f2>=_0x5cf9cb){_0x2bf8f2=_0x5cf9cb;_0x1f7c75=0xc8;}else{_0x1f7c75=0xce;}_0x21c87f['status'](_0x1f7c75);return _0x21c87f[_0x088f('0x1e')]('Content-Range',_0x1c4b07+'-'+_0x2bf8f2+'/'+_0x5cf9cb)[_0x088f('0x1a')](_0x492f41);}return null;};}function patchUpdates(_0x2a6bfd){return function(_0x13c79b){try{jsonpatch[_0x088f('0x1f')](_0x13c79b,_0x2a6bfd,!![]);}catch(_0x4c5f69){return BPromise['reject'](_0x4c5f69);}return _0x13c79b[_0x088f('0x20')]();};}function saveUpdates(_0x12775,_0xbf62f0){return function(_0x5f02b6){if(_0x5f02b6){return _0x5f02b6[_0x088f('0x21')](_0x12775)[_0x088f('0x22')](function(_0x3ece82){return _0x3ece82;});}return null;};}function removeEntity(_0x18d7e9,_0x307cab){return function(_0x2d41be){if(_0x2d41be){return _0x2d41be['destroy']()[_0x088f('0x22')](function(){_0x18d7e9[_0x088f('0x18')](0xcc)[_0x088f('0x19')]();});}};}function handleEntityNotFound(_0x95ed84,_0x137233){return function(_0xa18a78){if(!_0xa18a78){_0x95ed84['sendStatus'](0x194);}return _0xa18a78;};}function handleError(_0x398f9f,_0x3709b2){_0x3709b2=_0x3709b2||0x1f4;return function(_0x2bdc0a){logger[_0x088f('0x23')](_0x2bdc0a['stack']);if(_0x2bdc0a[_0x088f('0x24')]){delete _0x2bdc0a[_0x088f('0x24')];}_0x398f9f[_0x088f('0x18')](_0x3709b2)['send'](_0x2bdc0a);};}exports[_0x088f('0x25')]=function(_0x4a9d10,_0x417c6a){var _0x6e4c66={},_0x248a9b={},_0x128a7c={'count':0x0,'rows':[]};var _0x10eec8=_[_0x088f('0x26')](db[_0x088f('0x27')]['rawAttributes'],function(_0x436de9){return{'name':_0x436de9[_0x088f('0x28')],'type':_0x436de9[_0x088f('0x29')][_0x088f('0x2a')]};});_0x248a9b[_0x088f('0x2b')]=_['map'](_0x10eec8,'name');_0x248a9b['query']=_[_0x088f('0x2c')](_0x4a9d10['query']);_0x248a9b['filters']=_['intersection'](_0x248a9b[_0x088f('0x2b')],_0x248a9b[_0x088f('0x2d')]);_0x6e4c66[_0x088f('0x2e')]=_[_0x088f('0x2f')](_0x248a9b[_0x088f('0x2b')],qs['fields'](_0x4a9d10[_0x088f('0x2d')][_0x088f('0x30')]));_0x6e4c66[_0x088f('0x2e')]=_0x6e4c66['attributes'][_0x088f('0x31')]?_0x6e4c66[_0x088f('0x2e')]:_0x248a9b[_0x088f('0x2b')];if(!_0x4a9d10['query'][_0x088f('0x32')](_0x088f('0x33'))){_0x6e4c66[_0x088f('0x1c')]=qs[_0x088f('0x1c')](_0x4a9d10[_0x088f('0x2d')][_0x088f('0x1c')]);_0x6e4c66['offset']=qs[_0x088f('0x1d')](_0x4a9d10['query'][_0x088f('0x1d')]);}_0x6e4c66[_0x088f('0x34')]=qs[_0x088f('0x35')](_0x4a9d10[_0x088f('0x2d')]['sort']);_0x6e4c66['where']=qs[_0x088f('0x36')](_[_0x088f('0x37')](_0x4a9d10[_0x088f('0x2d')],_0x248a9b[_0x088f('0x36')]),_0x10eec8);if(_0x4a9d10[_0x088f('0x2d')][_0x088f('0x38')]){_0x6e4c66[_0x088f('0x39')]=_[_0x088f('0x3a')](_0x6e4c66[_0x088f('0x39')],{'$or':_[_0x088f('0x26')](_0x10eec8,function(_0x5d3019){if(_0x5d3019[_0x088f('0x29')]!==_0x088f('0x3b')){var _0x33151b={};_0x33151b[_0x5d3019[_0x088f('0x24')]]={'$like':'%'+_0x4a9d10[_0x088f('0x2d')][_0x088f('0x38')]+'%'};return _0x33151b;}})});}_0x6e4c66=_[_0x088f('0x3a')]({},_0x6e4c66,_0x4a9d10[_0x088f('0x3c')]);var _0x3cb299={'where':_0x6e4c66['where']};return db[_0x088f('0x27')][_0x088f('0x3d')](_0x3cb299)['then'](function(_0x2f4361){_0x128a7c['count']=_0x2f4361;if(_0x4a9d10[_0x088f('0x2d')][_0x088f('0x3e')]){_0x6e4c66[_0x088f('0x3f')]=[{'all':!![]}];}return db['Dynamics365Field']['findAll'](_0x6e4c66);})[_0x088f('0x22')](function(_0x9fafca){_0x128a7c['rows']=_0x9fafca;return _0x128a7c;})[_0x088f('0x22')](respondWithFilteredResult(_0x417c6a,_0x6e4c66))[_0x088f('0x40')](handleError(_0x417c6a,null));};exports[_0x088f('0x41')]=function(_0x3c4acb,_0x24f232){var _0x2c6085={'raw':!![],'where':{'id':_0x3c4acb[_0x088f('0x42')]['id']}},_0x26cfe8={};_0x26cfe8[_0x088f('0x2b')]=_[_0x088f('0x2c')](db[_0x088f('0x27')][_0x088f('0x43')]);_0x26cfe8[_0x088f('0x2d')]=_[_0x088f('0x2c')](_0x3c4acb[_0x088f('0x2d')]);_0x26cfe8[_0x088f('0x36')]=_[_0x088f('0x2f')](_0x26cfe8[_0x088f('0x2b')],_0x26cfe8[_0x088f('0x2d')]);_0x2c6085[_0x088f('0x2e')]=_['intersection'](_0x26cfe8['model'],qs[_0x088f('0x30')](_0x3c4acb[_0x088f('0x2d')][_0x088f('0x30')]));_0x2c6085[_0x088f('0x2e')]=_0x2c6085[_0x088f('0x2e')][_0x088f('0x31')]?_0x2c6085[_0x088f('0x2e')]:_0x26cfe8[_0x088f('0x2b')];if(_0x3c4acb['query'][_0x088f('0x3e')]){_0x2c6085[_0x088f('0x3f')]=[{'all':!![]}];}_0x2c6085=_[_0x088f('0x3a')]({},_0x2c6085,_0x3c4acb[_0x088f('0x3c')]);return db[_0x088f('0x27')]['find'](_0x2c6085)[_0x088f('0x22')](handleEntityNotFound(_0x24f232,null))[_0x088f('0x22')](respondWithResult(_0x24f232,null))[_0x088f('0x40')](handleError(_0x24f232,null));};exports[_0x088f('0x44')]=function(_0x44dd9a,_0x26a146){return db['Dynamics365Field']['create'](_0x44dd9a[_0x088f('0x45')],{})[_0x088f('0x22')](respondWithResult(_0x26a146,0xc9))[_0x088f('0x40')](handleError(_0x26a146,null));};exports['update']=function(_0x222e6b,_0x12874a){if(_0x222e6b[_0x088f('0x45')]['id']){delete _0x222e6b[_0x088f('0x45')]['id'];}return db['Dynamics365Field'][_0x088f('0x46')]({'where':{'id':_0x222e6b['params']['id']}})['then'](handleEntityNotFound(_0x12874a,null))[_0x088f('0x22')](saveUpdates(_0x222e6b[_0x088f('0x45')],null))[_0x088f('0x22')](respondWithResult(_0x12874a,null))['catch'](handleError(_0x12874a,null));};exports[_0x088f('0x47')]=function(_0x200712,_0x3662ad){return db[_0x088f('0x27')][_0x088f('0x46')]({'where':{'id':_0x200712[_0x088f('0x42')]['id']}})['then'](handleEntityNotFound(_0x3662ad,null))[_0x088f('0x22')](removeEntity(_0x3662ad,null))['catch'](handleError(_0x3662ad,null));};