Add health checks to Node.js services (#26)
* Move Node healthchecks to gRPC * gitignore proto files * Switch to standard health RPC * Fix lint * Update client.js * Add protos back + update them * node services: fix & run genproto.sh this gets currencyservice to work but paymentservice is still crashing in the docker container. Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com> * Fix docker breaking * update dockerfiles with released health probe Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
360d983512
commit
6c37a96f3a
18 changed files with 313 additions and 109 deletions
|
@ -16,26 +16,26 @@ const cardValidator = require('simple-card-validator');
|
|||
const uuid = require('uuid/v4');
|
||||
|
||||
class CreditCardError extends Error {
|
||||
constructor(message) {
|
||||
constructor (message) {
|
||||
super(message);
|
||||
this.code = 400; // Invalid argument error
|
||||
}
|
||||
}
|
||||
|
||||
class InvalidCreditCard extends CreditCardError {
|
||||
constructor(cardType) {
|
||||
constructor (cardType) {
|
||||
super(`Credit card info is invalid`);
|
||||
}
|
||||
}
|
||||
|
||||
class UnacceptedCreditCard extends CreditCardError {
|
||||
constructor(cardType) {
|
||||
constructor (cardType) {
|
||||
super(`Sorry, we cannot process ${cardType} credit cards. Only VISA or MasterCard is accepted.`);
|
||||
}
|
||||
}
|
||||
|
||||
class ExpiredCreditCard extends CreditCardError {
|
||||
constructor(number, month, year) {
|
||||
constructor (number, month, year) {
|
||||
super(`Your credit card (ending ${number.substr(-4)}) expired on ${month}/${year}`);
|
||||
}
|
||||
}
|
||||
|
@ -46,34 +46,29 @@ class ExpiredCreditCard extends CreditCardError {
|
|||
* @param {*} request
|
||||
* @return transaction_id - a random uuid v4.
|
||||
*/
|
||||
module.exports = function charge(request) {
|
||||
module.exports = function charge (request) {
|
||||
const { amount, credit_card: creditCard } = request;
|
||||
const cardNumber = creditCard.credit_card_number;
|
||||
const cardInfo = cardValidator(cardNumber);
|
||||
const {
|
||||
card_type: cardType,
|
||||
valid,
|
||||
cvv_length: cvvLength,
|
||||
valid
|
||||
} = cardInfo.getCardDetails();
|
||||
|
||||
if (!valid)
|
||||
throw new InvalidCreditCard();
|
||||
if (!valid) { throw new InvalidCreditCard(); }
|
||||
|
||||
// Only VISA and mastercard is accepted, other card types (AMEX, dinersclub) will
|
||||
// throw UnacceptedCreditCard error.
|
||||
if (!(cardType === 'visa' || cardType == 'mastercard'))
|
||||
throw new UnacceptedCreditCard(cardType);
|
||||
if (!(cardType === 'visa' || cardType === 'mastercard')) { throw new UnacceptedCreditCard(cardType); }
|
||||
|
||||
// Also validate expiration is > today.
|
||||
const currentMonth = new Date().getMonth() + 1;
|
||||
const currentYear = new Date().getFullYear();
|
||||
const { credit_card_expiration_year: year, credit_card_expiration_month: month } = creditCard;
|
||||
if ((currentYear * 12 + currentMonth) > (year * 12 + month))
|
||||
throw new ExpiredCreditCard(cardNumber.replace('-', ''), month, year);
|
||||
if ((currentYear * 12 + currentMonth) > (year * 12 + month)) { throw new ExpiredCreditCard(cardNumber.replace('-', ''), month, year); }
|
||||
|
||||
console.log(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
|
||||
Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`)
|
||||
|
||||
return { transaction_id: uuid() }
|
||||
}
|
||||
Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`);
|
||||
|
||||
return { transaction_id: uuid() };
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue