c89a4c07a72a7fbcd2edc5dad9a5cbf75d59d563
[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 _0x37f8=['basename','.csv','join','mkdirSync','%s%d.%s','csv','%s.%s','writeFileSync','zip','catch','Error\x20during\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','inspect','Failed','finally','path','to-csv','bluebird','zip-dir','rimraf','../../mysqldb','../../config/logger','analytics','argv','savename','update','info','Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','CSV','Setting','find','splitSizeCsv','then','format','SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t','sequelize','QueryTypes','SELECT','num','isInteger','forEach','%s\x20limit\x20%d\x20OFFSET\x20%d','query','push','dirname'];(function(_0x23266e,_0x3647e2){var _0x55c2b6=function(_0x32afd4){while(--_0x32afd4){_0x23266e['push'](_0x23266e['shift']());}};_0x55c2b6(++_0x3647e2);}(_0x37f8,0x1f2));var _0x837f=function(_0x48b2ea,_0x81391e){_0x48b2ea=_0x48b2ea-0x0;var _0x4c412e=_0x37f8[_0x48b2ea];return _0x4c412e;};'use strict';var util=require('util');var path=require(_0x837f('0x0'));var _=require('lodash');var csv=require(_0x837f('0x1'));var fs=require('fs');var BPromise=require(_0x837f('0x2'));var zipdir=require(_0x837f('0x3'));var rimraf=require(_0x837f('0x4'));var db=require(_0x837f('0x5'))['db'];var dbH=require(_0x837f('0x5'))['dbH'];var logger=require(_0x837f('0x6'))(_0x837f('0x7'));var EXTRACTED_REPORT_ID=Number(process[_0x837f('0x8')][0x2]);var QUERY=process[_0x837f('0x8')][0x3];var PATH=process['argv'][0x4];var EXIT_CODE=0x0;function analyticExtractedReport(_0x3afbde,_0x1820a4){var _0x41cfb2={'status':_0x3afbde};var _0x4b8d15={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x1820a4){_0x41cfb2[_0x837f('0x9')]=_0x1820a4;}return db['AnalyticExtractedReport'][_0x837f('0xa')](_0x41cfb2,_0x4b8d15);}function main(){var _0x3cbc60=0x1388;var _0x3968b9;logger[_0x837f('0xb')](util['format'](_0x837f('0xc'),EXTRACTED_REPORT_ID,_0x837f('0xd')));return db[_0x837f('0xe')][_0x837f('0xf')]({'where':{'id':0x1},'raw':!![],'attributes':['id','split',_0x837f('0x10')]})[_0x837f('0x11')](function(_0x51f863){if(_0x51f863){_0x3968b9=_0x51f863;_0x3cbc60=_0x3968b9[_0x837f('0x10')]||0x1388;}})['then'](function(){return dbH['sequelize']['query'](util[_0x837f('0x12')](_0x837f('0x13'),QUERY),{'type':dbH[_0x837f('0x14')][_0x837f('0x15')][_0x837f('0x16')]});})[_0x837f('0x11')](function(_0xa3c4d0){if(!_0xa3c4d0[0x0][_0x837f('0x17')]){return analyticExtractedReport('Empty');}var _0xd32abd=[];var _0x2435df=0x0;if(_0x3968b9['split']){if(_0xa3c4d0&&_0xa3c4d0['length']){var _0x3a69d4=_['range'](_0xa3c4d0[0x0][_0x837f('0x17')]/_0x3cbc60);var _0x524f8e=Number[_0x837f('0x18')](_0xa3c4d0[0x0][_0x837f('0x17')]/_0x3cbc60)?_0xa3c4d0[0x0][_0x837f('0x17')]%_0x3cbc60:0x0;_0x3a69d4[_0x837f('0x19')](function(_0x2c49dd){if(_0x2c49dd>0x0){_0x2435df+=_0x3cbc60;}_0xd32abd['push'](dbH[_0x837f('0x14')]['query'](util[_0x837f('0x12')](_0x837f('0x1a'),QUERY,_0x3cbc60,_0x2435df),{'type':dbH[_0x837f('0x14')][_0x837f('0x15')][_0x837f('0x16')]}));});if(_0x524f8e>0x0){_0x2435df+=_0x3cbc60;_0xd32abd['push'](dbH[_0x837f('0x14')][_0x837f('0x1b')](util[_0x837f('0x12')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0x524f8e,_0x2435df),{'type':dbH[_0x837f('0x14')][_0x837f('0x15')]['SELECT']}));}}}else{_0xd32abd[_0x837f('0x1c')](dbH[_0x837f('0x14')][_0x837f('0x1b')](QUERY,{'type':dbH['sequelize']['QueryTypes'][_0x837f('0x16')]}));}var _0x26489f=path[_0x837f('0x1d')](PATH);var _0x5d93ab=path[_0x837f('0x1e')](PATH,_0x837f('0x1f'));var _0x5df62f=_0x3968b9['split']&&_0xd32abd['length']>0x1;var _0x37e2b3=[];if(!fs['existsSync'](path[_0x837f('0x20')](_0x26489f,_0x5d93ab))&&_0x5df62f){fs[_0x837f('0x21')](path['join'](_0x26489f,_0x5d93ab));}return BPromise['each'](_0xd32abd,function(_0x30d0b9){_0x37e2b3[_0x837f('0x1c')](_0x30d0b9);})[_0x837f('0x11')](function(){for(var _0x5cfd8b=0x0;_0x5cfd8b<_0x37e2b3['length'];_0x5cfd8b+=0x1){var _0x16fb37;var _0x4e6e31=_0x37e2b3[_0x5cfd8b];if(_0x5df62f){_0x16fb37=path[_0x837f('0x20')](_0x26489f,_0x5d93ab,util[_0x837f('0x12')](_0x837f('0x22'),_0x5d93ab,_0x5cfd8b,_0x837f('0x23')));}else{_0x16fb37=path[_0x837f('0x20')](_0x26489f,util[_0x837f('0x12')](_0x837f('0x24'),_0x5d93ab,_0x837f('0x23')));}fs[_0x837f('0x25')](_0x16fb37,csv(_0x4e6e31));}})[_0x837f('0x11')](function(){if(_0x5df62f){return new Promise(function(_0x4031b4){zipdir(path['join'](_0x26489f,_0x5d93ab),{'saveTo':path['join'](_0x26489f,util[_0x837f('0x12')](_0x837f('0x24'),_0x5d93ab,_0x837f('0x26')))},function(_0x53b723,_0x180e8d){rimraf(path[_0x837f('0x20')](_0x26489f,_0x5d93ab),function(){});_0x4031b4('zip\x20finished');});});}})[_0x837f('0x11')](function(){logger['info'](util[_0x837f('0x12')]('Extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format\x20completed',EXTRACTED_REPORT_ID,_0x837f('0xd')));return analyticExtractedReport('Completed',_0x5df62f?util[_0x837f('0x12')](_0x837f('0x24'),_0x5d93ab,_0x5df62f?_0x837f('0x26'):_0x837f('0x23')):null);});})[_0x837f('0x27')](function(_0x5d3a4d){logger['info'](util[_0x837f('0x12')](_0x837f('0x28'),EXTRACTED_REPORT_ID,_0x837f('0xd')),util[_0x837f('0x29')](_0x5d3a4d,{'showHidden':![],'depth':null}));EXIT_CODE=0x1;return analyticExtractedReport(_0x837f('0x2a'));})[_0x837f('0x2b')](function(){process['exit'](EXIT_CODE);});}main();