Built motion from commit (unavailable).|2.5.22
[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 _0x22fb=['savename','AnalyticExtractedReport','update','info','format','Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','CSV','splitSizeCsv','then','sequelize','query','SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t','SELECT','Empty','length','num','push','dirname','basename','.csv','split','join','mkdirSync','each','%s%d.%s','csv','%s.%s','zip','Extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format\x20completed','Completed','catch','inspect','Failed','lodash','bluebird','zip-dir','rimraf','../../mysqldb','../../config/logger','analytics','argv'];(function(_0x2c4d11,_0xebb391){var _0x5e7a07=function(_0x44af67){while(--_0x44af67){_0x2c4d11['push'](_0x2c4d11['shift']());}};_0x5e7a07(++_0xebb391);}(_0x22fb,0x192));var _0xb22f=function(_0xc81ea9,_0x359ca8){_0xc81ea9=_0xc81ea9-0x0;var _0xce0a44=_0x22fb[_0xc81ea9];return _0xce0a44;};'use strict';var util=require('util');var path=require('path');var _=require(_0xb22f('0x0'));var csv=require('to-csv');var fs=require('fs');var BPromise=require(_0xb22f('0x1'));var zipdir=require(_0xb22f('0x2'));var rimraf=require(_0xb22f('0x3'));var db=require(_0xb22f('0x4'))['db'];var dbH=require(_0xb22f('0x4'))['dbH'];var logger=require(_0xb22f('0x5'))(_0xb22f('0x6'));var EXTRACTED_REPORT_ID=Number(process[_0xb22f('0x7')][0x2]);var QUERY=process[_0xb22f('0x7')][0x3];var PATH=process[_0xb22f('0x7')][0x4];function analyticExtractedReport(_0x367d4b,_0x47f61a){var _0x34433b={'status':_0x367d4b};var _0x22c509={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x47f61a){_0x34433b[_0xb22f('0x8')]=_0x47f61a;}return db[_0xb22f('0x9')][_0xb22f('0xa')](_0x34433b,_0x22c509);}function main(){var _0x3593fb=0x1388;var _0x188cea;logger[_0xb22f('0xb')](util[_0xb22f('0xc')](_0xb22f('0xd'),EXTRACTED_REPORT_ID,_0xb22f('0xe')));return db['Setting']['find']({'where':{'id':0x1},'raw':!![],'attributes':['id','split',_0xb22f('0xf')]})['then'](function(_0x4bb02a){if(_0x4bb02a){_0x188cea=_0x4bb02a;_0x3593fb=_0x188cea['splitSizeCsv']||0x1388;}})[_0xb22f('0x10')](function(){return dbH[_0xb22f('0x11')][_0xb22f('0x12')](util[_0xb22f('0xc')](_0xb22f('0x13'),QUERY),{'type':dbH[_0xb22f('0x11')]['QueryTypes'][_0xb22f('0x14')]});})['then'](function(_0x36e2a8){if(!_0x36e2a8[0x0]['num']){return analyticExtractedReport(_0xb22f('0x15'));}var _0x514a8f=[];var _0x160417=0x0;var _0xc36af4=!![];if(_0x36e2a8&&_0x36e2a8[_0xb22f('0x16')]){var _0x538da0=_0x36e2a8[0x0][_0xb22f('0x17')]%(_0x160417+_0x3593fb);if(_0x188cea['split']){for(;_0xc36af4;_0x160417+=_0x3593fb,_0x538da0=_0x36e2a8[0x0][_0xb22f('0x17')]%(_0x160417+_0x3593fb)){if(_0x538da0==0x0||_0x538da0==_0x36e2a8[0x0][_0xb22f('0x17')]){_0xc36af4=![];}_0x514a8f[_0xb22f('0x18')](dbH[_0xb22f('0x11')][_0xb22f('0x12')](util[_0xb22f('0xc')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0x3593fb,_0x160417),{'type':dbH[_0xb22f('0x11')]['QueryTypes'][_0xb22f('0x14')]}));}}else{_0x514a8f[_0xb22f('0x18')](dbH[_0xb22f('0x11')][_0xb22f('0x12')](QUERY,{'type':dbH[_0xb22f('0x11')]['QueryTypes']['SELECT']}));}}var _0x3da442=path[_0xb22f('0x19')](PATH);var _0xf34e0d=path[_0xb22f('0x1a')](PATH,_0xb22f('0x1b'));var _0xcb9a74=_0x188cea[_0xb22f('0x1c')]&&_0x514a8f[_0xb22f('0x16')]>0x1;var _0x583fbe=[];if(!fs['existsSync'](path[_0xb22f('0x1d')](_0x3da442,_0xf34e0d))&&_0xcb9a74){fs[_0xb22f('0x1e')](path['join'](_0x3da442,_0xf34e0d));}return BPromise[_0xb22f('0x1f')](_0x514a8f,function(_0x4364f5){_0x583fbe['push'](_0x4364f5);})[_0xb22f('0x10')](function(){for(var _0x2918be=0x0;_0x2918be<_0x583fbe['length'];_0x2918be+=0x1){var _0x3e471f;var _0x2c4a9e=_0x583fbe[_0x2918be];if(_0xcb9a74){_0x3e471f=path[_0xb22f('0x1d')](_0x3da442,_0xf34e0d,util[_0xb22f('0xc')](_0xb22f('0x20'),_0xf34e0d,_0x2918be,_0xb22f('0x21')));}else{_0x3e471f=path[_0xb22f('0x1d')](_0x3da442,util[_0xb22f('0xc')]('%s.%s',_0xf34e0d,'csv'));}fs['writeFileSync'](_0x3e471f,csv(_0x2c4a9e));}})[_0xb22f('0x10')](function(){if(_0xcb9a74){zipdir(path['join'](_0x3da442,_0xf34e0d),{'saveTo':path[_0xb22f('0x1d')](_0x3da442,util[_0xb22f('0xc')](_0xb22f('0x22'),_0xf34e0d,_0xb22f('0x23')))},function(_0x3bf636,_0x5b2a42){});rimraf(path['join'](_0x3da442,_0xf34e0d),function(){});}})[_0xb22f('0x10')](function(){logger[_0xb22f('0xb')](util[_0xb22f('0xc')](_0xb22f('0x24'),EXTRACTED_REPORT_ID,'CSV'));return analyticExtractedReport(_0xb22f('0x25'),_0xcb9a74?util[_0xb22f('0xc')]('%s.%s',_0xf34e0d,_0xcb9a74?_0xb22f('0x23'):_0xb22f('0x21')):null);});})[_0xb22f('0x26')](function(_0x225194){logger[_0xb22f('0xb')](util['format']('Error\x20during\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format',EXTRACTED_REPORT_ID,_0xb22f('0xe')),util[_0xb22f('0x27')](_0x225194,{'showHidden':![],'depth':null}));return analyticExtractedReport(_0xb22f('0x28'));});}main();