455f10db34fda337138d48cb8881948e0c0f57c7
[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 _0x507f=['num','forEach','push','SELECT','query','%s\x20limit\x20%d\x20OFFSET\x20%d','QueryTypes','basename','.csv','join','mkdirSync','each','%s%d.%s','csv','%s.%s','writeFileSync','zip','zip\x20finished','Extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format\x20completed','Completed','catch','Error\x20during\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','inspect','Failed','finally','exit','util','path','lodash','to-csv','bluebird','zip-dir','rimraf','../../mysqldb','dbH','../../config/logger','analytics','argv','savename','AnalyticExtractedReport','update','info','format','CSV','Setting','find','split','then','splitSizeCsv','SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t','sequelize','Empty','length','range'];(function(_0x26a74d,_0x37ecfc){var _0xcbb1a3=function(_0x24b978){while(--_0x24b978){_0x26a74d['push'](_0x26a74d['shift']());}};_0xcbb1a3(++_0x37ecfc);}(_0x507f,0xf2));var _0xf507=function(_0x5b150f,_0x185e99){_0x5b150f=_0x5b150f-0x0;var _0x3a246e=_0x507f[_0x5b150f];return _0x3a246e;};'use strict';var util=require(_0xf507('0x0'));var path=require(_0xf507('0x1'));var _=require(_0xf507('0x2'));var csv=require(_0xf507('0x3'));var fs=require('fs');var BPromise=require(_0xf507('0x4'));var zipdir=require(_0xf507('0x5'));var rimraf=require(_0xf507('0x6'));var db=require('../../mysqldb')['db'];var dbH=require(_0xf507('0x7'))[_0xf507('0x8')];var logger=require(_0xf507('0x9'))(_0xf507('0xa'));var EXTRACTED_REPORT_ID=Number(process['argv'][0x2]);var QUERY=process['argv'][0x3];var PATH=process[_0xf507('0xb')][0x4];var EXIT_CODE=0x0;function analyticExtractedReport(_0x564657,_0x32ff37){var _0x470e10={'status':_0x564657};var _0x513d1f={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x32ff37){_0x470e10[_0xf507('0xc')]=_0x32ff37;}return db[_0xf507('0xd')][_0xf507('0xe')](_0x470e10,_0x513d1f);}function main(){var _0x37b208=0x1388;var _0x40ab89;logger[_0xf507('0xf')](util[_0xf507('0x10')]('Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format',EXTRACTED_REPORT_ID,_0xf507('0x11')));return db[_0xf507('0x12')][_0xf507('0x13')]({'where':{'id':0x1},'raw':!![],'attributes':['id',_0xf507('0x14'),'splitSizeCsv']})[_0xf507('0x15')](function(_0x3f69af){if(_0x3f69af){_0x40ab89=_0x3f69af;_0x37b208=_0x40ab89[_0xf507('0x16')]||0x1388;}})[_0xf507('0x15')](function(){return dbH['sequelize']['query'](util[_0xf507('0x10')](_0xf507('0x17'),QUERY),{'type':dbH[_0xf507('0x18')]['QueryTypes']['SELECT']});})[_0xf507('0x15')](function(_0x511b1e){if(!_0x511b1e[0x0]['num']){return analyticExtractedReport(_0xf507('0x19'));}var _0x90f7a1=[];var _0x82d08b=0x0;if(_0x40ab89[_0xf507('0x14')]){if(_0x511b1e&&_0x511b1e[_0xf507('0x1a')]){var _0xce4bbe=_[_0xf507('0x1b')](_0x511b1e[0x0][_0xf507('0x1c')]/_0x37b208);var _0x1bcccf=Number['isInteger'](_0x511b1e[0x0][_0xf507('0x1c')]/_0x37b208)?_0x511b1e[0x0][_0xf507('0x1c')]%_0x37b208:0x0;_0xce4bbe[_0xf507('0x1d')](function(_0x4ea20a){if(_0x4ea20a>0x0){_0x82d08b+=_0x37b208;}_0x90f7a1[_0xf507('0x1e')](dbH[_0xf507('0x18')]['query'](util[_0xf507('0x10')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0x37b208,_0x82d08b),{'type':dbH[_0xf507('0x18')]['QueryTypes'][_0xf507('0x1f')]}));});if(_0x1bcccf>0x0){_0x82d08b+=_0x37b208;_0x90f7a1[_0xf507('0x1e')](dbH[_0xf507('0x18')][_0xf507('0x20')](util[_0xf507('0x10')](_0xf507('0x21'),QUERY,_0x1bcccf,_0x82d08b),{'type':dbH[_0xf507('0x18')][_0xf507('0x22')][_0xf507('0x1f')]}));}}}else{_0x90f7a1[_0xf507('0x1e')](dbH[_0xf507('0x18')][_0xf507('0x20')](QUERY,{'type':dbH[_0xf507('0x18')]['QueryTypes'][_0xf507('0x1f')]}));}var _0x4e10c9=path['dirname'](PATH);var _0x2dfe5a=path[_0xf507('0x23')](PATH,_0xf507('0x24'));var _0xe15ceb=_0x40ab89['split']&&_0x90f7a1[_0xf507('0x1a')]>0x1;var _0x408c5f=[];if(!fs['existsSync'](path[_0xf507('0x25')](_0x4e10c9,_0x2dfe5a))&&_0xe15ceb){fs[_0xf507('0x26')](path[_0xf507('0x25')](_0x4e10c9,_0x2dfe5a));}return BPromise[_0xf507('0x27')](_0x90f7a1,function(_0x409404){_0x408c5f[_0xf507('0x1e')](_0x409404);})[_0xf507('0x15')](function(){for(var _0x5165e5=0x0;_0x5165e5<_0x408c5f[_0xf507('0x1a')];_0x5165e5+=0x1){var _0x1e02ba;var _0xb30353=_0x408c5f[_0x5165e5];if(_0xe15ceb){_0x1e02ba=path[_0xf507('0x25')](_0x4e10c9,_0x2dfe5a,util[_0xf507('0x10')](_0xf507('0x28'),_0x2dfe5a,_0x5165e5,_0xf507('0x29')));}else{_0x1e02ba=path[_0xf507('0x25')](_0x4e10c9,util['format'](_0xf507('0x2a'),_0x2dfe5a,_0xf507('0x29')));}fs[_0xf507('0x2b')](_0x1e02ba,csv(_0xb30353));}})['then'](function(){if(_0xe15ceb){return new Promise(function(_0x468e2f){zipdir(path[_0xf507('0x25')](_0x4e10c9,_0x2dfe5a),{'saveTo':path[_0xf507('0x25')](_0x4e10c9,util[_0xf507('0x10')](_0xf507('0x2a'),_0x2dfe5a,_0xf507('0x2c')))},function(_0x1063bc,_0x2da4e4){rimraf(path[_0xf507('0x25')](_0x4e10c9,_0x2dfe5a),function(){});_0x468e2f(_0xf507('0x2d'));});});}})[_0xf507('0x15')](function(){logger[_0xf507('0xf')](util['format'](_0xf507('0x2e'),EXTRACTED_REPORT_ID,_0xf507('0x11')));return analyticExtractedReport(_0xf507('0x2f'),_0xe15ceb?util[_0xf507('0x10')](_0xf507('0x2a'),_0x2dfe5a,_0xe15ceb?'zip':'csv'):null);});})[_0xf507('0x30')](function(_0xe78929){logger[_0xf507('0xf')](util[_0xf507('0x10')](_0xf507('0x31'),EXTRACTED_REPORT_ID,_0xf507('0x11')),util[_0xf507('0x32')](_0xe78929,{'showHidden':![],'depth':null}));EXIT_CODE=0x1;return analyticExtractedReport(_0xf507('0x33'));})[_0xf507('0x34')](function(){process[_0xf507('0x35')](EXIT_CODE);});}main();