Skip to content

Commit

Permalink
enhancement(PDF): Create fetchWithTimeout util and use in getTransact…
Browse files Browse the repository at this point in the history
…ionPdf
  • Loading branch information
sbinlondon committed Oct 21, 2020
1 parent b2848ee commit f1e5b06
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
42 changes: 42 additions & 0 deletions server/lib/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import fetch from 'node-fetch';

import logger from './logger';

/**
* Make a fetch call with a timeout. Returns a thenable Promise.
* @param {Object} fetchParams The object of options to pass into the fetch request.
* @param {String} fetchParams.url The url to fetch.
* @param {Object} [fetchParams.options] The method, headers, and other options for node-fetch. Default method is GET.
* @param {Number} [timeoutInMs] The callback to call with the authorized client. Default is 5000 ms.
*/

export const fetchWithTimeout = (fetchParams, timeoutInMs = 5000) => {
return new Promise((resolve, reject) => {
let timedOut = false;

const timer = setTimeout(() => {
timedOut = true;
logger.warn(`Fetch request to ${fetchParams.url} timed out`);
return reject(new Error(`Fetch request to ${fetchParams.url} timed out`));
}, timeoutInMs);

fetch(fetchParams.url, fetchParams.options)
.then(
response => {
if (!timedOut) {
logger.info(`Fetch request to ${fetchParams.url} successful`);
return resolve(response);
}
},
err => {
if (timedOut) {
return;
}
return reject(new Error(`Fetch error: ${err.message}`));
},
)
.finally(() => {
clearTimeout(timer);
});
});
};
9 changes: 6 additions & 3 deletions server/lib/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { find, get, includes, omit, pick } from 'lodash';

import activities from '../constants/activities';
import status from '../constants/order_status';
import { PAYMENT_METHOD_TYPES } from '../constants/paymentMethods';
import roles from '../constants/roles';
import tiers from '../constants/tiers';
import { FEES_ON_TOP_TRANSACTION_PROPERTIES, OC_FEE_PERCENT } from '../constants/transactions';
Expand Down Expand Up @@ -394,7 +395,7 @@ const validatePayment = payment => {
const sendOrderConfirmedEmail = async (order, transaction) => {
let pdf;
const attachments = [];
const { collective, tier, interval, fromCollective } = order;
const { collective, tier, interval, fromCollective, paymentMethod } = order;
const user = order.createdByUser;
const host = await collective.getHostCollective();

Expand Down Expand Up @@ -428,8 +429,10 @@ const sendOrderConfirmedEmail = async (order, transaction) => {
subscriptionsLink: interval && `${config.host.website}/${fromCollective.slug}/recurring-contributions`,
};

// hit PDF service and get PDF
pdf = await getTransactionPdf(transaction, user);
// hit PDF service and get PDF (unless payment method type is gift card)
if (paymentMethod.type !== PAYMENT_METHOD_TYPES.VIRTUALCARD) {
pdf = await getTransactionPdf(transaction, user);
}

// attach pdf
if (pdf) {
Expand Down
11 changes: 6 additions & 5 deletions server/lib/pdf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import config from 'config';
import fetch from 'node-fetch';

import { TOKEN_EXPIRATION_LOGIN } from '../lib/auth';
import logger from '../lib/logger';
import { TOKEN_EXPIRATION_LOGIN } from './auth';
import { fetchWithTimeout } from './fetch';
import logger from './logger';

export const getTransactionPdf = async (transaction, user) => {
if (['ci', 'test'].includes(config.env)) {
Expand All @@ -13,7 +13,8 @@ export const getTransactionPdf = async (transaction, user) => {
const headers = {
Authorization: `Bearer ${accessToken}`,
};
return await fetch(pdfUrl, { method: 'get', headers })

return fetchWithTimeout({ url: pdfUrl, options: { method: 'get', headers } })
.then(response => {
const { status } = response;
if (status >= 200 && status < 300) {
Expand All @@ -24,6 +25,6 @@ export const getTransactionPdf = async (transaction, user) => {
}
})
.catch(error => {
logger.error(`Error fetching PDF: ${error}`);
logger.error(`Error fetching PDF: ${error.message}`);
});
};

0 comments on commit f1e5b06

Please sign in to comment.