Skip to content
This repository has been archived by the owner on Oct 15, 2019. It is now read-only.

Commit

Permalink
Fixed account tickets list
Browse files Browse the repository at this point in the history
  • Loading branch information
Zenedith committed Nov 11, 2015
1 parent a199b76 commit e827704
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
80 changes: 70 additions & 10 deletions lib/client/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,70 @@ var receiptsModel = require('receipts-model');
var ServiceUnavailableError = receiptsModel.error.ServiceUnavailableError;
var UnauthorizedUserError = receiptsModel.error.UnauthorizedUserError;
var moment = require('moment-timezone');
var receiptIdRegex = /edytuj\/(.+)$/;
var receiptIdRegex = /edytuj\/(.+)\/.*$/;

var MONTHS = [
'10.2015',
'11.2015',
'12.2015',
'01.2016',
'02.2016',
'03.2016',
'04.2016',
'05.2016',
'06.2016',
'07.2016',
'08.2016',
'09.2016',
'10.2016'
];

var handleReceipts = function handleReceipts(jar, formConfig, callback) {
logger.debug('handleReceipts');

async.map(
MONTHS,
function (month, cb) {
handleReceiptsForMonth(jar, month, formConfig, cb);
},
function (err, res) {
if (err) {
return callback(err);
}

res.reverse();

var collection = [];

for (var i = 0; i < res.length; ++i) {
var monthRes = res[i];

if (monthRes.length) {
for (var j = 0; j < monthRes.length; ++j) {
collection = collection.concat(monthRes[j]);
}
}
}

return callback(err, collection);
}
);

};

var handleReceiptsForMonth = function handleReceiptsForMonth(jar, month, formConfig, callback) {
logger.debug('handleReceiptsForMonth', month);

var collection = [];
var page = 0;

async.forever(
function (cb) {
page++;
buildList(jar, page, formConfig, function (err, list) {
buildList(jar, month, page, formConfig, function (err, list) {
if (err) {
return cb(err);
}

if (list && list.length > 0) {
collection = collection.concat(list);
Expand All @@ -36,12 +88,16 @@ var handleReceipts = function handleReceipts(jar, formConfig, callback) {

};

var buildList = function buildList(jar, page, formConfig, callback) {
logger.debug('buildList', page);
var buildList = function buildList(jar, month, page, formConfig, callback) {
logger.debug('buildList', month, page);

return getTicketsRequest(jar, page, formConfig, function (err, body) {
var receipts = [];
return getTicketsRequest(jar, month, page, formConfig, function (err, body) {

if (err) {
return callback(err);
}

var receipts = [];
var $ = cheerio.load(body);
$('tr', '.table-responsive').each(function (i, tr) {
var receipt = null;
Expand All @@ -64,7 +120,7 @@ var buildList = function buildList(jar, page, formConfig, callback) {
break;
case 4:
receipt.id = null;
var href = $('a', $(this)).attr('href');
var href = $('a.btn-edit', $(this)).attr('href');
if (href && href.match(receiptIdRegex)) {
receipt.id = href.match(receiptIdRegex)[1];
}
Expand All @@ -81,16 +137,19 @@ var buildList = function buildList(jar, page, formConfig, callback) {
});
};

var getTicketsRequest = function getTicketsRequest(jar, page, formConfig, callback) {
logger.debug('getTicketsRequest');
var getTicketsRequest = function getTicketsRequest(jar, month, page, formConfig, callback) {
logger.debug('getTicketsRequest', month, page);

assert.number(page, 'page');
assert.string(month, 'month');
assert.object(formConfig, 'formConfig');
assert.object(formConfig.headers, 'formConfig.headers');
assert.string(formConfig.url, 'formConfig.url');
assert.number(formConfig.timeout, 'formConfig.timeout');

var url = formConfig.url + '?page=' + page;
var url = formConfig.url + '?type=' + month + '&page=' + page;
console.log(url);

var opt = {
url: url,
method: 'GET',
Expand Down Expand Up @@ -139,6 +198,7 @@ var getTickets = function getTickets(jar, options, callback) {

module.exports = {
handleReceipts: handleReceipts,
handleReceiptsForMonth: handleReceiptsForMonth,
getTicketsRequest: getTicketsRequest,
getTickets: getTickets
};
2 changes: 1 addition & 1 deletion test/client/accountTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('account client test', function () {
// accountClient.getTickets(jar, options, function (err, collection) {
// console.log(collection);
// expect(err).to.be.not.undefined;
// expect(collection).to.be.undefined;
// expect(collection).to.be.not.empty;
// done();
// });
// });
Expand Down

0 comments on commit e827704

Please sign in to comment.