76c5ae951c54f31b17a47059e48f45db20536c11
[motion2.git] / server / components / export / csv.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 _0x1f54=['dbH','analytics','argv','savename','update','format','Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','Setting','find','split','splitSizeCsv','then','sequelize','query','SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t','QueryTypes','SELECT','num','Empty','length','range','forEach','push','dirname','basename','.csv','existsSync','join','mkdirSync','each','csv','%s.%s','writeFileSync','zip','zip\x20finished','Extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format\x20completed','CSV','Error\x20during\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','inspect','Failed','finally','util','lodash','zip-dir','../../mysqldb'];(function(_0x487b0c,_0x14ff74){var _0x1adbdc=function(_0x2e7b9d){while(--_0x2e7b9d){_0x487b0c['push'](_0x487b0c['shift']());}};_0x1adbdc(++_0x14ff74);}(_0x1f54,0x191));var _0x41f5=function(_0xed58fb,_0x3ad96e){_0xed58fb=_0xed58fb-0x0;var _0x34ff8a=_0x1f54[_0xed58fb];return _0x34ff8a;};'use strict';var util=require(_0x41f5('0x0'));var path=require('path');var _=require(_0x41f5('0x1'));var csv=require('to-csv');var fs=require('fs');var BPromise=require('bluebird');var zipdir=require(_0x41f5('0x2'));var rimraf=require('rimraf');var db=require(_0x41f5('0x3'))['db'];var dbH=require(_0x41f5('0x3'))[_0x41f5('0x4')];var logger=require('../../config/logger')(_0x41f5('0x5'));var EXTRACTED_REPORT_ID=Number(process[_0x41f5('0x6')][0x2]);var QUERY=process[_0x41f5('0x6')][0x3];var PATH=process['argv'][0x4];var EXIT_CODE=0x0;function analyticExtractedReport(_0x39c9a6,_0x370906){var _0x2e9182={'status':_0x39c9a6};var _0x19653d={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x370906){_0x2e9182[_0x41f5('0x7')]=_0x370906;}return db['AnalyticExtractedReport'][_0x41f5('0x8')](_0x2e9182,_0x19653d);}function main(){var _0x19287b=0x1388;var _0x337ebc;logger['info'](util[_0x41f5('0x9')](_0x41f5('0xa'),EXTRACTED_REPORT_ID,'CSV'));return db[_0x41f5('0xb')][_0x41f5('0xc')]({'where':{'id':0x1},'raw':!![],'attributes':['id',_0x41f5('0xd'),'splitSizeCsv']})['then'](function(_0x247e89){if(_0x247e89){_0x337ebc=_0x247e89;_0x19287b=_0x337ebc[_0x41f5('0xe')]||0x1388;}})[_0x41f5('0xf')](function(){return dbH[_0x41f5('0x10')][_0x41f5('0x11')](util[_0x41f5('0x9')](_0x41f5('0x12'),QUERY),{'type':dbH[_0x41f5('0x10')][_0x41f5('0x13')][_0x41f5('0x14')]});})['then'](function(_0x2ea95d){if(!_0x2ea95d[0x0][_0x41f5('0x15')]){return analyticExtractedReport(_0x41f5('0x16'));}var _0x1f1c72=[];var _0x19f3f1=0x0;if(_0x337ebc['split']){if(_0x2ea95d&&_0x2ea95d[_0x41f5('0x17')]){var _0x103277=_[_0x41f5('0x18')](_0x2ea95d[0x0][_0x41f5('0x15')]/_0x19287b);var _0x3d0bc5=Number['isInteger'](_0x2ea95d[0x0][_0x41f5('0x15')]/_0x19287b)?_0x2ea95d[0x0][_0x41f5('0x15')]%_0x19287b:0x0;_0x103277[_0x41f5('0x19')](function(_0x50fbb0){if(_0x50fbb0>0x0){_0x19f3f1+=_0x19287b;}_0x1f1c72['push'](dbH[_0x41f5('0x10')][_0x41f5('0x11')](util[_0x41f5('0x9')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0x19287b,_0x19f3f1),{'type':dbH['sequelize'][_0x41f5('0x13')][_0x41f5('0x14')]}));});if(_0x3d0bc5>0x0){_0x19f3f1+=_0x19287b;_0x1f1c72['push'](dbH[_0x41f5('0x10')][_0x41f5('0x11')](util[_0x41f5('0x9')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0x3d0bc5,_0x19f3f1),{'type':dbH[_0x41f5('0x10')][_0x41f5('0x13')][_0x41f5('0x14')]}));}}}else{_0x1f1c72[_0x41f5('0x1a')](dbH[_0x41f5('0x10')][_0x41f5('0x11')](QUERY,{'type':dbH[_0x41f5('0x10')][_0x41f5('0x13')]['SELECT']}));}var _0x2efe30=path[_0x41f5('0x1b')](PATH);var _0x474a0b=path[_0x41f5('0x1c')](PATH,_0x41f5('0x1d'));var _0x57c009=_0x337ebc['split']&&_0x1f1c72[_0x41f5('0x17')]>0x1;var _0x162fba=[];if(!fs[_0x41f5('0x1e')](path[_0x41f5('0x1f')](_0x2efe30,_0x474a0b))&&_0x57c009){fs[_0x41f5('0x20')](path[_0x41f5('0x1f')](_0x2efe30,_0x474a0b));}return BPromise[_0x41f5('0x21')](_0x1f1c72,function(_0x29739f){_0x162fba['push'](_0x29739f);})['then'](function(){for(var _0x1cac69=0x0;_0x1cac69<_0x162fba[_0x41f5('0x17')];_0x1cac69+=0x1){var _0x1b7880;var _0x154576=_0x162fba[_0x1cac69];if(_0x57c009){_0x1b7880=path[_0x41f5('0x1f')](_0x2efe30,_0x474a0b,util[_0x41f5('0x9')]('%s%d.%s',_0x474a0b,_0x1cac69,_0x41f5('0x22')));}else{_0x1b7880=path['join'](_0x2efe30,util[_0x41f5('0x9')](_0x41f5('0x23'),_0x474a0b,'csv'));}fs[_0x41f5('0x24')](_0x1b7880,csv(_0x154576));}})[_0x41f5('0xf')](function(){if(_0x57c009){return new Promise(function(_0x1eeef1){zipdir(path[_0x41f5('0x1f')](_0x2efe30,_0x474a0b),{'saveTo':path[_0x41f5('0x1f')](_0x2efe30,util[_0x41f5('0x9')](_0x41f5('0x23'),_0x474a0b,_0x41f5('0x25')))},function(_0xb6acc0,_0x43efca){rimraf(path[_0x41f5('0x1f')](_0x2efe30,_0x474a0b),function(){});_0x1eeef1(_0x41f5('0x26'));});});}})['then'](function(){logger['info'](util[_0x41f5('0x9')](_0x41f5('0x27'),EXTRACTED_REPORT_ID,_0x41f5('0x28')));return analyticExtractedReport('Completed',_0x57c009?util['format']('%s.%s',_0x474a0b,_0x57c009?_0x41f5('0x25'):_0x41f5('0x22')):null);});})['catch'](function(_0x2eaec2){logger['info'](util[_0x41f5('0x9')](_0x41f5('0x29'),EXTRACTED_REPORT_ID,_0x41f5('0x28')),util[_0x41f5('0x2a')](_0x2eaec2,{'showHidden':![],'depth':null}));EXIT_CODE=0x1;return analyticExtractedReport(_0x41f5('0x2b'));})[_0x41f5('0x2c')](function(){process['exit'](EXIT_CODE);});}main();