Skip to content

Commit

Permalink
Refactor api handling and logging
Browse files Browse the repository at this point in the history
- unified api response
- rename vars
  • Loading branch information
paulborm committed Mar 9, 2020
1 parent 858ebca commit e9bb0ea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 27 deletions.
14 changes: 0 additions & 14 deletions lib/api/getOrderStatus.js

This file was deleted.

21 changes: 8 additions & 13 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const dayjs = require('dayjs');
const ora = require('ora');
const chalk = require('chalk');
const { STATE_CODES } = require('./constants');
const getOrderStatus = require('./api/getOrderStatus');
const { getOrderStatus } = require('./utils/api');

dayjs.locale('de');
const spinner = ora();
Expand All @@ -32,24 +32,19 @@ const cli = {
console.log(data);
}

const {
summaryStateCode,
summaryStateText,
summaryDate,
summaryPriceText,
} = data;
const { stateCode, stateText, stateDate, priceText } = data;

switch (summaryStateCode) {
switch (stateCode) {
case STATE_CODES.SUBMITTED:
case STATE_CODES.PROCESSING:
case STATE_CODES.PRODUCED:
case STATE_CODES.SHIPPED:
case STATE_CODES.DELIVERED:
spinner.succeed(chalk.bold(summaryStateText));
spinner.succeed(chalk.bold(stateText));
console.log('');
console.log(
chalk.dim('Status vom:'),
dayjs(summaryDate).format('DD MMMM YYYY'),
dayjs(stateDate).format('DD MMMM YYYY'),
);
case STATE_CODES.SUBMITTED:
case STATE_CODES.PROCESSING:
Expand All @@ -61,14 +56,14 @@ const cli = {
case STATE_CODES.PRODUCED:
case STATE_CODES.SHIPPED:
case STATE_CODES.DELIVERED:
console.log(chalk.dim('Preis:'), summaryPriceText);
console.log(chalk.dim('Preis:'), priceText);
break;
case STATE_CODES.CANCELED:
spinner.warn(chalk.bold(summaryStateText));
spinner.warn(chalk.bold(stateText));
break;
case STATE_CODES.ERROR:
default:
spinner.fail(chalk.bold(summaryStateText));
spinner.fail(chalk.bold(stateText));
}
} catch (error) {
const { response } = error;
Expand Down
74 changes: 74 additions & 0 deletions lib/utils/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const axios = require('axios');
const { API_URL } = require('../constants');

/**
* Fetch the order status from api.
* @param {string} shop
* @param {string} order
*/
const fetchOrderStatus = async (shop, order) => {
const params = {
config: 1320,
shop,
order,
};
const response = await axios.get(API_URL, { params });
return response.data;
};

/**
* Transform the input data into a standardized format.
* @param {object[]} data
*/
const transformApiData = data => {
const { shopNo = null, orderNo = null, subOrders = [] } = data;

if (data.subOrders && data.subOrders.length) {
const {
stateCode = null,
stateDate = null,
stateText = null,
priceText = null,
} = subOrders[0];

return {
shopNo,
orderNo,
stateCode,
stateDate,
stateText,
priceText,
};
}

const {
summaryStateCode: stateCode = null,
summaryDate: stateDate = null,
summaryStateText: stateText = null,
summaryPriceText: priceText = null,
} = data;

return {
shopNo,
orderNo,
stateCode,
stateDate,
stateText,
priceText,
};
};

/**
* Get the necessary data from order status.
* @param {string} shop
* @param {string} order
*/
const getOrderStatus = async (shop, order) => {
const data = await fetchOrderStatus(shop, order);
const transformedData = await transformApiData(data);
return transformedData;
};

module.exports = {
getOrderStatus,
};

0 comments on commit e9bb0ea

Please sign in to comment.