afe454fa1e9648581f23a52b93553130c7692991
[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 _0x495f=['Setting','splitSizeCsv','then','sequelize','query','SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t','QueryTypes','SELECT','num','Empty','split','length','isInteger','push','dirname','basename','.csv','existsSync','join','%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','util','path','to-csv','bluebird','zip-dir','rimraf','../../mysqldb','../../config/logger','analytics','argv','savename','AnalyticExtractedReport','info','format','Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','CSV'];(function(_0x3cdcad,_0x41456e){var _0xf3d481=function(_0x21e41a){while(--_0x21e41a){_0x3cdcad['push'](_0x3cdcad['shift']());}};_0xf3d481(++_0x41456e);}(_0x495f,0x1d0));var _0xf495=function(_0x4fde08,_0x463a36){_0x4fde08=_0x4fde08-0x0;var _0x12e725=_0x495f[_0x4fde08];return _0x12e725;};'use strict';var util=require(_0xf495('0x0'));var path=require(_0xf495('0x1'));var _=require('lodash');var csv=require(_0xf495('0x2'));var fs=require('fs');var BPromise=require(_0xf495('0x3'));var zipdir=require(_0xf495('0x4'));var rimraf=require(_0xf495('0x5'));var db=require(_0xf495('0x6'))['db'];var dbH=require('../../mysqldb')['dbH'];var logger=require(_0xf495('0x7'))(_0xf495('0x8'));var EXTRACTED_REPORT_ID=Number(process[_0xf495('0x9')][0x2]);var QUERY=process[_0xf495('0x9')][0x3];var PATH=process[_0xf495('0x9')][0x4];var EXIT_CODE=0x0;function analyticExtractedReport(_0x3393e8,_0x54b03b){var _0x4c5ada={'status':_0x3393e8};var _0x2a2a2b={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x54b03b){_0x4c5ada[_0xf495('0xa')]=_0x54b03b;}return db[_0xf495('0xb')]['update'](_0x4c5ada,_0x2a2a2b);}function main(){var _0x2672f7=0x1388;var _0x1077e8;logger[_0xf495('0xc')](util[_0xf495('0xd')](_0xf495('0xe'),EXTRACTED_REPORT_ID,_0xf495('0xf')));return db[_0xf495('0x10')]['find']({'where':{'id':0x1},'raw':!![],'attributes':['id','split',_0xf495('0x11')]})['then'](function(_0x4dd9bf){if(_0x4dd9bf){_0x1077e8=_0x4dd9bf;_0x2672f7=_0x1077e8['splitSizeCsv']||0x1388;}})[_0xf495('0x12')](function(){return dbH[_0xf495('0x13')][_0xf495('0x14')](util[_0xf495('0xd')](_0xf495('0x15'),QUERY),{'type':dbH['sequelize'][_0xf495('0x16')][_0xf495('0x17')]});})['then'](function(_0x46ecc0){if(!_0x46ecc0[0x0][_0xf495('0x18')]){return analyticExtractedReport(_0xf495('0x19'));}var _0x5c4f09=[];var _0xdc8441=0x0;if(_0x1077e8[_0xf495('0x1a')]){if(_0x46ecc0&&_0x46ecc0[_0xf495('0x1b')]){var _0x5c5572=_['range'](_0x46ecc0[0x0]['num']/_0x2672f7);var _0x37115f=Number[_0xf495('0x1c')](_0x46ecc0[0x0][_0xf495('0x18')]/_0x2672f7)?_0x46ecc0[0x0]['num']%_0x2672f7:0x0;_0x5c5572['forEach'](function(_0x2a2768){if(_0x2a2768>0x0){_0xdc8441+=_0x2672f7;}_0x5c4f09[_0xf495('0x1d')](dbH['sequelize'][_0xf495('0x14')](util[_0xf495('0xd')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0x2672f7,_0xdc8441),{'type':dbH[_0xf495('0x13')]['QueryTypes'][_0xf495('0x17')]}));});if(_0x37115f>0x0){_0xdc8441+=_0x2672f7;_0x5c4f09[_0xf495('0x1d')](dbH[_0xf495('0x13')][_0xf495('0x14')](util[_0xf495('0xd')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0x37115f,_0xdc8441),{'type':dbH[_0xf495('0x13')][_0xf495('0x16')][_0xf495('0x17')]}));}}}else{_0x5c4f09[_0xf495('0x1d')](dbH[_0xf495('0x13')]['query'](QUERY,{'type':dbH[_0xf495('0x13')]['QueryTypes']['SELECT']}));}var _0x2e7f73=path[_0xf495('0x1e')](PATH);var _0x166f56=path[_0xf495('0x1f')](PATH,_0xf495('0x20'));var _0x204d06=_0x1077e8[_0xf495('0x1a')]&&_0x5c4f09[_0xf495('0x1b')]>0x1;var _0x33fb8b=[];if(!fs[_0xf495('0x21')](path['join'](_0x2e7f73,_0x166f56))&&_0x204d06){fs['mkdirSync'](path['join'](_0x2e7f73,_0x166f56));}return BPromise['each'](_0x5c4f09,function(_0x17f4ef){_0x33fb8b[_0xf495('0x1d')](_0x17f4ef);})[_0xf495('0x12')](function(){for(var _0x23b8f7=0x0;_0x23b8f7<_0x33fb8b['length'];_0x23b8f7+=0x1){var _0x4f7b6d;var _0x2dc37e=_0x33fb8b[_0x23b8f7];if(_0x204d06){_0x4f7b6d=path[_0xf495('0x22')](_0x2e7f73,_0x166f56,util[_0xf495('0xd')](_0xf495('0x23'),_0x166f56,_0x23b8f7,_0xf495('0x24')));}else{_0x4f7b6d=path[_0xf495('0x22')](_0x2e7f73,util['format'](_0xf495('0x25'),_0x166f56,_0xf495('0x24')));}fs[_0xf495('0x26')](_0x4f7b6d,csv(_0x2dc37e));}})[_0xf495('0x12')](function(){if(_0x204d06){return new Promise(function(_0x5f402b){zipdir(path[_0xf495('0x22')](_0x2e7f73,_0x166f56),{'saveTo':path[_0xf495('0x22')](_0x2e7f73,util[_0xf495('0xd')](_0xf495('0x25'),_0x166f56,_0xf495('0x27')))},function(_0x41026c,_0x3c1559){rimraf(path[_0xf495('0x22')](_0x2e7f73,_0x166f56),function(){});_0x5f402b(_0xf495('0x28'));});});}})[_0xf495('0x12')](function(){logger[_0xf495('0xc')](util[_0xf495('0xd')](_0xf495('0x29'),EXTRACTED_REPORT_ID,_0xf495('0xf')));return analyticExtractedReport(_0xf495('0x2a'),_0x204d06?util[_0xf495('0xd')]('%s.%s',_0x166f56,_0x204d06?_0xf495('0x27'):_0xf495('0x24')):null);});})[_0xf495('0x2b')](function(_0x5a0384){logger[_0xf495('0xc')](util['format'](_0xf495('0x2c'),EXTRACTED_REPORT_ID,_0xf495('0xf')),util[_0xf495('0x2d')](_0x5a0384,{'showHidden':![],'depth':null}));EXIT_CODE=0x1;return analyticExtractedReport(_0xf495('0x2e'));})[_0xf495('0x2f')](function(){process['exit'](EXIT_CODE);});}main();