Built motion from commit (unavailable).|2.5.18
[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 _0x9f62=['find','split','then','sequelize','query','format','SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t','QueryTypes','SELECT','num','Empty','length','push','dirname','basename','.csv','existsSync','join','mkdirSync','each','csv','%s.%s','writeFileSync','zip','info','Error\x20during\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','inspect','Failed','path','to-csv','bluebird','zip-dir','../../mysqldb','dbH','../../config/logger','analytics','argv','Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','CSV'];(function(_0xc0638b,_0x2fccf4){var _0xd57dfa=function(_0x322fba){while(--_0x322fba){_0xc0638b['push'](_0xc0638b['shift']());}};_0xd57dfa(++_0x2fccf4);}(_0x9f62,0x154));var _0x29f6=function(_0x553173,_0x4864a3){_0x553173=_0x553173-0x0;var _0x9c2cbb=_0x9f62[_0x553173];return _0x9c2cbb;};'use strict';var util=require('util');var path=require(_0x29f6('0x0'));var _=require('lodash');var csv=require(_0x29f6('0x1'));var fs=require('fs');var BPromise=require(_0x29f6('0x2'));var zipdir=require(_0x29f6('0x3'));var rimraf=require('rimraf');var db=require('../../mysqldb')['db'];var dbH=require(_0x29f6('0x4'))[_0x29f6('0x5')];var logger=require(_0x29f6('0x6'))(_0x29f6('0x7'));var EXTRACTED_REPORT_ID=Number(process['argv'][0x2]);var QUERY=process[_0x29f6('0x8')][0x3];var PATH=process[_0x29f6('0x8')][0x4];function analyticExtractedReport(_0x9a43fe,_0x2ba602){var _0x416c6c={'status':_0x9a43fe};var _0x5e9945={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x2ba602){_0x416c6c['savename']=_0x2ba602;}return db['AnalyticExtractedReport']['update'](_0x416c6c,_0x5e9945);}function main(){var _0xe25841=0x1388;var _0x44db16;logger['info'](util['format'](_0x29f6('0x9'),EXTRACTED_REPORT_ID,_0x29f6('0xa')));return db['Setting'][_0x29f6('0xb')]({'where':{'id':0x1},'raw':!![],'attributes':['id',_0x29f6('0xc'),'splitSizeCsv']})['then'](function(_0x559585){if(_0x559585){_0x44db16=_0x559585;_0xe25841=_0x44db16['splitSizeCsv']||0x1388;}})[_0x29f6('0xd')](function(){return dbH[_0x29f6('0xe')][_0x29f6('0xf')](util[_0x29f6('0x10')](_0x29f6('0x11'),QUERY),{'type':dbH[_0x29f6('0xe')][_0x29f6('0x12')][_0x29f6('0x13')]});})[_0x29f6('0xd')](function(_0x19331f){if(!_0x19331f[0x0][_0x29f6('0x14')]){return analyticExtractedReport(_0x29f6('0x15'));}var _0x1dde97=[];var _0x3a75d0=0x0;var _0x25e5a2=!![];if(_0x19331f&&_0x19331f[_0x29f6('0x16')]){var _0x3584a1=_0x19331f[0x0]['num']%(_0x3a75d0+_0xe25841);if(_0x44db16['split']){for(;_0x25e5a2;_0x3a75d0+=_0xe25841,_0x3584a1=_0x19331f[0x0][_0x29f6('0x14')]%(_0x3a75d0+_0xe25841)){if(_0x3584a1==0x0||_0x3584a1==_0x19331f[0x0][_0x29f6('0x14')]){_0x25e5a2=![];}_0x1dde97[_0x29f6('0x17')](dbH['sequelize'][_0x29f6('0xf')](util[_0x29f6('0x10')]('%s\x20limit\x20%d\x20OFFSET\x20%d',QUERY,_0xe25841,_0x3a75d0),{'type':dbH[_0x29f6('0xe')][_0x29f6('0x12')][_0x29f6('0x13')]}));}}else{_0x1dde97[_0x29f6('0x17')](dbH[_0x29f6('0xe')][_0x29f6('0xf')](QUERY,{'type':dbH['sequelize'][_0x29f6('0x12')]['SELECT']}));}}var _0x4acb15=path[_0x29f6('0x18')](PATH);var _0x1fa159=path[_0x29f6('0x19')](PATH,_0x29f6('0x1a'));var _0x54ddeb=_0x44db16[_0x29f6('0xc')]&&_0x1dde97['length']>0x1;var _0xd4179c=[];if(!fs[_0x29f6('0x1b')](path[_0x29f6('0x1c')](_0x4acb15,_0x1fa159))&&_0x54ddeb){fs[_0x29f6('0x1d')](path['join'](_0x4acb15,_0x1fa159));}return BPromise[_0x29f6('0x1e')](_0x1dde97,function(_0x4d494f){_0xd4179c['push'](_0x4d494f);})[_0x29f6('0xd')](function(){for(var _0x45f792=0x0;_0x45f792<_0xd4179c[_0x29f6('0x16')];_0x45f792+=0x1){var _0x342038;var _0x84ccb7=_0xd4179c[_0x45f792];if(_0x54ddeb){_0x342038=path[_0x29f6('0x1c')](_0x4acb15,_0x1fa159,util[_0x29f6('0x10')]('%s%d.%s',_0x1fa159,_0x45f792,_0x29f6('0x1f')));}else{_0x342038=path[_0x29f6('0x1c')](_0x4acb15,util[_0x29f6('0x10')](_0x29f6('0x20'),_0x1fa159,_0x29f6('0x1f')));}fs[_0x29f6('0x21')](_0x342038,csv(_0x84ccb7));}})[_0x29f6('0xd')](function(){if(_0x54ddeb){zipdir(path[_0x29f6('0x1c')](_0x4acb15,_0x1fa159),{'saveTo':path[_0x29f6('0x1c')](_0x4acb15,util[_0x29f6('0x10')](_0x29f6('0x20'),_0x1fa159,_0x29f6('0x22')))},function(_0x40c830,_0x304c28){});rimraf(path[_0x29f6('0x1c')](_0x4acb15,_0x1fa159),function(){});}})[_0x29f6('0xd')](function(){logger[_0x29f6('0x23')](util['format']('Extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format\x20completed',EXTRACTED_REPORT_ID,_0x29f6('0xa')));return analyticExtractedReport('Completed',_0x54ddeb?util[_0x29f6('0x10')](_0x29f6('0x20'),_0x1fa159,_0x54ddeb?_0x29f6('0x22'):_0x29f6('0x1f')):null);});})['catch'](function(_0x53e11f){logger[_0x29f6('0x23')](util[_0x29f6('0x10')](_0x29f6('0x24'),EXTRACTED_REPORT_ID,_0x29f6('0xa')),util[_0x29f6('0x25')](_0x53e11f,{'showHidden':![],'depth':null}));return analyticExtractedReport(_0x29f6('0x26'));});}main();