Built motion from commit 753c950e.|2.5.42
[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 _0x76f8=['attributes','fields','length','hasOwnProperty','nolimit','sort','pick','filter','where','merge','VIRTUAL','options','count','then','rows','catch','show','params','includeAll','include','find','clone','omit','createdAt','updatedAt','create','body','file','originalname','basename','filename','mimetype','download','findOne','ChatOfflineMessageId','join','root','rimraf','request-promise','mustache','util','path','sox','to-csv','fs-extra','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','set','Content-Range','apply','save','update','destroy','stack','name','send','map','Attachment','rawAttributes','type','key','model','query','keys','filters','intersection'];(function(_0x870b62,_0x2143f9){var _0x2dcb39=function(_0x2fb63d){while(--_0x2fb63d){_0x870b62['push'](_0x870b62['shift']());}};_0x2dcb39(++_0x2143f9);}(_0x76f8,0xd3));var _0x876f=function(_0x4365cb,_0x55ed1e){_0x4365cb=_0x4365cb-0x0;var _0x3ec9f7=_0x76f8[_0x4365cb];return _0x3ec9f7;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x876f('0x0'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x876f('0x1'));var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0x876f('0x2'));var util=require(_0x876f('0x3'));var path=require(_0x876f('0x4'));var sox=require(_0x876f('0x5'));var csv=require(_0x876f('0x6'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x876f('0x7'));var _=require(_0x876f('0x8'));var squel=require(_0x876f('0x9'));var crypto=require(_0x876f('0xa'));var jsforce=require('jsforce');var deskjs=require(_0x876f('0xb'));var toCsv=require(_0x876f('0x6'));var querystring=require(_0x876f('0xc'));var Papa=require(_0x876f('0xd'));var Redis=require(_0x876f('0xe'));var authService=require(_0x876f('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0x876f('0x10'));var hardwareService=require(_0x876f('0x11'));var logger=require(_0x876f('0x12'))(_0x876f('0x13'));var utils=require(_0x876f('0x14'));var config=require(_0x876f('0x15'));var licenseUtil=require(_0x876f('0x16'));var db=require(_0x876f('0x17'))['db'];function respondWithStatusCode(_0xf4f57,_0x547351){_0x547351=_0x547351||0xcc;return function(_0x2c2a38){if(_0x2c2a38){return _0xf4f57[_0x876f('0x18')](_0x547351);}return _0xf4f57[_0x876f('0x19')](_0x547351)[_0x876f('0x1a')]();};}function respondWithResult(_0x536af9,_0x1ace0b){_0x1ace0b=_0x1ace0b||0xc8;return function(_0x422676){if(_0x422676){return _0x536af9[_0x876f('0x19')](_0x1ace0b)[_0x876f('0x1b')](_0x422676);}};}function respondWithFilteredResult(_0x3f400a,_0x4e3e33){return function(_0x4a1cf9){if(_0x4a1cf9){var _0x5b9cd5=typeof _0x4e3e33[_0x876f('0x1c')]===_0x876f('0x1d')&&typeof _0x4e3e33[_0x876f('0x1e')]===_0x876f('0x1d');var _0x36eb99=_0x4a1cf9['count'];var _0xbdcf0a=_0x5b9cd5?0x0:_0x4e3e33[_0x876f('0x1c')];var _0xcd4986=_0x5b9cd5?_0x4a1cf9['count']:_0x4e3e33[_0x876f('0x1c')]+_0x4e3e33[_0x876f('0x1e')];var _0x1121d0;if(_0xcd4986>=_0x36eb99){_0xcd4986=_0x36eb99;_0x1121d0=0xc8;}else{_0x1121d0=0xce;}_0x3f400a[_0x876f('0x19')](_0x1121d0);return _0x3f400a[_0x876f('0x1f')](_0x876f('0x20'),_0xbdcf0a+'-'+_0xcd4986+'/'+_0x36eb99)[_0x876f('0x1b')](_0x4a1cf9);}return null;};}function patchUpdates(_0x539bd4){return function(_0x8d4932){try{jsonpatch[_0x876f('0x21')](_0x8d4932,_0x539bd4,!![]);}catch(_0x22a12d){return BPromise['reject'](_0x22a12d);}return _0x8d4932[_0x876f('0x22')]();};}function saveUpdates(_0x4004df,_0x427b03){return function(_0x2a85e1){if(_0x2a85e1){return _0x2a85e1[_0x876f('0x23')](_0x4004df)['then'](function(_0x1c641d){return _0x1c641d;});}return null;};}function removeEntity(_0x164ed9,_0x4d6edf){return function(_0x40ff3f){if(_0x40ff3f){return _0x40ff3f[_0x876f('0x24')]()['then'](function(){_0x164ed9[_0x876f('0x19')](0xcc)[_0x876f('0x1a')]();});}};}function handleEntityNotFound(_0x140d47,_0x157802){return function(_0x58f516){if(!_0x58f516){_0x140d47[_0x876f('0x18')](0x194);}return _0x58f516;};}function handleError(_0x1663e8,_0x3221aa){_0x3221aa=_0x3221aa||0x1f4;return function(_0x3f9d7a){logger['error'](_0x3f9d7a[_0x876f('0x25')]);if(_0x3f9d7a[_0x876f('0x26')]){delete _0x3f9d7a[_0x876f('0x26')];}_0x1663e8[_0x876f('0x19')](_0x3221aa)[_0x876f('0x27')](_0x3f9d7a);};}exports['index']=function(_0x3db4f6,_0x449f34){var _0x568ef8={},_0x155490={},_0x349930={'count':0x0,'rows':[]};var _0x1f21e8=_[_0x876f('0x28')](db[_0x876f('0x29')][_0x876f('0x2a')],function(_0x23a87f){return{'name':_0x23a87f['fieldName'],'type':_0x23a87f[_0x876f('0x2b')][_0x876f('0x2c')]};});_0x155490[_0x876f('0x2d')]=_[_0x876f('0x28')](_0x1f21e8,_0x876f('0x26'));_0x155490[_0x876f('0x2e')]=_[_0x876f('0x2f')](_0x3db4f6['query']);_0x155490[_0x876f('0x30')]=_[_0x876f('0x31')](_0x155490['model'],_0x155490['query']);_0x568ef8[_0x876f('0x32')]=_[_0x876f('0x31')](_0x155490[_0x876f('0x2d')],qs['fields'](_0x3db4f6['query'][_0x876f('0x33')]));_0x568ef8['attributes']=_0x568ef8[_0x876f('0x32')][_0x876f('0x34')]?_0x568ef8['attributes']:_0x155490[_0x876f('0x2d')];if(!_0x3db4f6[_0x876f('0x2e')][_0x876f('0x35')](_0x876f('0x36'))){_0x568ef8[_0x876f('0x1e')]=qs[_0x876f('0x1e')](_0x3db4f6[_0x876f('0x2e')]['limit']);_0x568ef8[_0x876f('0x1c')]=qs[_0x876f('0x1c')](_0x3db4f6['query']['offset']);}_0x568ef8['order']=qs[_0x876f('0x37')](_0x3db4f6['query']['sort']);_0x568ef8['where']=qs[_0x876f('0x30')](_[_0x876f('0x38')](_0x3db4f6['query'],_0x155490[_0x876f('0x30')]),_0x1f21e8);if(_0x3db4f6['query'][_0x876f('0x39')]){_0x568ef8[_0x876f('0x3a')]=_[_0x876f('0x3b')](_0x568ef8['where'],{'$or':_[_0x876f('0x28')](_0x1f21e8,function(_0x306a01){if(_0x306a01[_0x876f('0x2b')]!==_0x876f('0x3c')){var _0x15add2={};_0x15add2[_0x306a01['name']]={'$like':'%'+_0x3db4f6[_0x876f('0x2e')][_0x876f('0x39')]+'%'};return _0x15add2;}})});}_0x568ef8=_[_0x876f('0x3b')]({},_0x568ef8,_0x3db4f6[_0x876f('0x3d')]);var _0x3b28dd={'where':_0x568ef8[_0x876f('0x3a')]};return db[_0x876f('0x29')][_0x876f('0x3e')](_0x3b28dd)[_0x876f('0x3f')](function(_0x53166c){_0x349930[_0x876f('0x3e')]=_0x53166c;if(_0x3db4f6[_0x876f('0x2e')]['includeAll']){_0x568ef8['include']=[{'all':!![]}];}return db['Attachment']['findAll'](_0x568ef8);})[_0x876f('0x3f')](function(_0x17a04b){_0x349930[_0x876f('0x40')]=_0x17a04b;return _0x349930;})[_0x876f('0x3f')](respondWithFilteredResult(_0x449f34,_0x568ef8))[_0x876f('0x41')](handleError(_0x449f34,null));};exports[_0x876f('0x42')]=function(_0x13d9d7,_0x5a8aa6){var _0x4b78da={'raw':!![],'where':{'id':_0x13d9d7[_0x876f('0x43')]['id']}},_0x41ed57={};_0x41ed57[_0x876f('0x2d')]=_[_0x876f('0x2f')](db[_0x876f('0x29')][_0x876f('0x2a')]);_0x41ed57[_0x876f('0x2e')]=_[_0x876f('0x2f')](_0x13d9d7['query']);_0x41ed57[_0x876f('0x30')]=_['intersection'](_0x41ed57[_0x876f('0x2d')],_0x41ed57[_0x876f('0x2e')]);_0x4b78da[_0x876f('0x32')]=_[_0x876f('0x31')](_0x41ed57[_0x876f('0x2d')],qs[_0x876f('0x33')](_0x13d9d7[_0x876f('0x2e')][_0x876f('0x33')]));_0x4b78da[_0x876f('0x32')]=_0x4b78da[_0x876f('0x32')]['length']?_0x4b78da['attributes']:_0x41ed57[_0x876f('0x2d')];if(_0x13d9d7['query'][_0x876f('0x44')]){_0x4b78da[_0x876f('0x45')]=[{'all':!![]}];}_0x4b78da=_[_0x876f('0x3b')]({},_0x4b78da,_0x13d9d7[_0x876f('0x3d')]);return db[_0x876f('0x29')][_0x876f('0x46')](_0x4b78da)[_0x876f('0x3f')](handleEntityNotFound(_0x5a8aa6,null))['then'](respondWithResult(_0x5a8aa6,null))['catch'](handleError(_0x5a8aa6,null));};exports[_0x876f('0x47')]=function(_0x4cd5b0,_0x11942f){var _0x34bfdf={'where':{'id':_0x4cd5b0[_0x876f('0x43')]['id']}},_0x4f5c54={};_0x4f5c54[_0x876f('0x2d')]=_[_0x876f('0x2f')](db[_0x876f('0x29')]['rawAttributes']);_0x34bfdf['attributes']=_[_0x876f('0x31')](_0x4f5c54[_0x876f('0x2d')],qs[_0x876f('0x33')](_0x4cd5b0[_0x876f('0x2e')][_0x876f('0x33')]));_0x34bfdf['attributes']=_0x34bfdf['attributes'][_0x876f('0x34')]?_0x34bfdf[_0x876f('0x32')]:_0x4f5c54[_0x876f('0x2d')];if(_0x4cd5b0[_0x876f('0x2e')][_0x876f('0x44')]){_0x34bfdf[_0x876f('0x45')]=[{'all':!![]}];}_0x34bfdf=_['merge']({},_0x34bfdf,_0x4cd5b0[_0x876f('0x3d')]);return db[_0x876f('0x29')][_0x876f('0x46')](_0x34bfdf)[_0x876f('0x3f')](handleEntityNotFound(_0x11942f,null))[_0x876f('0x3f')](function(_0x15e944){if(_0x15e944){var _0xb77034=_0x15e944['get']({'plain':!![]});_0xb77034=qs[_0x876f('0x48')](_0xb77034,['id',_0x876f('0x49'),_0x876f('0x4a')]);_0x4cd5b0['body']=_['omit'](_0x4cd5b0['body'],['id',_0x876f('0x49'),'updatedAt']);return db['Attachment'][_0x876f('0x4b')](_[_0x876f('0x3b')](_0xb77034,_0x4cd5b0['body']),{'include':_0x4cd5b0[_0x876f('0x2e')][_0x876f('0x44')]?[{'all':!![]}]:undefined});}})[_0x876f('0x3f')](respondWithResult(_0x11942f,0xc9))[_0x876f('0x41')](handleError(_0x11942f,null));};exports['update']=function(_0x29bef2,_0x491cc1){if(_0x29bef2[_0x876f('0x4c')]['id']){delete _0x29bef2['body']['id'];}return db['Attachment'][_0x876f('0x46')]({'where':{'id':_0x29bef2[_0x876f('0x43')]['id']}})['then'](handleEntityNotFound(_0x491cc1,null))[_0x876f('0x3f')](saveUpdates(_0x29bef2[_0x876f('0x4c')],null))[_0x876f('0x3f')](respondWithResult(_0x491cc1,null))[_0x876f('0x41')](handleError(_0x491cc1,null));};exports[_0x876f('0x24')]=function(_0x53b718,_0x553258){return db['Attachment'][_0x876f('0x46')]({'where':{'id':_0x53b718[_0x876f('0x43')]['id']}})[_0x876f('0x3f')](handleEntityNotFound(_0x553258,null))[_0x876f('0x3f')](removeEntity(_0x553258,null))[_0x876f('0x41')](handleError(_0x553258,null));};exports[_0x876f('0x4b')]=function(_0x4a47d7,_0x3f1e7d,_0x3b43a5){_0x4a47d7['body'][_0x876f('0x26')]=_0x4a47d7[_0x876f('0x4d')][_0x876f('0x4e')];_0x4a47d7[_0x876f('0x4c')][_0x876f('0x4f')]=_0x4a47d7[_0x876f('0x4d')][_0x876f('0x50')];_0x4a47d7[_0x876f('0x4c')][_0x876f('0x2b')]=_0x4a47d7[_0x876f('0x4d')][_0x876f('0x51')];return db[_0x876f('0x29')][_0x876f('0x4b')](_0x4a47d7['body'])[_0x876f('0x3f')](respondWithResult(_0x3f1e7d,0xc9))[_0x876f('0x41')](handleError(_0x3f1e7d,null));};exports[_0x876f('0x52')]=function(_0x7bf600,_0x4e7056,_0x1af92b){return db['Attachment'][_0x876f('0x53')]({'where':{'id':_0x7bf600['params']['id']},'raw':!![]})[_0x876f('0x3f')](handleEntityNotFound(_0x4e7056,null))[_0x876f('0x3f')](function(_0x4a300b){if(_0x4a300b){var _0x335d8c=_0x4a300b[_0x876f('0x54')]!==null;var _0x27631c=path[_0x876f('0x55')](config[_0x876f('0x56')],'server/files/attachments/');if(_0x335d8c){_0x27631c=path[_0x876f('0x55')](_0x27631c,'offline-chat/');}var _0x4b8de9=path['join'](_0x27631c,_0x4a300b[_0x876f('0x4f')]);return _0x4e7056[_0x876f('0x52')](_0x4b8de9);}})[_0x876f('0x41')](handleError(_0x4e7056,null));};