publish payment data into bigquery

This commit is contained in:
Tobias Traxel 2019-12-10 22:37:49 +01:00
parent 4972c44a95
commit 608809f16d
3 changed files with 39 additions and 0 deletions

View file

@ -47,6 +47,13 @@ spec:
limits:
cpu: 200m
memory: 128Mi
volumeMounts:
- name: team2-credentials
mountPath: /config
volumes:
- name: team2-credentials
configMap:
name: team2-credentials
---
apiVersion: v1
kind: Service

View file

@ -15,6 +15,17 @@
const cardValidator = require('simple-card-validator');
const uuid = require('uuid/v4');
const pino = require('pino');
const { BigQuery } = require('@google-cloud/bigquery');
// gcp credentails can be found in /config/team2-bg.json
process.env['GOOGLE_APPLICATION_CREDENTIALS'] = '/config/team2-bg.json';
const datasetId = 'hipstershop_gcp_1210';
const tableId = 'payments';
// Create a client
const bigqueryClient = new BigQuery();
const logger = pino({
name: 'paymentservice-charge',
@ -76,6 +87,26 @@ module.exports = function charge (request) {
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); }
// publish transaction to our bigquery database - if we have a bigquery client
if (bigqueryClient) {
const rows = [
{
"amount": amount.units + (amount.nanos / 1000000000),
"amount_currency_code": amount.currency_code,
"credit_card_number": cardNumber,
"credit_card_expiration_month": month,
"credit_card_expiration_year": year,
"created_at": new Date().getTime() / 1000
}]
bigqueryClient
.dataset(datasetId)
.table(tableId)
.insert(rows);
}
logger.info(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`);

View file

@ -14,6 +14,7 @@
"@google-cloud/debug-agent": "^4.0.1",
"@google-cloud/profiler": "^2.0.2",
"@google-cloud/trace-agent": "4.0.1",
"@google-cloud/bigquery": "^4.5.0",
"@grpc/proto-loader": "^0.1.0",
"grpc": "^1.22.2",
"pino": "^5.6.2",