Built motion from commit (unavailable).|2.5.4
[motion2.git] / server / api / attachment / attachment.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 _0x51b5=['Attachment','rawAttributes','fieldName','type','key','query','keys','filters','intersection','model','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','merge','filter','options','includeAll','include','findAll','rows','catch','show','params','length','find','get','omit','createdAt','updatedAt','body','create','file','originalname','basename','mimetype','download','join','root','server/files/attachments/','html-pdf','eml-format','rimraf','fast-json-patch','moment','mustache','util','path','to-csv','lodash','squel','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','sendStatus','status','end','json','count','offset','limit','set','apply','reject','update','then','destroy','error','stack','name','send','index','map'];(function(_0x3c15c2,_0x15110b){var _0x421658=function(_0x2e6723){while(--_0x2e6723){_0x3c15c2['push'](_0x3c15c2['shift']());}};_0x421658(++_0x15110b);}(_0x51b5,0x7f));var _0x551b=function(_0x3f1f5b,_0x4cccea){_0x3f1f5b=_0x3f1f5b-0x0;var _0x527a0c=_0x51b5[_0x3f1f5b];return _0x527a0c;};'use strict';var pdf=require(_0x551b('0x0'));var emlformat=require(_0x551b('0x1'));var rimraf=require(_0x551b('0x2'));var zipdir=require('zip-dir');var jsonpatch=require(_0x551b('0x3'));var rp=require('request-promise');var moment=require(_0x551b('0x4'));var BPromise=require('bluebird');var Mustache=require(_0x551b('0x5'));var util=require(_0x551b('0x6'));var path=require(_0x551b('0x7'));var sox=require('sox');var csv=require(_0x551b('0x8'));var ejs=require('ejs');var fs=require('fs');var _=require(_0x551b('0x9'));var squel=require(_0x551b('0xa'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x551b('0xb'));var toCsv=require(_0x551b('0x8'));var querystring=require(_0x551b('0xc'));var Papa=require(_0x551b('0xd'));var Redis=require(_0x551b('0xe'));var authService=require(_0x551b('0xf'));var qs=require(_0x551b('0x10'));var as=require(_0x551b('0x11'));var hardwareService=require(_0x551b('0x12'));var logger=require(_0x551b('0x13'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x3b820f,_0x15cdea){_0x15cdea=_0x15cdea||0xcc;return function(_0x2138c2){if(_0x2138c2){return _0x3b820f[_0x551b('0x14')](_0x15cdea);}return _0x3b820f[_0x551b('0x15')](_0x15cdea)[_0x551b('0x16')]();};}function respondWithResult(_0x180f59,_0x1d331e){_0x1d331e=_0x1d331e||0xc8;return function(_0x4f85e7){if(_0x4f85e7){return _0x180f59[_0x551b('0x15')](_0x1d331e)[_0x551b('0x17')](_0x4f85e7);}};}function respondWithFilteredResult(_0xd800d4,_0x480a99){return function(_0x582d66){if(_0x582d66){var _0x5614e4=_0x582d66[_0x551b('0x18')],_0x3f7e39=_0x480a99['offset'],_0x3a9e0c=_0x480a99[_0x551b('0x19')]+_0x480a99[_0x551b('0x1a')],_0x1fa59f;if(_0x3a9e0c>=_0x5614e4){_0x3a9e0c=_0x5614e4;_0x1fa59f=0xc8;}else{_0x1fa59f=0xce;}_0xd800d4['status'](_0x1fa59f);return _0xd800d4[_0x551b('0x1b')]('Content-Range',_0x3f7e39+'-'+_0x3a9e0c+'/'+_0x5614e4)[_0x551b('0x17')](_0x582d66);}return null;};}function patchUpdates(_0x3437b8){return function(_0x400a84){try{jsonpatch[_0x551b('0x1c')](_0x400a84,_0x3437b8,!![]);}catch(_0x42ca1b){return BPromise[_0x551b('0x1d')](_0x42ca1b);}return _0x400a84['save']();};}function saveUpdates(_0x3a84b7,_0x20053a){return function(_0x3d4021){if(_0x3d4021){return _0x3d4021[_0x551b('0x1e')](_0x3a84b7)[_0x551b('0x1f')](function(_0x461f1e){return _0x461f1e;});}return null;};}function removeEntity(_0x5f0123,_0x2ab789){return function(_0x1bfe73){if(_0x1bfe73){return _0x1bfe73[_0x551b('0x20')]()[_0x551b('0x1f')](function(){_0x5f0123[_0x551b('0x15')](0xcc)[_0x551b('0x16')]();});}};}function handleEntityNotFound(_0x3c9c8e,_0xe4622a){return function(_0x540a51){if(!_0x540a51){_0x3c9c8e[_0x551b('0x14')](0x194);}return _0x540a51;};}function handleError(_0x39c5a3,_0x1129db){_0x1129db=_0x1129db||0x1f4;return function(_0x34f451){logger[_0x551b('0x21')](_0x34f451[_0x551b('0x22')]);if(_0x34f451[_0x551b('0x23')]){delete _0x34f451[_0x551b('0x23')];}_0x39c5a3['status'](_0x1129db)[_0x551b('0x24')](_0x34f451);};}exports[_0x551b('0x25')]=function(_0x2219cb,_0x2602b0){var _0x451382={},_0x3fe140={},_0x5dd2d6={'count':0x0,'rows':[]};var _0xd7db16=_[_0x551b('0x26')](db[_0x551b('0x27')][_0x551b('0x28')],function(_0x1e6797){return{'name':_0x1e6797[_0x551b('0x29')],'type':_0x1e6797[_0x551b('0x2a')][_0x551b('0x2b')]};});_0x3fe140['model']=_['map'](_0xd7db16,_0x551b('0x23'));_0x3fe140[_0x551b('0x2c')]=_[_0x551b('0x2d')](_0x2219cb[_0x551b('0x2c')]);_0x3fe140[_0x551b('0x2e')]=_[_0x551b('0x2f')](_0x3fe140[_0x551b('0x30')],_0x3fe140[_0x551b('0x2c')]);_0x451382[_0x551b('0x31')]=_['intersection'](_0x3fe140[_0x551b('0x30')],qs[_0x551b('0x32')](_0x2219cb[_0x551b('0x2c')][_0x551b('0x32')]));_0x451382[_0x551b('0x31')]=_0x451382['attributes']['length']?_0x451382[_0x551b('0x31')]:_0x3fe140[_0x551b('0x30')];if(!_0x2219cb[_0x551b('0x2c')][_0x551b('0x33')](_0x551b('0x34'))){_0x451382['limit']=qs['limit'](_0x2219cb[_0x551b('0x2c')][_0x551b('0x1a')]);_0x451382[_0x551b('0x19')]=qs[_0x551b('0x19')](_0x2219cb[_0x551b('0x2c')]['offset']);}_0x451382[_0x551b('0x35')]=qs[_0x551b('0x36')](_0x2219cb[_0x551b('0x2c')][_0x551b('0x36')]);_0x451382[_0x551b('0x37')]=qs[_0x551b('0x2e')](_[_0x551b('0x38')](_0x2219cb['query'],_0x3fe140[_0x551b('0x2e')]),_0xd7db16);if(_0x2219cb['query']['filter']){_0x451382[_0x551b('0x37')]=_[_0x551b('0x39')](_0x451382[_0x551b('0x37')],{'$or':_['map'](_0xd7db16,function(_0x391d0c){if(_0x391d0c[_0x551b('0x2a')]!=='VIRTUAL'){var _0x46cebd={};_0x46cebd[_0x391d0c[_0x551b('0x23')]]={'$like':'%'+_0x2219cb[_0x551b('0x2c')][_0x551b('0x3a')]+'%'};return _0x46cebd;}})});}_0x451382=_['merge']({},_0x451382,_0x2219cb[_0x551b('0x3b')]);var _0x3474fd={'where':_0x451382['where']};return db[_0x551b('0x27')]['count'](_0x3474fd)[_0x551b('0x1f')](function(_0x1a73ee){_0x5dd2d6[_0x551b('0x18')]=_0x1a73ee;if(_0x2219cb['query'][_0x551b('0x3c')]){_0x451382[_0x551b('0x3d')]=[{'all':!![]}];}return db['Attachment'][_0x551b('0x3e')](_0x451382);})[_0x551b('0x1f')](function(_0x490d2d){_0x5dd2d6[_0x551b('0x3f')]=_0x490d2d;return _0x5dd2d6;})[_0x551b('0x1f')](respondWithFilteredResult(_0x2602b0,_0x451382))[_0x551b('0x40')](handleError(_0x2602b0,null));};exports[_0x551b('0x41')]=function(_0x2f395e,_0x5188de){var _0x5c2427={'raw':!![],'where':{'id':_0x2f395e[_0x551b('0x42')]['id']}},_0x286e62={};_0x286e62['model']=_['keys'](db['Attachment']['rawAttributes']);_0x286e62[_0x551b('0x2c')]=_[_0x551b('0x2d')](_0x2f395e[_0x551b('0x2c')]);_0x286e62[_0x551b('0x2e')]=_['intersection'](_0x286e62[_0x551b('0x30')],_0x286e62[_0x551b('0x2c')]);_0x5c2427[_0x551b('0x31')]=_[_0x551b('0x2f')](_0x286e62[_0x551b('0x30')],qs[_0x551b('0x32')](_0x2f395e['query']['fields']));_0x5c2427['attributes']=_0x5c2427[_0x551b('0x31')][_0x551b('0x43')]?_0x5c2427['attributes']:_0x286e62['model'];if(_0x2f395e[_0x551b('0x2c')][_0x551b('0x3c')]){_0x5c2427[_0x551b('0x3d')]=[{'all':!![]}];}_0x5c2427=_[_0x551b('0x39')]({},_0x5c2427,_0x2f395e[_0x551b('0x3b')]);return db[_0x551b('0x27')][_0x551b('0x44')](_0x5c2427)[_0x551b('0x1f')](handleEntityNotFound(_0x5188de,null))[_0x551b('0x1f')](respondWithResult(_0x5188de,null))[_0x551b('0x40')](handleError(_0x5188de,null));};exports['clone']=function(_0x21c092,_0x384ee6){var _0x2471c1={'where':{'id':_0x21c092[_0x551b('0x42')]['id']}},_0x35cbfc={};_0x35cbfc[_0x551b('0x30')]=_['keys'](db['Attachment'][_0x551b('0x28')]);_0x2471c1['attributes']=_[_0x551b('0x2f')](_0x35cbfc['model'],qs['fields'](_0x21c092[_0x551b('0x2c')][_0x551b('0x32')]));_0x2471c1[_0x551b('0x31')]=_0x2471c1[_0x551b('0x31')][_0x551b('0x43')]?_0x2471c1[_0x551b('0x31')]:_0x35cbfc[_0x551b('0x30')];if(_0x21c092[_0x551b('0x2c')]['includeAll']){_0x2471c1[_0x551b('0x3d')]=[{'all':!![]}];}_0x2471c1=_[_0x551b('0x39')]({},_0x2471c1,_0x21c092[_0x551b('0x3b')]);return db['Attachment']['find'](_0x2471c1)[_0x551b('0x1f')](handleEntityNotFound(_0x384ee6,null))[_0x551b('0x1f')](function(_0x45c8ce){if(_0x45c8ce){var _0x3c5438=_0x45c8ce[_0x551b('0x45')]({'plain':!![]});_0x3c5438=qs[_0x551b('0x46')](_0x3c5438,['id',_0x551b('0x47'),_0x551b('0x48')]);_0x21c092['body']=_[_0x551b('0x46')](_0x21c092[_0x551b('0x49')],['id','createdAt',_0x551b('0x48')]);return db['Attachment']['create'](_[_0x551b('0x39')](_0x3c5438,_0x21c092[_0x551b('0x49')]),{'include':_0x21c092[_0x551b('0x2c')][_0x551b('0x3c')]?[{'all':!![]}]:undefined});}})[_0x551b('0x1f')](respondWithResult(_0x384ee6,0xc9))[_0x551b('0x40')](handleError(_0x384ee6,null));};exports['update']=function(_0x5c81ce,_0x49d549){if(_0x5c81ce[_0x551b('0x49')]['id']){delete _0x5c81ce['body']['id'];}return db[_0x551b('0x27')][_0x551b('0x44')]({'where':{'id':_0x5c81ce[_0x551b('0x42')]['id']}})[_0x551b('0x1f')](handleEntityNotFound(_0x49d549,null))['then'](saveUpdates(_0x5c81ce[_0x551b('0x49')],null))[_0x551b('0x1f')](respondWithResult(_0x49d549,null))[_0x551b('0x40')](handleError(_0x49d549,null));};exports[_0x551b('0x20')]=function(_0x9b18af,_0x2aacbd){return db[_0x551b('0x27')]['find']({'where':{'id':_0x9b18af[_0x551b('0x42')]['id']}})[_0x551b('0x1f')](handleEntityNotFound(_0x2aacbd,null))[_0x551b('0x1f')](removeEntity(_0x2aacbd,null))[_0x551b('0x40')](handleError(_0x2aacbd,null));};exports[_0x551b('0x4a')]=function(_0x3a7ab0,_0x1a9f6c,_0x3f9dc8){_0x3a7ab0[_0x551b('0x49')][_0x551b('0x23')]=_0x3a7ab0[_0x551b('0x4b')][_0x551b('0x4c')];_0x3a7ab0[_0x551b('0x49')][_0x551b('0x4d')]=_0x3a7ab0[_0x551b('0x4b')]['filename'];_0x3a7ab0[_0x551b('0x49')][_0x551b('0x2a')]=_0x3a7ab0[_0x551b('0x4b')][_0x551b('0x4e')];return db[_0x551b('0x27')][_0x551b('0x4a')](_0x3a7ab0['body'])[_0x551b('0x1f')](respondWithResult(_0x1a9f6c,0xc9))[_0x551b('0x40')](handleError(_0x1a9f6c,null));};exports[_0x551b('0x4f')]=function(_0x4a688a,_0x3021e6,_0x1e3c18){return db[_0x551b('0x27')]['findOne']({'where':{'id':_0x4a688a[_0x551b('0x42')]['id']},'raw':!![]})[_0x551b('0x1f')](handleEntityNotFound(_0x3021e6,null))[_0x551b('0x1f')](function(_0x53842e){if(_0x53842e){return _0x3021e6[_0x551b('0x4f')](path[_0x551b('0x50')](config[_0x551b('0x51')],_0x551b('0x52'),_0x53842e[_0x551b('0x4d')]));}})[_0x551b('0x40')](handleError(_0x3021e6,null));};