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

Commit

Permalink
Handle adding ticket logic from controller in lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Zenedith committed Nov 11, 2015
1 parent e827704 commit aa24916
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
85 changes: 81 additions & 4 deletions lib/client/tickets.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
var extend = require('util')._extend;
var cheerio = require('cheerio');
var assert = require('assert-plus');
var request = require('request');
var logger = require('../logger/logger').logger;
var receiptsModel = require('receipts-model');
var ServiceUnavailableError = receiptsModel.error.ServiceUnavailableError;
var AlreadyExistsError = receiptsModel.error.AlreadyExistsError;
var UnknownValidateError = receiptsModel.error.UnknownValidateError;
var InvalidPointOfSaleError = receiptsModel.error.InvalidPointOfSaleError;
var InvalidDateError = receiptsModel.error.InvalidDateError;
var InvalidAmountError = receiptsModel.error.InvalidAmountError;
var InvalidTaxRegistrationNumberError = receiptsModel.error.InvalidTaxRegistrationNumberError;
var mainClient = require('./main');
var moment = require('moment-timezone');
var receiptIdRegex = /drukuj\/(.+)$/;

var buildForm = function buildForm(ticketRequest, captcha) {
logger.debug('buildForm');
Expand Down Expand Up @@ -143,6 +151,67 @@ var sendCaptchaRequest = function sendCaptchaRequest(jar, token, captcha, captch
}
};

var buildResponse = function buildResponse(body, callback) {
assert.string(body, 'body');

var json = JSON.parse(body);

if (json.hasOwnProperty('success')) {

if (!json.success) {

if (json.message.indexOf('paragon fiskalny został już zgłoszony.') > -1) {
return callback(new AlreadyExistsError('Ticket already exists', json.message));
}

return callback(new UnknownValidateError('Unable to validate parameters', json.message));
}
else {
var $ = cheerio.load(json.message);

var id = null;
var code = $('.recipe-number', '.ty-form').text();
$('div.links a').each(function (i, elem) {
var href = $(this).attr('href');
if (href && href.match(receiptIdRegex)) {
id = href.match(receiptIdRegex)[1];
}
});

var response = {
id: id,
code: code
};

return callback(null, response);
}
}
else {
if (json.hasOwnProperty('nr_kasy')) {
return callback(new InvalidPointOfSaleError('Invalid pointOfSale number', json.nr_kasy.join(','), 'pointOfSale'));
}

if (json.hasOwnProperty('miesiac')) {
return callback(new InvalidDateError('Invalid date', json.miesiac.join(','), 'date'));
}

if (json.hasOwnProperty('kwota_zl')) {
return callback(new InvalidAmountError('Invalid amount value', json.kwota_zl.join(','), 'amount.value'));
}

if (json.hasOwnProperty('nip')) {
if (json.nip.length > 1) {
return callback(new InvalidTaxRegistrationNumberError('Invalid taxRegistrationNumber number', json.nip[1], 'taxRegistrationNumber'));
}
else {
return callback(new InvalidTaxRegistrationNumberError('Invalid taxRegistrationNumber number', json.nip[0], 'taxRegistrationNumber'));
}
}
}

return callback(new UnknownValidateError('Unknown validate exception'));
};

var sendLotteryTicket = function sendLotteryTicket(ticketRequest, options, callback) {
assert.object(ticketRequest, 'ticketRequest');
assert.object(options, 'options');
Expand All @@ -165,18 +234,25 @@ var sendLotteryTicket = function sendLotteryTicket(ticketRequest, options, callb
ticketsRequestConfig.url = data.url;
}

sendCaptchaRequest(jar, data.token, data.captcha, captchaRequestConfig, function (err, body) {
sendCaptchaRequest(jar, data.token, data.captcha, captchaRequestConfig, function (err, captchBody) {
if (err) {
logger.error('sendCaptchaRequest returned with error: ', err);
return callback(err);
}

if (body !== 'true') {
logger.error('sendCaptchaRequest invalid body: ', body);
if (captchBody !== 'true') {
logger.error('sendCaptchaRequest invalid body: ', captchBody);
return callback(new ServiceUnavailableError('Unable to send captcha'));
}

return sendLotteryTicketRequest(jar, data.token, data.captcha, ticketRequest, ticketsRequestConfig, callback);
return sendLotteryTicketRequest(jar, data.token, data.captcha, ticketRequest, ticketsRequestConfig, function (err, body) {
if (err) {
logger.error('sendLotteryTicketRequest returned with error: ', err);
return callback(err);
}

return buildResponse(body, callback);
});
});
});
};
Expand All @@ -185,5 +261,6 @@ module.exports = {
buildForm: buildForm,
sendLotteryTicketRequest: sendLotteryTicketRequest,
sendCaptchaRequest: sendCaptchaRequest,
buildResponse: buildResponse,
sendLotteryTicket: sendLotteryTicket
};
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "receipts-lottery-client",
"version": "0.5.4",
"version": "0.5.5",
"description": "Receipts lottery client",
"main": "./index.js",
"dependencies": {
"assert-plus": "^0.1.5",
"async": "^1.5.0",
"cheerio": "^0.19.0",
"moment-timezone": "^0.4.1",
"receipts-model": "0.5.3",
"receipts-model": "0.5.5",
"request": "^2.65.0",
"winston": "^2.0.0",
"winston-loggly": "^1.2.0"
Expand Down Expand Up @@ -45,6 +45,6 @@
"node": ">=0.10.0"
},
"readmeFilename": "README.md",
"_id": "receipts-lottery-client@0.5.4",
"_from": "receipts-lottery-client@^0.5.4"
"_id": "receipts-lottery-client@0.5.5",
"_from": "receipts-lottery-client@^0.5.5"
}
8 changes: 4 additions & 4 deletions test/client/ticketsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ describe('tickets client test', function () {
}
};

ticketsClient.sendLotteryTicket(ticketRequest, options, function (err, body) {
//console.log(err, body);
expect(err).to.be.null;
expect(body).to.be.not.undefined;
ticketsClient.sendLotteryTicket(ticketRequest, options, function (err, data) {
console.log(err, data);
//expect(err).to.be.null;
//expect(data).to.be.not.undefined;
done();
});
});
Expand Down

0 comments on commit aa24916

Please sign in to comment.