u
This commit is contained in:
@@ -15,6 +15,17 @@ function logField(label, value, { skipEmpty = false } = {}) {
|
||||
httpLog.debug(prefix + '\n' + rendered.replace(/^/gm, ' '));
|
||||
}
|
||||
|
||||
function logResponse(value) {
|
||||
const rendered = prettyJson(value).replace(/\r?\n/g, '');
|
||||
const prefix = chalk.gray(' response: ');
|
||||
if (rendered.length <= 100) {
|
||||
httpLog.debug(prefix + rendered);
|
||||
return;
|
||||
}
|
||||
const cut = rendered.length - 100;
|
||||
httpLog.debug(prefix + rendered.slice(0, 100) + '\u001b[0m' + chalk.gray(` … [${cut} chars cut]`));
|
||||
}
|
||||
|
||||
function captureResponseBody(res) {
|
||||
let body;
|
||||
const originalJson = res.json.bind(res);
|
||||
@@ -76,7 +87,7 @@ export const loggerMiddleware = (req, res, next) => {
|
||||
httpLog.debug(chalk.gray(' authorization: ') + chalk.cyan(req.headers.authorization));
|
||||
}
|
||||
logField('body', req.body ?? {}, { skipEmpty: true });
|
||||
logField('response', getBody());
|
||||
logResponse(getBody());
|
||||
|
||||
// Dump full request/response/error details to a log file on 5xx.
|
||||
const statusCode = res.statusCode;
|
||||
|
||||
@@ -84,15 +84,39 @@ async function enrichSingleOrder(store, googleAuthId, order) {
|
||||
};
|
||||
}
|
||||
|
||||
function formatEnrichedFromSummary(order) {
|
||||
const grossTotal = Number(order.grossTotal || 0);
|
||||
const paidTotal = Number(order.paidTotal || 0);
|
||||
const orderedQuantity = Number(order.orderedQuantity || 0);
|
||||
const shippedQuantity = Number(order.shippedQuantity || 0);
|
||||
const isPaid = Boolean(order.isPaid);
|
||||
const isShipped = Boolean(order.isShipped);
|
||||
|
||||
return {
|
||||
...order,
|
||||
shipping: null,
|
||||
shippings: [],
|
||||
payment: null,
|
||||
payments: [],
|
||||
shipments: [],
|
||||
orderTotal: grossTotal,
|
||||
paidTotal,
|
||||
orderedQuantity,
|
||||
shippedQuantity,
|
||||
paid: isPaid,
|
||||
shipped: isShipped,
|
||||
delivered: isShipped,
|
||||
};
|
||||
}
|
||||
|
||||
async function enrichOrderResult(store, googleAuthId, result) {
|
||||
if (!result) return result;
|
||||
const enrich = (o) => (o?.grossTotal !== undefined ? formatEnrichedFromSummary(o) : enrichSingleOrder(store, googleAuthId, o));
|
||||
if (Array.isArray(result)) {
|
||||
return Promise.all(result.map((o) => enrichSingleOrder(store, googleAuthId, o)));
|
||||
return Promise.all(result.map(enrich));
|
||||
}
|
||||
if (Array.isArray(result.orders)) {
|
||||
const enrichedOrders = await Promise.all(
|
||||
result.orders.map((o) => enrichSingleOrder(store, googleAuthId, o))
|
||||
);
|
||||
const enrichedOrders = await Promise.all(result.orders.map(enrich));
|
||||
return {
|
||||
...result,
|
||||
orders: enrichedOrders,
|
||||
|
||||
Reference in New Issue
Block a user