c3e3537abd6f5c554edae548d5e2fb13f01e5b06
[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 _0x1f0f=['finally','exit','util','path','lodash','to-csv','bluebird','zip-dir','rimraf','../../mysqldb','dbH','analytics','argv','savename','AnalyticExtractedReport','info','format','Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','CSV','find','split','splitSizeCsv','then','sequelize','SELECT','num','Empty','query','%s\x20limit\x20%d\x20OFFSET\x20%d','QueryTypes','push','dirname','basename','.csv','existsSync','join','mkdirSync','each','length','%s.%s','csv','writeFileSync','zip','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'];(function(_0x57b54c,_0x54ce0d){var _0x59cc19=function(_0xa33bc6){while(--_0xa33bc6){_0x57b54c['push'](_0x57b54c['shift']());}};_0x59cc19(++_0x54ce0d);}(_0x1f0f,0x1bb));var _0xf1f0=function(_0x5ab403,_0x34aa42){_0x5ab403=_0x5ab403-0x0;var _0x2e30f1=_0x1f0f[_0x5ab403];return _0x2e30f1;};'use strict';var util=require(_0xf1f0('0x0'));var path=require(_0xf1f0('0x1'));var _=require(_0xf1f0('0x2'));var csv=require(_0xf1f0('0x3'));var fs=require('fs');var BPromise=require(_0xf1f0('0x4'));var zipdir=require(_0xf1f0('0x5'));var rimraf=require(_0xf1f0('0x6'));var db=require(_0xf1f0('0x7'))['db'];var dbH=require(_0xf1f0('0x7'))[_0xf1f0('0x8')];var logger=require('../../config/logger')(_0xf1f0('0x9'));var EXTRACTED_REPORT_ID=Number(process[_0xf1f0('0xa')][0x2]);var QUERY=process[_0xf1f0('0xa')][0x3];var PATH=process[_0xf1f0('0xa')][0x4];var EXIT_CODE=0x0;function analyticExtractedReport(_0x29842f,_0x165671){var _0x3b6a12={'status':_0x29842f};var _0x3cb8c0={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x165671){_0x3b6a12[_0xf1f0('0xb')]=_0x165671;}return db[_0xf1f0('0xc')]['update'](_0x3b6a12,_0x3cb8c0);}function main(){var _0x393e5c=0x1388;var _0x5698bb;logger[_0xf1f0('0xd')](util[_0xf1f0('0xe')](_0xf1f0('0xf'),EXTRACTED_REPORT_ID,_0xf1f0('0x10')));return db['Setting'][_0xf1f0('0x11')]({'where':{'id':0x1},'raw':!![],'attributes':['id',_0xf1f0('0x12'),'splitSizeCsv']})['then'](function(_0x383537){if(_0x383537){_0x5698bb=_0x383537;_0x393e5c=_0x5698bb[_0xf1f0('0x13')]||0x1388;}})[_0xf1f0('0x14')](function(){return dbH[_0xf1f0('0x15')]['query'](util[_0xf1f0('0xe')]('SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t',QUERY),{'type':dbH[_0xf1f0('0x15')]['QueryTypes'][_0xf1f0('0x16')]});})[_0xf1f0('0x14')](function(_0x48b5b4){if(!_0x48b5b4[0x0][_0xf1f0('0x17')]){return analyticExtractedReport(_0xf1f0('0x18'));}var _0x15f0a4=[];var _0x154af2=0x0;var _0x18ecff=!![];if(_0x48b5b4&&_0x48b5b4['length']){var _0x4afc83=_0x48b5b4[0x0][_0xf1f0('0x17')]%(_0x154af2+_0x393e5c);if(_0x5698bb[_0xf1f0('0x12')]){for(;_0x18ecff;_0x154af2+=_0x393e5c,_0x4afc83=_0x48b5b4[0x0][_0xf1f0('0x17')]%(_0x154af2+_0x393e5c)){if(_0x4afc83==0x0||_0x4afc83==_0x48b5b4[0x0][_0xf1f0('0x17')]){_0x18ecff=![];}_0x15f0a4['push'](dbH[_0xf1f0('0x15')][_0xf1f0('0x19')](util['format'](_0xf1f0('0x1a'),QUERY,_0x393e5c,_0x154af2),{'type':dbH[_0xf1f0('0x15')][_0xf1f0('0x1b')][_0xf1f0('0x16')]}));}}else{_0x15f0a4[_0xf1f0('0x1c')](dbH[_0xf1f0('0x15')][_0xf1f0('0x19')](QUERY,{'type':dbH['sequelize']['QueryTypes'][_0xf1f0('0x16')]}));}}var _0x3edcec=path[_0xf1f0('0x1d')](PATH);var _0x10a528=path[_0xf1f0('0x1e')](PATH,_0xf1f0('0x1f'));var _0x1d32bb=_0x5698bb[_0xf1f0('0x12')]&&_0x15f0a4['length']>0x1;var _0x11dfce=[];if(!fs[_0xf1f0('0x20')](path[_0xf1f0('0x21')](_0x3edcec,_0x10a528))&&_0x1d32bb){fs[_0xf1f0('0x22')](path[_0xf1f0('0x21')](_0x3edcec,_0x10a528));}return BPromise[_0xf1f0('0x23')](_0x15f0a4,function(_0x249abc){_0x11dfce[_0xf1f0('0x1c')](_0x249abc);})[_0xf1f0('0x14')](function(){for(var _0x1c86c5=0x0;_0x1c86c5<_0x11dfce[_0xf1f0('0x24')];_0x1c86c5+=0x1){var _0x2f31db;var _0x505004=_0x11dfce[_0x1c86c5];if(_0x1d32bb){_0x2f31db=path[_0xf1f0('0x21')](_0x3edcec,_0x10a528,util[_0xf1f0('0xe')]('%s%d.%s',_0x10a528,_0x1c86c5,'csv'));}else{_0x2f31db=path[_0xf1f0('0x21')](_0x3edcec,util[_0xf1f0('0xe')](_0xf1f0('0x25'),_0x10a528,_0xf1f0('0x26')));}fs[_0xf1f0('0x27')](_0x2f31db,csv(_0x505004));}})['then'](function(){if(_0x1d32bb){zipdir(path[_0xf1f0('0x21')](_0x3edcec,_0x10a528),{'saveTo':path['join'](_0x3edcec,util[_0xf1f0('0xe')](_0xf1f0('0x25'),_0x10a528,_0xf1f0('0x28')))},function(_0x50211f,_0x413a72){});rimraf(path[_0xf1f0('0x21')](_0x3edcec,_0x10a528),function(){});}})['then'](function(){logger[_0xf1f0('0xd')](util['format'](_0xf1f0('0x29'),EXTRACTED_REPORT_ID,'CSV'));return analyticExtractedReport(_0xf1f0('0x2a'),_0x1d32bb?util[_0xf1f0('0xe')](_0xf1f0('0x25'),_0x10a528,_0x1d32bb?_0xf1f0('0x28'):_0xf1f0('0x26')):null);});})[_0xf1f0('0x2b')](function(_0xaece02){logger['info'](util[_0xf1f0('0xe')](_0xf1f0('0x2c'),EXTRACTED_REPORT_ID,_0xf1f0('0x10')),util[_0xf1f0('0x2d')](_0xaece02,{'showHidden':![],'depth':null}));EXIT_CODE=0x1;return analyticExtractedReport(_0xf1f0('0x2e'));})[_0xf1f0('0x2f')](function(){process[_0xf1f0('0x30')](EXIT_CODE);});}main();