Built motion from commit 85328c08.|2.5.50
[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 _0xbf06=['QueryTypes','SELECT','length','isInteger','num','forEach','push','query','%s\x20limit\x20%d\x20OFFSET\x20%d','dirname','.csv','existsSync','join','mkdirSync','each','%s%d.%s','csv','%s.%s','writeFileSync','zip','zip\x20finished','Completed','Error\x20during\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','inspect','finally','exit','path','lodash','to-csv','bluebird','rimraf','../../mysqldb','dbH','argv','savename','AnalyticExtractedReport','update','info','format','Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','CSV','Setting','find','split','then','splitSizeCsv','sequelize'];(function(_0x25777c,_0x45ec48){var _0x2afe3=function(_0x17bf07){while(--_0x17bf07){_0x25777c['push'](_0x25777c['shift']());}};_0x2afe3(++_0x45ec48);}(_0xbf06,0xa7));var _0x6bf0=function(_0xf50820,_0x56f041){_0xf50820=_0xf50820-0x0;var _0x2a9b10=_0xbf06[_0xf50820];return _0x2a9b10;};'use strict';var util=require('util');var path=require(_0x6bf0('0x0'));var _=require(_0x6bf0('0x1'));var csv=require(_0x6bf0('0x2'));var fs=require('fs');var BPromise=require(_0x6bf0('0x3'));var zipdir=require('zip-dir');var rimraf=require(_0x6bf0('0x4'));var db=require(_0x6bf0('0x5'))['db'];var dbH=require(_0x6bf0('0x5'))[_0x6bf0('0x6')];var logger=require('../../config/logger')('analytics');var EXTRACTED_REPORT_ID=Number(process[_0x6bf0('0x7')][0x2]);var QUERY=process[_0x6bf0('0x7')][0x3];var PATH=process[_0x6bf0('0x7')][0x4];var EXIT_CODE=0x0;function analyticExtractedReport(_0x38d159,_0x29251f){var _0x39fc51={'status':_0x38d159};var _0x1f801e={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x29251f){_0x39fc51[_0x6bf0('0x8')]=_0x29251f;}return db[_0x6bf0('0x9')][_0x6bf0('0xa')](_0x39fc51,_0x1f801e);}function main(){var _0x316ac6=0x1388;var _0x26fa95;logger[_0x6bf0('0xb')](util[_0x6bf0('0xc')](_0x6bf0('0xd'),EXTRACTED_REPORT_ID,_0x6bf0('0xe')));return db[_0x6bf0('0xf')][_0x6bf0('0x10')]({'where':{'id':0x1},'raw':!![],'attributes':['id',_0x6bf0('0x11'),'splitSizeCsv']})[_0x6bf0('0x12')](function(_0x14e8f9){if(_0x14e8f9){_0x26fa95=_0x14e8f9;_0x316ac6=_0x26fa95[_0x6bf0('0x13')]||0x1388;}})[_0x6bf0('0x12')](function(){return dbH['sequelize']['query'](util[_0x6bf0('0xc')]('SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t',QUERY),{'type':dbH[_0x6bf0('0x14')][_0x6bf0('0x15')][_0x6bf0('0x16')]});})[_0x6bf0('0x12')](function(_0x52d69d){if(!_0x52d69d[0x0]['num']){return analyticExtractedReport('Empty');}var _0x5a4612=[];var _0x378af9=0x0;if(_0x26fa95[_0x6bf0('0x11')]){if(_0x52d69d&&_0x52d69d[_0x6bf0('0x17')]){var _0x27bf3e=_['range'](_0x52d69d[0x0]['num']/_0x316ac6);var _0x5873d6=Number[_0x6bf0('0x18')](_0x52d69d[0x0][_0x6bf0('0x19')]/_0x316ac6)?_0x52d69d[0x0][_0x6bf0('0x19')]%_0x316ac6:0x0;_0x27bf3e[_0x6bf0('0x1a')](function(_0x5e760d){if(_0x5e760d>0x0){_0x378af9+=_0x316ac6;}_0x5a4612[_0x6bf0('0x1b')](dbH[_0x6bf0('0x14')][_0x6bf0('0x1c')](util[_0x6bf0('0xc')](_0x6bf0('0x1d'),QUERY,_0x316ac6,_0x378af9),{'type':dbH[_0x6bf0('0x14')][_0x6bf0('0x15')][_0x6bf0('0x16')]}));});if(_0x5873d6>0x0){_0x378af9+=_0x316ac6;_0x5a4612[_0x6bf0('0x1b')](dbH[_0x6bf0('0x14')][_0x6bf0('0x1c')](util[_0x6bf0('0xc')](_0x6bf0('0x1d'),QUERY,_0x5873d6,_0x378af9),{'type':dbH[_0x6bf0('0x14')]['QueryTypes'][_0x6bf0('0x16')]}));}}}else{_0x5a4612[_0x6bf0('0x1b')](dbH[_0x6bf0('0x14')][_0x6bf0('0x1c')](QUERY,{'type':dbH[_0x6bf0('0x14')][_0x6bf0('0x15')][_0x6bf0('0x16')]}));}var _0x2635c7=path[_0x6bf0('0x1e')](PATH);var _0x3ee8a1=path['basename'](PATH,_0x6bf0('0x1f'));var _0x51d0f7=_0x26fa95[_0x6bf0('0x11')]&&_0x5a4612[_0x6bf0('0x17')]>0x1;var _0x1768d2=[];if(!fs[_0x6bf0('0x20')](path[_0x6bf0('0x21')](_0x2635c7,_0x3ee8a1))&&_0x51d0f7){fs[_0x6bf0('0x22')](path['join'](_0x2635c7,_0x3ee8a1));}return BPromise[_0x6bf0('0x23')](_0x5a4612,function(_0x89e371){_0x1768d2[_0x6bf0('0x1b')](_0x89e371);})[_0x6bf0('0x12')](function(){for(var _0x4ea552=0x0;_0x4ea552<_0x1768d2[_0x6bf0('0x17')];_0x4ea552+=0x1){var _0x59b00c;var _0x4a1b90=_0x1768d2[_0x4ea552];if(_0x51d0f7){_0x59b00c=path[_0x6bf0('0x21')](_0x2635c7,_0x3ee8a1,util[_0x6bf0('0xc')](_0x6bf0('0x24'),_0x3ee8a1,_0x4ea552,_0x6bf0('0x25')));}else{_0x59b00c=path[_0x6bf0('0x21')](_0x2635c7,util[_0x6bf0('0xc')](_0x6bf0('0x26'),_0x3ee8a1,_0x6bf0('0x25')));}fs[_0x6bf0('0x27')](_0x59b00c,csv(_0x4a1b90));}})[_0x6bf0('0x12')](function(){if(_0x51d0f7){return new Promise(function(_0x4d0128){zipdir(path['join'](_0x2635c7,_0x3ee8a1),{'saveTo':path['join'](_0x2635c7,util['format'](_0x6bf0('0x26'),_0x3ee8a1,_0x6bf0('0x28')))},function(_0x56606c,_0x2c8e47){rimraf(path[_0x6bf0('0x21')](_0x2635c7,_0x3ee8a1),function(){});_0x4d0128(_0x6bf0('0x29'));});});}})[_0x6bf0('0x12')](function(){logger[_0x6bf0('0xb')](util[_0x6bf0('0xc')]('Extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format\x20completed',EXTRACTED_REPORT_ID,_0x6bf0('0xe')));return analyticExtractedReport(_0x6bf0('0x2a'),_0x51d0f7?util[_0x6bf0('0xc')](_0x6bf0('0x26'),_0x3ee8a1,_0x51d0f7?'zip':_0x6bf0('0x25')):null);});})['catch'](function(_0x2ac4a2){logger[_0x6bf0('0xb')](util[_0x6bf0('0xc')](_0x6bf0('0x2b'),EXTRACTED_REPORT_ID,_0x6bf0('0xe')),util[_0x6bf0('0x2c')](_0x2ac4a2,{'showHidden':![],'depth':null}));EXIT_CODE=0x1;return analyticExtractedReport('Failed');})[_0x6bf0('0x2d')](function(){process[_0x6bf0('0x2e')](EXIT_CODE);});}main();