904c1de155afd3820d480afbb660c3f182a92168
[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 _0x2d89=['util','sox','to-csv','ejs','squel','querystring','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','offset','undefined','limit','count','set','apply','reject','update','then','error','stack','name','send','map','Attachment','rawAttributes','type','key','query','keys','filters','intersection','model','attributes','length','hasOwnProperty','nolimit','sort','where','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','show','params','fields','find','omit','createdAt','updatedAt','body','destroy','create','originalname','basename','filename','mimetype','findOne','ChatOfflineMessageId','join','root','download','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird'];(function(_0x3212c7,_0x25d193){var _0x1c1945=function(_0x2d0bbc){while(--_0x2d0bbc){_0x3212c7['push'](_0x3212c7['shift']());}};_0x1c1945(++_0x25d193);}(_0x2d89,0x9a));var _0x92d8=function(_0x1e5c2c,_0x440f8f){_0x1e5c2c=_0x1e5c2c-0x0;var _0xd68352=_0x2d89[_0x1e5c2c];return _0xd68352;};'use strict';var emlformat=require(_0x92d8('0x0'));var rimraf=require(_0x92d8('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x92d8('0x2'));var rp=require(_0x92d8('0x3'));var moment=require(_0x92d8('0x4'));var BPromise=require(_0x92d8('0x5'));var Mustache=require('mustache');var util=require(_0x92d8('0x6'));var path=require('path');var sox=require(_0x92d8('0x7'));var csv=require(_0x92d8('0x8'));var ejs=require(_0x92d8('0x9'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0x92d8('0xa'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0x92d8('0x8'));var querystring=require(_0x92d8('0xb'));var Papa=require('papaparse');var Redis=require(_0x92d8('0xc'));var authService=require('../../components/auth/service');var qs=require(_0x92d8('0xd'));var as=require(_0x92d8('0xe'));var hardwareService=require(_0x92d8('0xf'));var logger=require(_0x92d8('0x10'))('api');var utils=require('../../config/utils');var config=require(_0x92d8('0x11'));var licenseUtil=require(_0x92d8('0x12'));var db=require(_0x92d8('0x13'))['db'];function respondWithStatusCode(_0x61cf23,_0x4aed1a){_0x4aed1a=_0x4aed1a||0xcc;return function(_0x1b7af0){if(_0x1b7af0){return _0x61cf23[_0x92d8('0x14')](_0x4aed1a);}return _0x61cf23[_0x92d8('0x15')](_0x4aed1a)[_0x92d8('0x16')]();};}function respondWithResult(_0x311e56,_0x4092b9){_0x4092b9=_0x4092b9||0xc8;return function(_0x4d3354){if(_0x4d3354){return _0x311e56['status'](_0x4092b9)['json'](_0x4d3354);}};}function respondWithFilteredResult(_0x1a64e5,_0x18d0cb){return function(_0x56bc82){if(_0x56bc82){var _0x3e98e2=typeof _0x18d0cb[_0x92d8('0x17')]===_0x92d8('0x18')&&typeof _0x18d0cb[_0x92d8('0x19')]===_0x92d8('0x18');var _0x52e551=_0x56bc82[_0x92d8('0x1a')];var _0xc7a2f=_0x3e98e2?0x0:_0x18d0cb['offset'];var _0x1b9355=_0x3e98e2?_0x56bc82[_0x92d8('0x1a')]:_0x18d0cb[_0x92d8('0x17')]+_0x18d0cb[_0x92d8('0x19')];var _0x440be5;if(_0x1b9355>=_0x52e551){_0x1b9355=_0x52e551;_0x440be5=0xc8;}else{_0x440be5=0xce;}_0x1a64e5[_0x92d8('0x15')](_0x440be5);return _0x1a64e5[_0x92d8('0x1b')]('Content-Range',_0xc7a2f+'-'+_0x1b9355+'/'+_0x52e551)['json'](_0x56bc82);}return null;};}function patchUpdates(_0x26639f){return function(_0x39e3e5){try{jsonpatch[_0x92d8('0x1c')](_0x39e3e5,_0x26639f,!![]);}catch(_0x5835ba){return BPromise[_0x92d8('0x1d')](_0x5835ba);}return _0x39e3e5['save']();};}function saveUpdates(_0x4a472a,_0x1ae612){return function(_0x165da0){if(_0x165da0){return _0x165da0[_0x92d8('0x1e')](_0x4a472a)[_0x92d8('0x1f')](function(_0x52f595){return _0x52f595;});}return null;};}function removeEntity(_0x23de8e,_0x13a4b4){return function(_0x2bcaa7){if(_0x2bcaa7){return _0x2bcaa7['destroy']()['then'](function(){_0x23de8e[_0x92d8('0x15')](0xcc)[_0x92d8('0x16')]();});}};}function handleEntityNotFound(_0x1d8777,_0x3930d7){return function(_0x321f61){if(!_0x321f61){_0x1d8777[_0x92d8('0x14')](0x194);}return _0x321f61;};}function handleError(_0x5cec2f,_0x4771c2){_0x4771c2=_0x4771c2||0x1f4;return function(_0x1d43a9){logger[_0x92d8('0x20')](_0x1d43a9[_0x92d8('0x21')]);if(_0x1d43a9[_0x92d8('0x22')]){delete _0x1d43a9[_0x92d8('0x22')];}_0x5cec2f['status'](_0x4771c2)[_0x92d8('0x23')](_0x1d43a9);};}exports['index']=function(_0x1b41c1,_0x36e891){var _0xbd7654={},_0x7a8b15={},_0x1d0914={'count':0x0,'rows':[]};var _0x40efd8=_[_0x92d8('0x24')](db[_0x92d8('0x25')][_0x92d8('0x26')],function(_0x386ce2){return{'name':_0x386ce2['fieldName'],'type':_0x386ce2[_0x92d8('0x27')][_0x92d8('0x28')]};});_0x7a8b15['model']=_[_0x92d8('0x24')](_0x40efd8,_0x92d8('0x22'));_0x7a8b15[_0x92d8('0x29')]=_[_0x92d8('0x2a')](_0x1b41c1[_0x92d8('0x29')]);_0x7a8b15[_0x92d8('0x2b')]=_[_0x92d8('0x2c')](_0x7a8b15[_0x92d8('0x2d')],_0x7a8b15[_0x92d8('0x29')]);_0xbd7654[_0x92d8('0x2e')]=_['intersection'](_0x7a8b15[_0x92d8('0x2d')],qs['fields'](_0x1b41c1['query']['fields']));_0xbd7654[_0x92d8('0x2e')]=_0xbd7654['attributes'][_0x92d8('0x2f')]?_0xbd7654[_0x92d8('0x2e')]:_0x7a8b15[_0x92d8('0x2d')];if(!_0x1b41c1[_0x92d8('0x29')][_0x92d8('0x30')](_0x92d8('0x31'))){_0xbd7654[_0x92d8('0x19')]=qs[_0x92d8('0x19')](_0x1b41c1[_0x92d8('0x29')][_0x92d8('0x19')]);_0xbd7654[_0x92d8('0x17')]=qs[_0x92d8('0x17')](_0x1b41c1[_0x92d8('0x29')][_0x92d8('0x17')]);}_0xbd7654['order']=qs['sort'](_0x1b41c1['query'][_0x92d8('0x32')]);_0xbd7654[_0x92d8('0x33')]=qs[_0x92d8('0x2b')](_['pick'](_0x1b41c1[_0x92d8('0x29')],_0x7a8b15[_0x92d8('0x2b')]),_0x40efd8);if(_0x1b41c1[_0x92d8('0x29')][_0x92d8('0x34')]){_0xbd7654[_0x92d8('0x33')]=_[_0x92d8('0x35')](_0xbd7654[_0x92d8('0x33')],{'$or':_[_0x92d8('0x24')](_0x40efd8,function(_0x2aa39c){if(_0x2aa39c[_0x92d8('0x27')]!==_0x92d8('0x36')){var _0x1a5e8f={};_0x1a5e8f[_0x2aa39c[_0x92d8('0x22')]]={'$like':'%'+_0x1b41c1['query'][_0x92d8('0x34')]+'%'};return _0x1a5e8f;}})});}_0xbd7654=_[_0x92d8('0x35')]({},_0xbd7654,_0x1b41c1[_0x92d8('0x37')]);var _0x38a829={'where':_0xbd7654['where']};return db[_0x92d8('0x25')]['count'](_0x38a829)[_0x92d8('0x1f')](function(_0x4a66e1){_0x1d0914['count']=_0x4a66e1;if(_0x1b41c1[_0x92d8('0x29')][_0x92d8('0x38')]){_0xbd7654[_0x92d8('0x39')]=[{'all':!![]}];}return db['Attachment'][_0x92d8('0x3a')](_0xbd7654);})['then'](function(_0x1e1f70){_0x1d0914[_0x92d8('0x3b')]=_0x1e1f70;return _0x1d0914;})['then'](respondWithFilteredResult(_0x36e891,_0xbd7654))[_0x92d8('0x3c')](handleError(_0x36e891,null));};exports[_0x92d8('0x3d')]=function(_0x597f1c,_0x4e9136){var _0x16972f={'raw':!![],'where':{'id':_0x597f1c[_0x92d8('0x3e')]['id']}},_0x484ae8={};_0x484ae8[_0x92d8('0x2d')]=_[_0x92d8('0x2a')](db['Attachment']['rawAttributes']);_0x484ae8['query']=_['keys'](_0x597f1c['query']);_0x484ae8[_0x92d8('0x2b')]=_[_0x92d8('0x2c')](_0x484ae8['model'],_0x484ae8['query']);_0x16972f[_0x92d8('0x2e')]=_[_0x92d8('0x2c')](_0x484ae8[_0x92d8('0x2d')],qs[_0x92d8('0x3f')](_0x597f1c['query']['fields']));_0x16972f[_0x92d8('0x2e')]=_0x16972f[_0x92d8('0x2e')][_0x92d8('0x2f')]?_0x16972f[_0x92d8('0x2e')]:_0x484ae8[_0x92d8('0x2d')];if(_0x597f1c[_0x92d8('0x29')][_0x92d8('0x38')]){_0x16972f[_0x92d8('0x39')]=[{'all':!![]}];}_0x16972f=_[_0x92d8('0x35')]({},_0x16972f,_0x597f1c[_0x92d8('0x37')]);return db['Attachment']['find'](_0x16972f)['then'](handleEntityNotFound(_0x4e9136,null))[_0x92d8('0x1f')](respondWithResult(_0x4e9136,null))[_0x92d8('0x3c')](handleError(_0x4e9136,null));};exports['clone']=function(_0x4638ad,_0x6d3931){var _0x2332d8={'where':{'id':_0x4638ad[_0x92d8('0x3e')]['id']}},_0x3f2bd0={};_0x3f2bd0[_0x92d8('0x2d')]=_[_0x92d8('0x2a')](db['Attachment'][_0x92d8('0x26')]);_0x2332d8[_0x92d8('0x2e')]=_['intersection'](_0x3f2bd0[_0x92d8('0x2d')],qs[_0x92d8('0x3f')](_0x4638ad[_0x92d8('0x29')][_0x92d8('0x3f')]));_0x2332d8[_0x92d8('0x2e')]=_0x2332d8['attributes'][_0x92d8('0x2f')]?_0x2332d8['attributes']:_0x3f2bd0[_0x92d8('0x2d')];if(_0x4638ad['query'][_0x92d8('0x38')]){_0x2332d8[_0x92d8('0x39')]=[{'all':!![]}];}_0x2332d8=_['merge']({},_0x2332d8,_0x4638ad['options']);return db[_0x92d8('0x25')][_0x92d8('0x40')](_0x2332d8)[_0x92d8('0x1f')](handleEntityNotFound(_0x6d3931,null))['then'](function(_0x1a54df){if(_0x1a54df){var _0x2508e9=_0x1a54df['get']({'plain':!![]});_0x2508e9=qs[_0x92d8('0x41')](_0x2508e9,['id',_0x92d8('0x42'),_0x92d8('0x43')]);_0x4638ad[_0x92d8('0x44')]=_[_0x92d8('0x41')](_0x4638ad[_0x92d8('0x44')],['id',_0x92d8('0x42'),_0x92d8('0x43')]);return db[_0x92d8('0x25')]['create'](_[_0x92d8('0x35')](_0x2508e9,_0x4638ad[_0x92d8('0x44')]),{'include':_0x4638ad[_0x92d8('0x29')][_0x92d8('0x38')]?[{'all':!![]}]:undefined});}})['then'](respondWithResult(_0x6d3931,0xc9))[_0x92d8('0x3c')](handleError(_0x6d3931,null));};exports[_0x92d8('0x1e')]=function(_0x3bc61a,_0x17aa0f){if(_0x3bc61a[_0x92d8('0x44')]['id']){delete _0x3bc61a[_0x92d8('0x44')]['id'];}return db[_0x92d8('0x25')][_0x92d8('0x40')]({'where':{'id':_0x3bc61a[_0x92d8('0x3e')]['id']}})[_0x92d8('0x1f')](handleEntityNotFound(_0x17aa0f,null))[_0x92d8('0x1f')](saveUpdates(_0x3bc61a['body'],null))[_0x92d8('0x1f')](respondWithResult(_0x17aa0f,null))[_0x92d8('0x3c')](handleError(_0x17aa0f,null));};exports[_0x92d8('0x45')]=function(_0x2f5159,_0x579618){return db[_0x92d8('0x25')][_0x92d8('0x40')]({'where':{'id':_0x2f5159[_0x92d8('0x3e')]['id']}})[_0x92d8('0x1f')](handleEntityNotFound(_0x579618,null))['then'](removeEntity(_0x579618,null))[_0x92d8('0x3c')](handleError(_0x579618,null));};exports[_0x92d8('0x46')]=function(_0x53b9df,_0x40fa65,_0x580095){_0x53b9df[_0x92d8('0x44')][_0x92d8('0x22')]=_0x53b9df['file'][_0x92d8('0x47')];_0x53b9df['body'][_0x92d8('0x48')]=_0x53b9df['file'][_0x92d8('0x49')];_0x53b9df[_0x92d8('0x44')][_0x92d8('0x27')]=_0x53b9df['file'][_0x92d8('0x4a')];return db[_0x92d8('0x25')][_0x92d8('0x46')](_0x53b9df[_0x92d8('0x44')])[_0x92d8('0x1f')](respondWithResult(_0x40fa65,0xc9))['catch'](handleError(_0x40fa65,null));};exports['download']=function(_0x3486cd,_0x18edf3,_0x3bcc20){return db[_0x92d8('0x25')][_0x92d8('0x4b')]({'where':{'id':_0x3486cd[_0x92d8('0x3e')]['id']},'raw':!![]})[_0x92d8('0x1f')](handleEntityNotFound(_0x18edf3,null))[_0x92d8('0x1f')](function(_0xa562a1){if(_0xa562a1){var _0x5d6c3b=_0xa562a1[_0x92d8('0x4c')]!==null;var _0x5d9d34=path[_0x92d8('0x4d')](config[_0x92d8('0x4e')],'server/files/attachments/');if(_0x5d6c3b){_0x5d9d34=path[_0x92d8('0x4d')](_0x5d9d34,'offline-chat/');}var _0x37c57e=path[_0x92d8('0x4d')](_0x5d9d34,_0xa562a1['basename']);return _0x18edf3[_0x92d8('0x4f')](_0x37c57e);}})[_0x92d8('0x3c')](handleError(_0x18edf3,null));};